| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/io/qsavefile.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||||||||
| 2 | ** | - | ||||||||||||||||||
| 3 | ** Copyright (C) 2012 David Faure <faure@kde.org> | - | ||||||||||||||||||
| 4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
| 5 | ** | - | ||||||||||||||||||
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||||||||
| 7 | ** | - | ||||||||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
| 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 The Qt Company. For licensing terms | - | ||||||||||||||||||
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
| 15 | ** information use the contact form at http://www.qt.io/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 or version 3 as published by the Free | - | ||||||||||||||||||
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
| 22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
| 25 | ** | - | ||||||||||||||||||
| 26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
| 29 | ** | - | ||||||||||||||||||
| 30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
| 31 | ** | - | ||||||||||||||||||
| 32 | ****************************************************************************/ | - | ||||||||||||||||||
| 33 | - | |||||||||||||||||||
| 34 | #include "qsavefile.h" | - | ||||||||||||||||||
| 35 | - | |||||||||||||||||||
| 36 | #ifndef QT_NO_TEMPORARYFILE | - | ||||||||||||||||||
| 37 | - | |||||||||||||||||||
| 38 | #include "qplatformdefs.h" | - | ||||||||||||||||||
| 39 | #include "private/qsavefile_p.h" | - | ||||||||||||||||||
| 40 | #include "qfileinfo.h" | - | ||||||||||||||||||
| 41 | #include "qabstractfileengine_p.h" | - | ||||||||||||||||||
| 42 | #include "qdebug.h" | - | ||||||||||||||||||
| 43 | #include "qtemporaryfile.h" | - | ||||||||||||||||||
| 44 | #include "private/qiodevice_p.h" | - | ||||||||||||||||||
| 45 | #include "private/qtemporaryfile_p.h" | - | ||||||||||||||||||
| 46 | #ifdef Q_OS_UNIX | - | ||||||||||||||||||
| 47 | #include <errno.h> | - | ||||||||||||||||||
| 48 | #endif | - | ||||||||||||||||||
| 49 | - | |||||||||||||||||||
| 50 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
| 51 | - | |||||||||||||||||||
| 52 | QSaveFilePrivate::QSaveFilePrivate() | - | ||||||||||||||||||
| 53 | : writeError(QFileDevice::NoError), | - | ||||||||||||||||||
| 54 | useTemporaryFile(true), | - | ||||||||||||||||||
| 55 | directWriteFallback(false) | - | ||||||||||||||||||
| 56 | { | - | ||||||||||||||||||
| 57 | } executed 579 times by 7 tests: end of blockExecuted by:
| 579 | ||||||||||||||||||
| 58 | - | |||||||||||||||||||
| 59 | QSaveFilePrivate::~QSaveFilePrivate() | - | ||||||||||||||||||
| 60 | { | - | ||||||||||||||||||
| 61 | } | - | ||||||||||||||||||
| 62 | - | |||||||||||||||||||
| 63 | /*! | - | ||||||||||||||||||
| 64 | \class QSaveFile | - | ||||||||||||||||||
| 65 | \inmodule QtCore | - | ||||||||||||||||||
| 66 | \brief The QSaveFile class provides an interface for safely writing to files. | - | ||||||||||||||||||
| 67 | - | |||||||||||||||||||
| 68 | \ingroup io | - | ||||||||||||||||||
| 69 | - | |||||||||||||||||||
| 70 | \reentrant | - | ||||||||||||||||||
| 71 | - | |||||||||||||||||||
| 72 | \since 5.1 | - | ||||||||||||||||||
| 73 | - | |||||||||||||||||||
| 74 | QSaveFile is an I/O device for writing text and binary files, without losing | - | ||||||||||||||||||
| 75 | existing data if the writing operation fails. | - | ||||||||||||||||||
| 76 | - | |||||||||||||||||||
| 77 | While writing, the contents will be written to a temporary file, and if | - | ||||||||||||||||||
| 78 | no error happened, commit() will move it to the final file. This ensures that | - | ||||||||||||||||||
| 79 | no data at the final file is lost in case an error happens while writing, | - | ||||||||||||||||||
| 80 | and no partially-written file is ever present at the final location. Always | - | ||||||||||||||||||
| 81 | use QSaveFile when saving entire documents to disk. | - | ||||||||||||||||||
| 82 | - | |||||||||||||||||||
| 83 | QSaveFile automatically detects errors while writing, such as the full partition | - | ||||||||||||||||||
| 84 | situation, where write() cannot write all the bytes. It will remember that | - | ||||||||||||||||||
| 85 | an error happened, and will discard the temporary file in commit(). | - | ||||||||||||||||||
| 86 | - | |||||||||||||||||||
| 87 | Much like with QFile, the file is opened with open(). Data is usually read | - | ||||||||||||||||||
| 88 | and written using QDataStream or QTextStream, but you can also call the | - | ||||||||||||||||||
| 89 | QIODevice-inherited functions read(), readLine(), readAll(), write(). | - | ||||||||||||||||||
| 90 | - | |||||||||||||||||||
| 91 | Unlike QFile, calling close() is not allowed. commit() replaces it. If commit() | - | ||||||||||||||||||
| 92 | was not called and the QSaveFile instance is destroyed, the temporary file is | - | ||||||||||||||||||
| 93 | discarded. | - | ||||||||||||||||||
| 94 | - | |||||||||||||||||||
| 95 | To abort saving due to an application error, call cancelWriting(), so that | - | ||||||||||||||||||
| 96 | even a call to commit() later on will not save. | - | ||||||||||||||||||
| 97 | - | |||||||||||||||||||
| 98 | \sa QTextStream, QDataStream, QFileInfo, QDir, QFile, QTemporaryFile | - | ||||||||||||||||||
| 99 | */ | - | ||||||||||||||||||
| 100 | - | |||||||||||||||||||
| 101 | /*! | - | ||||||||||||||||||
| 102 | Constructs a new file object with the given \a parent. | - | ||||||||||||||||||
| 103 | */ | - | ||||||||||||||||||
| 104 | QSaveFile::QSaveFile(QObject *parent) | - | ||||||||||||||||||
| 105 | : QFileDevice(*new QSaveFilePrivate, parent) | - | ||||||||||||||||||
| 106 | { | - | ||||||||||||||||||
| 107 | } never executed: end of block | 0 | ||||||||||||||||||
| 108 | /*! | - | ||||||||||||||||||
| 109 | Constructs a new file object to represent the file with the given \a name. | - | ||||||||||||||||||
| 110 | */ | - | ||||||||||||||||||
| 111 | QSaveFile::QSaveFile(const QString &name) | - | ||||||||||||||||||
| 112 | : QFileDevice(*new QSaveFilePrivate, 0) | - | ||||||||||||||||||
| 113 | { | - | ||||||||||||||||||
| 114 | Q_D(QSaveFile); | - | ||||||||||||||||||
| 115 | d->fileName = name; | - | ||||||||||||||||||
| 116 | } executed 579 times by 7 tests: end of blockExecuted by:
| 579 | ||||||||||||||||||
| 117 | /*! | - | ||||||||||||||||||
| 118 | Constructs a new file object with the given \a parent to represent the | - | ||||||||||||||||||
| 119 | file with the specified \a name. | - | ||||||||||||||||||
| 120 | */ | - | ||||||||||||||||||
| 121 | QSaveFile::QSaveFile(const QString &name, QObject *parent) | - | ||||||||||||||||||
| 122 | : QFileDevice(*new QSaveFilePrivate, parent) | - | ||||||||||||||||||
| 123 | { | - | ||||||||||||||||||
| 124 | Q_D(QSaveFile); | - | ||||||||||||||||||
| 125 | d->fileName = name; | - | ||||||||||||||||||
| 126 | } never executed: end of block | 0 | ||||||||||||||||||
| 127 | - | |||||||||||||||||||
| 128 | /*! | - | ||||||||||||||||||
| 129 | Destroys the file object, discarding the saved contents unless commit() was called. | - | ||||||||||||||||||
| 130 | */ | - | ||||||||||||||||||
| 131 | QSaveFile::~QSaveFile() | - | ||||||||||||||||||
| 132 | { | - | ||||||||||||||||||
| 133 | Q_D(QSaveFile); | - | ||||||||||||||||||
| 134 | QFileDevice::close(); | - | ||||||||||||||||||
| 135 | if (d->fileEngine) {
| 3-576 | ||||||||||||||||||
| 136 | d->fileEngine->remove(); | - | ||||||||||||||||||
| 137 | delete d->fileEngine; | - | ||||||||||||||||||
| 138 | d->fileEngine = 0; | - | ||||||||||||||||||
| 139 | } executed 3 times by 1 test: end of blockExecuted by:
| 3 | ||||||||||||||||||
| 140 | } executed 579 times by 7 tests: end of blockExecuted by:
| 579 | ||||||||||||||||||
| 141 | - | |||||||||||||||||||
| 142 | /*! | - | ||||||||||||||||||
| 143 | Returns the name set by setFileName() or to the QSaveFile | - | ||||||||||||||||||
| 144 | constructor. | - | ||||||||||||||||||
| 145 | - | |||||||||||||||||||
| 146 | \sa setFileName() | - | ||||||||||||||||||
| 147 | */ | - | ||||||||||||||||||
| 148 | QString QSaveFile::fileName() const | - | ||||||||||||||||||
| 149 | { | - | ||||||||||||||||||
| 150 | return d_func()->fileName; executed 12 times by 1 test: return d_func()->fileName;Executed by:
| 12 | ||||||||||||||||||
| 151 | } | - | ||||||||||||||||||
| 152 | - | |||||||||||||||||||
| 153 | /*! | - | ||||||||||||||||||
| 154 | Sets the \a name of the file. The name can have no path, a | - | ||||||||||||||||||
| 155 | relative path, or an absolute path. | - | ||||||||||||||||||
| 156 | - | |||||||||||||||||||
| 157 | \sa QFile::setFileName(), fileName() | - | ||||||||||||||||||
| 158 | */ | - | ||||||||||||||||||
| 159 | void QSaveFile::setFileName(const QString &name) | - | ||||||||||||||||||
| 160 | { | - | ||||||||||||||||||
| 161 | d_func()->fileName = name; | - | ||||||||||||||||||
| 162 | } never executed: end of block | 0 | ||||||||||||||||||
| 163 | - | |||||||||||||||||||
| 164 | /*! | - | ||||||||||||||||||
| 165 | Opens the file using OpenMode \a mode, returning true if successful; | - | ||||||||||||||||||
| 166 | otherwise false. | - | ||||||||||||||||||
| 167 | - | |||||||||||||||||||
| 168 | Important: the \a mode must include QIODevice::WriteOnly. | - | ||||||||||||||||||
| 169 | It may also have additional flags, such as QIODevice::Text and QIODevice::Unbuffered. | - | ||||||||||||||||||
| 170 | - | |||||||||||||||||||
| 171 | QIODevice::ReadWrite and QIODevice::Append are not supported at the moment. | - | ||||||||||||||||||
| 172 | - | |||||||||||||||||||
| 173 | \sa QIODevice::OpenMode, setFileName() | - | ||||||||||||||||||
| 174 | */ | - | ||||||||||||||||||
| 175 | bool QSaveFile::open(OpenMode mode) | - | ||||||||||||||||||
| 176 | { | - | ||||||||||||||||||
| 177 | Q_D(QSaveFile); | - | ||||||||||||||||||
| 178 | if (isOpen()) {
| 0-581 | ||||||||||||||||||
| 179 | qWarning("QSaveFile::open: File (%s) already open", qPrintable(fileName())); | - | ||||||||||||||||||
| 180 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 181 | } | - | ||||||||||||||||||
| 182 | unsetError(); | - | ||||||||||||||||||
| 183 | if ((mode & (ReadOnly | WriteOnly)) == 0) {
| 0-581 | ||||||||||||||||||
| 184 | qWarning("QSaveFile::open: Open mode not specified"); | - | ||||||||||||||||||
| 185 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 186 | } | - | ||||||||||||||||||
| 187 | // In the future we could implement ReadWrite by copying from the existing file to the temp file... | - | ||||||||||||||||||
| 188 | if ((mode & ReadOnly) || (mode & Append)) { | - | ||||||||||||||||||
| 189 | qWarning("QSaveFile::open: Unsupported open mode 0x%x", int(mode)); | - | ||||||||||||||||||
| 190 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 191 | } | - | ||||||||||||||||||
| 192 | - | |||||||||||||||||||
| 193 | // check if existing file is writable | - | ||||||||||||||||||
| 194 | QFileInfo existingFile(d->fileName); | - | ||||||||||||||||||
| 195 | if (existingFile.exists() && !existingFile.isWritable()) {
| 1-455 | ||||||||||||||||||
| 196 | d->setError(QFileDevice::WriteError, QSaveFile::tr("Existing file %1 is not writable").arg(d->fileName)); | - | ||||||||||||||||||
| 197 | d->writeError = QFileDevice::WriteError; | - | ||||||||||||||||||
| 198 | return false; executed 1 time by 1 test: return false;Executed by:
| 1 | ||||||||||||||||||
| 199 | } | - | ||||||||||||||||||
| 200 | - | |||||||||||||||||||
| 201 | if (existingFile.isDir()) {
| 0-580 | ||||||||||||||||||
| 202 | d->setError(QFileDevice::WriteError, QSaveFile::tr("Filename refers to a directory")); | - | ||||||||||||||||||
| 203 | d->writeError = QFileDevice::WriteError; | - | ||||||||||||||||||
| 204 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 205 | } | - | ||||||||||||||||||
| 206 | - | |||||||||||||||||||
| 207 | // Resolve symlinks. Don't use QFileInfo::canonicalFilePath so it still give the expected | - | ||||||||||||||||||
| 208 | // target even if the file does not exist | - | ||||||||||||||||||
| 209 | d->finalFileName = d->fileName; | - | ||||||||||||||||||
| 210 | if (existingFile.isSymLink()) {
| 5-575 | ||||||||||||||||||
| 211 | int maxDepth = 128; | - | ||||||||||||||||||
| 212 | while (--maxDepth && existingFile.isSymLink())
| 2-261 | ||||||||||||||||||
| 213 | existingFile.setFile(existingFile.symLinkTarget()); executed 258 times by 1 test: existingFile.setFile(existingFile.symLinkTarget());Executed by:
| 258 | ||||||||||||||||||
| 214 | if (maxDepth > 0)
| 2-3 | ||||||||||||||||||
| 215 | d->finalFileName = existingFile.filePath(); executed 3 times by 1 test: d->finalFileName = existingFile.filePath();Executed by:
| 3 | ||||||||||||||||||
| 216 | } executed 5 times by 1 test: end of blockExecuted by:
| 5 | ||||||||||||||||||
| 217 | - | |||||||||||||||||||
| 218 | d->fileEngine = new QTemporaryFileEngine; | - | ||||||||||||||||||
| 219 | static_cast<QTemporaryFileEngine *>(d->fileEngine)->initialize(d->finalFileName, 0666); | - | ||||||||||||||||||
| 220 | // Same as in QFile: QIODevice provides the buffering, so there's no need to request it from the file engine. | - | ||||||||||||||||||
| 221 | if (!d->fileEngine->open(mode | QIODevice::Unbuffered)) {
| 5-575 | ||||||||||||||||||
| 222 | QFileDevice::FileError err = d->fileEngine->error(); | - | ||||||||||||||||||
| 223 | #ifdef Q_OS_UNIX | - | ||||||||||||||||||
| 224 | if (d->directWriteFallback && err == QFileDevice::OpenError && errno == EACCES) {
| 0-3 | ||||||||||||||||||
| 225 | delete d->fileEngine; | - | ||||||||||||||||||
| 226 | d->fileEngine = QAbstractFileEngine::create(d->finalFileName); | - | ||||||||||||||||||
| 227 | if (d->fileEngine->open(mode | QIODevice::Unbuffered)) {
| 0-2 | ||||||||||||||||||
| 228 | d->useTemporaryFile = false; | - | ||||||||||||||||||
| 229 | QFileDevice::open(mode); | - | ||||||||||||||||||
| 230 | return true; executed 2 times by 1 test: return true;Executed by:
| 2 | ||||||||||||||||||
| 231 | } | - | ||||||||||||||||||
| 232 | err = d->fileEngine->error(); | - | ||||||||||||||||||
| 233 | } never executed: end of block | 0 | ||||||||||||||||||
| 234 | #endif | - | ||||||||||||||||||
| 235 | if (err == QFileDevice::UnspecifiedError)
| 0-3 | ||||||||||||||||||
| 236 | err = QFileDevice::OpenError; never executed: err = QFileDevice::OpenError; | 0 | ||||||||||||||||||
| 237 | d->setError(err, d->fileEngine->errorString()); | - | ||||||||||||||||||
| 238 | delete d->fileEngine; | - | ||||||||||||||||||
| 239 | d->fileEngine = 0; | - | ||||||||||||||||||
| 240 | return false; executed 3 times by 1 test: return false;Executed by:
| 3 | ||||||||||||||||||
| 241 | } | - | ||||||||||||||||||
| 242 | - | |||||||||||||||||||
| 243 | d->useTemporaryFile = true; | - | ||||||||||||||||||
| 244 | QFileDevice::open(mode); | - | ||||||||||||||||||
| 245 | if (existingFile.exists())
| 124-451 | ||||||||||||||||||
| 246 | setPermissions(existingFile.permissions()); executed 451 times by 7 tests: setPermissions(existingFile.permissions());Executed by:
| 451 | ||||||||||||||||||
| 247 | return true; executed 575 times by 7 tests: return true;Executed by:
| 575 | ||||||||||||||||||
| 248 | } | - | ||||||||||||||||||
| 249 | - | |||||||||||||||||||
| 250 | /*! | - | ||||||||||||||||||
| 251 | \reimp | - | ||||||||||||||||||
| 252 | This method has been made private so that it cannot be called, in order to prevent mistakes. | - | ||||||||||||||||||
| 253 | In order to finish writing the file, call commit(). | - | ||||||||||||||||||
| 254 | If instead you want to abort writing, call cancelWriting(). | - | ||||||||||||||||||
| 255 | */ | - | ||||||||||||||||||
| 256 | void QSaveFile::close() | - | ||||||||||||||||||
| 257 | { | - | ||||||||||||||||||
| 258 | qFatal("QSaveFile::close called"); | - | ||||||||||||||||||
| 259 | } never executed: end of block | 0 | ||||||||||||||||||
| 260 | - | |||||||||||||||||||
| 261 | /*! | - | ||||||||||||||||||
| 262 | Commits the changes to disk, if all previous writes were successful. | - | ||||||||||||||||||
| 263 | - | |||||||||||||||||||
| 264 | It is mandatory to call this at the end of the saving operation, otherwise the file will be | - | ||||||||||||||||||
| 265 | discarded. | - | ||||||||||||||||||
| 266 | - | |||||||||||||||||||
| 267 | If an error happened during writing, deletes the temporary file and returns \c false. | - | ||||||||||||||||||
| 268 | Otherwise, renames it to the final fileName and returns \c true on success. | - | ||||||||||||||||||
| 269 | Finally, closes the device. | - | ||||||||||||||||||
| 270 | - | |||||||||||||||||||
| 271 | \sa cancelWriting() | - | ||||||||||||||||||
| 272 | */ | - | ||||||||||||||||||
| 273 | bool QSaveFile::commit() | - | ||||||||||||||||||
| 274 | { | - | ||||||||||||||||||
| 275 | Q_D(QSaveFile); | - | ||||||||||||||||||
| 276 | if (!d->fileEngine)
| 2-574 | ||||||||||||||||||
| 277 | return false; executed 2 times by 1 test: return false;Executed by:
| 2 | ||||||||||||||||||
| 278 | - | |||||||||||||||||||
| 279 | if (!isOpen()) {
| 0-574 | ||||||||||||||||||
| 280 | qWarning("QSaveFile::commit: File (%s) is not open", qPrintable(fileName())); | - | ||||||||||||||||||
| 281 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 282 | } | - | ||||||||||||||||||
| 283 | QFileDevice::close(); // calls flush() | - | ||||||||||||||||||
| 284 | - | |||||||||||||||||||
| 285 | // Sync to disk if possible. Ignore errors (e.g. not supported). | - | ||||||||||||||||||
| 286 | d->fileEngine->syncToDisk(); | - | ||||||||||||||||||
| 287 | - | |||||||||||||||||||
| 288 | if (d->useTemporaryFile) {
| 2-572 | ||||||||||||||||||
| 289 | if (d->writeError != QFileDevice::NoError) {
| 1-571 | ||||||||||||||||||
| 290 | d->fileEngine->remove(); | - | ||||||||||||||||||
| 291 | d->writeError = QFileDevice::NoError; | - | ||||||||||||||||||
| 292 | delete d->fileEngine; | - | ||||||||||||||||||
| 293 | d->fileEngine = 0; | - | ||||||||||||||||||
| 294 | return false; executed 1 time by 1 test: return false;Executed by:
| 1 | ||||||||||||||||||
| 295 | } | - | ||||||||||||||||||
| 296 | // atomically replace old file with new file | - | ||||||||||||||||||
| 297 | // Can't use QFile::rename for that, must use the file engine directly | - | ||||||||||||||||||
| 298 | Q_ASSERT(d->fileEngine); | - | ||||||||||||||||||
| 299 | if (!d->fileEngine->renameOverwrite(d->finalFileName)) {
| 1-570 | ||||||||||||||||||
| 300 | d->setError(d->fileEngine->error(), d->fileEngine->errorString()); | - | ||||||||||||||||||
| 301 | d->fileEngine->remove(); | - | ||||||||||||||||||
| 302 | delete d->fileEngine; | - | ||||||||||||||||||
| 303 | d->fileEngine = 0; | - | ||||||||||||||||||
| 304 | return false; executed 1 time by 1 test: return false;Executed by:
| 1 | ||||||||||||||||||
| 305 | } | - | ||||||||||||||||||
| 306 | } executed 570 times by 7 tests: end of blockExecuted by:
| 570 | ||||||||||||||||||
| 307 | delete d->fileEngine; | - | ||||||||||||||||||
| 308 | d->fileEngine = 0; | - | ||||||||||||||||||
| 309 | return true; executed 572 times by 7 tests: return true;Executed by:
| 572 | ||||||||||||||||||
| 310 | } | - | ||||||||||||||||||
| 311 | - | |||||||||||||||||||
| 312 | /*! | - | ||||||||||||||||||
| 313 | Cancels writing the new file. | - | ||||||||||||||||||
| 314 | - | |||||||||||||||||||
| 315 | If the application changes its mind while saving, it can call cancelWriting(), | - | ||||||||||||||||||
| 316 | which sets an error code so that commit() will discard the temporary file. | - | ||||||||||||||||||
| 317 | - | |||||||||||||||||||
| 318 | Alternatively, it can simply make sure not to call commit(). | - | ||||||||||||||||||
| 319 | - | |||||||||||||||||||
| 320 | Further write operations are possible after calling this method, but none | - | ||||||||||||||||||
| 321 | of it will have any effect, the written file will be discarded. | - | ||||||||||||||||||
| 322 | - | |||||||||||||||||||
| 323 | This method has no effect when direct write fallback is used. This is the case | - | ||||||||||||||||||
| 324 | when saving over an existing file in a readonly directory: no temporary file can | - | ||||||||||||||||||
| 325 | be created, so the existing file is overwritten no matter what, and cancelWriting() | - | ||||||||||||||||||
| 326 | cannot do anything about that, the contents of the existing file will be lost. | - | ||||||||||||||||||
| 327 | - | |||||||||||||||||||
| 328 | \sa commit() | - | ||||||||||||||||||
| 329 | */ | - | ||||||||||||||||||
| 330 | void QSaveFile::cancelWriting() | - | ||||||||||||||||||
| 331 | { | - | ||||||||||||||||||
| 332 | Q_D(QSaveFile); | - | ||||||||||||||||||
| 333 | if (!isOpen())
| 0-2 | ||||||||||||||||||
| 334 | return; never executed: return; | 0 | ||||||||||||||||||
| 335 | d->setError(QFileDevice::WriteError, QSaveFile::tr("Writing canceled by application")); | - | ||||||||||||||||||
| 336 | d->writeError = QFileDevice::WriteError; | - | ||||||||||||||||||
| 337 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||||||||
| 338 | - | |||||||||||||||||||
| 339 | /*! | - | ||||||||||||||||||
| 340 | \reimp | - | ||||||||||||||||||
| 341 | */ | - | ||||||||||||||||||
| 342 | qint64 QSaveFile::writeData(const char *data, qint64 len) | - | ||||||||||||||||||
| 343 | { | - | ||||||||||||||||||
| 344 | Q_D(QSaveFile); | - | ||||||||||||||||||
| 345 | if (d->writeError != QFileDevice::NoError)
| 0-6664 | ||||||||||||||||||
| 346 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
| 347 | - | |||||||||||||||||||
| 348 | const qint64 ret = QFileDevice::writeData(data, len); | - | ||||||||||||||||||
| 349 | - | |||||||||||||||||||
| 350 | if (d->error != QFileDevice::NoError)
| 0-6664 | ||||||||||||||||||
| 351 | d->writeError = d->error; never executed: d->writeError = d->error; | 0 | ||||||||||||||||||
| 352 | return ret; executed 6664 times by 7 tests: return ret;Executed by:
| 6664 | ||||||||||||||||||
| 353 | } | - | ||||||||||||||||||
| 354 | - | |||||||||||||||||||
| 355 | /*! | - | ||||||||||||||||||
| 356 | Allows writing over the existing file if necessary. | - | ||||||||||||||||||
| 357 | - | |||||||||||||||||||
| 358 | QSaveFile creates a temporary file in the same directory as the final | - | ||||||||||||||||||
| 359 | file and atomically renames it. However this is not possible if the | - | ||||||||||||||||||
| 360 | directory permissions do not allow creating new files. | - | ||||||||||||||||||
| 361 | In order to preserve atomicity guarantees, open() fails when it | - | ||||||||||||||||||
| 362 | cannot create the temporary file. | - | ||||||||||||||||||
| 363 | - | |||||||||||||||||||
| 364 | In order to allow users to edit files with write permissions in a | - | ||||||||||||||||||
| 365 | directory with restricted permissions, call setDirectWriteFallback() with | - | ||||||||||||||||||
| 366 | \a enabled set to true, and the following calls to open() will fallback to | - | ||||||||||||||||||
| 367 | opening the existing file directly and writing into it, without the use of | - | ||||||||||||||||||
| 368 | a temporary file. | - | ||||||||||||||||||
| 369 | This does not have atomicity guarantees, i.e. an application crash or | - | ||||||||||||||||||
| 370 | for instance a power failure could lead to a partially-written file on disk. | - | ||||||||||||||||||
| 371 | It also means cancelWriting() has no effect, in such a case. | - | ||||||||||||||||||
| 372 | - | |||||||||||||||||||
| 373 | Typically, to save documents edited by the user, call setDirectWriteFallback(true), | - | ||||||||||||||||||
| 374 | and to save application internal files (configuration files, data files, ...), keep | - | ||||||||||||||||||
| 375 | the default setting which ensures atomicity. | - | ||||||||||||||||||
| 376 | - | |||||||||||||||||||
| 377 | \sa directWriteFallback() | - | ||||||||||||||||||
| 378 | */ | - | ||||||||||||||||||
| 379 | void QSaveFile::setDirectWriteFallback(bool enabled) | - | ||||||||||||||||||
| 380 | { | - | ||||||||||||||||||
| 381 | Q_D(QSaveFile); | - | ||||||||||||||||||
| 382 | d->directWriteFallback = enabled; | - | ||||||||||||||||||
| 383 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||||||||
| 384 | - | |||||||||||||||||||
| 385 | /*! | - | ||||||||||||||||||
| 386 | Returns \c true if the fallback solution for saving files in read-only | - | ||||||||||||||||||
| 387 | directories is enabled. | - | ||||||||||||||||||
| 388 | - | |||||||||||||||||||
| 389 | \sa setDirectWriteFallback() | - | ||||||||||||||||||
| 390 | */ | - | ||||||||||||||||||
| 391 | bool QSaveFile::directWriteFallback() const | - | ||||||||||||||||||
| 392 | { | - | ||||||||||||||||||
| 393 | Q_D(const QSaveFile); | - | ||||||||||||||||||
| 394 | return d->directWriteFallback; executed 2 times by 1 test: return d->directWriteFallback;Executed by:
| 2 | ||||||||||||||||||
| 395 | } | - | ||||||||||||||||||
| 396 | - | |||||||||||||||||||
| 397 | QT_END_NAMESPACE | - | ||||||||||||||||||
| 398 | - | |||||||||||||||||||
| 399 | #endif // QT_NO_TEMPORARYFILE | - | ||||||||||||||||||
| Source code | Switch to Preprocessed file |