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 2003/08/13 19:47:39 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 /* 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    chmod(nme, mode&(~umsk));
00115 
00116    // Success!
00117    bOpen = true;
00118 
00119    // Set uid/gid (necessary for SUID programs)
00120    chown(nme, getuid(), getgid());
00121    return true;
00122 }
00123 
00124 KTempFile::~KTempFile()
00125 {
00126    close();
00127    if (bAutoDelete)
00128       unlink();
00129 }
00130 
00131 int
00132 KTempFile::status() const
00133 {
00134    return mError;
00135 }
00136 
00137 QString
00138 KTempFile::name() const
00139 {
00140    return mTmpName;
00141 }
00142 
00143 int
00144 KTempFile::handle() const
00145 {
00146    return mFd;
00147 }
00148 
00149 FILE *
00150 KTempFile::fstream()
00151 {
00152    if (mStream) return mStream;
00153    if (mFd < 0) return 0;
00154 
00155    // Create a stream
00156    mStream = fdopen(mFd, "r+");
00157    if (!mStream) {
00158      qWarning("KTempFile: Error trying to open %s: %s", mTmpName.latin1(), strerror(errno));
00159      mError = errno;
00160    }
00161    return mStream;
00162 }
00163 
00164 QFile *
00165 KTempFile::file()
00166 {
00167    if (mFile) return mFile;
00168    if ( !fstream() ) return 0;
00169 
00170    mFile = new QFile();
00171    mFile->setName( name() );
00172    mFile->open(IO_ReadWrite, mStream);
00173    return mFile;
00174 }
00175 
00176 QTextStream *
00177 KTempFile::textStream()
00178 {
00179    if (mTextStream) return mTextStream;
00180    if ( !file() ) return 0; // Initialize mFile
00181 
00182    mTextStream = new QTextStream( mFile );
00183    return mTextStream;
00184 }
00185 
00186 QDataStream *
00187 KTempFile::dataStream()
00188 {
00189    if (mDataStream) return mDataStream;
00190    if ( !file() ) return 0;  // Initialize mFile
00191 
00192    mDataStream = new QDataStream( mFile );
00193    return mDataStream;
00194 }
00195 
00196 void
00197 KTempFile::unlink()
00198 {
00199    if (!mTmpName.isEmpty())
00200       QFile::remove( mTmpName );
00201    mTmpName = QString::null;
00202 }
00203 
00204 bool
00205 KTempFile::close()
00206 {
00207    int result = 0;
00208    delete mTextStream; mTextStream = 0;
00209    delete mDataStream; mDataStream = 0;
00210    delete mFile; mFile = 0;
00211 
00212    if (mStream)
00213    {
00214       result = ferror(mStream);
00215       if (result)
00216          mError = ENOSPC; // Assume disk full.
00217 
00218       result = fclose(mStream);
00219       mStream = 0;
00220       mFd = -1;
00221       if (result != 0) {
00222          qWarning("KTempFile: Error trying to closing %s: %s", mTmpName.latin1(), strerror(errno));
00223          mError = errno;
00224       }
00225    }
00226 
00227 
00228    if (mFd >= 0)
00229    {
00230       result = ::close(mFd);
00231       mFd = -1;
00232       if (result != 0) {
00233          qWarning("KTempFile: Error trying to closing %s: %s", mTmpName.latin1(), strerror(errno));
00234          mError = errno;
00235       }
00236    }
00237 
00238    bOpen = false;
00239    return (mError == 0);
00240 }
00241 
KDE Logo
This file is part of the documentation for kdecore Library Version 3.2.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Feb 4 12:33:52 2004 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2003