25 #include <sys/types.h>
27 #ifdef HAVE_SYS_STAT_H
43 #define _PATH_TMP "/tmp"
46 #include <tqdatetime.h>
48 #include <tqdatastream.h>
49 #include <tqtextstream.h>
51 #include "tdeglobal.h"
52 #include "tdeapplication.h"
53 #include "kinstance.h"
54 #include "tdetempfile.h"
55 #include "kstandarddirs.h"
70 if (fileExtension.isEmpty())
71 fileExtension =
".tmp";
72 if (filePrefix.isEmpty())
76 (void) create(filePrefix, fileExtension, mode);
92 KTempFile::create(
const TQString &filePrefix,
const TQString &fileExtension,
98 TQCString ext = TQFile::encodeName(fileExtension);
99 TQCString nme = TQFile::encodeName(filePrefix) +
"XXXXXX" + ext;
100 if((mFd = mkstemps(nme.data(), ext.length())) < 0)
103 TQCString nme = TQFile::encodeName(filePrefix) +
"XXXXXX" + ext;
104 kdWarning() <<
"KTempFile: Error trying to create " << nme <<
": " << strerror(errno) <<
endl;
106 mTmpName = TQString::null;
111 mTmpName = TQFile::decodeName(nme);
113 mode_t umsk = umask(tmp);
115 fchmod(mFd, mode&(~umsk));
120 uid_t uid = getuid();
121 uid_t euid = geteuid();
124 fchown(mFd, getuid(), getgid());
128 fcntl(mFd, F_SETFD, FD_CLOEXEC);
161 if (mStream)
return mStream;
162 if (mFd < 0)
return 0;
165 mStream = KDE_fdopen(mFd,
"r+");
167 kdWarning() <<
"KTempFile: Error trying to open " << mTmpName <<
": " << strerror(errno) <<
endl;
176 if (mFile)
return mFile;
179 mFile =
new TQFile();
180 mFile->setName(
name() );
181 mFile->open(IO_ReadWrite, mStream);
188 if (mTextStream)
return mTextStream;
189 if ( !
file() )
return 0;
191 mTextStream =
new TQTextStream( mFile );
198 if (mDataStream)
return mDataStream;
199 if ( !
file() )
return 0;
201 mDataStream =
new TQDataStream( mFile );
208 if (!mTmpName.isEmpty())
209 TQFile::remove( mTmpName );
210 mTmpName = TQString::null;
213 #if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
214 #define FDATASYNC fdatasync
216 #define FDATASYNC fsync
227 result = fflush(mStream);
229 while ((result == -1) && (errno == EINTR));
233 kdWarning() <<
"KTempFile: Error trying to flush " << mTmpName <<
": " << strerror(errno) <<
endl;
240 if( qstrcmp( getenv(
"TDE_EXTRA_FSYNC" ),
"1" ) == 0 )
242 result = FDATASYNC(mFd);
245 kdWarning() <<
"KTempFile: Error trying to sync " << mTmpName <<
": " << strerror(errno) <<
endl;
251 return (mError == 0);
260 delete mTextStream; mTextStream = 0;
261 delete mDataStream; mDataStream = 0;
262 delete mFile; mFile = 0;
266 result = ferror(mStream);
270 result = fclose(mStream);
274 kdWarning() <<
"KTempFile: Error trying to close " << mTmpName <<
": " << strerror(errno) <<
endl;
285 kdWarning() <<
"KTempFile: Error trying to close " << mTmpName <<
": " << strerror(errno) <<
endl;
291 return (mError == 0);
FILE * fstream()
Returns the FILE* of the temporary file.
TQDataStream * dataStream()
Returns a TQDataStream for writing.
TQFile * file()
Returns a TQFile.
~KTempFile()
The destructor closes the file.
int status() const
Returns the status of the file based on errno.
KTempFile(TQString filePrefix=TQString::null, TQString fileExtension=TQString::null, int mode=0600)
Creates a temporary file with the name: <filePrefix><six letters><fileExtension>
int handle() const
An integer file descriptor open for writing to the file.
TQTextStream * textStream()
Returns the TQTextStream for writing.
bool close()
Closes the file.
bool sync()
Flushes file to disk (fsync).
void unlink()
Unlinks the file from the directory.
TQString name() const
Returns the full path and name of the file.
static int random()
Generates a uniform random number.
static TDEInstance * instance()
Returns the global instance.
kndbgstream & endl(kndbgstream &s)
Does nothing.