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