24 #include <sys/types.h> 26 #ifdef HAVE_SYS_STAT_H 40 #define _PATH_TMP "/tmp" 43 #include <QtCore/QCharRef> 44 #include <QtCore/QDate> 45 #include <QtCore/QFile> 46 #include <QtCore/QDataStream> 47 #include <QtCore/QTextIStream> 56 class K3TempFile::Private
60 #define mError d->_Error 62 #define mTmpName d->_TmpName 66 #define mStream d->_Stream 68 #define mFile d->_File 69 QTextStream *_TextStream;
70 #define mTextStream d->_TextStream 71 QDataStream *_DataStream;
72 #define mDataStream d->_DataStream 74 #define bOpen d->_Open 76 #define bAutoDelete d->_AutoDelete 80 const QString& fileExtension,
int mode)
91 QString extension = fileExtension;
92 QString
prefix = filePrefix;
93 if (extension.isEmpty())
94 extension = QLatin1String(
".tmp");
99 (void)
create(prefix, extension, mode);
121 QByteArray ext = QFile::encodeName(fileExtension);
122 QByteArray nme = QFile::encodeName(filePrefix) +
"XXXXXX" + ext;
123 if((
mFd = mkstemps(nme.data(), ext.length())) < 0)
126 nme = QFile::encodeName(filePrefix) +
"XXXXXX" + ext;
127 kWarning() <<
"K3TempFile: Error trying to create " << nme <<
": " << strerror(errno);
136 mode_t umsk =
umask(tmp);
138 fchmod(
mFd, mode&(~umsk));
144 fchown(
mFd, getuid(), getgid());
148 fcntl(
mFd, F_SETFD, FD_CLOEXEC);
183 if (
mFd < 0)
return 0;
188 kWarning() <<
"K3TempFile: Error trying to open " <<
mTmpName <<
": " << strerror(errno);
210 if ( !
file() )
return 0;
220 if ( !
file() )
return 0;
234 #if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0 235 #define FDATASYNC fdatasync 237 #define FDATASYNC fsync 250 while ((result == -1) && (errno == EINTR));
254 kWarning() <<
"K3TempFile: Error trying to flush " <<
mTmpName <<
": " << strerror(errno);
261 if( qgetenv(
"KDE_EXTRA_FSYNC" ) ==
"1" )
266 kWarning() <<
"K3TempFile: Error trying to sync " <<
mTmpName <<
": " << strerror(errno);
295 kWarning() <<
"K3TempFile: Error trying to close " <<
mTmpName <<
": " << strerror(errno);
306 kWarning() <<
"K3TempFile: Error trying to close " <<
mTmpName <<
": " << strerror(errno);
void setAutoDelete(bool autoDelete)
Turn automatic deletion on or off.
FILE * fstream()
Returns the FILE* of the temporary file.
~K3TempFile()
The destructor closes the file.
QDataStream * dataStream()
Returns a QDataStream for writing.
K3TempFile(const QString &filePrefix=QString(), const QString &fileExtension=QString(), int mode=0600)
Creates a temporary file with the name: <filePrefix><six letters><fileExtension>
bool close()
Closes the file.
void setError(int error)
Set the error value (for subclasses).
void unlink()
Unlinks the file from the directory.
QString name() const
Returns the full path and name of the file.
static QString locateLocal(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
int status() const
Returns the status of the file based on errno.
QTextStream * textStream()
Returns the QTextStream for writing.
const KComponentData & mainComponent()
int handle() const
An integer file descriptor open for writing to the file.
QFile * file()
Returns a QFile.
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
bool sync()
Flushes file to disk (fsync).
bool create(const QString &filePrefix, const QString &fileExtension, int mode)
Create function used internally by K3TempFile and KSaveFile.