You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
294 lines
6.1 KiB
294 lines
6.1 KiB
15 years ago
|
/*
|
||
|
*
|
||
|
* This file is part of the KDE libraries
|
||
|
* Copyright (c) 1999 Waldo Bastian <bastian@kde.org>
|
||
|
*
|
||
|
* $Id$
|
||
|
*
|
||
|
* This library is free software; you can redistribute it and/or
|
||
|
* modify it under the terms of the GNU Library General Public
|
||
|
* License version 2 as published by the Free Software Foundation.
|
||
|
*
|
||
|
* This library is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
* Library General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Library General Public License
|
||
|
* along with this library; see the file COPYING.LIB. If not, write to
|
||
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
* Boston, MA 02110-1301, USA.
|
||
|
**/
|
||
|
|
||
|
#include <config.h>
|
||
|
|
||
|
#include <sys/types.h>
|
||
|
|
||
|
#ifdef HAVE_SYS_STAT_H
|
||
|
#include <sys/stat.h>
|
||
|
#endif
|
||
|
|
||
|
#include <fcntl.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
#ifdef HAVE_TEST
|
||
|
#include <test.h>
|
||
|
#endif
|
||
|
#ifdef HAVE_PATHS_H
|
||
|
#include <paths.h>
|
||
|
#endif
|
||
|
|
||
|
#ifndef _PATH_TMP
|
||
|
#define _PATH_TMP "/tmp"
|
||
|
#endif
|
||
|
|
||
15 years ago
|
#include <tqdatetime.h>
|
||
|
#include <tqfile.h>
|
||
|
#include <tqdatastream.h>
|
||
13 years ago
|
#include <tqtextstream.h>
|
||
15 years ago
|
|
||
12 years ago
|
#include "tdeglobal.h"
|
||
12 years ago
|
#include "tdeapplication.h"
|
||
15 years ago
|
#include "kinstance.h"
|
||
12 years ago
|
#include "tdetempfile.h"
|
||
15 years ago
|
#include "kstandarddirs.h"
|
||
|
#include "kde_file.h"
|
||
|
#include "kdebug.h"
|
||
|
|
||
15 years ago
|
/* antlarr: KDE 4: make the parameters const TQString & */
|
||
|
KTempFile::KTempFile(TQString filePrefix, TQString fileExtension, int mode)
|
||
15 years ago
|
{
|
||
|
bAutoDelete = false;
|
||
|
mFd = -1;
|
||
|
mStream = 0;
|
||
|
mFile = 0;
|
||
|
mTextStream = 0;
|
||
|
mDataStream = 0;
|
||
|
mError = 0;
|
||
|
bOpen = false;
|
||
|
if (fileExtension.isEmpty())
|
||
|
fileExtension = ".tmp";
|
||
|
if (filePrefix.isEmpty())
|
||
|
{
|
||
12 years ago
|
filePrefix = locateLocal("tmp", TDEGlobal::instance()->instanceName());
|
||
15 years ago
|
}
|
||
|
(void) create(filePrefix, fileExtension, mode);
|
||
|
}
|
||
|
|
||
|
KTempFile::KTempFile(bool)
|
||
|
{
|
||
|
bAutoDelete = false;
|
||
|
mFd = -1;
|
||
|
mStream = 0;
|
||
|
mFile = 0;
|
||
|
mTextStream = 0;
|
||
|
mDataStream = 0;
|
||
|
mError = 0;
|
||
|
bOpen = false;
|
||
|
}
|
||
|
|
||
|
bool
|
||
15 years ago
|
KTempFile::create(const TQString &filePrefix, const TQString &fileExtension,
|
||
15 years ago
|
int mode)
|
||
|
{
|
||
|
// make sure the random seed is randomized
|
||
12 years ago
|
(void) TDEApplication::random();
|
||
15 years ago
|
|
||
15 years ago
|
TQCString ext = TQFile::encodeName(fileExtension);
|
||
|
TQCString nme = TQFile::encodeName(filePrefix) + "XXXXXX" + ext;
|
||
15 years ago
|
if((mFd = mkstemps(nme.data(), ext.length())) < 0)
|
||
|
{
|
||
|
// Recreate it for the warning, mkstemps emptied it
|
||
15 years ago
|
TQCString nme = TQFile::encodeName(filePrefix) + "XXXXXX" + ext;
|
||
15 years ago
|
kdWarning() << "KTempFile: Error trying to create " << nme << ": " << strerror(errno) << endl;
|
||
|
mError = errno;
|
||
15 years ago
|
mTmpName = TQString::null;
|
||
15 years ago
|
return false;
|
||
|
}
|
||
|
|
||
14 years ago
|
// got a file descriptor. nme contains the name
|
||
15 years ago
|
mTmpName = TQFile::decodeName(nme);
|
||
15 years ago
|
mode_t tmp = 0;
|
||
|
mode_t umsk = umask(tmp);
|
||
|
umask(umsk);
|
||
|
fchmod(mFd, mode&(~umsk));
|
||
|
|
||
|
// Success!
|
||
|
bOpen = true;
|
||
|
|
||
13 years ago
|
uid_t uid = getuid();
|
||
|
uid_t euid = geteuid();
|
||
|
if (uid != euid) {
|
||
|
// Set uid/gid (necessary for SUID programs)
|
||
|
fchown(mFd, getuid(), getgid());
|
||
|
}
|
||
15 years ago
|
|
||
|
// Set close on exec
|
||
|
fcntl(mFd, F_SETFD, FD_CLOEXEC);
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
KTempFile::~KTempFile()
|
||
|
{
|
||
|
close();
|
||
|
if (bAutoDelete)
|
||
|
unlink();
|
||
|
}
|
||
|
|
||
|
int
|
||
|
KTempFile::status() const
|
||
|
{
|
||
|
return mError;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString
|
||
15 years ago
|
KTempFile::name() const
|
||
|
{
|
||
|
return mTmpName;
|
||
|
}
|
||
|
|
||
|
int
|
||
|
KTempFile::handle() const
|
||
|
{
|
||
|
return mFd;
|
||
|
}
|
||
|
|
||
|
FILE *
|
||
|
KTempFile::fstream()
|
||
|
{
|
||
|
if (mStream) return mStream;
|
||
|
if (mFd < 0) return 0;
|
||
|
|
||
|
// Create a stream
|
||
|
mStream = KDE_fdopen(mFd, "r+");
|
||
|
if (!mStream) {
|
||
|
kdWarning() << "KTempFile: Error trying to open " << mTmpName << ": " << strerror(errno) << endl;
|
||
|
mError = errno;
|
||
|
}
|
||
|
return mStream;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQFile *
|
||
15 years ago
|
KTempFile::file()
|
||
|
{
|
||
|
if (mFile) return mFile;
|
||
|
if ( !fstream() ) return 0;
|
||
|
|
||
15 years ago
|
mFile = new TQFile();
|
||
15 years ago
|
mFile->setName( name() );
|
||
|
mFile->open(IO_ReadWrite, mStream);
|
||
|
return mFile;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQTextStream *
|
||
15 years ago
|
KTempFile::textStream()
|
||
|
{
|
||
|
if (mTextStream) return mTextStream;
|
||
|
if ( !file() ) return 0; // Initialize mFile
|
||
|
|
||
15 years ago
|
mTextStream = new TQTextStream( mFile );
|
||
15 years ago
|
return mTextStream;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQDataStream *
|
||
15 years ago
|
KTempFile::dataStream()
|
||
|
{
|
||
|
if (mDataStream) return mDataStream;
|
||
|
if ( !file() ) return 0; // Initialize mFile
|
||
|
|
||
15 years ago
|
mDataStream = new TQDataStream( mFile );
|
||
15 years ago
|
return mDataStream;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
KTempFile::unlink()
|
||
|
{
|
||
|
if (!mTmpName.isEmpty())
|
||
15 years ago
|
TQFile::remove( mTmpName );
|
||
|
mTmpName = TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
|
#if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
|
||
|
#define FDATASYNC fdatasync
|
||
|
#else
|
||
|
#define FDATASYNC fsync
|
||
|
#endif
|
||
|
|
||
|
bool
|
||
|
KTempFile::sync()
|
||
|
{
|
||
|
int result = 0;
|
||
|
|
||
|
if (mStream)
|
||
|
{
|
||
|
do {
|
||
|
result = fflush(mStream); // We need to flush first otherwise fsync may not have our data
|
||
|
}
|
||
|
while ((result == -1) && (errno == EINTR));
|
||
|
|
||
|
if (result)
|
||
|
{
|
||
|
kdWarning() << "KTempFile: Error trying to flush " << mTmpName << ": " << strerror(errno) << endl;
|
||
|
mError = errno;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (mFd >= 0)
|
||
|
{
|
||
13 years ago
|
if( qstrcmp( getenv( "TDE_EXTRA_FSYNC" ), "1" ) == 0 )
|
||
15 years ago
|
{
|
||
|
result = FDATASYNC(mFd);
|
||
|
if (result)
|
||
|
{
|
||
|
kdWarning() << "KTempFile: Error trying to sync " << mTmpName << ": " << strerror(errno) << endl;
|
||
|
mError = errno;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return (mError == 0);
|
||
|
}
|
||
|
|
||
|
#undef FDATASYNC
|
||
|
|
||
|
bool
|
||
|
KTempFile::close()
|
||
|
{
|
||
|
int result = 0;
|
||
|
delete mTextStream; mTextStream = 0;
|
||
|
delete mDataStream; mDataStream = 0;
|
||
|
delete mFile; mFile = 0;
|
||
|
|
||
|
if (mStream)
|
||
|
{
|
||
|
result = ferror(mStream);
|
||
|
if (result)
|
||
|
mError = ENOSPC; // Assume disk full.
|
||
|
|
||
|
result = fclose(mStream);
|
||
|
mStream = 0;
|
||
|
mFd = -1;
|
||
|
if (result != 0) {
|
||
|
kdWarning() << "KTempFile: Error trying to close " << mTmpName << ": " << strerror(errno) << endl;
|
||
|
mError = errno;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
if (mFd >= 0)
|
||
|
{
|
||
|
result = ::close(mFd);
|
||
|
mFd = -1;
|
||
|
if (result != 0) {
|
||
|
kdWarning() << "KTempFile: Error trying to close " << mTmpName << ": " << strerror(errno) << endl;
|
||
|
mError = errno;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bOpen = false;
|
||
|
return (mError == 0);
|
||
|
}
|
||
|
|