25 #include <sys/types.h> 27 #ifdef HAVE_SYS_STAT_H 44 #define _PATH_TMP "/tmp" 47 #include <tqdatetime.h> 51 #include "kapplication.h" 52 #include "kinstance.h" 54 #include "kstandarddirs.h" 64 if (directoryPrefix.isEmpty())
68 (void)
create(directoryPrefix , mode);
77 TQCString nme = TQFile::encodeName(directoryPrefix) +
"XXXXXX";
79 if((realName=mkdtemp(nme.data())) == 0)
82 TQCString nme = TQFile::encodeName(directoryPrefix) +
"XXXXXX";
83 qWarning(
"KTempDir: Error trying to create %s: %s", nme.data(), strerror(errno));
85 mTmpName = TQString::null;
90 TQCString realNameStr(realName);
91 mTmpName = TQFile::decodeName(realNameStr)+
"/";
92 kdDebug(180) <<
"KTempDir: Temporary directory created :" << mTmpName <<
endl;
94 mode_t umsk = umask(tmp);
96 chmod(nme, mode&(~umsk));
102 chown(nme, getuid(), getgid());
136 if (bExisting)
return new TQDir(mTmpName);
143 if (!bExisting)
return;
153 rmtree(
const TQCString&
name)
155 kdDebug() <<
"Checking directory for remove " << name <<
endl;
157 if ( KDE_lstat( name.data(), &st ) == -1 )
159 if ( S_ISDIR( st.st_mode ) )
162 kdDebug() <<
"File " << name <<
" is DIRECTORY!" <<
endl;
163 KDE_struct_dirent* ep;
164 DIR* dp = ::opendir( name.data() );
167 while ( ( ep = KDE_readdir( dp ) ) )
169 kdDebug() <<
"CHECKING " << name <<
"/" << ep->d_name <<
endl;
170 if ( !qstrcmp( ep->d_name,
"." ) || !qstrcmp( ep->d_name,
".." ) )
172 TQCString newName( name );
174 newName += ep->d_name;
183 if ( ::closedir( dp ) )
186 kdDebug() <<
"RECURSE: " << newName <<
endl;
187 if ( ! rmtree( newName ) )
190 dp = ::opendir( name.data() );
194 if ( ::closedir( dp ) )
196 kdDebug() <<
"RMDIR dir " << name <<
endl;
197 return ! ::rmdir( name );
202 kdDebug() <<
"UNLINKING file " << name <<
endl;
210 kdDebug() << k_funcinfo <<
" " << path <<
endl;
211 if ( !TQFile::exists( path ) )
214 const TQCString cstr( TQFile::encodeName( path ) );
215 return rmtree( cstr );
TQDir * qDir()
Returns the TQDir* of the temporary directory.
bool create(const TQString &directoryPrefix, int mode)
Creates a "random" directory with specified mode.
void unlink()
Deletes the directory recursively.
~KTempDir()
The destructor deletes the directory and it's contents if autoDelete is enabled.
int status() const
Returns the status of the directory creation based on errno.
static KInstance * instance()
Returns the global instance.
KTempDir(TQString directoryPrefix=TQString::null, int mode=0700)
Creates a temporary directory with the name: <directoryPrefix><six letters>
TQString name() const
Returns the full path and name of the directory, including a trailing '/'.
static int random()
Generates a uniform random number.
static bool removeDir(const TQString &path)
Remove a directory and all its contents.
kndbgstream & endl(kndbgstream &s)
Does nothing.