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.24 2001/12/03 00:47:34 mueller 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 KTempFile::KTempFile(QString filePrefix, QString fileExtension, int mode)
00059 {
00060    bAutoDelete = false;
00061    mFd = -1;
00062    mStream = 0;
00063    mFile = 0;
00064    mTextStream = 0;
00065    mDataStream = 0;
00066    mError = 0;
00067    bOpen = false;
00068    if (fileExtension.isEmpty())
00069       fileExtension = ".tmp";
00070    if (filePrefix.isEmpty())
00071    {
00072       filePrefix = locateLocal("tmp", KGlobal::instance()->instanceName());
00073    }
00074    (void) create(filePrefix, fileExtension, mode);
00075 }
00076 
00077 KTempFile::KTempFile(bool)
00078 {
00079    bAutoDelete = false;
00080    mFd = -1;
00081    mStream = 0;
00082    mFile = 0;
00083    mTextStream = 0;
00084    mDataStream = 0;
00085    mError = 0;
00086    bOpen = false;
00087 }
00088 
00089 bool
00090 KTempFile::create(const QString &filePrefix, const QString &fileExtension,
00091           int mode)
00092 {
00093    // make sure the random seed is randomized
00094    (void) KApplication::random();
00095 
00096    QCString ext = QFile::encodeName(fileExtension);
00097    QCString nme = QFile::encodeName(filePrefix) + "XXXXXX" + ext;
00098    if((mFd = mkstemps(nme.data(), ext.length())) < 0)
00099    {
00100        // Recreate it for the warning, mkstemps emptied it
00101        QCString nme = QFile::encodeName(filePrefix) + "XXXXXX" + ext;
00102        qWarning("KTempFile: Error trying to create %s: %s", nme.data(), strerror(errno));
00103        mError = errno;
00104        mTmpName = QString::null;
00105        return false;
00106    }
00107 
00108    // got a file descriptor. nme contains the name
00109    mTmpName = QFile::decodeName(nme);
00110    mode_t tmp = 0;
00111    mode_t umsk = umask(tmp);
00112    umask(umsk);
00113    chmod(nme, mode&(~umsk));
00114 
00115    // Success!
00116    bOpen = true;
00117 
00118    // Set uid/gid (neccesary for SUID programs)
00119    chown(nme, getuid(), getgid());
00120    return true;
00121 }
00122 
00123 KTempFile::~KTempFile()
00124 {
00125    close();
00126    if (bAutoDelete)
00127       unlink();
00128 }
00129 
00130 int
00131 KTempFile::status() const
00132 {
00133    return mError;
00134 }
00135 
00136 QString
00137 KTempFile::name() const
00138 {
00139    return mTmpName;
00140 }
00141 
00142 int
00143 KTempFile::handle() const
00144 {
00145    return mFd;
00146 }
00147 
00148 FILE *
00149 KTempFile::fstream()
00150 {
00151    if (mStream) return mStream;
00152    if (mFd < 0) return 0;
00153 
00154    // Create a stream
00155    mStream = fdopen(mFd, "r+");
00156    if (!mStream) {
00157      qWarning("KTempFile: Error trying to open %s: %s", mTmpName.latin1(), strerror(errno));
00158      mError = errno;
00159    }
00160    return mStream;
00161 }
00162 
00163 QFile *
00164 KTempFile::file()
00165 {
00166    if (mFile) return mFile;
00167    if ( !fstream() ) return 0;
00168 
00169    mFile = new QFile();
00170    mFile->setName( name() );
00171    mFile->open(IO_ReadWrite, mStream);
00172    return mFile;
00173 }
00174 
00175 QTextStream *
00176 KTempFile::textStream()
00177 {
00178    if (mTextStream) return mTextStream;
00179    if ( !file() ) return 0; // Initialize mFile
00180 
00181    mTextStream = new QTextStream( mFile );
00182    return mTextStream;
00183 }
00184 
00185 QDataStream *
00186 KTempFile::dataStream()
00187 {
00188    if (mDataStream) return mDataStream;
00189    if ( !file() ) return 0;  // Initialize mFile
00190 
00191    mDataStream = new QDataStream( mFile );
00192    return mDataStream;
00193 }
00194 
00195 void
00196 KTempFile::unlink()
00197 {
00198    if (!mTmpName.isEmpty())
00199       QFile::remove( mTmpName );
00200    mTmpName = QString::null;
00201 }
00202 
00203 bool
00204 KTempFile::close()
00205 {
00206    int result = 0;
00207    delete mTextStream; mTextStream = 0;
00208    delete mDataStream; mDataStream = 0;
00209    delete mFile; mFile = 0;
00210 
00211    if (mStream)
00212    {
00213       result = ferror(mStream);
00214       if (result)
00215          mError = ENOSPC; // Assume disk full.
00216 
00217       result = fclose(mStream);
00218       mStream = 0;
00219       mFd = -1;
00220       if (result != 0) {
00221          qWarning("KTempFile: Error trying to closing %s: %s", mTmpName.latin1(), strerror(errno));
00222          mError = errno;
00223       }
00224    }
00225 
00226 
00227    if (mFd >= 0)
00228    {
00229       result = ::close(mFd);
00230       mFd = -1;
00231       if (result != 0) {
00232          qWarning("KTempFile: Error trying to closing %s: %s", mTmpName.latin1(), strerror(errno));
00233          mError = errno;
00234       }
00235    }
00236 
00237    bOpen = false;
00238    return (mError == 0);
00239 }
00240 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:20:42 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001