src/corelib/io/qtemporaryfile.cpp File Reference

#include "qplatformdefs.h"
#include "qtemporaryfile.h"
#include "qabstractfileengine.h"
#include "private/qfile_p.h"
#include "private/qabstractfileengine_p.h"
#include "private/qfsfileengine_p.h"
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <ctype.h>

Include dependency graph for qtemporaryfile.cpp:

Go to the source code of this file.

Classes

class  QTemporaryFileEngine
class  QTemporaryFilePrivate

Defines

#define S_ISDIR(m)   (((m) & S_IFMT) == S_IFDIR)

Functions

static int _gettemp (char *path, int *doopen, int domkdir, int slen)
static int qt_mkstemps (char *path, int slen)


Define Documentation

#define S_ISDIR (  )     (((m) & S_IFMT) == S_IFDIR)

Referenced by _gettemp(), QKqueueFileSystemWatcherEngine::addPaths(), QFSFileEngine::fileName(), and qws_dataDir().


Function Documentation

static int _gettemp ( char *  path,
int *  doopen,
int  domkdir,
int  slen 
) [static]

Definition at line 76 of file qtemporaryfile.cpp.

References c, qrand(), S_ISDIR, and start.

Referenced by qt_mkstemps().

00077 {
00078   char *start, *trv, *suffp;
00079   struct stat sbuf;
00080   int rval;
00081 #ifdef Q_OS_WIN
00082     int pid;
00083 #else
00084   pid_t pid;
00085 #endif
00086 
00087   if (doopen && domkdir) {
00088     errno = EINVAL;
00089     return(0);
00090   }
00091 
00092   for (trv = path; *trv; ++trv)
00093     ;
00094   trv -= slen;
00095   suffp = trv;
00096   --trv;
00097   if (trv < path) {
00098     errno = EINVAL;
00099     return (0);
00100   }
00101 #if defined(Q_OS_WIN) && defined(_MSC_VER) && _MSC_VER >= 1400
00102         pid = _getpid();
00103 #else
00104         pid = getpid();
00105 #endif
00106   while (trv >= path && *trv == 'X' && pid != 0) {
00107     *trv-- = (pid % 10) + '0';
00108     pid /= 10;
00109         }
00110 
00111 #ifndef S_ISDIR
00112 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
00113 #endif
00114 
00115         while (trv >= path && *trv == 'X') {
00116             char c;
00117 
00118             // CHANGE arc4random() -> random()
00119             pid = (qrand() & 0xffff) % (26+26);
00120     if (pid < 26)
00121       c = pid + 'A';
00122     else
00123       c = (pid - 26) + 'a';
00124     *trv-- = c;
00125   }
00126   start = trv + 1;
00127 
00128   /*
00129    * check the target directory; if you have six X's and it
00130    * doesn't exist this runs for a *very* long time.
00131    */
00132   if (doopen || domkdir) {
00133     for (;; --trv) {
00134       if (trv <= path)
00135         break;
00136       if (*trv == '/') {
00137         *trv = '\0';
00138         rval = stat(path, &sbuf);
00139         *trv = '/';
00140         if (rval != 0)
00141           return(0);
00142         if (!S_ISDIR(sbuf.st_mode)) {
00143           errno = ENOTDIR;
00144           return(0);
00145         }
00146         break;
00147       }
00148     }
00149   }
00150 
00151   for (;;) {
00152     if (doopen) {
00153 #if defined(Q_OS_WIN) && defined(_MSC_VER) && _MSC_VER >= 1400
00154                         if (_sopen_s(doopen, path, O_CREAT|O_EXCL|O_RDWR|O_BINARY, _SH_DENYNO, _S_IREAD | _S_IWRITE)== 0)
00155 #else
00156                         if ((*doopen =
00157                             open(path, O_CREAT|O_EXCL|O_RDWR
00158 #  if defined(Q_OS_WIN)
00159                                  |O_BINARY
00160 #  endif
00161                                  , 0600)) >= 0)
00162 #endif
00163 
00164         return(1);
00165       if (errno != EEXIST)
00166         return(0);
00167     } else if (domkdir) {
00168 #ifdef Q_OS_WIN
00169                     if (QT_MKDIR(path) == 0)
00170 #else
00171                     if (mkdir(path, 0700) == 0)
00172 #endif
00173         return(1);
00174       if (errno != EEXIST)
00175         return(0);
00176             }
00177 #ifndef Q_OS_WIN
00178             else if (lstat(path, &sbuf))
00179       return(errno == ENOENT ? 1 : 0);
00180 #endif
00181 
00182     /* tricky little algorwwithm for backward compatibility */
00183     for (trv = start;;) {
00184       if (!*trv)
00185         return (0);
00186       if (*trv == 'Z') {
00187         if (trv == suffp)
00188           return (0);
00189         *trv++ = 'a';
00190       } else {
00191         if (isdigit(*trv))
00192           *trv = 'a';
00193         else if (*trv == 'z') /* inc from z to A */
00194           *trv = 'A';
00195         else {
00196           if (trv == suffp)
00197             return (0);
00198           ++*trv;
00199         }
00200         break;
00201       }
00202     }
00203   }
00204   /*NOTREACHED*/
00205 }

Here is the call graph for this function:

static int qt_mkstemps ( char *  path,
int  slen 
) [static]

Definition at line 207 of file qtemporaryfile.cpp.

References _gettemp().

Referenced by QTemporaryFileEngine::open().

00208 {
00209   int fd;
00210   return (_gettemp(path, &fd, 0, slen) ? fd : -1);
00211 }

Here is the call graph for this function:


Generated on Thu Mar 15 12:37:13 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1