• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
ktempdir.cpp
1 /*
2  *
3  * This file is part of the KDE libraries
4  * Copyright (c) 2003 Joseph Wenninger <jowenn@kde.org>
5  *
6  * $Id$
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License version 2 as published by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  **/
22 
23 #include <config.h>
24 
25 #include <sys/types.h>
26 
27 #ifdef HAVE_SYS_STAT_H
28 #include <sys/stat.h>
29 #endif
30 
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <dirent.h>
35 
36 #ifdef HAVE_TEST
37 #include <test.h>
38 #endif
39 #ifdef HAVE_PATHS_H
40 #include <paths.h>
41 #endif
42 
43 #ifndef _PATH_TMP
44 #define _PATH_TMP "/tmp"
45 #endif
46 
47 #include <tqdatetime.h>
48 #include <tqdir.h>
49 
50 #include "tdeglobal.h"
51 #include "tdeapplication.h"
52 #include "kinstance.h"
53 #include "ktempdir.h"
54 #include "kstandarddirs.h"
55 #include "tdeprocess.h"
56 #include <kdebug.h>
57 #include "kde_file.h"
58 
59 KTempDir::KTempDir(TQString directoryPrefix, int mode)
60 {
61  bAutoDelete = false;
62  bExisting = false;
63  mError=0;
64  if (directoryPrefix.isEmpty())
65  {
66  directoryPrefix = locateLocal("tmp", TDEGlobal::instance()->instanceName());
67  }
68  (void) create(directoryPrefix , mode);
69 }
70 
71 bool
72 KTempDir::create(const TQString &directoryPrefix, int mode)
73 {
74  // make sure the random seed is randomized
75  (void) TDEApplication::random();
76 
77  TQCString nme = TQFile::encodeName(directoryPrefix) + "XXXXXX";
78  char *realName;
79  if((realName=mkdtemp(nme.data())) == 0)
80  {
81  // Recreate it for the warning, mkdtemps emptied it
82  TQCString nme = TQFile::encodeName(directoryPrefix) + "XXXXXX";
83  tqWarning("KTempDir: Error trying to create %s: %s", nme.data(), strerror(errno));
84  mError = errno;
85  mTmpName = TQString::null;
86  return false;
87  }
88 
89  // got a return value != 0
90  TQCString realNameStr(realName);
91  mTmpName = TQFile::decodeName(realNameStr)+"/";
92  kdDebug(180) << "KTempDir: Temporary directory created :" << mTmpName << endl;
93  mode_t tmp = 0;
94  mode_t umsk = umask(tmp);
95  umask(umsk);
96  chmod(nme, mode&(~umsk));
97 
98  // Success!
99  bExisting = true;
100 
101  // Set uid/gid (necessary for SUID programs)
102  chown(nme, getuid(), getgid());
103  return true;
104 }
105 
106 KTempDir::~KTempDir()
107 {
108  if (bAutoDelete)
109  unlink();
110 
111 // KTempDirPrivate doesn't exist, so it can't be deleted
112 // delete d;
113 }
114 
115 int
116 KTempDir::status() const
117 {
118  return mError;
119 }
120 
121 TQString
122 KTempDir::name() const
123 {
124  return mTmpName;
125 }
126 
127 bool
128 KTempDir::existing() const
129 {
130  return bExisting;
131 }
132 
133 TQDir *
134 KTempDir::qDir()
135 {
136  if (bExisting) return new TQDir(mTmpName);
137  return 0;
138 }
139 
140 void
141 KTempDir::unlink()
142 {
143  if (!bExisting) return;
144  if (KTempDir::removeDir(mTmpName))
145  mError=0;
146  else
147  mError=errno;
148  bExisting=false;
149 }
150 
151 // Auxiliary recursive function for removeDirs
152 static bool
153 rmtree(const TQCString& name)
154 {
155  kdDebug() << "Checking directory for remove " << name << endl;
156  KDE_struct_stat st;
157  if ( KDE_lstat( name.data(), &st ) == -1 ) // Do not dereference symlink!
158  return false;
159  if ( S_ISDIR( st.st_mode ) )
160  {
161  // This is a directory, so process it
162  kdDebug() << "File " << name << " is DIRECTORY!" << endl;
163  KDE_struct_dirent* ep;
164  DIR* dp = ::opendir( name.data() );
165  if ( !dp )
166  return false;
167  while ( ( ep = KDE_readdir( dp ) ) )
168  {
169  kdDebug() << "CHECKING " << name << "/" << ep->d_name << endl;
170  if ( !qstrcmp( ep->d_name, "." ) || !qstrcmp( ep->d_name, ".." ) )
171  continue;
172  TQCString newName( name );
173  newName += "/"; // Careful: do not add '/' instead or you get problems with Qt3.
174  newName += ep->d_name;
175  /*
176  * Be defensive and close the directory.
177  *
178  * Potential problems:
179  * - opendir/readdir/closedir is not re-entrant
180  * - unlink and rmdir invalidates a opendir/readdir/closedir
181  * - limited number of file descriptors for opendir/readdir/closedir
182  */
183  if ( ::closedir( dp ) )
184  return false;
185  // Recurse!
186  kdDebug() << "RECURSE: " << newName << endl;
187  if ( ! rmtree( newName ) )
188  return false;
189  // We have to re-open the directory before continuing
190  dp = ::opendir( name.data() );
191  if ( !dp )
192  return false;
193  }
194  if ( ::closedir( dp ) )
195  return false;
196  kdDebug() << "RMDIR dir " << name << endl;
197  return ! ::rmdir( name );
198  }
199  else
200  {
201  // This is a non-directory file, so remove it
202  kdDebug() << "UNLINKING file " << name << endl;
203  return ! ::unlink( name );
204  }
205 }
206 
207 bool
208 KTempDir::removeDir(const TQString& path)
209 {
210  kdDebug() << k_funcinfo << " " << path << endl;
211  if ( !TQFile::exists( path ) )
212  return true; // The goal is that there is no directory
213 
214  const TQCString cstr( TQFile::encodeName( path ) );
215  return rmtree( cstr );
216 }
217 
218 
KTempDir::unlink
void unlink()
Deletes the directory recursively.
Definition: ktempdir.cpp:141
KTempDir::KTempDir
KTempDir(TQString directoryPrefix=TQString::null, int mode=0700)
Creates a temporary directory with the name: <directoryPrefix><six letters>
Definition: ktempdir.cpp:59
KTempDir::name
TQString name() const
Returns the full path and name of the directory, including a trailing '/'.
Definition: ktempdir.cpp:122
KTempDir::create
bool create(const TQString &directoryPrefix, int mode)
Creates a "random" directory with specified mode.
Definition: ktempdir.cpp:72
KTempDir::existing
bool existing() const
Definition: ktempdir.cpp:128
KTempDir::qDir
TQDir * qDir()
Returns the TQDir* of the temporary directory.
Definition: ktempdir.cpp:134
KTempDir::status
int status() const
Returns the status of the directory creation based on errno.
Definition: ktempdir.cpp:116
KTempDir::~KTempDir
~KTempDir()
The destructor deletes the directory and it's contents if autoDelete is enabled.
Definition: ktempdir.cpp:106
KTempDir::removeDir
static bool removeDir(const TQString &path)
Remove a directory and all its contents.
Definition: ktempdir.cpp:208
TDEApplication::random
static int random()
Generates a uniform random number.
Definition: tdeapplication.cpp:3393
TDEGlobal::instance
static TDEInstance * instance()
Returns the global instance.
Definition: tdeglobal.cpp:102
endl
kndbgstream & endl(kndbgstream &s)
Does nothing.
Definition: kdebug.h:583
TDEGlobal::kdDebug
kdbgstream kdDebug(int area=0)
Returns a debug stream.
Definition: kdebug.cpp:371
TDEGlobal::endl
kdbgstream & endl(kdbgstream &s)
Prints an "\n".
Definition: kdebug.h:430
KStdAction::name
const char * name(StdAction id)

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.1
This website is maintained by Timothy Pearson.