kdecore Library API Documentation

ktempfile.cpp

00001 /*
00002  *
00003  *  This file is part of the KDE libraries
00004  *  Copyright (c) 1999 Waldo Bastian <bastian@kde.org>
00005  *
00006  * $Id: ktempfile.cpp,v 1.28.2.2 2004/03/01 12:34:52 waba Exp $
00007  *
00008  *  This library is free software; you can redistribute it and/or
00009  *  modify it under the terms of the GNU Library General Public
00010  *  License version 2 as published by the Free Software Foundation.
00011  *
00012  *  This library is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  *  Library General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU Library General Public License
00018  *  along with this library; see the file COPYING.LIB.  If not, write to
00019  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020  *  Boston, MA 02111-1307, USA.
00021  **/
00022 
00023 #include <config.h>
00024 
00025 #include <sys/types.h>
00026 
00027 #ifdef HAVE_SYS_STAT_H
00028 #include <sys/stat.h>
00029 #endif
00030 
00031 #include <fcntl.h>
00032 #include <stdlib.h>
00033 #include <unistd.h>
00034 
00035 #ifdef HAVE_TEST
00036 #include <test.h>
00037 #endif
00038 #ifdef HAVE_PATHS_H
00039 #include <paths.h>
00040 #endif
00041 
00042 #ifndef _PATH_TMP
00043 #define _PATH_TMP "/tmp"
00044 #endif
00045 
00046 #include <qdatetime.h>
00047 #include <qfile.h>
00048 #include <qdatastream.h>
00049 #include <qtextstream.h>
00050 
00051 #include "kglobal.h"
00052 #include "kapplication.h"
00053 #include "kinstance.h"
00054 #include "ktempfile.h"
00055 #include "kstandarddirs.h"
00056 
00057 
00058 /* antlarr: KDE 4: make the parameters const QString & */
00059 KTempFile::KTempFile(QString filePrefix, QString fileExtension, int mode)
00060 {
00061    bAutoDelete = false;
00062    mFd = -1;
00063    mStream = 0;
00064    mFile = 0;
00065    mTextStream = 0;
00066    mDataStream = 0;
00067    mError = 0;
00068    bOpen = false;
00069    if (fileExtension.isEmpty())
00070       fileExtension = ".tmp";
00071    if (filePrefix.isEmpty())
00072    {
00073       filePrefix = locateLocal("tmp", KGlobal::instance()->instanceName());
00074    }
00075    (void) create(filePrefix, fileExtension, mode);
00076 }
00077 
00078 KTempFile::KTempFile(bool)
00079 {
00080    bAutoDelete = false;
00081    mFd = -1;
00082    mStream = 0;
00083    mFile = 0;
00084    mTextStream = 0;
00085    mDataStream = 0;
00086    mError = 0;
00087    bOpen = false;
00088 }
00089 
00090 bool
00091 KTempFile::create(const QString &filePrefix, const QString &fileExtension,
00092           int mode)
00093 {
00094    // make sure the random seed is randomized
00095    (void) KApplication::random();
00096 
00097    QCString ext = QFile::encodeName(fileExtension);
00098    QCString nme = QFile::encodeName(filePrefix) + "XXXXXX" + ext;
00099    if((mFd = mkstemps(nme.data(), ext.length())) < 0)
00100    {
00101        // Recreate it for the warning, mkstemps emptied it
00102        QCString nme = QFile::encodeName(filePrefix) + "XXXXXX" + ext;
00103        qWarning("KTempFile: Error trying to create %s: %s", nme.data(), strerror(errno));
00104        mError = errno;
00105        mTmpName = QString::null;
00106        return false;
00107    }
00108 
00109    // got a file descriptor. nme contains the name
00110    mTmpName = QFile::decodeName(nme);
00111    mode_t tmp = 0;
00112    mode_t umsk = umask(tmp);
00113    umask(umsk);
00114    fchmod(mFd, mode&(~umsk));
00115 
00116    // Success!
00117    bOpen = true;
00118 
00119    // Set uid/gid (necessary for SUID programs)
00120    fchown(mFd, getuid(), getgid());
00121 
00122    // Set close on exec
00123    fcntl(mFd, F_SETFD, FD_CLOEXEC);
00124    
00125    return true;
00126 }
00127 
00128 KTempFile::~KTempFile()
00129 {
00130    close();
00131    if (bAutoDelete)
00132       unlink();
00133 }
00134 
00135 int
00136 KTempFile::status() const
00137 {
00138    return mError;
00139 }
00140 
00141 QString
00142 KTempFile::name() const
00143 {
00144    return mTmpName;
00145 }
00146 
00147 int
00148 KTempFile::handle() const
00149 {
00150    return mFd;
00151 }
00152 
00153 FILE *
00154 KTempFile::fstream()
00155 {
00156    if (mStream) return mStream;
00157    if (mFd < 0) return 0;
00158 
00159    // Create a stream
00160    mStream = fdopen(mFd, "r+");
00161    if (!mStream) {
00162      qWarning("KTempFile: Error trying to open %s: %s", mTmpName.latin1(), strerror(errno));
00163      mError = errno;
00164    }
00165    return mStream;
00166 }
00167 
00168 QFile *
00169 KTempFile::file()
00170 {
00171    if (mFile) return mFile;
00172    if ( !fstream() ) return 0;
00173 
00174    mFile = new QFile();
00175    mFile->setName( name() );
00176    mFile->open(IO_ReadWrite, mStream);
00177    return mFile;
00178 }
00179 
00180 QTextStream *
00181 KTempFile::textStream()
00182 {
00183    if (mTextStream) return mTextStream;
00184    if ( !file() ) return 0; // Initialize mFile
00185 
00186    mTextStream = new QTextStream( mFile );
00187    return mTextStream;
00188 }
00189 
00190 QDataStream *
00191 KTempFile::dataStream()
00192 {
00193    if (mDataStream) return mDataStream;
00194    if ( !file() ) return 0;  // Initialize mFile
00195 
00196    mDataStream = new QDataStream( mFile );
00197    return mDataStream;
00198 }
00199 
00200 void
00201 KTempFile::unlink()
00202 {
00203    if (!mTmpName.isEmpty())
00204       QFile::remove( mTmpName );
00205    mTmpName = QString::null;
00206 }
00207 
00208 bool
00209 KTempFile::close()
00210 {
00211    int result = 0;
00212    delete mTextStream; mTextStream = 0;
00213    delete mDataStream; mDataStream = 0;
00214    delete mFile; mFile = 0;
00215 
00216    if (mStream)
00217    {
00218       result = ferror(mStream);
00219       if (result)
00220          mError = ENOSPC; // Assume disk full.
00221 
00222       result = fclose(mStream);
00223       mStream = 0;
00224       mFd = -1;
00225       if (result != 0) {
00226          qWarning("KTempFile: Error trying to closing %s: %s", mTmpName.latin1(), strerror(errno));
00227          mError = errno;
00228       }
00229    }
00230 
00231 
00232    if (mFd >= 0)
00233    {
00234       result = ::close(mFd);
00235       mFd = -1;
00236       if (result != 0) {
00237          qWarning("KTempFile: Error trying to closing %s: %s", mTmpName.latin1(), strerror(errno));
00238          mError = errno;
00239       }
00240    }
00241 
00242    bOpen = false;
00243    return (mError == 0);
00244 }
00245 
KDE Logo
This file is part of the documentation for kdecore Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Sep 30 05:15:55 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2003