| # kdecore Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# ktempdir.cpp

    00001 /*
    00002  *
    00003  *  This file is part of the KDE libraries
    00004  *  Copyright (c) 2003 Joseph Wenninger <jowenn@kde.org>
    00005  *
    00006  * $Id: ktempdir.cpp 380801 2005-01-21 14:35:25Z dfaure $
    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 <qdir.h>
    00048
    00049 #include "kglobal.h"
    00050 #include "kapplication.h"
    00051 #include "kinstance.h"
    00052 #include "ktempdir.h"
    00053 #include "kstandarddirs.h"
    00054 #include "kprocess.h"
    00055 #include <kdebug.h>
    00056
    00057 KTempDir::KTempDir(QString directoryPrefix, int mode)
    00058 {
    00059    bAutoDelete = false;
    00060    bExisting = false;
    00061    mError=0;
    00062    if (directoryPrefix.isEmpty())
    00063    {
    00064       directoryPrefix = locateLocal("tmp", KGlobal::instance()->instanceName());
    00065    }
    00066    (void) create(directoryPrefix , mode);
    00067 }
    00068
    00069 bool
    00070 KTempDir::create(const QString &directoryPrefix, int mode)
    00071 {
    00072    // make sure the random seed is randomized
    00073    (void) KApplication::random();
    00074
    00075    QCString nme = QFile::encodeName(directoryPrefix) + "XXXXXX";
    00076    char *realName;
    00077    if((realName=mkdtemp(nme.data())) == 0)
    00078    {
    00079        // Recreate it for the warning, mkdtemps emptied it
    00080        QCString nme = QFile::encodeName(directoryPrefix) + "XXXXXX";
    00081        qWarning("KTempDir: Error trying to create %s: %s", nme.data(), strerror(errno));
    00082        mError = errno;
    00083        mTmpName = QString::null;
    00084        return false;
    00085    }
    00086
    00087    // got a return value != 0
    00088    QCString realNameStr(realName);
    00089    mTmpName = QFile::decodeName(realNameStr)+"/";
    00090    kdDebug(180) << "KTempDir: Temporary directory created :" << mTmpName << endl;
    00091    mode_t tmp = 0;
    00092    mode_t umsk = umask(tmp);
    00093    umask(umsk);
    00094    chmod(nme, mode&(~umsk));
    00095
    00096    // Success!
    00097    bExisting = true;
    00098
    00099    // Set uid/gid (necessary for SUID programs)
    00100    chown(nme, getuid(), getgid());
    00101    return true;
    00102 }
    00103
    00104 KTempDir::~KTempDir()
    00105 {
    00106    if (bAutoDelete)
    00107       unlink();
    00108 }
    00109
    00110 int
    00111 KTempDir::status() const
    00112 {
    00113    return mError;
    00114 }
    00115
    00116 QString
    00117 KTempDir::name() const
    00118 {
    00119    return mTmpName;
    00120 }
    00121
    00122 bool
    00123 KTempDir::existing() const
    00124 {
    00125    return bExisting;
    00126 }
    00127
    00128 QDir *
    00129 KTempDir::qDir()
    00130 {
    00131    if (bExisting) return new QDir(mTmpName);
    00132    return 0;
    00133 }
    00134
    00135 void
    00136 KTempDir::unlink()
    00137 {
    00138    if (!bExisting) return;
    00139    QString rmstr("/bin/rm -rf ");
    00140    rmstr += KProcess::quote(mTmpName);
    00141    ::system( QFile::encodeName(rmstr) );
    00142
    00143    bExisting=false;
    00144    mError=0;
    00145 }
    00146
    00147

This file is part of the documentation for kdecore Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:18:12 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003