| Line | Source Code | Coverage |
|---|
| 1 | /**************************************************************************** | - |
| | ** | |
| | ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). | |
| | ** Contact: http://www.qt-project.org/legal | |
| | ** | |
| | ** This file is part of the QtCore module of the Qt Toolkit. | |
| | ** | |
| | ** $QT_BEGIN_LICENSE:LGPL$ | |
| | ** Commercial License Usage | |
| | ** Licensees holding valid commercial Qt licenses may use this file in | |
| | ** accordance with the commercial license agreement provided with the | |
| | ** Software or, alternatively, in accordance with the terms contained in | |
| | ** a written agreement between you and Digia. For licensing terms and | |
| | ** conditions see http://qt.digia.com/licensing. For further information | |
| | ** use the contact form at http://qt.digia.com/contact-us. | |
| | ** | |
| | ** GNU Lesser General Public License Usage | |
| | ** Alternatively, this file may be used under the terms of the GNU Lesser | |
| | ** General Public License version 2.1 as published by the Free Software | |
| | ** Foundation and appearing in the file LICENSE.LGPL included in the | |
| | ** packaging of this file. Please review the following information to | |
| | ** ensure the GNU Lesser General Public License version 2.1 requirements | |
| | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | |
| | ** | |
| | ** In addition, as a special exception, Digia gives you certain additional | |
| | ** rights. These rights are described in the Digia Qt LGPL Exception | |
| | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | |
| | ** | |
| | ** GNU General Public License Usage | |
| | ** Alternatively, this file may be used under the terms of the GNU | |
| | ** General Public License version 3.0 as published by the Free Software | |
| | ** Foundation and appearing in the file LICENSE.GPL included in the | |
| | ** packaging of this file. Please review the following information to | |
| | ** ensure the GNU General Public License version 3.0 requirements will be | |
| | ** met: http://www.gnu.org/copyleft/gpl.html. | |
| | ** | |
| | ** | |
| | ** $QT_END_LICENSE$ | |
| | ** | |
| | ****************************************************************************/**************************************************************************** | |
| 2 | ** | - |
| 3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | - |
| 4 | ** Contact: http://www.qt-project.org/legal | - |
| 5 | ** | - |
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | - |
| 7 | ** | - |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
| 9 | ** Commercial License Usage | - |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - |
| 11 | ** accordance with the commercial license agreement provided with the | - |
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - |
| 13 | ** a written agreement between you and Digia. For licensing terms and | - |
| 14 | ** conditions see http://qt.digia.com/licensing. For further information | - |
| 15 | ** use the contact form at http://qt.digia.com/contact-us. | - |
| 16 | ** | - |
| 17 | ** GNU Lesser General Public License Usage | - |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - |
| 19 | ** General Public License version 2.1 as published by the Free Software | - |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL included in the | - |
| 21 | ** packaging of this file. Please review the following information to | - |
| 22 | ** ensure the GNU Lesser General Public License version 2.1 requirements | - |
| 23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - |
| 24 | ** | - |
| 25 | ** In addition, as a special exception, Digia gives you certain additional | - |
| 26 | ** rights. These rights are described in the Digia Qt LGPL Exception | - |
| 27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - |
| 28 | ** | - |
| 29 | ** GNU General Public License Usage | - |
| 30 | ** Alternatively, this file may be used under the terms of the GNU | - |
| 31 | ** General Public License version 3.0 as published by the Free Software | - |
| 32 | ** Foundation and appearing in the file LICENSE.GPL included in the | - |
| 33 | ** packaging of this file. Please review the following information to | - |
| 34 | ** ensure the GNU General Public License version 3.0 requirements will be | - |
| 35 | ** met: http://www.gnu.org/copyleft/gpl.html. | - |
| 36 | ** | - |
| 37 | ** | - |
| 38 | ** $QT_END_LICENSE$ | - |
| 39 | ** | - |
| 40 | ****************************************************************************/ | - |
| 41 | | - |
| 42 | #include "qplatformdefs.h" | - |
| 43 | #include "qdebug.h" | - |
| 44 | #include "qfile.h" | - |
| 45 | #include "qfsfileengine_p.h" | - |
| 46 | #include "qtemporaryfile.h" | - |
| 47 | #include "qlist.h" | - |
| 48 | #include "qfileinfo.h" | - |
| 49 | #include "private/qiodevice_p.h" | - |
| 50 | #include "private/qfile_p.h" | - |
| 51 | #include "private/qfilesystemengine_p.h" | - |
| 52 | #include "private/qsystemerror_p.h" | - |
| 53 | #if defined(QT_BUILD_CORE_LIB) | - |
| 54 | # include "qcoreapplication.h" | - |
| 55 | #endif | - |
| 56 | | - |
| 57 | #ifdef QT_NO_QOBJECT | - |
| 58 | #define tr(X) QString::fromLatin1(X) | - |
| 59 | #endif | - |
| 60 | | - |
| 61 | QT_BEGIN_NAMESPACE | - |
| 62 | | - |
| 63 | //************* QFilePrivate | - |
| 64 | QFilePrivate::QFilePrivate() | - |
| 65 | { | - |
| 66 | } | - |
| 67 | | - |
| 68 | QFilePrivate::~QFilePrivate() | - |
| 69 | { | - |
| 70 | } | - |
| 71 | | - |
| 72 | bool | - |
| 73 | QFilePrivate::openExternalFile(int flags, int fd, QFile::FileHandleFlags handleFlags) | - |
| 74 | { | - |
| 75 | #ifdef QT_NO_FSFILEENGINE | - |
| 76 | Q_UNUSED(flags); | - |
| 77 | Q_UNUSED(fd); | - |
| 78 | return false; | - |
| 79 | #else | - |
| 80 | delete fileEngine; | - |
| 81 | fileEngine = 0; | - |
| 82 | QFSFileEngine *fe = new QFSFileEngine; | - |
| 83 | fileEngine = fe; | - |
| 84 | return fe->open(QIODevice::OpenMode(flags), fd, handleFlags); | - |
| 85 | #endif | - |
| 86 | } | - |
| 87 | | - |
| 88 | bool | - |
| 89 | QFilePrivate::openExternalFile(int flags, FILE *fh, QFile::FileHandleFlags handleFlags) | - |
| 90 | { | - |
| 91 | #ifdef QT_NO_FSFILEENGINE | - |
| 92 | Q_UNUSED(flags); | - |
| 93 | Q_UNUSED(fh); | - |
| 94 | return false; | - |
| 95 | #else | - |
| 96 | delete fileEngine; | - |
| 97 | fileEngine = 0; | - |
| 98 | QFSFileEngine *fe = new QFSFileEngine; | - |
| 99 | fileEngine = fe; | - |
| 100 | return fe->open(QIODevice::OpenMode(flags), fh, handleFlags); | - |
| 101 | #endif | - |
| 102 | } | - |
| 103 | | - |
| 104 | QAbstractFileEngine *QFilePrivate::engine() const | - |
| 105 | { | - |
| 106 | if (!fileEngine) | - |
| 107 | fileEngine = QAbstractFileEngine::create(fileName); | - |
| 108 | return fileEngine; | - |
| 109 | } | - |
| 110 | | - |
| 111 | //************* QFile | - |
| 112 | | - |
| 113 | /*! | - |
| 114 | \class QFile | - |
| 115 | \inmodule QtCore | - |
| 116 | \brief The QFile class provides an interface for reading from and writing to files. | - |
| 117 | | - |
| 118 | \ingroup io | - |
| 119 | | - |
| 120 | \reentrant | - |
| 121 | | - |
| 122 | QFile is an I/O device for reading and writing text and binary | - |
| 123 | files and \l{The Qt Resource System}{resources}. A QFile may be | - |
| 124 | used by itself or, more conveniently, with a QTextStream or | - |
| 125 | QDataStream. | - |
| 126 | | - |
| 127 | The file name is usually passed in the constructor, but it can be | - |
| 128 | set at any time using setFileName(). QFile expects the file | - |
| 129 | separator to be '/' regardless of operating system. The use of | - |
| 130 | other separators (e.g., '\\') is not supported. | - |
| 131 | | - |
| 132 | You can check for a file's existence using exists(), and remove a | - |
| 133 | file using remove(). (More advanced file system related operations | - |
| 134 | are provided by QFileInfo and QDir.) | - |
| 135 | | - |
| 136 | The file is opened with open(), closed with close(), and flushed | - |
| 137 | with flush(). Data is usually read and written using QDataStream | - |
| 138 | or QTextStream, but you can also call the QIODevice-inherited | - |
| 139 | functions read(), readLine(), readAll(), write(). QFile also | - |
| 140 | inherits getChar(), putChar(), and ungetChar(), which work one | - |
| 141 | character at a time. | - |
| 142 | | - |
| 143 | The size of the file is returned by size(). You can get the | - |
| 144 | current file position using pos(), or move to a new file position | - |
| 145 | using seek(). If you've reached the end of the file, atEnd() | - |
| 146 | returns true. | - |
| 147 | | - |
| 148 | \section1 Reading Files Directly | - |
| 149 | | - |
| 150 | The following example reads a text file line by line: | - |
| 151 | | - |
| 152 | \snippet file/file.cpp 0 | - |
| 153 | | - |
| 154 | The QIODevice::Text flag passed to open() tells Qt to convert | - |
| 155 | Windows-style line terminators ("\\r\\n") into C++-style | - |
| 156 | terminators ("\\n"). By default, QFile assumes binary, i.e. it | - |
| 157 | doesn't perform any conversion on the bytes stored in the file. | - |
| 158 | | - |
| 159 | \section1 Using Streams to Read Files | - |
| 160 | | - |
| 161 | The next example uses QTextStream to read a text file | - |
| 162 | line by line: | - |
| 163 | | - |
| 164 | \snippet file/file.cpp 1 | - |
| 165 | | - |
| 166 | QTextStream takes care of converting the 8-bit data stored on | - |
| 167 | disk into a 16-bit Unicode QString. By default, it assumes that | - |
| 168 | the user system's local 8-bit encoding is used (e.g., UTF-8 | - |
| 169 | on most unix based operating systems; see QTextCodec::codecForLocale() for | - |
| 170 | details). This can be changed using setCodec(). | - |
| 171 | | - |
| 172 | To write text, we can use operator<<(), which is overloaded to | - |
| 173 | take a QTextStream on the left and various data types (including | - |
| 174 | QString) on the right: | - |
| 175 | | - |
| 176 | \snippet file/file.cpp 2 | - |
| 177 | | - |
| 178 | QDataStream is similar, in that you can use operator<<() to write | - |
| 179 | data and operator>>() to read it back. See the class | - |
| 180 | documentation for details. | - |
| 181 | | - |
| 182 | When you use QFile, QFileInfo, and QDir to access the file system | - |
| 183 | with Qt, you can use Unicode file names. On Unix, these file | - |
| 184 | names are converted to an 8-bit encoding. If you want to use | - |
| 185 | standard C++ APIs (\c <cstdio> or \c <iostream>) or | - |
| 186 | platform-specific APIs to access files instead of QFile, you can | - |
| 187 | use the encodeName() and decodeName() functions to convert | - |
| 188 | between Unicode file names and 8-bit file names. | - |
| 189 | | - |
| 190 | On Unix, there are some special system files (e.g. in \c /proc) for which | - |
| 191 | size() will always return 0, yet you may still be able to read more data | - |
| 192 | from such a file; the data is generated in direct response to you calling | - |
| 193 | read(). In this case, however, you cannot use atEnd() to determine if | - |
| 194 | there is more data to read (since atEnd() will return true for a file that | - |
| 195 | claims to have size 0). Instead, you should either call readAll(), or call | - |
| 196 | read() or readLine() repeatedly until no more data can be read. The next | - |
| 197 | example uses QTextStream to read \c /proc/modules line by line: | - |
| 198 | | - |
| 199 | \snippet file/file.cpp 3 | - |
| 200 | | - |
| 201 | \section1 Signals | - |
| 202 | | - |
| 203 | Unlike other QIODevice implementations, such as QTcpSocket, QFile does not | - |
| 204 | emit the aboutToClose(), bytesWritten(), or readyRead() signals. This | - |
| 205 | implementation detail means that QFile is not suitable for reading and | - |
| 206 | writing certain types of files, such as device files on Unix platforms. | - |
| 207 | | - |
| 208 | \section1 Platform Specific Issues | - |
| 209 | | - |
| 210 | File permissions are handled differently on Linux/Mac OS X and | - |
| 211 | Windows. In a non \l{QIODevice::isWritable()}{writable} | - |
| 212 | directory on Linux, files cannot be created. This is not always | - |
| 213 | the case on Windows, where, for instance, the 'My Documents' | - |
| 214 | directory usually is not writable, but it is still possible to | - |
| 215 | create files in it. | - |
| 216 | | - |
| 217 | \sa QTextStream, QDataStream, QFileInfo, QDir, {The Qt Resource System} | - |
| 218 | */ | - |
| 219 | | - |
| 220 | #ifdef QT_NO_QOBJECT | - |
| 221 | QFile::QFile() | - |
| 222 | : QFileDevice(*new QFilePrivate) | - |
| 223 | { | - |
| 224 | } | - |
| 225 | QFile::QFile(const QString &name) | - |
| 226 | : QFileDevice(*new QFilePrivate) | - |
| 227 | { | - |
| 228 | d_func()->fileName = name; | - |
| 229 | } | - |
| 230 | QFile::QFile(QFilePrivate &dd) | - |
| 231 | : QFileDevice(dd) | - |
| 232 | { | - |
| 233 | } | - |
| 234 | #else | - |
| 235 | /*! | - |
| 236 | \internal | - |
| 237 | */ | - |
| 238 | QFile::QFile() | - |
| 239 | : QFileDevice(*new QFilePrivate, 0) | - |
| 240 | { | - |
| 241 | } | - |
| 242 | /*! | - |
| 243 | Constructs a new file object with the given \a parent. | - |
| 244 | */ | - |
| 245 | QFile::QFile(QObject *parent) | - |
| 246 | : QFileDevice(*new QFilePrivate, parent) | - |
| 247 | { | - |
| 248 | } | - |
| 249 | /*! | - |
| 250 | Constructs a new file object to represent the file with the given \a name. | - |
| 251 | */ | - |
| 252 | QFile::QFile(const QString &name) | - |
| 253 | : QFileDevice(*new QFilePrivate, 0) | - |
| 254 | { | - |
| 255 | Q_D(QFile); | - |
| 256 | d->fileName = name; | - |
| 257 | } | - |
| 258 | /*! | - |
| 259 | Constructs a new file object with the given \a parent to represent the | - |
| 260 | file with the specified \a name. | - |
| 261 | */ | - |
| 262 | QFile::QFile(const QString &name, QObject *parent) | - |
| 263 | : QFileDevice(*new QFilePrivate, parent) | - |
| 264 | { | - |
| 265 | Q_D(QFile); | - |
| 266 | d->fileName = name; | - |
| 267 | } | - |
| 268 | /*! | - |
| 269 | \internal | - |
| 270 | */ | - |
| 271 | QFile::QFile(QFilePrivate &dd, QObject *parent) | - |
| 272 | : QFileDevice(dd, parent) | - |
| 273 | { | - |
| 274 | } | - |
| 275 | #endif | - |
| 276 | | - |
| 277 | /*! | - |
| 278 | Destroys the file object, closing it if necessary. | - |
| 279 | */ | - |
| 280 | QFile::~QFile() | - |
| 281 | { | - |
| 282 | } | - |
| 283 | | - |
| 284 | /*! | - |
| 285 | Returns the name set by setFileName() or to the QFile | - |
| 286 | constructors. | - |
| 287 | | - |
| 288 | \sa setFileName(), QFileInfo::fileName() | - |
| 289 | */ | - |
| 290 | QString QFile::fileName() const | - |
| 291 | { | - |
| 292 | Q_D(const QFile); | - |
| 293 | return d->engine()->fileName(QAbstractFileEngine::DefaultName); | - |
| 294 | } | - |
| 295 | | - |
| 296 | /*! | - |
| 297 | Sets the \a name of the file. The name can have no path, a | - |
| 298 | relative path, or an absolute path. | - |
| 299 | | - |
| 300 | Do not call this function if the file has already been opened. | - |
| 301 | | - |
| 302 | If the file name has no path or a relative path, the path used | - |
| 303 | will be the application's current directory path | - |
| 304 | \e{at the time of the open()} call. | - |
| 305 | | - |
| 306 | Example: | - |
| 307 | \snippet code/src_corelib_io_qfile.cpp 0 | - |
| 308 | | - |
| 309 | Note that the directory separator "/" works for all operating | - |
| 310 | systems supported by Qt. | - |
| 311 | | - |
| 312 | \sa fileName(), QFileInfo, QDir | - |
| 313 | */ | - |
| 314 | void | - |
| 315 | QFile::setFileName(const QString &name) | - |
| 316 | { | - |
| 317 | Q_D(QFile); | - |
| 318 | if (isOpen()) { | - |
| 319 | qWarning("QFile::setFileName: File (%s) is already opened", | - |
| 320 | qPrintable(fileName())); | - |
| 321 | close(); | - |
| 322 | } | - |
| 323 | if(d->fileEngine) { //get a new file engine later | - |
| 324 | delete d->fileEngine; | - |
| 325 | d->fileEngine = 0; | - |
| 326 | } | - |
| 327 | d->fileName = name; | - |
| 328 | } | - |
| 329 | | - |
| 330 | /*! | - |
| 331 | \fn QString QFile::decodeName(const char *localFileName) | - |
| 332 | | - |
| 333 | \overload | - |
| 334 | | - |
| 335 | Returns the Unicode version of the given \a localFileName. See | - |
| 336 | encodeName() for details. | - |
| 337 | */ | - |
| 338 | | - |
| 339 | /*! | - |
| 340 | \fn QByteArray QFile::encodeName(const QString &fileName) | - |
| 341 | | - |
| 342 | Converts \a fileName to the local 8-bit | - |
| 343 | encoding determined by the user's locale. This is sufficient for | - |
| 344 | file names that the user chooses. File names hard-coded into the | - |
| 345 | application should only use 7-bit ASCII filename characters. | - |
| 346 | | - |
| 347 | \sa decodeName() | - |
| 348 | */ | - |
| 349 | | - |
| 350 | /*! | - |
| 351 | \typedef QFile::EncoderFn | - |
| 352 | \obsolete | - |
| 353 | | - |
| 354 | This is a typedef for a pointer to a function with the following | - |
| 355 | signature: | - |
| 356 | | - |
| 357 | \snippet code/src_corelib_io_qfile.cpp 1 | - |
| 358 | | - |
| 359 | \sa setEncodingFunction(), encodeName() | - |
| 360 | */ | - |
| 361 | | - |
| 362 | /*! | - |
| 363 | \fn QString QFile::decodeName(const QByteArray &localFileName) | - |
| 364 | | - |
| 365 | This does the reverse of QFile::encodeName() using \a localFileName. | - |
| 366 | | - |
| 367 | \sa encodeName() | - |
| 368 | */ | - |
| 369 | | - |
| 370 | /*! | - |
| 371 | \fn void QFile::setEncodingFunction(EncoderFn function) | - |
| 372 | \obsolete | - |
| 373 | | - |
| 374 | This function does nothing. It is provided for compatibility with Qt 4 code | - |
| 375 | that attempted to set a different encoding function for file names. That | - |
| 376 | feature is flawed and no longer supported in Qt 5. | - |
| 377 | | - |
| 378 | \sa encodeName(), setDecodingFunction() | - |
| 379 | */ | - |
| 380 | | - |
| 381 | /*! | - |
| 382 | \typedef QFile::DecoderFn | - |
| 383 | | - |
| 384 | This is a typedef for a pointer to a function with the following | - |
| 385 | signature: | - |
| 386 | | - |
| 387 | \snippet code/src_corelib_io_qfile.cpp 2 | - |
| 388 | | - |
| 389 | \sa setDecodingFunction() | - |
| 390 | */ | - |
| 391 | | - |
| 392 | /*! | - |
| 393 | \fn void QFile::setDecodingFunction(DecoderFn function) | - |
| 394 | \obsolete | - |
| 395 | | - |
| 396 | This function does nothing. It is provided for compatibility with Qt 4 code | - |
| 397 | that attempted to set a different decoding function for file names. That | - |
| 398 | feature is flawed and no longer supported in Qt 5. | - |
| 399 | | - |
| 400 | \sa setEncodingFunction(), decodeName() | - |
| 401 | */ | - |
| 402 | | - |
| 403 | /*! | - |
| 404 | \overload | - |
| 405 | | - |
| 406 | Returns true if the file specified by fileName() exists; otherwise | - |
| 407 | returns false. | - |
| 408 | | - |
| 409 | \sa fileName(), setFileName() | - |
| 410 | */ | - |
| 411 | | - |
| 412 | bool | - |
| 413 | QFile::exists() const | - |
| 414 | { | - |
| 415 | Q_D(const QFile); | - |
| 416 | // 0x1000000 = QAbstractFileEngine::Refresh, forcing an update | - |
| 417 | return (d->engine()->fileFlags(QAbstractFileEngine::FlagsMask | - |
| 418 | | QAbstractFileEngine::FileFlag(0x1000000)) & QAbstractFileEngine::ExistsFlag); | - |
| 419 | } | - |
| 420 | | - |
| 421 | /*! | - |
| 422 | Returns true if the file specified by \a fileName exists; otherwise | - |
| 423 | returns false. | - |
| 424 | */ | - |
| 425 | | - |
| 426 | bool | - |
| 427 | QFile::exists(const QString &fileName) | - |
| 428 | { | - |
| 429 | return QFileInfo(fileName).exists(); | - |
| 430 | } | - |
| 431 | | - |
| 432 | /*! | - |
| 433 | \fn QString QFile::symLinkTarget() const | - |
| 434 | \since 4.2 | - |
| 435 | \overload | - |
| 436 | | - |
| 437 | Returns the absolute path of the file or directory a symlink (or shortcut | - |
| 438 | on Windows) points to, or a an empty string if the object isn't a symbolic | - |
| 439 | link. | - |
| 440 | | - |
| 441 | This name may not represent an existing file; it is only a string. | - |
| 442 | QFile::exists() returns true if the symlink points to an existing file. | - |
| 443 | | - |
| 444 | \sa fileName(), setFileName() | - |
| 445 | */ | - |
| 446 | | - |
| 447 | /*! | - |
| 448 | \obsolete | - |
| 449 | | - |
| 450 | Use symLinkTarget() instead. | - |
| 451 | */ | - |
| 452 | QString | - |
| 453 | QFile::readLink() const | - |
| 454 | { | - |
| 455 | Q_D(const QFile); | - |
| 456 | return d->engine()->fileName(QAbstractFileEngine::LinkName); | - |
| 457 | } | - |
| 458 | | - |
| 459 | /*! | - |
| 460 | \fn static QString QFile::symLinkTarget(const QString &fileName) | - |
| 461 | \since 4.2 | - |
| 462 | | - |
| 463 | Returns the absolute path of the file or directory referred to by the | - |
| 464 | symlink (or shortcut on Windows) specified by \a fileName, or returns an | - |
| 465 | empty string if the \a fileName does not correspond to a symbolic link. | - |
| 466 | | - |
| 467 | This name may not represent an existing file; it is only a string. | - |
| 468 | QFile::exists() returns true if the symlink points to an existing file. | - |
| 469 | */ | - |
| 470 | | - |
| 471 | /*! | - |
| 472 | \obsolete | - |
| 473 | | - |
| 474 | Use symLinkTarget() instead. | - |
| 475 | */ | - |
| 476 | QString | - |
| 477 | QFile::readLink(const QString &fileName) | - |
| 478 | { | - |
| 479 | return QFileInfo(fileName).readLink(); | - |
| 480 | } | - |
| 481 | | - |
| 482 | /*! | - |
| 483 | Removes the file specified by fileName(). Returns true if successful; | - |
| 484 | otherwise returns false. | - |
| 485 | | - |
| 486 | The file is closed before it is removed. | - |
| 487 | | - |
| 488 | \sa setFileName() | - |
| 489 | */ | - |
| 490 | | - |
| 491 | bool | - |
| 492 | QFile::remove() | - |
| 493 | { | - |
| 494 | Q_D(QFile); | - |
| 495 | if (d->fileName.isEmpty()) { | - |
| 496 | qWarning("QFile::remove: Empty or null file name"); | - |
| 497 | return false; | - |
| 498 | } | - |
| 499 | unsetError(); | - |
| 500 | close(); | - |
| 501 | if(error() == QFile::NoError) { | - |
| 502 | if (d->engine()->remove()) { | - |
| 503 | unsetError(); | - |
| 504 | return true; | - |
| 505 | } | - |
| 506 | d->setError(QFile::RemoveError, d->fileEngine->errorString()); | - |
| 507 | } | - |
| 508 | return false; | - |
| 509 | } | - |
| 510 | | - |
| 511 | /*! | - |
| 512 | \overload | - |
| 513 | | - |
| 514 | Removes the file specified by the \a fileName given. | - |
| 515 | | - |
| 516 | Returns true if successful; otherwise returns false. | - |
| 517 | | - |
| 518 | \sa remove() | - |
| 519 | */ | - |
| 520 | | - |
| 521 | bool | - |
| 522 | QFile::remove(const QString &fileName) | - |
| 523 | { | - |
| 524 | return QFile(fileName).remove(); | - |
| 525 | } | - |
| 526 | | - |
| 527 | /*! | - |
| 528 | Renames the file currently specified by fileName() to \a newName. | - |
| 529 | Returns true if successful; otherwise returns false. | - |
| 530 | | - |
| 531 | If a file with the name \a newName already exists, rename() returns false | - |
| 532 | (i.e., QFile will not overwrite it). | - |
| 533 | | - |
| 534 | The file is closed before it is renamed. | - |
| 535 | | - |
| 536 | If the rename operation fails, Qt will attempt to copy this file's | - |
| 537 | contents to \a newName, and then remove this file, keeping only | - |
| 538 | \a newName. If that copy operation fails or this file can't be removed, | - |
| 539 | the destination file \a newName is removed to restore the old state. | - |
| 540 | | - |
| 541 | \sa setFileName() | - |
| 542 | */ | - |
| 543 | | - |
| 544 | bool | - |
| 545 | QFile::rename(const QString &newName) | - |
| 546 | { | - |
| 547 | Q_D(QFile); executed (the execution status of this line is deduced): QFilePrivate * const d = d_func(); | - |
| 548 | if (d->fileName.isEmpty()) { partially evaluated: d->fileName.isEmpty()| no Evaluation Count:0 | yes Evaluation Count:211 |
| 0-211 |
| 549 | qWarning("QFile::rename: Empty or null file name"); never executed (the execution status of this line is deduced): QMessageLogger("io/qfile.cpp", 549, __PRETTY_FUNCTION__).warning("QFile::rename: Empty or null file name"); | - |
| 550 | return false; never executed: return false; | 0 |
| 551 | } | - |
| 552 | if (d->fileName == newName) { evaluated: d->fileName == newName| yes Evaluation Count:1 | yes Evaluation Count:210 |
| 1-210 |
| 553 | d->setError(QFile::RenameError, tr(newName)."Destination file is the same file.")); executed (the execution status of this line is deduced): d->setError(QFile::RenameError, tr("Destination file is the same file.")); | - |
| 554 | return false; executed: return false;Execution Count:1 | 1 |
| 555 | } | - |
| 556 | if (!exists()) { evaluated: !exists()| yes Evaluation Count:2 | yes Evaluation Count:208 |
| 2-208 |
| 557 | d->setError(QFile::RenameError, tr("Source file does not exist.")); executed (the execution status of this line is deduced): d->setError(QFile::RenameError, tr("Source file does not exist.")); | - |
| 558 | return false; executed: return false;Execution Count:2 | 2 |
| 559 | } | - |
| 560 | // If the file exists and it is a case-changing rename ("foo" -> "Foo"), | - |
| 561 | // compare Ids to make sure it really is a different file. | - |
| 562 | if (QFile::exists(newName) evaluated: QFile::exists(newName)| yes Evaluation Count:4 | yes Evaluation Count:204 |
| 4-204 |
| 563 | && (d->fileName.compare(newName, Qt::CaseInsensitive) partially evaluated: d->fileName.compare(newName, Qt::CaseInsensitive)| yes Evaluation Count:4 | no Evaluation Count:0 |
| 0-4 |
| 564 | || QFileSystemEngine::id(QFileSystemEntry(d->fileName)) != QFileSystemEngine::id(QFileSystemEntry(newName)))) { never evaluated: QFileSystemEngine::id(QFileSystemEntry(d->fileName)) != QFileSystemEngine::id(QFileSystemEntry(newName)) | 0 |
| 565 | // ### Race condition. If a file is moved in after this, it /will/ be | - |
| 566 | // overwritten. On Unix, the proper solution is to use hardlinks: | - |
| 567 | // return ::link(old, new) && ::remove(old); | - |
| 568 | d->setError(QFile::RenameError, tr("Destination file exists")); executed (the execution status of this line is deduced): d->setError(QFile::RenameError, tr("Destination file exists")); | - |
| 569 | return false; executed: return false;Execution Count:4 | 4 |
| 570 | } | - |
| 571 | unsetError(); executed (the execution status of this line is deduced): unsetError(); | - |
| 572 | close(); executed (the execution status of this line is deduced): close(); | - |
| 573 | if(error() == QFile::NoError) { partially evaluated: error() == QFile::NoError| yes Evaluation Count:204 | no Evaluation Count:0 |
| 0-204 |
| 574 | if (d->engine()->rename(newName)) { evaluated: d->engine()->rename(newName)| yes Evaluation Count:200 | yes Evaluation Count:4 |
| 4-200 |
| 575 | unsetError(); executed (the execution status of this line is deduced): unsetError(); | - |
| 576 | // engine was able to handle the new name so we just reset it | - |
| 577 | d->fileEngine->setFileName(newName); executed (the execution status of this line is deduced): d->fileEngine->setFileName(newName); | - |
| 578 | d->fileName = newName; executed (the execution status of this line is deduced): d->fileName = newName; | - |
| 579 | return true; executed: return true;Execution Count:200 | 200 |
| 580 | } | - |
| 581 | | - |
| 582 | if (isSequential()) { partially evaluated: isSequential()| no Evaluation Count:0 | yes Evaluation Count:4 |
| 0-4 |
| 583 | d->setError(QFile::RenameError, tr("Will not rename sequential file using block copy")); never executed (the execution status of this line is deduced): d->setError(QFile::RenameError, tr("Will not rename sequential file using block copy")); | - |
| 584 | return false; never executed: return false; | 0 |
| 585 | } | - |
| 586 | | - |
| 587 | QFile out(newName); executed (the execution status of this line is deduced): QFile out(newName); | - |
| 588 | if (open(QIODevice::ReadOnly)) { partially evaluated: open(QIODevice::ReadOnly)| yes Evaluation Count:4 | no Evaluation Count:0 |
| 0-4 |
| 589 | if (out.open(QIODevice::WriteOnly | QIODevice::Truncate)) { evaluated: out.open(QIODevice::WriteOnly | QIODevice::Truncate)| yes Evaluation Count:1 | yes Evaluation Count:3 |
| 1-3 |
| 590 | bool error = false; executed (the execution status of this line is deduced): bool error = false; | - |
| 591 | char block[4096]; executed (the execution status of this line is deduced): char block[4096]; | - |
| 592 | qint64 bytes; executed (the execution status of this line is deduced): qint64 bytes; | - |
| 593 | while ((bytes = read(block, sizeof(block))) > 0) { evaluated: (bytes = read(block, sizeof(block))) > 0| yes Evaluation Count:1 | yes Evaluation Count:1 |
| 1 |
| 594 | if (bytes != out.write(block, bytes)) { partially evaluated: bytes != out.write(block, bytes)| no Evaluation Count:0 | yes Evaluation Count:1 |
| 0-1 |
| 595 | d->setError(QFile::RenameError, out.errorString()); never executed (the execution status of this line is deduced): d->setError(QFile::RenameError, out.errorString()); | - |
| 596 | error = true; never executed (the execution status of this line is deduced): error = true; | - |
| 597 | break; | 0 |
| 598 | } | - |
| 599 | } executed: }Execution Count:1 | 1 |
| 600 | if (bytes == -1) { partially evaluated: bytes == -1| no Evaluation Count:0 | yes Evaluation Count:1 |
| 0-1 |
| 601 | d->setError(QFile::RenameError, errorString()); never executed (the execution status of this line is deduced): d->setError(QFile::RenameError, errorString()); | - |
| 602 | error = true; never executed (the execution status of this line is deduced): error = true; | - |
| 603 | } | 0 |
| 604 | if(!error) { partially evaluated: !error| yes Evaluation Count:1 | no Evaluation Count:0 |
| 0-1 |
| 605 | if (!remove()) { partially evaluated: !remove()| yes Evaluation Count:1 | no Evaluation Count:0 |
| 0-1 |
| 606 | d->setError(QFile::RenameError, tr("Cannot remove source file")); executed (the execution status of this line is deduced): d->setError(QFile::RenameError, tr("Cannot remove source file")); | - |
| 607 | error = true; executed (the execution status of this line is deduced): error = true; | - |
| 608 | } executed: }Execution Count:1 | 1 |
| 609 | } executed: }Execution Count:1 | 1 |
| 610 | if (error) { partially evaluated: error| yes Evaluation Count:1 | no Evaluation Count:0 |
| 0-1 |
| 611 | out.remove(); executed (the execution status of this line is deduced): out.remove(); | - |
| 612 | } else { executed: }Execution Count:1 | 1 |
| 613 | d->fileEngine->setFileName(newName); never executed (the execution status of this line is deduced): d->fileEngine->setFileName(newName); | - |
| 614 | setPermissions(permissions()); never executed (the execution status of this line is deduced): setPermissions(permissions()); | - |
| 615 | unsetError(); never executed (the execution status of this line is deduced): unsetError(); | - |
| 616 | setFileName(newName); never executed (the execution status of this line is deduced): setFileName(newName); | - |
| 617 | } | 0 |
| 618 | close(); executed (the execution status of this line is deduced): close(); | - |
| 619 | return !error; executed: return !error;Execution Count:1 | 1 |
| 620 | } | - |
| 621 | close(); executed (the execution status of this line is deduced): close(); | - |
| 622 | } executed: }Execution Count:3 | 3 |
| 623 | d->setError(QFile::RenameError, out.isOpen() ? errorString() : out.errorString()); executed (the execution status of this line is deduced): d->setError(QFile::RenameError, out.isOpen() ? errorString() : out.errorString()); | - |
| 624 | } executed: }Execution Count:3 | 3 |
| 625 | return false; executed: return false;Execution Count:3 | 3 |
| 626 | } | - |
| 627 | | - |
| 628 | /*! | - |
| 629 | \overload | - |
| 630 | | - |
| 631 | Renames the file \a oldName to \a newName. Returns true if | - |
| 632 | successful; otherwise returns false. | - |
| 633 | | - |
| 634 | If a file with the name \a newName already exists, rename() returns false | - |
| 635 | (i.e., QFile will not overwrite it). | - |
| 636 | | - |
| 637 | \sa rename() | - |
| 638 | */ | - |
| 639 | | - |
| 640 | bool | - |
| 641 | QFile::rename(const QString &oldName, const QString &newName) | - |
| 642 | { | - |
| 643 | return QFile(oldName).rename(newName); | - |
| 644 | } | - |
| 645 | | - |
| 646 | /*! | - |
| 647 | | - |
| 648 | Creates a link named \a linkName that points to the file currently specified by | - |
| 649 | fileName(). What a link is depends on the underlying filesystem (be it a | - |
| 650 | shortcut on Windows or a symbolic link on Unix). Returns true if successful; | - |
| 651 | otherwise returns false. | - |
| 652 | | - |
| 653 | This function will not overwrite an already existing entity in the file system; | - |
| 654 | in this case, \c link() will return false and set \l{QFile::}{error()} to | - |
| 655 | return \l{QFile::}{RenameError}. | - |
| 656 | | - |
| 657 | \note To create a valid link on Windows, \a linkName must have a \c{.lnk} file extension. | - |
| 658 | | - |
| 659 | \sa setFileName() | - |
| 660 | */ | - |
| 661 | | - |
| 662 | bool | - |
| 663 | QFile::link(const QString &linkName) | - |
| 664 | { | - |
| 665 | Q_D(QFile); | - |
| 666 | if (d->fileName.isEmpty()) { | - |
| 667 | qWarning("QFile::link: Empty or null file name"); | - |
| 668 | return false; | - |
| 669 | } | - |
| 670 | QFileInfo fi(linkName); | - |
| 671 | if (d->engine()->link(fi.absoluteFilePath())) { | - |
| 672 | unsetError(); | - |
| 673 | return true; | - |
| 674 | } | - |
| 675 | d->setError(QFile::RenameError, d->fileEngine->errorString()); | - |
| 676 | return false; | - |
| 677 | } | - |
| 678 | | - |
| 679 | /*! | - |
| 680 | \overload | - |
| 681 | | - |
| 682 | Creates a link named \a linkName that points to the file \a fileName. What a link is | - |
| 683 | depends on the underlying filesystem (be it a shortcut on Windows | - |
| 684 | or a symbolic link on Unix). Returns true if successful; otherwise | - |
| 685 | returns false. | - |
| 686 | | - |
| 687 | \sa link() | - |
| 688 | */ | - |
| 689 | | - |
| 690 | bool | - |
| 691 | QFile::link(const QString &fileName, const QString &linkName) | - |
| 692 | { | - |
| 693 | return QFile(fileName).link(linkName); | - |
| 694 | } | - |
| 695 | | - |
| 696 | /*! | - |
| 697 | Copies the file currently specified by fileName() to a file called | - |
| 698 | \a newName. Returns true if successful; otherwise returns false. | - |
| 699 | | - |
| 700 | Note that if a file with the name \a newName already exists, | - |
| 701 | copy() returns false (i.e. QFile will not overwrite it). | - |
| 702 | | - |
| 703 | The source file is closed before it is copied. | - |
| 704 | | - |
| 705 | \sa setFileName() | - |
| 706 | */ | - |
| 707 | | - |
| 708 | bool | - |
| 709 | QFile::copy(const QString &newName) | - |
| 710 | { | - |
| 711 | Q_D(QFile); | - |
| 712 | if (d->fileName.isEmpty()) { | - |
| 713 | qWarning("QFile::copy: Empty or null file name"); | - |
| 714 | return false; | - |
| 715 | } | - |
| 716 | if (QFile(newName).exists()) { | - |
| 717 | // ### Race condition. If a file is moved in after this, it /will/ be | - |
| 718 | // overwritten. On Unix, the proper solution is to use hardlinks: | - |
| 719 | // return ::link(old, new) && ::remove(old); See also rename(). | - |
| 720 | d->setError(QFile::CopyError, tr("Destination file exists")); | - |
| 721 | return false; | - |
| 722 | } | - |
| 723 | unsetError(); | - |
| 724 | close(); | - |
| 725 | if(error() == QFile::NoError) { | - |
| 726 | if (d->engine()->copy(newName)) { | - |
| 727 | unsetError(); | - |
| 728 | return true; | - |
| 729 | } else { | - |
| 730 | bool error = false; | - |
| 731 | if(!open(QFile::ReadOnly)) { | - |
| 732 | error = true; | - |
| 733 | d->setError(QFile::CopyError, tr("Cannot open %1 for input").arg(d->fileName)); | - |
| 734 | } else { | - |
| 735 | QString fileTemplate = QLatin1String("%1/qt_temp.XXXXXX"); | - |
| 736 | #ifdef QT_NO_TEMPORARYFILE | - |
| 737 | QFile out(fileTemplate.arg(QFileInfo(newName).path())); | - |
| 738 | if (!out.open(QIODevice::ReadWrite)) | - |
| 739 | error = true; | - |
| 740 | #else | - |
| 741 | QTemporaryFile out(fileTemplate.arg(QFileInfo(newName).path())); | - |
| 742 | if (!out.open()) { | - |
| 743 | out.setFileTemplate(fileTemplate.arg(QDir::tempPath())); | - |
| 744 | if (!out.open()) | - |
| 745 | error = true; | - |
| 746 | } | - |
| 747 | #endif | - |
| 748 | if (error) { | - |
| 749 | out.close(); | - |
| 750 | close(); | - |
| 751 | d->setError(QFile::CopyError, tr("Cannot open for output")); | - |
| 752 | } else { | - |
| 753 | char block[4096]; | - |
| 754 | qint64 totalRead = 0; | - |
| 755 | while(!atEnd()) { | - |
| 756 | qint64 in = read(block, sizeof(block)); | - |
| 757 | if (in <= 0) | - |
| 758 | break; | - |
| 759 | totalRead += in; | - |
| 760 | if(in != out.write(block, in)) { | - |
| 761 | close(); | - |
| 762 | d->setError(QFile::CopyError, tr("Failure to write block")); | - |
| 763 | error = true; | - |
| 764 | break; | - |
| 765 | } | - |
| 766 | } | - |
| 767 | | - |
| 768 | if (totalRead != size()) { | - |
| 769 | // Unable to read from the source. The error string is | - |
| 770 | // already set from read(). | - |
| 771 | error = true; | - |
| 772 | } | - |
| 773 | if (!error && !out.rename(newName)) { | - |
| 774 | error = true; | - |
| 775 | close(); | - |
| 776 | d->setError(QFile::CopyError, tr("Cannot create %1 for output").arg(newName)); | - |
| 777 | } | - |
| 778 | #ifdef QT_NO_TEMPORARYFILE | - |
| 779 | if (error) | - |
| 780 | out.remove(); | - |
| 781 | #else | - |
| 782 | if (!error) | - |
| 783 | out.setAutoRemove(false); | - |
| 784 | #endif | - |
| 785 | } | - |
| 786 | } | - |
| 787 | if(!error) { | - |
| 788 | QFile::setPermissions(newName, permissions()); | - |
| 789 | close(); | - |
| 790 | unsetError(); | - |
| 791 | return true; | - |
| 792 | } | - |
| 793 | } | - |
| 794 | } | - |
| 795 | return false; | - |
| 796 | } | - |
| 797 | | - |
| 798 | /*! | - |
| 799 | \overload | - |
| 800 | | - |
| 801 | Copies the file \a fileName to \a newName. Returns true if successful; | - |
| 802 | otherwise returns false. | - |
| 803 | | - |
| 804 | If a file with the name \a newName already exists, copy() returns false | - |
| 805 | (i.e., QFile will not overwrite it). | - |
| 806 | | - |
| 807 | \sa rename() | - |
| 808 | */ | - |
| 809 | | - |
| 810 | bool | - |
| 811 | QFile::copy(const QString &fileName, const QString &newName) | - |
| 812 | { | - |
| 813 | return QFile(fileName).copy(newName); | - |
| 814 | } | - |
| 815 | | - |
| 816 | /*! | - |
| 817 | Opens the file using OpenMode \a mode, returning true if successful; | - |
| 818 | otherwise false. | - |
| 819 | | - |
| 820 | The \a mode must be QIODevice::ReadOnly, QIODevice::WriteOnly, or | - |
| 821 | QIODevice::ReadWrite. It may also have additional flags, such as | - |
| 822 | QIODevice::Text and QIODevice::Unbuffered. | - |
| 823 | | - |
| 824 | \note In \l{QIODevice::}{WriteOnly} or \l{QIODevice::}{ReadWrite} | - |
| 825 | mode, if the relevant file does not already exist, this function | - |
| 826 | will try to create a new file before opening it. | - |
| 827 | | - |
| 828 | \sa QIODevice::OpenMode, setFileName() | - |
| 829 | */ | - |
| 830 | bool QFile::open(OpenMode mode) | - |
| 831 | { | - |
| 832 | Q_D(QFile); | - |
| 833 | if (isOpen()) { | - |
| 834 | qWarning("QFile::open: File (%s) already open", qPrintable(fileName())); | - |
| 835 | return false; | - |
| 836 | } | - |
| 837 | if (mode & Append) | - |
| 838 | mode |= WriteOnly; | - |
| 839 | | - |
| 840 | unsetError(); | - |
| 841 | if ((mode & (ReadOnly | WriteOnly)) == 0) { | - |
| 842 | qWarning("QIODevice::open: File access not specified"); | - |
| 843 | return false; | - |
| 844 | } | - |
| 845 | | - |
| 846 | // QIODevice provides the buffering, so there's no need to request it from the file engine. | - |
| 847 | if (d->engine()->open(mode | QIODevice::Unbuffered)) { | - |
| 848 | QIODevice::open(mode); | - |
| 849 | if (mode & Append) | - |
| 850 | seek(size()); | - |
| 851 | return true; | - |
| 852 | } | - |
| 853 | QFile::FileError err = d->fileEngine->error(); | - |
| 854 | if(err == QFile::UnspecifiedError) | - |
| 855 | err = QFile::OpenError; | - |
| 856 | d->setError(err, d->fileEngine->errorString()); | - |
| 857 | return false; | - |
| 858 | } | - |
| 859 | | - |
| 860 | /*! | - |
| 861 | \overload | - |
| 862 | | - |
| 863 | Opens the existing file handle \a fh in the given \a mode. | - |
| 864 | \a handleFlags may be used to specify additional options. | - |
| 865 | Returns true if successful; otherwise returns false. | - |
| 866 | | - |
| 867 | Example: | - |
| 868 | \snippet code/src_corelib_io_qfile.cpp 3 | - |
| 869 | | - |
| 870 | When a QFile is opened using this function, behaviour of close() is | - |
| 871 | controlled by the AutoCloseHandle flag. | - |
| 872 | If AutoCloseHandle is specified, and this function succeeds, | - |
| 873 | then calling close() closes the adopted handle. | - |
| 874 | Otherwise, close() does not actually close the file, but only flushes it. | - |
| 875 | | - |
| 876 | \b{Warning:} | - |
| 877 | \list 1 | - |
| 878 | \li If \a fh does not refer to a regular file, e.g., it is \c stdin, | - |
| 879 | \c stdout, or \c stderr, you may not be able to seek(). size() | - |
| 880 | returns \c 0 in those cases. See QIODevice::isSequential() for | - |
| 881 | more information. | - |
| 882 | \li Since this function opens the file without specifying the file name, | - |
| 883 | you cannot use this QFile with a QFileInfo. | - |
| 884 | \endlist | - |
| 885 | | - |
| 886 | \note For Windows CE you may not be able to call resize(). | - |
| 887 | | - |
| 888 | \sa close() | - |
| 889 | | - |
| 890 | \b{Note for the Windows Platform} | - |
| 891 | | - |
| 892 | \a fh must be opened in binary mode (i.e., the mode string must contain | - |
| 893 | 'b', as in "rb" or "wb") when accessing files and other random-access | - |
| 894 | devices. Qt will translate the end-of-line characters if you pass | - |
| 895 | QIODevice::Text to \a mode. Sequential devices, such as stdin and stdout, | - |
| 896 | are unaffected by this limitation. | - |
| 897 | | - |
| 898 | You need to enable support for console applications in order to use the | - |
| 899 | stdin, stdout and stderr streams at the console. To do this, add the | - |
| 900 | following declaration to your application's project file: | - |
| 901 | | - |
| 902 | \snippet code/src_corelib_io_qfile.cpp 4 | - |
| 903 | */ | - |
| 904 | bool QFile::open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags) | - |
| 905 | { | - |
| 906 | Q_D(QFile); | - |
| 907 | if (isOpen()) { | - |
| 908 | qWarning("QFile::open: File (%s) already open", qPrintable(fileName())); | - |
| 909 | return false; | - |
| 910 | } | - |
| 911 | if (mode & Append) | - |
| 912 | mode |= WriteOnly; | - |
| 913 | unsetError(); | - |
| 914 | if ((mode & (ReadOnly | WriteOnly)) == 0) { | - |
| 915 | qWarning("QFile::open: File access not specified"); | - |
| 916 | return false; | - |
| 917 | } | - |
| 918 | if (d->openExternalFile(mode, fh, handleFlags)) { | - |
| 919 | QIODevice::open(mode); | - |
| 920 | if (!(mode & Append) && !isSequential()) { | - |
| 921 | qint64 pos = (qint64)QT_FTELL(fh); | - |
| 922 | if (pos != -1) { | - |
| 923 | // Skip redundant checks in QFileDevice::seek(). | - |
| 924 | QIODevice::seek(pos); | - |
| 925 | } | - |
| 926 | } | - |
| 927 | return true; | - |
| 928 | } | - |
| 929 | return false; | - |
| 930 | } | - |
| 931 | | - |
| 932 | /*! | - |
| 933 | \overload | - |
| 934 | | - |
| 935 | Opens the existing file descriptor \a fd in the given \a mode. | - |
| 936 | \a handleFlags may be used to specify additional options. | - |
| 937 | Returns true if successful; otherwise returns false. | - |
| 938 | | - |
| 939 | When a QFile is opened using this function, behaviour of close() is | - |
| 940 | controlled by the AutoCloseHandle flag. | - |
| 941 | If AutoCloseHandle is specified, and this function succeeds, | - |
| 942 | then calling close() closes the adopted handle. | - |
| 943 | Otherwise, close() does not actually close the file, but only flushes it. | - |
| 944 | | - |
| 945 | The QFile that is opened using this function is automatically set | - |
| 946 | to be in raw mode; this means that the file input/output functions | - |
| 947 | are slow. If you run into performance issues, you should try to | - |
| 948 | use one of the other open functions. | - |
| 949 | | - |
| 950 | \warning If \a fd is not a regular file, e.g, it is 0 (\c stdin), | - |
| 951 | 1 (\c stdout), or 2 (\c stderr), you may not be able to seek(). In | - |
| 952 | those cases, size() returns \c 0. See QIODevice::isSequential() | - |
| 953 | for more information. | - |
| 954 | | - |
| 955 | \warning For Windows CE you may not be able to call seek(), setSize(), | - |
| 956 | fileTime(). size() returns \c 0. | - |
| 957 | | - |
| 958 | \warning Since this function opens the file without specifying the file name, | - |
| 959 | you cannot use this QFile with a QFileInfo. | - |
| 960 | | - |
| 961 | \sa close() | - |
| 962 | */ | - |
| 963 | bool QFile::open(int fd, OpenMode mode, FileHandleFlags handleFlags) | - |
| 964 | { | - |
| 965 | Q_D(QFile); | - |
| 966 | if (isOpen()) { | - |
| 967 | qWarning("QFile::open: File (%s) already open", qPrintable(fileName())); | - |
| 968 | return false; | - |
| 969 | } | - |
| 970 | if (mode & Append) | - |
| 971 | mode |= WriteOnly; | - |
| 972 | unsetError(); | - |
| 973 | if ((mode & (ReadOnly | WriteOnly)) == 0) { | - |
| 974 | qWarning("QFile::open: File access not specified"); | - |
| 975 | return false; | - |
| 976 | } | - |
| 977 | if (d->openExternalFile(mode, fd, handleFlags)) { | - |
| 978 | QIODevice::open(mode); | - |
| 979 | if (!(mode & Append) && !isSequential()) { | - |
| 980 | qint64 pos = (qint64)QT_LSEEK(fd, QT_OFF_T(0), SEEK_CUR); | - |
| 981 | if (pos != -1) { | - |
| 982 | // Skip redundant checks in QFileDevice::seek(). | - |
| 983 | QIODevice::seek(pos); | - |
| 984 | } | - |
| 985 | } | - |
| 986 | return true; | - |
| 987 | } | - |
| 988 | return false; | - |
| 989 | } | - |
| 990 | | - |
| 991 | /*! | - |
| 992 | \reimp | - |
| 993 | */ | - |
| 994 | bool QFile::resize(qint64 sz) | - |
| 995 | { | - |
| 996 | return QFileDevice::resize(sz); // for now | - |
| 997 | } | - |
| 998 | | - |
| 999 | /*! | - |
| 1000 | \overload | - |
| 1001 | | - |
| 1002 | Sets \a fileName to size (in bytes) \a sz. Returns true if the file if | - |
| 1003 | the resize succeeds; false otherwise. If \a sz is larger than \a | - |
| 1004 | fileName currently is the new bytes will be set to 0, if \a sz is | - |
| 1005 | smaller the file is simply truncated. | - |
| 1006 | | - |
| 1007 | \sa resize() | - |
| 1008 | */ | - |
| 1009 | | - |
| 1010 | bool | - |
| 1011 | QFile::resize(const QString &fileName, qint64 sz) | - |
| 1012 | { | - |
| 1013 | return QFile(fileName).resize(sz); | - |
| 1014 | } | - |
| 1015 | | - |
| 1016 | /*! | - |
| 1017 | \reimp | - |
| 1018 | */ | - |
| 1019 | QFile::Permissions QFile::permissions() const | - |
| 1020 | { | - |
| 1021 | return QFileDevice::permissions(); // for now | - |
| 1022 | } | - |
| 1023 | | - |
| 1024 | /*! | - |
| 1025 | \overload | - |
| 1026 | | - |
| 1027 | Returns the complete OR-ed together combination of | - |
| 1028 | QFile::Permission for \a fileName. | - |
| 1029 | */ | - |
| 1030 | | - |
| 1031 | QFile::Permissions | - |
| 1032 | QFile::permissions(const QString &fileName) | - |
| 1033 | { | - |
| 1034 | return QFile(fileName).permissions(); | - |
| 1035 | } | - |
| 1036 | | - |
| 1037 | /*! | - |
| 1038 | Sets the permissions for the file to the \a permissions specified. | - |
| 1039 | Returns true if successful, or false if the permissions cannot be | - |
| 1040 | modified. | - |
| 1041 | | - |
| 1042 | \sa permissions(), setFileName() | - |
| 1043 | */ | - |
| 1044 | | - |
| 1045 | bool QFile::setPermissions(Permissions permissions) | - |
| 1046 | { | - |
| 1047 | return QFileDevice::setPermissions(permissions); // for now | - |
| 1048 | } | - |
| 1049 | | - |
| 1050 | /*! | - |
| 1051 | \overload | - |
| 1052 | | - |
| 1053 | Sets the permissions for \a fileName file to \a permissions. | - |
| 1054 | */ | - |
| 1055 | | - |
| 1056 | bool | - |
| 1057 | QFile::setPermissions(const QString &fileName, Permissions permissions) | - |
| 1058 | { | - |
| 1059 | return QFile(fileName).setPermissions(permissions); | - |
| 1060 | } | - |
| 1061 | | - |
| 1062 | /*! | - |
| 1063 | \reimp | - |
| 1064 | */ | - |
| 1065 | qint64 QFile::size() const | - |
| 1066 | { | - |
| 1067 | return QFileDevice::size(); // for now | - |
| 1068 | } | - |
| 1069 | | - |
| 1070 | QT_END_NAMESPACE | - |
| 1071 | | - |
| | |