kdecore Library API Documentation

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,v 1.3 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 <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()<<"KTempDir: Temporary directory created :"<<realNameStr<<endl; 00091 kdDebug()<<"KTempDir: Temporary directory created :"<<mTmpName<<endl; 00092 mode_t tmp = 0; 00093 mode_t umsk = umask(tmp); 00094 umask(umsk); 00095 chmod(nme, mode&(~umsk)); 00096 00097 // Success! 00098 bExisting = true; 00099 00100 // Set uid/gid (necessary for SUID programs) 00101 chown(nme, getuid(), getgid()); 00102 return true; 00103 } 00104 00105 KTempDir::~KTempDir() 00106 { 00107 if (bAutoDelete) 00108 unlink(); 00109 } 00110 00111 int 00112 KTempDir::status() const 00113 { 00114 return mError; 00115 } 00116 00117 QString 00118 KTempDir::name() const 00119 { 00120 return mTmpName; 00121 } 00122 00123 bool 00124 KTempDir::existing() const 00125 { 00126 return bExisting; 00127 } 00128 00129 QDir * 00130 KTempDir::qDir() 00131 { 00132 if (bExisting) return new QDir(mTmpName); 00133 return 0; 00134 } 00135 00136 void 00137 KTempDir::unlink() 00138 { 00139 if (!bExisting) return; 00140 QString rmstr("/bin/rm -rf "); 00141 rmstr += KProcess::quote(mTmpName); 00142 ::system( QFile::encodeName(rmstr) ); 00143 00144 bExisting=false; 00145 mError=0; 00146 } 00147 00148
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 Wed Mar 16 17:21:43 2005 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003