Qt 4.8
Classes | Typedefs | Functions
qtemporaryfile.cpp File Reference
#include "qtemporaryfile.h"
#include "qplatformdefs.h"
#include "private/qfile_p.h"
#include "private/qfsfileengine_p.h"
#include "private/qsystemerror_p.h"
#include "private/qfilesystemengine_p.h"

Go to the source code of this file.

Classes

class  QTemporaryFileEngine
 
class  QTemporaryFilePrivate
 

Typedefs

typedef ushort Char
 
typedef HANDLE NativeFileHandle
 

Functions

static bool createFileFromTemplate (NativeFileHandle &file, QFileSystemEntry::NativePath &path, size_t pos, size_t length, QSystemError &error)
 Generates a unique file path and returns a native handle to the open file. More...
 
static Char Latin1Char (char ch)
 

Typedef Documentation

◆ Char

typedef ushort Char

Definition at line 68 of file qtemporaryfile.cpp.

◆ NativeFileHandle

typedef HANDLE NativeFileHandle

Definition at line 76 of file qtemporaryfile.cpp.

Function Documentation

◆ createFileFromTemplate()

static bool createFileFromTemplate ( NativeFileHandle file,
QFileSystemEntry::NativePath path,
size_t  pos,
size_t  length,
QSystemError error 
)
static

Generates a unique file path and returns a native handle to the open file.

Warning
This function is not part of the public interface.

path is used as a template when generating unique paths, pos identifies the position of the first character that will be replaced in the template and length the number of characters that may be substituted.

Returns an open handle to the newly created file if successful, an invalid handle otherwise. In both cases, the string in path will be changed and contain the generated path name.

Definition at line 135 of file qtemporaryfile.cpp.

Referenced by QTemporaryFileEngine::open().

138 {
139  Q_ASSERT(length != 0);
140  Q_ASSERT(pos < size_t(path.length()));
141  Q_ASSERT(length <= size_t(path.length()) - pos);
142 
143  Char *const placeholderStart = (Char *)path.data() + pos;
144  Char *const placeholderEnd = placeholderStart + length;
145 
146  // Initialize placeholder with random chars + PID.
147  {
148  Char *rIter = placeholderEnd;
149 
150 #if defined(QT_BUILD_CORE_LIB)
152  do {
153  *--rIter = Latin1Char((pid % 10) + '0');
154  pid /= 10;
155  } while (rIter != placeholderStart && pid != 0);
156 #endif
157 
158  while (rIter != placeholderStart) {
159  char ch = char((qrand() & 0xffff) % (26 + 26));
160  if (ch < 26)
161  *--rIter = Latin1Char(ch + 'A');
162  else
163  *--rIter = Latin1Char(ch - 26 + 'a');
164  }
165  }
166 
167 #ifdef Q_OS_SYMBIAN
168  RFs& fs = qt_s60GetRFs();
169 #endif
170 
171  for (;;) {
172  // Atomically create file and obtain handle
173 #if defined(Q_OS_WIN)
174  file = CreateFile((const wchar_t *)path.constData(),
175  GENERIC_READ | GENERIC_WRITE,
176  FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_NEW,
177  FILE_ATTRIBUTE_NORMAL, NULL);
178 
179  if (file != INVALID_HANDLE_VALUE)
180  return true;
181 
182  DWORD err = GetLastError();
183  if (err != ERROR_FILE_EXISTS) {
185  return false;
186  }
187 #elif defined(Q_OS_SYMBIAN)
188  TInt err = file.Create(fs, qt_QString2TPtrC(path),
189  EFileRead | EFileWrite | EFileShareReadersOrWriters);
190 
191  if (err == KErrNone)
192  return true;
193 
194  if (err != KErrAlreadyExists) {
196  return false;
197  }
198 #else // POSIX
199  file = QT_OPEN(path.constData(),
200  QT_OPEN_CREAT | O_EXCL | QT_OPEN_RDWR | QT_OPEN_LARGEFILE,
201  0600);
202 
203  if (file != -1)
204  return true;
205 
206  int err = errno;
207  if (err != EEXIST) {
209  return false;
210  }
211 #endif
212 
213  /* tricky little algorwwithm for backward compatibility */
214  for (Char *iter = placeholderStart;;) {
215  // Character progression: [0-9] => 'a' ... 'z' => 'A' .. 'Z'
216  // String progression: "ZZaiC" => "aabiC"
217  switch (char(*iter)) {
218  case 'Z':
219  // Rollover, advance next character
220  *iter = Latin1Char('a');
221  if (++iter == placeholderEnd) {
222  // Out of alternatives. Return file exists error, previously set.
224  return false;
225  }
226 
227  continue;
228 
229  case '0': case '1': case '2': case '3': case '4':
230  case '5': case '6': case '7': case '8': case '9':
231  *iter = Latin1Char('a');
232  break;
233 
234  case 'z':
235  // increment 'z' to 'A'
236  *iter = Latin1Char('A');
237  break;
238 
239  default:
240  ++*iter;
241  break;
242  }
243  break;
244  }
245  }
246 
247  Q_ASSERT(false);
248 }
static Char Latin1Char(char ch)
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
Q_CORE_EXPORT int qrand()
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QChar * data()
Returns a pointer to the data stored in the QString.
Definition: qstring.h:710
unsigned __int64 quint64
Definition: qglobal.h:943
static qint64 applicationPid()
Returns the current process ID for the application.
ushort Char
#define QT_OPEN
Definition: qcore_unix_p.h:186
#define O_EXCL
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712
int errno

◆ Latin1Char()

static Char Latin1Char ( char  ch)
inlinestatic

Definition at line 70 of file qtemporaryfile.cpp.

Referenced by createFileFromTemplate(), and QTemporaryFileEngine::open().

71 {
72  return ushort(uchar(ch));
73 }
unsigned char uchar
Definition: qglobal.h:994
unsigned short ushort
Definition: qglobal.h:995