io/qfile.cpp

Switch to Source codePreprocessed file
LineSource CodeCoverage
1 -
2 -
3 -
4 -
5 -
6 -
7 -
8 -
9 -
10 -
11QFilePrivate::QFilePrivate() -
12{ -
13} -
14 -
15QFilePrivate::~QFilePrivate() -
16{ -
17} -
18 -
19bool -
20QFilePrivate::openExternalFile(int flags, int fd, QFile::FileHandleFlags handleFlags) -
21{ -
22 -
23 -
24 -
25 -
26 -
27 delete fileEngine; -
28 fileEngine = 0; -
29 QFSFileEngine *fe = new QFSFileEngine; -
30 fileEngine = fe; -
31 return fe->open(QIODevice::OpenMode(flags), fd, handleFlags);
-
32 -
33} -
34 -
35bool -
36QFilePrivate::openExternalFile(int flags, FILE *fh, QFile::FileHandleFlags handleFlags) -
37{ -
38 -
39 -
40 -
41 -
42 -
43 delete fileEngine; -
44 fileEngine = 0; -
45 QFSFileEngine *fe = new QFSFileEngine; -
46 fileEngine = fe; -
47 return fe->open(QIODevice::OpenMode(flags), fh, handleFlags);
-
48 -
49} -
50 -
51QAbstractFileEngine *QFilePrivate::engine() const -
52{ -
53 if (!fileEngine)
-
54 fileEngine = QAbstractFileEngine::create(fileName);
-
55 return fileEngine;
-
56} -
57QFile::QFile() -
58 : QFileDevice(*new QFilePrivate, 0) -
59{ -
60}
-
61 -
62 -
63 -
64QFile::QFile(QObject *parent) -
65 : QFileDevice(*new QFilePrivate, parent) -
66{ -
67}
-
68 -
69 -
70 -
71QFile::QFile(const QString &name) -
72 : QFileDevice(*new QFilePrivate, 0) -
73{ -
74 QFilePrivate * const d = d_func(); -
75 d->fileName = name; -
76}
-
77 -
78 -
79 -
80 -
81QFile::QFile(const QString &name, QObject *parent) -
82 : QFileDevice(*new QFilePrivate, parent) -
83{ -
84 QFilePrivate * const d = d_func(); -
85 d->fileName = name; -
86}
-
87 -
88 -
89 -
90QFile::QFile(QFilePrivate &dd, QObject *parent) -
91 : QFileDevice(dd, parent) -
92{ -
93}
-
94 -
95 -
96 -
97 -
98 -
99QFile::~QFile() -
100{ -
101} -
102 -
103 -
104 -
105 -
106 -
107 -
108 -
109QString QFile::fileName() const -
110{ -
111 const QFilePrivate * const d = d_func(); -
112 return d->engine()->fileName(QAbstractFileEngine::DefaultName);
-
113} -
114void -
115QFile::setFileName(const QString &name) -
116{ -
117 QFilePrivate * const d = d_func(); -
118 if (isOpen()) {
-
119 QMessageLogger("io/qfile.cpp", 318319, __PRETTY_FUNCTION__).warning("QFile::setFileName: File (%s) is already opened", -
120 QString(fileName()).toLocal8Bit().constData()); -
121 close(); -
122 }
-
123 if(d->fileEngine) {
-
124 delete d->fileEngine; -
125 d->fileEngine = 0; -
126 }
-
127 d->fileName = name; -
128}
-
129bool -
130QFile::exists() const -
131{ -
132 const QFilePrivate * const d = d_func(); -
133 -
134 return (d->engine()->fileFlags(QAbstractFileEngine::FlagsMask -
135 | QAbstractFileEngine::FileFlag(0x1000000)) & QAbstractFileEngine::ExistsFlag);
-
136} -
137 -
138 -
139 -
140 -
141 -
142 -
143bool -
144QFile::exists(const QString &fileName) -
145{ -
146 return QFileInfo(fileName).exists();
-
147} -
148QString -
149QFile::readLink() const -
150{ -
151 const QFilePrivate * const d = d_func(); -
152 return d->engine()->fileName(QAbstractFileEngine::LinkName);
-
153} -
154QString -
155QFile::readLink(const QString &fileName) -
156{ -
157 return QFileInfo(fileName).readLink();
-
158} -
159bool -
160QFile::remove() -
161{ -
162 QFilePrivate * const d = d_func(); -
163 if (d->fileName.isEmpty()) {
-
164 QMessageLogger("io/qfile.cpp", 495496, __PRETTY_FUNCTION__).warning("QFile::remove: Empty or null file name"); -
165 return false;
-
166 } -
167 unsetError(); -
168 close(); -
169 if(error() == QFile::NoError) {
-
170 if (d->engine()->remove()) {
-
171 unsetError(); -
172 return true;
-
173 } -
174 d->setError(QFile::RemoveError, d->fileEngine->errorString()); -
175 }
-
176 return false;
-
177} -
178bool -
179QFile::remove(const QString &fileName) -
180{ -
181 return QFile(fileName).remove();
-
182} -
183bool -
184QFile::rename(const QString &newName) -
185{ -
186 QFilePrivate * const d = d_func(); -
187 if (d->fileName.isEmpty()) {
partially evaluated: d->fileName.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:211
0-211
188 QMessageLogger("io/qfile.cpp", 548549, __PRETTY_FUNCTION__).warning("QFile::rename: Empty or null file name"); -
189 return false;
never executed: return false;
0
190 } -
191 if (d->fileName == newName) {
evaluated: d->fileName == newName
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:210
1-210
192 d->setError(QFile::RenameError, tr(newName)."Destination file is the same file.")); -
193 return false;
executed: return false;
Execution Count:1
1
194 } -
195 if (!exists()) {
evaluated: !exists()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:208
2-208
196 d->setError(QFile::RenameError, tr("Source file does not exist.")); -
197 return false;
executed: return false;
Execution Count:2
2
198 } -
199 -
200 -
201 if (QFile::exists(newName)
evaluated: QFile::exists(newName)
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:204
4-204
202 && (d->fileName.compare(newName, Qt::CaseInsensitive)
partially evaluated: d->fileName.compare(newName, Qt::CaseInsensitive)
TRUEFALSE
yes
Evaluation Count:4
no
Evaluation Count:0
0-4
203 || QFileSystemEngine::id(QFileSystemEntry(d->fileName)) != QFileSystemEngine::id(QFileSystemEntry(newName)))) {
never evaluated: QFileSystemEngine::id(QFileSystemEntry(d->fileName)) != QFileSystemEngine::id(QFileSystemEntry(newName))
0
204 -
205 -
206 -
207 d->setError(QFile::RenameError, tr("Destination file exists")); -
208 return false;
executed: return false;
Execution Count:4
4
209 } -
210 unsetError(); -
211 close(); -
212 if(error() == QFile::NoError) {
partially evaluated: error() == QFile::NoError
TRUEFALSE
yes
Evaluation Count:204
no
Evaluation Count:0
0-204
213 if (d->engine()->rename(newName)) {
evaluated: d->engine()->rename(newName)
TRUEFALSE
yes
Evaluation Count:200
yes
Evaluation Count:4
4-200
214 unsetError(); -
215 -
216 d->fileEngine->setFileName(newName); -
217 d->fileName = newName; -
218 return true;
executed: return true;
Execution Count:200
200
219 } -
220 -
221 if (isSequential()) {
partially evaluated: isSequential()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:4
0-4
222 d->setError(QFile::RenameError, tr("Will not rename sequential file using block copy")); -
223 return false;
never executed: return false;
0
224 } -
225 -
226 QFile out(newName); -
227 if (open(QIODevice::ReadOnly)) {
partially evaluated: open(QIODevice::ReadOnly)
TRUEFALSE
yes
Evaluation Count:4
no
Evaluation Count:0
0-4
228 if (out.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
evaluated: out.open(QIODevice::WriteOnly | QIODevice::Truncate)
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:3
1-3
229 bool error = false; -
230 char block[4096]; -
231 qint64 bytes; -
232 while ((bytes = read(block, sizeof(block))) > 0) {
evaluated: (bytes = read(block, sizeof(block))) > 0
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:1
1
233 if (bytes != out.write(block, bytes)) {
partially evaluated: bytes != out.write(block, bytes)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-1
234 d->setError(QFile::RenameError, out.errorString()); -
235 error = true; -
236 break;
never executed: break;
0
237 } -
238 }
executed: }
Execution Count:1
1
239 if (bytes == -1) {
partially evaluated: bytes == -1
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-1
240 d->setError(QFile::RenameError, errorString()); -
241 error = true; -
242 }
never executed: }
0
243 if(!error) {
partially evaluated: !error
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
244 if (!remove()) {
partially evaluated: !remove()
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
245 d->setError(QFile::RenameError, tr("Cannot remove source file")); -
246 error = true; -
247 }
executed: }
Execution Count:1
1
248 }
executed: }
Execution Count:1
1
249 if (error) {
partially evaluated: error
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
250 out.remove(); -
251 } else {
executed: }
Execution Count:1
1
252 d->fileEngine->setFileName(newName); -
253 setPermissions(permissions()); -
254 unsetError(); -
255 setFileName(newName); -
256 }
never executed: }
0
257 close(); -
258 return !error;
executed: return !error;
Execution Count:1
1
259 } -
260 close(); -
261 }
executed: }
Execution Count:3
3
262 d->setError(QFile::RenameError, out.isOpen() ? errorString() : out.errorString()); -
263 }
executed: }
Execution Count:3
3
264 return false;
executed: return false;
Execution Count:3
3
265} -
266bool -
267QFile::rename(const QString &oldName, const QString &newName) -
268{ -
269 return QFile(oldName).rename(newName);
-
270} -
271bool -
272QFile::link(const QString &linkName) -
273{ -
274 QFilePrivate * const d = d_func(); -
275 if (d->fileName.isEmpty()) {
-
276 QMessageLogger("io/qfile.cpp", 654667, __PRETTY_FUNCTION__).warning("QFile::link: Empty or null file name"); -
277 return false;
-
278 } -
279 QFileInfo fi(linkName); -
280 if (d->engine()->link(fi.absoluteFilePath())) {
-
281 unsetError(); -
282 return true;
-
283 } -
284 d->setError(QFile::RenameError, d->fileEngine->errorString()); -
285 return false;
-
286} -
287bool -
288QFile::link(const QString &fileName, const QString &linkName) -
289{ -
290 return QFile(fileName).link(linkName);
-
291} -
292bool -
293QFile::copy(const QString &newName) -
294{ -
295 QFilePrivate * const d = d_func(); -
296 if (d->fileName.isEmpty()) {
-
297 QMessageLogger("io/qfile.cpp", 700713, __PRETTY_FUNCTION__).warning("QFile::copy: Empty or null file name"); -
298 return false;
-
299 } -
300 if (QFile(newName).exists()) {
-
301 -
302 -
303 -
304 d->setError(QFile::CopyError, tr("Destination file exists")); -
305 return false;
-
306 } -
307 unsetError(); -
308 close(); -
309 if(error() == QFile::NoError) {
-
310 if (d->engine()->copy(newName)) {
-
311 unsetError(); -
312 return true;
-
313 } else { -
314 bool error = false; -
315 if(!open(QFile::ReadOnly)) {
-
316 error = true; -
317 d->setError(QFile::CopyError, tr("Cannot open %1 for input").arg(d->fileName)); -
318 } else {
-
319 QString fileTemplate = QLatin1String("%1/qt_temp.XXXXXX"); -
320 -
321 -
322 -
323 -
324 -
325 QTemporaryFile out(fileTemplate.arg(QFileInfo(newName).path())); -
326 if (!out.open()) {
-
327 out.setFileTemplate(fileTemplate.arg(QDir::tempPath())); -
328 if (!out.open())
-
329 error = true;
-
330 }
-
331 -
332 if (error) {
-
333 out.close(); -
334 close(); -
335 d->setError(QFile::CopyError, tr("Cannot open for output")); -
336 } else {
-
337 char block[4096]; -
338 qint64 totalRead = 0; -
339 while(!atEnd()) {
-
340 qint64 in = read(block, sizeof(block)); -
341 if (in <= 0)
-
342 break;
-
343 totalRead += in; -
344 if(in != out.write(block, in)) {
-
345 close(); -
346 d->setError(QFile::CopyError, tr("Failure to write block")); -
347 error = true; -
348 break;
-
349 } -
350 }
-
351 -
352 if (totalRead != size()) {
-
353 -
354 -
355 error = true; -
356 }
-
357 if (!error && !out.rename(newName)) {
-
358 error = true; -
359 close(); -
360 d->setError(QFile::CopyError, tr("Cannot create %1 for output").arg(newName)); -
361 }
-
362 -
363 -
364 -
365 -
366 if (!error)
-
367 out.setAutoRemove(false);
-
368 -
369 }
-
370 } -
371 if(!error) {
-
372 QFile::setPermissions(newName, permissions()); -
373 close(); -
374 unsetError(); -
375 return true;
-
376 } -
377 }
-
378 } -
379 return false;
-
380} -
381bool -
382QFile::copy(const QString &fileName, const QString &newName) -
383{ -
384 return QFile(fileName).copy(newName);
-
385} -
386bool QFile::open(OpenMode mode) -
387{ -
388 QFilePrivate * const d = d_func(); -
389 if (isOpen()) {
-
390 QMessageLogger("io/qfile.cpp", 821834, __PRETTY_FUNCTION__).warning("QFile::open: File (%s) already open", QString(fileName()).toLocal8Bit().constData()); -
391 return false;
-
392 } -
393 if (mode & Append)
-
394 mode |= WriteOnly;
-
395 -
396 unsetError(); -
397 if ((mode & (ReadOnly | WriteOnly)) == 0) {
-
398 QMessageLogger("io/qfile.cpp", 829842, __PRETTY_FUNCTION__).warning("QIODevice::open: File access not specified"); -
399 return false;
-
400 } -
401 -
402 -
403 if (d->engine()->open(mode | QIODevice::Unbuffered)) {
-
404 QIODevice::open(mode); -
405 if (mode & Append)
-
406 seek(size());
-
407 return true;
-
408 } -
409 QFile::FileError err = d->fileEngine->error(); -
410 if(err == QFile::UnspecifiedError)
-
411 err = QFile::OpenError;
-
412 d->setError(err, d->fileEngine->errorString()); -
413 return false;
-
414} -
415bool QFile::open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags) -
416{ -
417 QFilePrivate * const d = d_func(); -
418 if (isOpen()) {
-
419 QMessageLogger("io/qfile.cpp", 895908, __PRETTY_FUNCTION__).warning("QFile::open: File (%s) already open", QString(fileName()).toLocal8Bit().constData()); -
420 return false;
-
421 } -
422 if (mode & Append)
-
423 mode |= WriteOnly;
-
424 unsetError(); -
425 if ((mode & (ReadOnly | WriteOnly)) == 0) {
-
426 QMessageLogger("io/qfile.cpp", 902915, __PRETTY_FUNCTION__).warning("QFile::open: File access not specified"); -
427 return false;
-
428 } -
429 if (d->openExternalFile(mode, fh, handleFlags)) {
-
430 QIODevice::open(mode); -
431 if (!(mode & Append) && !isSequential()) {
-
432 qint64 pos = (qint64)::ftello64(fh); -
433 if (pos != -1) {
-
434 -
435 QIODevice::seek(pos); -
436 }
-
437 }
-
438 return true;
-
439 } -
440 return false;
-
441} -
442bool QFile::open(int fd, OpenMode mode, FileHandleFlags handleFlags) -
443{ -
444 QFilePrivate * const d = d_func(); -
445 if (isOpen()) {
-
446 QMessageLogger("io/qfile.cpp", 954967, __PRETTY_FUNCTION__).warning("QFile::open: File (%s) already open", QString(fileName()).toLocal8Bit().constData()); -
447 return false;
-
448 } -
449 if (mode & Append)
-
450 mode |= WriteOnly;
-
451 unsetError(); -
452 if ((mode & (ReadOnly | WriteOnly)) == 0) {
-
453 QMessageLogger("io/qfile.cpp", 961974, __PRETTY_FUNCTION__).warning("QFile::open: File access not specified"); -
454 return false;
-
455 } -
456 if (d->openExternalFile(mode, fd, handleFlags)) {
-
457 QIODevice::open(mode); -
458 if (!(mode & Append) && !isSequential()) {
-
459 qint64 pos = (qint64)::lseek64(fd, off64_t(0), 1); -
460 if (pos != -1) {
-
461 -
462 QIODevice::seek(pos); -
463 }
-
464 }
-
465 return true;
-
466 } -
467 return false;
-
468} -
469 -
470 -
471 -
472 -
473bool QFile::resize(qint64 sz) -
474{ -
475 return QFileDevice::resize(sz);
-
476} -
477bool -
478QFile::resize(const QString &fileName, qint64 sz) -
479{ -
480 return QFile(fileName).resize(sz);
-
481} -
482 -
483 -
484 -
485 -
486QFile::Permissions QFile::permissions() const -
487{ -
488 return QFileDevice::permissions();
-
489} -
490QFile::Permissions -
491QFile::permissions(const QString &fileName) -
492{ -
493 return QFile(fileName).permissions();
-
494} -
495bool QFile::setPermissions(Permissions permissions) -
496{ -
497 return QFileDevice::setPermissions(permissions);
-
498} -
499 -
500 -
501 -
502 -
503 -
504 -
505 -
506bool -
507QFile::setPermissions(const QString &fileName, Permissions permissions) -
508{ -
509 return QFile(fileName).setPermissions(permissions);
-
510} -
511 -
512 -
513 -
514 -
515qint64 QFile::size() const -
516{ -
517 return QFileDevice::size();
-
518} -
519 -
520 -
521 -
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial