io/qtemporaryfile.cpp

Switch to Source codePreprocessed file
LineSource CodeCoverage
1 -
2 -
3 -
4 -
5 -
6 -
7 -
8 -
9 -
10 -
11 -
12typedef char Char; -
13typedef char Latin1Char; -
14typedef int NativeFileHandle; -
15static bool createFileFromTemplate(NativeFileHandle &file, -
16 QFileSystemEntry::NativePath &path, size_t pos, size_t length, -
17 QSystemError &error) -
18{ -
19 qt_noop(); -
20 qt_noop(); -
21 qt_noop(); -
22 -
23 Char *const placeholderStart = (Char *)path.data() + pos; -
24 Char *const placeholderEnd = placeholderStart + length; -
25 -
26 -
27 { -
28 Char *rIter = placeholderEnd; -
29 -
30 -
31 quint64 pid = quint64(QCoreApplication::applicationPid()); -
32 do { -
33 *--rIter = Latin1Char((pid % 10) + '0'); -
34 pid /= 10; -
35 } while (rIter != placeholderStart && pid != 0);
executed: }
Execution Count:7264
partially evaluated: rIter != placeholderStart
TRUEFALSE
yes
Evaluation Count:7264
no
Evaluation Count:0
evaluated: pid != 0
TRUEFALSE
yes
Evaluation Count:5784
yes
Evaluation Count:1480
0-7264
36 -
37 -
38 while (rIter != placeholderStart) {
evaluated: rIter != placeholderStart
TRUEFALSE
yes
Evaluation Count:1658
yes
Evaluation Count:1480
1480-1658
39 char ch = char((qrand() & 0xffff) % (26 + 26)); -
40 if (ch < 26)
evaluated: ch < 26
TRUEFALSE
yes
Evaluation Count:835
yes
Evaluation Count:823
823-835
41 *--rIter = Latin1Char(ch + 'A');
executed: *--rIter = Latin1Char(ch + 'A');
Execution Count:835
835
42 else -
43 *--rIter = Latin1Char(ch - 26 + 'a');
executed: *--rIter = Latin1Char(ch - 26 + 'a');
Execution Count:823
823
44 } -
45 } -
46 -
47 for (;;) { -
48 file = qt_safe_open(path.constData(), -
49 0100 | 0200 | 02 | 0, -
50 0600); -
51 -
52 if (file != -1)
evaluated: file != -1
TRUEFALSE
yes
Evaluation Count:1472
yes
Evaluation Count:474537
1472-474537
53 return true;
executed: return true;
Execution Count:1472
1472
54 -
55 int err = (*__errno_location ()); -
56 if (err != 17) {
evaluated: err != 17
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:474529
8-474529
57 error = QSystemError(err, QSystemError::NativeError); -
58 return false;
executed: return false;
Execution Count:8
8
59 } -
60 -
61 -
62 -
63 for (Char *iter = placeholderStart;;) { -
64 -
65 -
66 switch (char(*iter)) { -
67 case 'Z': -
68 -
69 *iter = Latin1Char('a'); -
70 if (++iter == placeholderEnd) {
partially evaluated: ++iter == placeholderEnd
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:9120
0-9120
71 -
72 error = QSystemError(err, QSystemError::NativeError); -
73 return false;
never executed: return false;
0
74 } -
75 -
76 continue;
executed: continue;
Execution Count:9120
9120
77 -
78 case '0': case '1': case '2': case '3': case '4': -
79 case '5': case '6': case '7': case '8': case '9': -
80 *iter = Latin1Char('a'); -
81 break;
executed: break;
Execution Count:948
948
82 -
83 case 'z': -
84 -
85 *iter = Latin1Char('A'); -
86 break;
executed: break;
Execution Count:9127
9127
87 -
88 default: -
89 ++*iter; -
90 break;
executed: break;
Execution Count:464454
464454
91 } -
92 break;
executed: break;
Execution Count:474529
474529
93 } -
94 }
executed: }
Execution Count:474529
474529
95 -
96 qt_noop(); -
97}
never executed: }
0
98 -
99 -
100class QTemporaryFileEngine : public QFSFileEngine -
101{ -
102 inline QFSFileEnginePrivate* d_func() { return reinterpret_cast<QFSFileEnginePrivate *>(qGetPtrHelper(d_ptr)); } inline const QFSFileEnginePrivate* d_func() const { return reinterpret_cast<const QFSFileEnginePrivate *>(qGetPtrHelper(d_ptr)); } friend class QFSFileEnginePrivate;
executed: return reinterpret_cast<QFSFileEnginePrivate *>(qGetPtrHelper(d_ptr));
Execution Count:3257
never executed: return reinterpret_cast<const QFSFileEnginePrivate *>(qGetPtrHelper(d_ptr));
0-3257
103public: -
104 QTemporaryFileEngine(const QString &file, bool fileIsTemplate = true) -
105 : QFSFileEngine(), filePathIsTemplate(fileIsTemplate), -
106 filePathWasTemplate(fileIsTemplate) -
107 { -
108 QFSFileEnginePrivate * const d = d_func(); -
109 d->fileEntry = QFileSystemEntry(file); -
110 -
111 if (!filePathIsTemplate)
partially evaluated: !filePathIsTemplate
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1478
0-1478
112 QFSFileEngine::setFileName(file);
never executed: QFSFileEngine::setFileName(file);
0
113 }
executed: }
Execution Count:1478
1478
114 -
115 ~QTemporaryFileEngine(); -
116 -
117 bool isReallyOpen(); -
118 void setFileName(const QString &file); -
119 void setFileTemplate(const QString &fileTemplate); -
120 -
121 bool open(QIODevice::OpenMode flags); -
122 bool remove(); -
123 bool rename(const QString &newName); -
124 bool close(); -
125 -
126 bool filePathIsTemplate; -
127 bool filePathWasTemplate; -
128}; -
129 -
130QTemporaryFileEngine::~QTemporaryFileEngine() -
131{ -
132 QFSFileEngine::close(); -
133}
executed: }
Execution Count:1477
1477
134 -
135bool QTemporaryFileEngine::isReallyOpen() -
136{ -
137 QFSFileEnginePrivate * const d = d_func(); -
138 -
139 if (!((0 == d->fh) && (-1 == d->fd)
partially evaluated: (0 == d->fh)
TRUEFALSE
yes
Evaluation Count:8
no
Evaluation Count:0
evaluated: (-1 == d->fd)
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:5
0-8
140 -
141 -
142 -
143 )) -
144 return true;
executed: return true;
Execution Count:5
5
145 -
146 return false;
executed: return false;
Execution Count:3
3
147 -
148} -
149 -
150void QTemporaryFileEngine::setFileName(const QString &file) -
151{ -
152 -
153 QFSFileEngine::close(); -
154 QFSFileEngine::setFileName(file); -
155}
executed: }
Execution Count:184
184
156 -
157void QTemporaryFileEngine::setFileTemplate(const QString &fileTemplate) -
158{ -
159 QFSFileEnginePrivate * const d = d_func(); -
160 if (filePathIsTemplate)
evaluated: filePathIsTemplate
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:1
1
161 d->fileEntry = QFileSystemEntry(fileTemplate);
executed: d->fileEntry = QFileSystemEntry(fileTemplate);
Execution Count:1
1
162}
executed: }
Execution Count:2
2
163 -
164bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode) -
165{ -
166 QFSFileEnginePrivate * const d = d_func(); -
167 qt_noop(); -
168 -
169 openMode |= QIODevice::ReadWrite; -
170 -
171 if (!filePathIsTemplate)
evaluated: !filePathIsTemplate
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:1480
2-1480
172 return QFSFileEngine::open(openMode);
executed: return QFSFileEngine::open(openMode);
Execution Count:2
2
173 -
174 QString qfilename = d->fileEntry.filePath(); -
175 -
176 -
177 uint phPos = qfilename.length(); -
178 uint phLength = 0; -
179 -
180 while (phPos != 0) {
evaluated: phPos != 0
TRUEFALSE
yes
Evaluation Count:11622
yes
Evaluation Count:27
27-11622
181 --phPos; -
182 -
183 if (qfilename[phPos] == QLatin1Char('X')) {
evaluated: qfilename[phPos] == QLatin1Char('X')
TRUEFALSE
yes
Evaluation Count:8242
yes
Evaluation Count:3380
3380-8242
184 ++phLength; -
185 continue;
executed: continue;
Execution Count:8242
8242
186 } -
187 -
188 if (phLength >= 6
evaluated: phLength >= 6
TRUEFALSE
yes
Evaluation Count:1344
yes
Evaluation Count:2036
1344-2036
189 || qfilename[phPos] == QLatin1Char('/')) {
evaluated: qfilename[phPos] == QLatin1Char('/')
TRUEFALSE
yes
Evaluation Count:109
yes
Evaluation Count:1927
109-1927
190 ++phPos; -
191 break;
executed: break;
Execution Count:1453
1453
192 } -
193 -
194 -
195 phLength = 0; -
196 }
executed: }
Execution Count:1927
1927
197 -
198 if (phLength < 6)
evaluated: phLength < 6
TRUEFALSE
yes
Evaluation Count:130
yes
Evaluation Count:1350
130-1350
199 qfilename.append(QLatin1String(".XXXXXX"));
executed: qfilename.append(QLatin1String(".XXXXXX"));
Execution Count:130
130
200 -
201 -
202 QFileSystemEntry::NativePath filename = QFileSystemEngine::absoluteName( -
203 QFileSystemEntry(qfilename, QFileSystemEntry::FromInternalPath())) -
204 .nativeFilePath(); -
205 -
206 -
207 phPos = filename.length(); -
208 phLength = 0; -
209 while (phPos != 0) {
partially evaluated: phPos != 0
TRUEFALSE
yes
Evaluation Count:10682
no
Evaluation Count:0
0-10682
210 --phPos; -
211 -
212 if (filename[phPos] == Latin1Char('X')) {
evaluated: filename[phPos] == Latin1Char('X')
TRUEFALSE
yes
Evaluation Count:8941
yes
Evaluation Count:1741
1741-8941
213 ++phLength; -
214 continue;
executed: continue;
Execution Count:8941
8941
215 } -
216 -
217 if (phLength >= 6) {
evaluated: phLength >= 6
TRUEFALSE
yes
Evaluation Count:1480
yes
Evaluation Count:261
261-1480
218 ++phPos; -
219 break;
executed: break;
Execution Count:1480
1480
220 } -
221 -
222 -
223 phLength = 0; -
224 }
executed: }
Execution Count:261
261
225 -
226 qt_noop(); -
227 -
228 QSystemError error; -
229 -
230 -
231 -
232 NativeFileHandle &file = d->fd; -
233 -
234 -
235 if (!createFileFromTemplate(file, filename, phPos, phLength, error)) {
evaluated: !createFileFromTemplate(file, filename, phPos, phLength, error)
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:1472
8-1472
236 setError(QFile::OpenError, error.toString()); -
237 return false;
executed: return false;
Execution Count:8
8
238 } -
239 -
240 d->fileEntry = QFileSystemEntry(filename, QFileSystemEntry::FromNativePath()); -
241 -
242 -
243 d->closeFileHandle = true; -
244 -
245 -
246 filePathIsTemplate = false; -
247 -
248 d->openMode = openMode; -
249 d->lastFlushFailed = false; -
250 d->tried_stat = 0; -
251 -
252 return true;
executed: return true;
Execution Count:1472
1472
253} -
254 -
255bool QTemporaryFileEngine::remove() -
256{ -
257 QFSFileEnginePrivate * const d = d_func(); -
258 -
259 -
260 QFSFileEngine::close(); -
261 if (QFSFileEngine::remove()) {
evaluated: QFSFileEngine::remove()
TRUEFALSE
yes
Evaluation Count:286
yes
Evaluation Count:1
1-286
262 d->fileEntry.clear(); -
263 -
264 -
265 -
266 -
267 -
268 filePathIsTemplate = filePathWasTemplate; -
269 return true;
executed: return true;
Execution Count:286
286
270 } -
271 return false;
executed: return false;
Execution Count:1
1
272} -
273 -
274bool QTemporaryFileEngine::rename(const QString &newName) -
275{ -
276 QFSFileEngine::close(); -
277 return QFSFileEngine::rename(newName);
executed: return QFSFileEngine::rename(newName);
Execution Count:185
185
278} -
279 -
280bool QTemporaryFileEngine::close() -
281{ -
282 -
283 seek(0); -
284 setError(QFile::UnspecifiedError, QString()); -
285 return true;
executed: return true;
Execution Count:1477
1477
286} -
287 -
288 -
289class QTemporaryFilePrivate : public QFilePrivate -
290{ -
291 inline QTemporaryFile* q_func() { return static_cast<QTemporaryFile *>(q_ptr); } inline const QTemporaryFile* q_func() const { return static_cast<const QTemporaryFile *>(q_ptr); } friend class QTemporaryFile; -
292 -
293protected: -
294 QTemporaryFilePrivate(); -
295 ~QTemporaryFilePrivate(); -
296 -
297 QAbstractFileEngine *engine() const; -
298 -
299 bool autoRemove; -
300 QString templateName; -
301}; -
302 -
303QTemporaryFilePrivate::QTemporaryFilePrivate() : autoRemove(true) -
304{ -
305}
executed: }
Execution Count:1485
1485
306 -
307QTemporaryFilePrivate::~QTemporaryFilePrivate() -
308{ -
309} -
310 -
311QAbstractFileEngine *QTemporaryFilePrivate::engine() const -
312{ -
313 if (!fileEngine) {
evaluated: !fileEngine
TRUEFALSE
yes
Evaluation Count:1478
yes
Evaluation Count:3091
1478-3091
314 if (fileName.isEmpty())
partially evaluated: fileName.isEmpty()
TRUEFALSE
yes
Evaluation Count:1478
no
Evaluation Count:0
0-1478
315 fileEngine = new QTemporaryFileEngine(templateName);
executed: fileEngine = new QTemporaryFileEngine(templateName);
Execution Count:1478
1478
316 else -
317 fileEngine = new QTemporaryFileEngine(fileName, false);
never executed: fileEngine = new QTemporaryFileEngine(fileName, false);
0
318 } -
319 return fileEngine;
executed: return fileEngine;
Execution Count:4569
4569
320} -
321 -
322static QString defaultTemplateName() -
323{ -
324 QString baseName; -
325 -
326 baseName = QCoreApplication::applicationName(); -
327 if (baseName.isEmpty())
partially evaluated: baseName.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1028
0-1028
328 -
329 baseName = QLatin1String("qt_temp");
never executed: baseName = QLatin1String("qt_temp");
0
330 -
331 return QDir::tempPath() + QLatin1Char('/') + baseName + QLatin1String(".XXXXXX");
executed: return QDir::tempPath() + QLatin1Char('/') + baseName + QLatin1String(".XXXXXX");
Execution Count:1028
1028
332} -
333QTemporaryFile::QTemporaryFile() -
334 : QFile(*new QTemporaryFilePrivate, 0) -
335{ -
336 QTemporaryFilePrivate * const d = d_func(); -
337 d->templateName = defaultTemplateName(); -
338}
executed: }
Execution Count:1027
1027
339QTemporaryFile::QTemporaryFile(const QString &templateName) -
340 : QFile(*new QTemporaryFilePrivate, 0) -
341{ -
342 QTemporaryFilePrivate * const d = d_func(); -
343 d->templateName = templateName; -
344}
executed: }
Execution Count:371
371
345QTemporaryFile::QTemporaryFile(QObject *parent) -
346 : QFile(*new QTemporaryFilePrivate, parent) -
347{ -
348 QTemporaryFilePrivate * const d = d_func(); -
349 d->templateName = defaultTemplateName(); -
350}
executed: }
Execution Count:1
1
351QTemporaryFile::QTemporaryFile(const QString &templateName, QObject *parent) -
352 : QFile(*new QTemporaryFilePrivate, parent) -
353{ -
354 QTemporaryFilePrivate * const d = d_func(); -
355 d->templateName = templateName; -
356}
executed: }
Execution Count:86
86
357QTemporaryFile::~QTemporaryFile() -
358{ -
359 QTemporaryFilePrivate * const d = d_func(); -
360 close(); -
361 if (!d->fileName.isEmpty() && d->autoRemove)
evaluated: !d->fileName.isEmpty()
TRUEFALSE
yes
Evaluation Count:1470
yes
Evaluation Count:12
evaluated: d->autoRemove
TRUEFALSE
yes
Evaluation Count:286
yes
Evaluation Count:1184
12-1470
362 remove();
executed: remove();
Execution Count:286
286
363}
executed: }
Execution Count:1482
1482
364bool QTemporaryFile::autoRemove() const -
365{ -
366 const QTemporaryFilePrivate * const d = d_func(); -
367 return d->autoRemove;
executed: return d->autoRemove;
Execution Count:2
2
368} -
369void QTemporaryFile::setAutoRemove(bool b) -
370{ -
371 QTemporaryFilePrivate * const d = d_func(); -
372 d->autoRemove = b; -
373}
executed: }
Execution Count:1206
1206
374QString QTemporaryFile::fileName() const -
375{ -
376 const QTemporaryFilePrivate * const d = d_func(); -
377 if(d->fileName.isEmpty())
evaluated: d->fileName.isEmpty()
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:2178
6-2178
378 return QString();
executed: return QString();
Execution Count:6
6
379 return d->engine()->fileName(QAbstractFileEngine::DefaultName);
executed: return d->engine()->fileName(QAbstractFileEngine::DefaultName);
Execution Count:2178
2178
380} -
381 -
382 -
383 -
384 -
385 -
386 -
387 -
388QString QTemporaryFile::fileTemplate() const -
389{ -
390 const QTemporaryFilePrivate * const d = d_func(); -
391 return d->templateName;
executed: return d->templateName;
Execution Count:10
10
392} -
393void QTemporaryFile::setFileTemplate(const QString &name) -
394{ -
395 QTemporaryFilePrivate * const d = d_func(); -
396 d->templateName = name; -
397 if (d->fileEngine)
evaluated: d->fileEngine
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:6
2-6
398 static_cast<QTemporaryFileEngine*>(d->fileEngine)->setFileTemplate(name);
executed: static_cast<QTemporaryFileEngine*>(d->fileEngine)->setFileTemplate(name);
Execution Count:2
2
399}
executed: }
Execution Count:8
8
400QTemporaryFile *QTemporaryFile::createNativeFile(QFile &file) -
401{ -
402 if (QAbstractFileEngine *engine = file.d_func()->engine()) {
partially evaluated: QAbstractFileEngine *engine = file.d_func()->engine()
TRUEFALSE
yes
Evaluation Count:4
no
Evaluation Count:0
0-4
403 if(engine->fileFlags(QAbstractFileEngine::FlagsMask) & QAbstractFileEngine::LocalDiskFlag)
evaluated: engine->fileFlags(QAbstractFileEngine::FlagsMask) & QAbstractFileEngine::LocalDiskFlag
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:2
2
404 return 0;
executed: return 0;
Execution Count:2
2
405 -
406 bool wasOpen = file.isOpen(); -
407 qint64 old_off = 0; -
408 if(wasOpen)
evaluated: wasOpen
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:1
1
409 old_off = file.pos();
executed: old_off = file.pos();
Execution Count:1
1
410 else -
411 file.open(QIODevice::ReadOnly);
executed: file.open(QIODevice::ReadOnly);
Execution Count:1
1
412 -
413 QTemporaryFile *ret = new QTemporaryFile; -
414 ret->open(); -
415 file.seek(0); -
416 char buffer[1024]; -
417 while(true) {
partially evaluated: true
TRUEFALSE
yes
Evaluation Count:4
no
Evaluation Count:0
0-4
418 qint64 len = file.read(buffer, 1024); -
419 if(len < 1)
evaluated: len < 1
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:2
2
420 break;
executed: break;
Execution Count:2
2
421 ret->write(buffer, len); -
422 }
executed: }
Execution Count:2
2
423 ret->seek(0); -
424 -
425 if(wasOpen)
evaluated: wasOpen
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:1
1
426 file.seek(old_off);
executed: file.seek(old_off);
Execution Count:1
1
427 else -
428 file.close();
executed: file.close();
Execution Count:1
1
429 -
430 return ret;
executed: return ret;
Execution Count:2
2
431 } -
432 return 0;
never executed: return 0;
0
433} -
434bool QTemporaryFile::open(OpenMode flags) -
435{ -
436 QTemporaryFilePrivate * const d = d_func(); -
437 if (!d->fileName.isEmpty()) {
evaluated: !d->fileName.isEmpty()
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:1479
8-1479
438 if (static_cast<QTemporaryFileEngine*>(d->engine())->isReallyOpen()) {
evaluated: static_cast<QTemporaryFileEngine*>(d->engine())->isReallyOpen()
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:3
3-5
439 setOpenMode(flags); -
440 return true;
executed: return true;
Execution Count:5
5
441 } -
442 }
executed: }
Execution Count:3
3
443 -
444 if (QFile::open(flags)) {
evaluated: QFile::open(flags)
TRUEFALSE
yes
Evaluation Count:1474
yes
Evaluation Count:8
8-1474
445 d->fileName = d->fileEngine->fileName(QAbstractFileEngine::DefaultName); -
446 return true;
executed: return true;
Execution Count:1474
1474
447 } -
448 return false;
executed: return false;
Execution Count:8
8
449} -
450 -
451 -
452 -
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial