| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/io/qtemporaryfile.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
| 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 "qtemporaryfile.h" | - | ||||||||||||
| 35 | - | |||||||||||||
| 36 | #ifndef QT_NO_TEMPORARYFILE | - | ||||||||||||
| 37 | - | |||||||||||||
| 38 | #include "qplatformdefs.h" | - | ||||||||||||
| 39 | #include "private/qtemporaryfile_p.h" | - | ||||||||||||
| 40 | #include "private/qfile_p.h" | - | ||||||||||||
| 41 | #include "private/qsystemerror_p.h" | - | ||||||||||||
| 42 | - | |||||||||||||
| 43 | #if !defined(Q_OS_WIN) | - | ||||||||||||
| 44 | #include "private/qcore_unix_p.h" // overrides QT_OPEN | - | ||||||||||||
| 45 | #include <errno.h> | - | ||||||||||||
| 46 | #endif | - | ||||||||||||
| 47 | - | |||||||||||||
| 48 | #if defined(QT_BUILD_CORE_LIB) | - | ||||||||||||
| 49 | #include "qcoreapplication.h" | - | ||||||||||||
| 50 | #endif | - | ||||||||||||
| 51 | - | |||||||||||||
| 52 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 53 | - | |||||||||||||
| 54 | #if defined(Q_OS_WIN) | - | ||||||||||||
| 55 | typedef ushort Char; | - | ||||||||||||
| 56 | - | |||||||||||||
| 57 | static inline Char Latin1Char(char ch) | - | ||||||||||||
| 58 | { | - | ||||||||||||
| 59 | return ushort(uchar(ch)); | - | ||||||||||||
| 60 | } | - | ||||||||||||
| 61 | - | |||||||||||||
| 62 | typedef HANDLE NativeFileHandle; | - | ||||||||||||
| 63 | - | |||||||||||||
| 64 | #else // POSIX | - | ||||||||||||
| 65 | typedef char Char; | - | ||||||||||||
| 66 | typedef char Latin1Char; | - | ||||||||||||
| 67 | typedef int NativeFileHandle; | - | ||||||||||||
| 68 | #endif | - | ||||||||||||
| 69 | - | |||||||||||||
| 70 | /* | - | ||||||||||||
| 71 | * Copyright (c) 1987, 1993 | - | ||||||||||||
| 72 | * The Regents of the University of California. All rights reserved. | - | ||||||||||||
| 73 | * | - | ||||||||||||
| 74 | * Redistribution and use in source and binary forms, with or without | - | ||||||||||||
| 75 | * modification, are permitted provided that the following conditions | - | ||||||||||||
| 76 | * are met: | - | ||||||||||||
| 77 | * 1. Redistributions of source code must retain the above copyright | - | ||||||||||||
| 78 | * notice, this list of conditions and the following disclaimer. | - | ||||||||||||
| 79 | * 2. Redistributions in binary form must reproduce the above copyright | - | ||||||||||||
| 80 | * notice, this list of conditions and the following disclaimer in the | - | ||||||||||||
| 81 | * documentation and/or other materials provided with the distribution. | - | ||||||||||||
| 82 | * 3. Neither the name of the University nor the names of its contributors | - | ||||||||||||
| 83 | * may be used to endorse or promote products derived from this software | - | ||||||||||||
| 84 | * without specific prior written permission. | - | ||||||||||||
| 85 | * | - | ||||||||||||
| 86 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | - | ||||||||||||
| 87 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | - | ||||||||||||
| 88 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | - | ||||||||||||
| 89 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | - | ||||||||||||
| 90 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | - | ||||||||||||
| 91 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | - | ||||||||||||
| 92 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | - | ||||||||||||
| 93 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | - | ||||||||||||
| 94 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | - | ||||||||||||
| 95 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | - | ||||||||||||
| 96 | * SUCH DAMAGE. | - | ||||||||||||
| 97 | */ | - | ||||||||||||
| 98 | - | |||||||||||||
| 99 | /*! | - | ||||||||||||
| 100 | \internal | - | ||||||||||||
| 101 | - | |||||||||||||
| 102 | Generates a unique file path and returns a native handle to the open file. | - | ||||||||||||
| 103 | \a path is used as a template when generating unique paths, \a pos | - | ||||||||||||
| 104 | identifies the position of the first character that will be replaced in the | - | ||||||||||||
| 105 | template and \a length the number of characters that may be substituted. | - | ||||||||||||
| 106 | \a mode specifies the file mode bits (not used on Windows). | - | ||||||||||||
| 107 | - | |||||||||||||
| 108 | Returns an open handle to the newly created file if successful, an invalid | - | ||||||||||||
| 109 | handle otherwise. In both cases, the string in \a path will be changed and | - | ||||||||||||
| 110 | contain the generated path name. | - | ||||||||||||
| 111 | */ | - | ||||||||||||
| 112 | static bool createFileFromTemplate(NativeFileHandle &file, | - | ||||||||||||
| 113 | QFileSystemEntry::NativePath &path, size_t pos, size_t length, quint32 mode, | - | ||||||||||||
| 114 | QSystemError &error) | - | ||||||||||||
| 115 | { | - | ||||||||||||
| 116 | Q_ASSERT(length != 0); | - | ||||||||||||
| 117 | Q_ASSERT(pos < size_t(path.length())); | - | ||||||||||||
| 118 | Q_ASSERT(length <= size_t(path.length()) - pos); | - | ||||||||||||
| 119 | - | |||||||||||||
| 120 | Char *const placeholderStart = (Char *)path.data() + pos; | - | ||||||||||||
| 121 | Char *const placeholderEnd = placeholderStart + length; | - | ||||||||||||
| 122 | - | |||||||||||||
| 123 | // Initialize placeholder with random chars + PID. | - | ||||||||||||
| 124 | { | - | ||||||||||||
| 125 | Char *rIter = placeholderEnd; | - | ||||||||||||
| 126 | - | |||||||||||||
| 127 | #if defined(QT_BUILD_CORE_LIB) | - | ||||||||||||
| 128 | quint64 pid = quint64(QCoreApplication::applicationPid()); | - | ||||||||||||
| 129 | do { | - | ||||||||||||
| 130 | *--rIter = Latin1Char((pid % 10) + '0'); | - | ||||||||||||
| 131 | pid /= 10; | - | ||||||||||||
| 132 | } while (rIter != placeholderStart && pid != 0); executed 8846 times by 25 tests: end of blockExecuted by:
| 0-8846 | ||||||||||||
| 133 | #endif | - | ||||||||||||
| 134 | - | |||||||||||||
| 135 | while (rIter != placeholderStart) {
| 2156-4132 | ||||||||||||
| 136 | char ch = char((qrand() & 0xffff) % (26 + 26)); | - | ||||||||||||
| 137 | if (ch < 26)
| 1970-2162 | ||||||||||||
| 138 | *--rIter = Latin1Char(ch + 'A'); executed 2162 times by 25 tests: *--rIter = Latin1Char(ch + 'A');Executed by:
| 2162 | ||||||||||||
| 139 | else | - | ||||||||||||
| 140 | *--rIter = Latin1Char(ch - 26 + 'a'); executed 1970 times by 17 tests: *--rIter = Latin1Char(ch - 26 + 'a');Executed by:
| 1970 | ||||||||||||
| 141 | } | - | ||||||||||||
| 142 | } | - | ||||||||||||
| 143 | - | |||||||||||||
| 144 | for (;;) { | - | ||||||||||||
| 145 | // Atomically create file and obtain handle | - | ||||||||||||
| 146 | #if defined(Q_OS_WIN) | - | ||||||||||||
| 147 | Q_UNUSED(mode); | - | ||||||||||||
| 148 | - | |||||||||||||
| 149 | # ifndef Q_OS_WINRT | - | ||||||||||||
| 150 | file = CreateFile((const wchar_t *)path.constData(), | - | ||||||||||||
| 151 | GENERIC_READ | GENERIC_WRITE, | - | ||||||||||||
| 152 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_NEW, | - | ||||||||||||
| 153 | FILE_ATTRIBUTE_NORMAL, NULL); | - | ||||||||||||
| 154 | # else // !Q_OS_WINRT | - | ||||||||||||
| 155 | file = CreateFile2((const wchar_t *)path.constData(), | - | ||||||||||||
| 156 | GENERIC_READ | GENERIC_WRITE, | - | ||||||||||||
| 157 | FILE_SHARE_READ | FILE_SHARE_WRITE, CREATE_NEW, | - | ||||||||||||
| 158 | NULL); | - | ||||||||||||
| 159 | # endif // Q_OS_WINRT | - | ||||||||||||
| 160 | - | |||||||||||||
| 161 | if (file != INVALID_HANDLE_VALUE) | - | ||||||||||||
| 162 | return true; | - | ||||||||||||
| 163 | - | |||||||||||||
| 164 | DWORD err = GetLastError(); | - | ||||||||||||
| 165 | if (err == ERROR_ACCESS_DENIED) { | - | ||||||||||||
| 166 | WIN32_FILE_ATTRIBUTE_DATA attributes; | - | ||||||||||||
| 167 | if (!GetFileAttributesEx((const wchar_t *)path.constData(), | - | ||||||||||||
| 168 | GetFileExInfoStandard, &attributes) | - | ||||||||||||
| 169 | || attributes.dwFileAttributes == INVALID_FILE_ATTRIBUTES) { | - | ||||||||||||
| 170 | // Potential write error (read-only parent directory, etc.). | - | ||||||||||||
| 171 | error = QSystemError(err, QSystemError::NativeError); | - | ||||||||||||
| 172 | return false; | - | ||||||||||||
| 173 | } // else file already exists as a directory. | - | ||||||||||||
| 174 | } else if (err != ERROR_FILE_EXISTS) { | - | ||||||||||||
| 175 | error = QSystemError(err, QSystemError::NativeError); | - | ||||||||||||
| 176 | return false; | - | ||||||||||||
| 177 | } | - | ||||||||||||
| 178 | #else // POSIX | - | ||||||||||||
| 179 | file = QT_OPEN(path.constData(), | - | ||||||||||||
| 180 | QT_OPEN_CREAT | O_EXCL | QT_OPEN_RDWR | QT_OPEN_LARGEFILE, | - | ||||||||||||
| 181 | static_cast<mode_t>(mode)); | - | ||||||||||||
| 182 | - | |||||||||||||
| 183 | if (file != -1)
| 316-2143 | ||||||||||||
| 184 | return true; executed 2143 times by 25 tests: return true;Executed by:
| 2143 | ||||||||||||
| 185 | - | |||||||||||||
| 186 | int err = errno; | - | ||||||||||||
| 187 | if (err != EEXIST) {
| 13-303 | ||||||||||||
| 188 | error = QSystemError(err, QSystemError::NativeError); | - | ||||||||||||
| 189 | return false; executed 13 times by 2 tests: return false;Executed by:
| 13 | ||||||||||||
| 190 | } | - | ||||||||||||
| 191 | #endif | - | ||||||||||||
| 192 | - | |||||||||||||
| 193 | /* tricky little algorwwithm for backward compatibility */ | - | ||||||||||||
| 194 | for (Char *iter = placeholderStart;;) { | - | ||||||||||||
| 195 | // Character progression: [0-9] => 'a' ... 'z' => 'A' .. 'Z' | - | ||||||||||||
| 196 | // String progression: "ZZaiC" => "aabiC" | - | ||||||||||||
| 197 | switch (char(*iter)) { | - | ||||||||||||
| 198 | case 'Z': executed 6 times by 1 test: case 'Z':Executed by:
| 6 | ||||||||||||
| 199 | // Rollover, advance next character | - | ||||||||||||
| 200 | *iter = Latin1Char('a'); | - | ||||||||||||
| 201 | if (++iter == placeholderEnd) {
| 0-6 | ||||||||||||
| 202 | // Out of alternatives. Return file exists error, previously set. | - | ||||||||||||
| 203 | error = QSystemError(err, QSystemError::NativeError); | - | ||||||||||||
| 204 | return false; never executed: return false; | 0 | ||||||||||||
| 205 | } | - | ||||||||||||
| 206 | - | |||||||||||||
| 207 | continue; executed 6 times by 1 test: continue;Executed by:
| 6 | ||||||||||||
| 208 | - | |||||||||||||
| 209 | case '0': case '1': case '2': case '3': case '4': never executed: case '0':never executed: case '1':never executed: case '2':never executed: case '3':never executed: case '4': | 0 | ||||||||||||
| 210 | case '5': case '6': case '7': case '8': case '9': never executed: case '5':never executed: case '6':never executed: case '7':never executed: case '8':never executed: case '9': | 0 | ||||||||||||
| 211 | *iter = Latin1Char('a'); | - | ||||||||||||
| 212 | break; never executed: break; | 0 | ||||||||||||
| 213 | - | |||||||||||||
| 214 | case 'z': executed 8 times by 1 test: case 'z':Executed by:
| 8 | ||||||||||||
| 215 | // increment 'z' to 'A' | - | ||||||||||||
| 216 | *iter = Latin1Char('A'); | - | ||||||||||||
| 217 | break; executed 8 times by 1 test: break;Executed by:
| 8 | ||||||||||||
| 218 | - | |||||||||||||
| 219 | default: executed 295 times by 1 test: default:Executed by:
| 295 | ||||||||||||
| 220 | ++*iter; | - | ||||||||||||
| 221 | break; executed 295 times by 1 test: break;Executed by:
| 295 | ||||||||||||
| 222 | } | - | ||||||||||||
| 223 | break; executed 303 times by 1 test: break;Executed by:
| 303 | ||||||||||||
| 224 | } | - | ||||||||||||
| 225 | } executed 303 times by 1 test: end of blockExecuted by:
| 303 | ||||||||||||
| 226 | - | |||||||||||||
| 227 | Q_ASSERT(false); | - | ||||||||||||
| 228 | return false; never executed: return false; | 0 | ||||||||||||
| 229 | } | - | ||||||||||||
| 230 | - | |||||||||||||
| 231 | //************* QTemporaryFileEngine | - | ||||||||||||
| 232 | QTemporaryFileEngine::~QTemporaryFileEngine() | - | ||||||||||||
| 233 | { | - | ||||||||||||
| 234 | Q_D(QFSFileEngine); | - | ||||||||||||
| 235 | d->unmapAll(); | - | ||||||||||||
| 236 | QFSFileEngine::close(); | - | ||||||||||||
| 237 | } executed 2155 times by 25 tests: end of blockExecuted by:
| 2155 | ||||||||||||
| 238 | - | |||||||||||||
| 239 | bool QTemporaryFileEngine::isReallyOpen() const | - | ||||||||||||
| 240 | { | - | ||||||||||||
| 241 | Q_D(const QFSFileEngine); | - | ||||||||||||
| 242 | - | |||||||||||||
| 243 | if (!((0 == d->fh) && (-1 == d->fd)
| 0-4343 | ||||||||||||
| 244 | #if defined Q_OS_WIN | - | ||||||||||||
| 245 | && (INVALID_HANDLE_VALUE == d->fileHandle) | - | ||||||||||||
| 246 | #endif | - | ||||||||||||
| 247 | )) | - | ||||||||||||
| 248 | return true; executed 22 times by 3 tests: return true;Executed by:
| 22 | ||||||||||||
| 249 | - | |||||||||||||
| 250 | return false; executed 4321 times by 25 tests: return false;Executed by:
| 4321 | ||||||||||||
| 251 | - | |||||||||||||
| 252 | } | - | ||||||||||||
| 253 | - | |||||||||||||
| 254 | void QTemporaryFileEngine::setFileName(const QString &file) | - | ||||||||||||
| 255 | { | - | ||||||||||||
| 256 | // Really close the file, so we don't leak | - | ||||||||||||
| 257 | QFSFileEngine::close(); | - | ||||||||||||
| 258 | QFSFileEngine::setFileName(file); | - | ||||||||||||
| 259 | } executed 218 times by 8 tests: end of blockExecuted by:
| 218 | ||||||||||||
| 260 | - | |||||||||||||
| 261 | void QTemporaryFileEngine::setFileTemplate(const QString &fileTemplate) | - | ||||||||||||
| 262 | { | - | ||||||||||||
| 263 | Q_D(QFSFileEngine); | - | ||||||||||||
| 264 | if (filePathIsTemplate)
| 1 | ||||||||||||
| 265 | d->fileEntry = QFileSystemEntry(fileTemplate); executed 1 time by 1 test: d->fileEntry = QFileSystemEntry(fileTemplate);Executed by:
| 1 | ||||||||||||
| 266 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||
| 267 | - | |||||||||||||
| 268 | bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode) | - | ||||||||||||
| 269 | { | - | ||||||||||||
| 270 | Q_D(QFSFileEngine); | - | ||||||||||||
| 271 | Q_ASSERT(!isReallyOpen()); | - | ||||||||||||
| 272 | - | |||||||||||||
| 273 | openMode |= QIODevice::ReadWrite; | - | ||||||||||||
| 274 | - | |||||||||||||
| 275 | if (!filePathIsTemplate)
| 3-2156 | ||||||||||||
| 276 | return QFSFileEngine::open(openMode); executed 3 times by 1 test: return QFSFileEngine::open(openMode);Executed by:
| 3 | ||||||||||||
| 277 | - | |||||||||||||
| 278 | QString qfilename = d->fileEntry.filePath(); | - | ||||||||||||
| 279 | - | |||||||||||||
| 280 | // Ensure there is a placeholder mask | - | ||||||||||||
| 281 | uint phPos = qfilename.length(); | - | ||||||||||||
| 282 | uint phLength = 0; | - | ||||||||||||
| 283 | - | |||||||||||||
| 284 | while (phPos != 0) {
| 26-21363 | ||||||||||||
| 285 | --phPos; | - | ||||||||||||
| 286 | - | |||||||||||||
| 287 | if (qfilename[phPos] == QLatin1Char('X')) {
| 8536-12827 | ||||||||||||
| 288 | ++phLength; | - | ||||||||||||
| 289 | continue; executed 8536 times by 18 tests: continue;Executed by:
| 8536 | ||||||||||||
| 290 | } | - | ||||||||||||
| 291 | - | |||||||||||||
| 292 | if (phLength >= 6
| 1393-11434 | ||||||||||||
| 293 | || qfilename[phPos] == QLatin1Char('/')) {
| 737-10697 | ||||||||||||
| 294 | ++phPos; | - | ||||||||||||
| 295 | break; executed 2130 times by 25 tests: break;Executed by:
| 2130 | ||||||||||||
| 296 | } | - | ||||||||||||
| 297 | - | |||||||||||||
| 298 | // start over | - | ||||||||||||
| 299 | phLength = 0; | - | ||||||||||||
| 300 | } executed 10697 times by 15 tests: end of blockExecuted by:
| 10697 | ||||||||||||
| 301 | - | |||||||||||||
| 302 | if (phLength < 6)
| 757-1399 | ||||||||||||
| 303 | qfilename.append(QLatin1String(".XXXXXX")); executed 757 times by 10 tests: qfilename.append(QLatin1String(".XXXXXX"));Executed by:
| 757 | ||||||||||||
| 304 | - | |||||||||||||
| 305 | // "Nativify" :-) | - | ||||||||||||
| 306 | QFileSystemEntry::NativePath filename = QFileSystemEngine::absoluteName( | - | ||||||||||||
| 307 | QFileSystemEntry(qfilename, QFileSystemEntry::FromInternalPath())) | - | ||||||||||||
| 308 | .nativeFilePath(); | - | ||||||||||||
| 309 | - | |||||||||||||
| 310 | // Find mask in native path | - | ||||||||||||
| 311 | phPos = filename.length(); | - | ||||||||||||
| 312 | phLength = 0; | - | ||||||||||||
| 313 | while (phPos != 0) {
| 0-15452 | ||||||||||||
| 314 | --phPos; | - | ||||||||||||
| 315 | - | |||||||||||||
| 316 | if (filename[phPos] == Latin1Char('X')) {
| 2455-12997 | ||||||||||||
| 317 | ++phLength; | - | ||||||||||||
| 318 | continue; executed 12997 times by 25 tests: continue;Executed by:
| 12997 | ||||||||||||
| 319 | } | - | ||||||||||||
| 320 | - | |||||||||||||
| 321 | if (phLength >= 6) {
| 299-2156 | ||||||||||||
| 322 | ++phPos; | - | ||||||||||||
| 323 | break; executed 2156 times by 25 tests: break;Executed by:
| 2156 | ||||||||||||
| 324 | } | - | ||||||||||||
| 325 | - | |||||||||||||
| 326 | // start over | - | ||||||||||||
| 327 | phLength = 0; | - | ||||||||||||
| 328 | } executed 299 times by 8 tests: end of blockExecuted by:
| 299 | ||||||||||||
| 329 | - | |||||||||||||
| 330 | Q_ASSERT(phLength >= 6); | - | ||||||||||||
| 331 | - | |||||||||||||
| 332 | QSystemError error; | - | ||||||||||||
| 333 | #if defined(Q_OS_WIN) | - | ||||||||||||
| 334 | NativeFileHandle &file = d->fileHandle; | - | ||||||||||||
| 335 | #else // POSIX | - | ||||||||||||
| 336 | NativeFileHandle &file = d->fd; | - | ||||||||||||
| 337 | #endif | - | ||||||||||||
| 338 | - | |||||||||||||
| 339 | if (!createFileFromTemplate(file, filename, phPos, phLength, fileMode, error)) {
| 13-2143 | ||||||||||||
| 340 | setError(QFile::OpenError, error.toString()); | - | ||||||||||||
| 341 | return false; executed 13 times by 2 tests: return false;Executed by:
| 13 | ||||||||||||
| 342 | } | - | ||||||||||||
| 343 | - | |||||||||||||
| 344 | d->fileEntry = QFileSystemEntry(filename, QFileSystemEntry::FromNativePath()); | - | ||||||||||||
| 345 | - | |||||||||||||
| 346 | #if !defined(Q_OS_WIN) || defined(Q_OS_WINRT) | - | ||||||||||||
| 347 | d->closeFileHandle = true; | - | ||||||||||||
| 348 | #endif | - | ||||||||||||
| 349 | - | |||||||||||||
| 350 | filePathIsTemplate = false; | - | ||||||||||||
| 351 | - | |||||||||||||
| 352 | d->openMode = openMode; | - | ||||||||||||
| 353 | d->lastFlushFailed = false; | - | ||||||||||||
| 354 | d->tried_stat = 0; | - | ||||||||||||
| 355 | - | |||||||||||||
| 356 | return true; executed 2143 times by 25 tests: return true;Executed by:
| 2143 | ||||||||||||
| 357 | } | - | ||||||||||||
| 358 | - | |||||||||||||
| 359 | bool QTemporaryFileEngine::remove() | - | ||||||||||||
| 360 | { | - | ||||||||||||
| 361 | Q_D(QFSFileEngine); | - | ||||||||||||
| 362 | // Since the QTemporaryFileEngine::close() does not really close the file, | - | ||||||||||||
| 363 | // we must explicitly call QFSFileEngine::close() before we remove it. | - | ||||||||||||
| 364 | d->unmapAll(); | - | ||||||||||||
| 365 | QFSFileEngine::close(); | - | ||||||||||||
| 366 | if (QFSFileEngine::remove()) {
| 1-353 | ||||||||||||
| 367 | d->fileEntry.clear(); | - | ||||||||||||
| 368 | // If a QTemporaryFile is constructed using a template file path, the path | - | ||||||||||||
| 369 | // is generated in QTemporaryFileEngine::open() and then filePathIsTemplate | - | ||||||||||||
| 370 | // is set to false. If remove() and then open() are called on the same | - | ||||||||||||
| 371 | // QTemporaryFile, the path is not regenerated. Here we ensure that if the | - | ||||||||||||
| 372 | // file path was generated, it will be generated again in the scenario above. | - | ||||||||||||
| 373 | filePathIsTemplate = filePathWasTemplate; | - | ||||||||||||
| 374 | return true; executed 353 times by 23 tests: return true;Executed by:
| 353 | ||||||||||||
| 375 | } | - | ||||||||||||
| 376 | return false; executed 1 time by 1 test: return false;Executed by:
| 1 | ||||||||||||
| 377 | } | - | ||||||||||||
| 378 | - | |||||||||||||
| 379 | bool QTemporaryFileEngine::rename(const QString &newName) | - | ||||||||||||
| 380 | { | - | ||||||||||||
| 381 | QFSFileEngine::close(); | - | ||||||||||||
| 382 | return QFSFileEngine::rename(newName); executed 790 times by 15 tests: return QFSFileEngine::rename(newName);Executed by:
| 790 | ||||||||||||
| 383 | } | - | ||||||||||||
| 384 | - | |||||||||||||
| 385 | bool QTemporaryFileEngine::renameOverwrite(const QString &newName) | - | ||||||||||||
| 386 | { | - | ||||||||||||
| 387 | QFSFileEngine::close(); | - | ||||||||||||
| 388 | return QFSFileEngine::renameOverwrite(newName); executed 571 times by 7 tests: return QFSFileEngine::renameOverwrite(newName);Executed by:
| 571 | ||||||||||||
| 389 | } | - | ||||||||||||
| 390 | - | |||||||||||||
| 391 | bool QTemporaryFileEngine::close() | - | ||||||||||||
| 392 | { | - | ||||||||||||
| 393 | // Don't close the file, just seek to the front. | - | ||||||||||||
| 394 | seek(0); | - | ||||||||||||
| 395 | setError(QFile::UnspecifiedError, QString()); | - | ||||||||||||
| 396 | return true; executed 2166 times by 25 tests: return true;Executed by:
| 2166 | ||||||||||||
| 397 | } | - | ||||||||||||
| 398 | - | |||||||||||||
| 399 | //************* QTemporaryFilePrivate | - | ||||||||||||
| 400 | - | |||||||||||||
| 401 | QTemporaryFilePrivate::QTemporaryFilePrivate() : autoRemove(true) | - | ||||||||||||
| 402 | { | - | ||||||||||||
| 403 | } executed 1579 times by 24 tests: end of blockExecuted by:
| 1579 | ||||||||||||
| 404 | - | |||||||||||||
| 405 | QTemporaryFilePrivate::~QTemporaryFilePrivate() | - | ||||||||||||
| 406 | { | - | ||||||||||||
| 407 | } | - | ||||||||||||
| 408 | - | |||||||||||||
| 409 | QAbstractFileEngine *QTemporaryFilePrivate::engine() const | - | ||||||||||||
| 410 | { | - | ||||||||||||
| 411 | if (!fileEngine) {
| 1575-3404 | ||||||||||||
| 412 | fileEngine = new QTemporaryFileEngine; | - | ||||||||||||
| 413 | resetFileEngine(); | - | ||||||||||||
| 414 | } executed 1575 times by 24 tests: end of blockExecuted by:
| 1575 | ||||||||||||
| 415 | return fileEngine; executed 4979 times by 25 tests: return fileEngine;Executed by:
| 4979 | ||||||||||||
| 416 | } | - | ||||||||||||
| 417 | - | |||||||||||||
| 418 | void QTemporaryFilePrivate::resetFileEngine() const | - | ||||||||||||
| 419 | { | - | ||||||||||||
| 420 | if (!fileEngine)
| 1575-1579 | ||||||||||||
| 421 | return; executed 1575 times by 24 tests: return;Executed by:
| 1575 | ||||||||||||
| 422 | - | |||||||||||||
| 423 | QTemporaryFileEngine *tef = static_cast<QTemporaryFileEngine *>(fileEngine); | - | ||||||||||||
| 424 | if (fileName.isEmpty())
| 3-1576 | ||||||||||||
| 425 | tef->initialize(templateName, 0600); executed 1576 times by 24 tests: tef->initialize(templateName, 0600);Executed by:
| 1576 | ||||||||||||
| 426 | else | - | ||||||||||||
| 427 | tef->initialize(fileName, 0600, false); executed 3 times by 1 test: tef->initialize(fileName, 0600, false);Executed by:
| 3 | ||||||||||||
| 428 | } | - | ||||||||||||
| 429 | - | |||||||||||||
| 430 | QString QTemporaryFilePrivate::defaultTemplateName() | - | ||||||||||||
| 431 | { | - | ||||||||||||
| 432 | QString baseName; | - | ||||||||||||
| 433 | #if defined(QT_BUILD_CORE_LIB) | - | ||||||||||||
| 434 | baseName = QCoreApplication::applicationName(); | - | ||||||||||||
| 435 | if (baseName.isEmpty())
| 0-1039 | ||||||||||||
| 436 | #endif | - | ||||||||||||
| 437 | baseName = QLatin1String("qt_temp"); never executed: baseName = QLatin1String("qt_temp"); | 0 | ||||||||||||
| 438 | - | |||||||||||||
| 439 | return QDir::tempPath() + QLatin1Char('/') + baseName + QLatin1String(".XXXXXX"); executed 1039 times by 9 tests: return QDir::tempPath() + QLatin1Char('/') + baseName + QLatin1String(".XXXXXX");Executed by:
| 1039 | ||||||||||||
| 440 | } | - | ||||||||||||
| 441 | - | |||||||||||||
| 442 | //************* QTemporaryFile | - | ||||||||||||
| 443 | - | |||||||||||||
| 444 | /*! | - | ||||||||||||
| 445 | \class QTemporaryFile | - | ||||||||||||
| 446 | \inmodule QtCore | - | ||||||||||||
| 447 | \reentrant | - | ||||||||||||
| 448 | \brief The QTemporaryFile class is an I/O device that operates on temporary files. | - | ||||||||||||
| 449 | - | |||||||||||||
| 450 | \ingroup io | - | ||||||||||||
| 451 | - | |||||||||||||
| 452 | - | |||||||||||||
| 453 | QTemporaryFile is used to create unique temporary files safely. | - | ||||||||||||
| 454 | The file itself is created by calling open(). The name of the | - | ||||||||||||
| 455 | temporary file is guaranteed to be unique (i.e., you are | - | ||||||||||||
| 456 | guaranteed to not overwrite an existing file), and the file will | - | ||||||||||||
| 457 | subsequently be removed upon destruction of the QTemporaryFile | - | ||||||||||||
| 458 | object. This is an important technique that avoids data | - | ||||||||||||
| 459 | corruption for applications that store data in temporary files. | - | ||||||||||||
| 460 | The file name is either auto-generated, or created based on a | - | ||||||||||||
| 461 | template, which is passed to QTemporaryFile's constructor. | - | ||||||||||||
| 462 | - | |||||||||||||
| 463 | Example: | - | ||||||||||||
| 464 | - | |||||||||||||
| 465 | \snippet code/src_corelib_io_qtemporaryfile.cpp 0 | - | ||||||||||||
| 466 | - | |||||||||||||
| 467 | Reopening a QTemporaryFile after calling close() is safe. For as long as | - | ||||||||||||
| 468 | the QTemporaryFile object itself is not destroyed, the unique temporary | - | ||||||||||||
| 469 | file will exist and be kept open internally by QTemporaryFile. | - | ||||||||||||
| 470 | - | |||||||||||||
| 471 | The file name of the temporary file can be found by calling fileName(). | - | ||||||||||||
| 472 | Note that this is only defined after the file is first opened; the function | - | ||||||||||||
| 473 | returns an empty string before this. | - | ||||||||||||
| 474 | - | |||||||||||||
| 475 | A temporary file will have some static part of the name and some | - | ||||||||||||
| 476 | part that is calculated to be unique. The default filename will be | - | ||||||||||||
| 477 | determined from QCoreApplication::applicationName() (otherwise \c qt_temp) and will | - | ||||||||||||
| 478 | be placed into the temporary path as returned by QDir::tempPath(). | - | ||||||||||||
| 479 | If you specify your own filename, a relative file path will not be placed in the | - | ||||||||||||
| 480 | temporary directory by default, but be relative to the current working directory. | - | ||||||||||||
| 481 | - | |||||||||||||
| 482 | Specified filenames can contain the following template \c XXXXXX | - | ||||||||||||
| 483 | (six upper case "X" characters), which will be replaced by the | - | ||||||||||||
| 484 | auto-generated portion of the filename. Note that the template is | - | ||||||||||||
| 485 | case sensitive. If the template is not present in the filename, | - | ||||||||||||
| 486 | QTemporaryFile appends the generated part to the filename given. | - | ||||||||||||
| 487 | - | |||||||||||||
| 488 | \sa QDir::tempPath(), QFile | - | ||||||||||||
| 489 | */ | - | ||||||||||||
| 490 | - | |||||||||||||
| 491 | #ifdef QT_NO_QOBJECT | - | ||||||||||||
| 492 | QTemporaryFile::QTemporaryFile() | - | ||||||||||||
| 493 | : QFile(*new QTemporaryFilePrivate) | - | ||||||||||||
| 494 | { | - | ||||||||||||
| 495 | Q_D(QTemporaryFile); | - | ||||||||||||
| 496 | d->templateName = QTemporaryFilePrivate::defaultTemplateName(); | - | ||||||||||||
| 497 | } | - | ||||||||||||
| 498 | - | |||||||||||||
| 499 | QTemporaryFile::QTemporaryFile(const QString &templateName) | - | ||||||||||||
| 500 | : QFile(*new QTemporaryFilePrivate) | - | ||||||||||||
| 501 | { | - | ||||||||||||
| 502 | Q_D(QTemporaryFile); | - | ||||||||||||
| 503 | d->templateName = templateName; | - | ||||||||||||
| 504 | } | - | ||||||||||||
| 505 | - | |||||||||||||
| 506 | #else | - | ||||||||||||
| 507 | /*! | - | ||||||||||||
| 508 | Constructs a QTemporaryFile using as file template | - | ||||||||||||
| 509 | the application name returned by QCoreApplication::applicationName() | - | ||||||||||||
| 510 | (otherwise \c qt_temp) followed by ".XXXXXX". | - | ||||||||||||
| 511 | The file is stored in the system's temporary directory, QDir::tempPath(). | - | ||||||||||||
| 512 | - | |||||||||||||
| 513 | \sa setFileTemplate(), QDir::tempPath() | - | ||||||||||||
| 514 | */ | - | ||||||||||||
| 515 | QTemporaryFile::QTemporaryFile() | - | ||||||||||||
| 516 | : QFile(*new QTemporaryFilePrivate, 0) | - | ||||||||||||
| 517 | { | - | ||||||||||||
| 518 | Q_D(QTemporaryFile); | - | ||||||||||||
| 519 | d->templateName = QTemporaryFilePrivate::defaultTemplateName(); | - | ||||||||||||
| 520 | } executed 1038 times by 9 tests: end of blockExecuted by:
| 1038 | ||||||||||||
| 521 | - | |||||||||||||
| 522 | /*! | - | ||||||||||||
| 523 | Constructs a QTemporaryFile with a template filename of \a | - | ||||||||||||
| 524 | templateName. Upon opening the temporary file this will be used to create | - | ||||||||||||
| 525 | a unique filename. | - | ||||||||||||
| 526 | - | |||||||||||||
| 527 | If the \a templateName does not contain XXXXXX it will automatically be | - | ||||||||||||
| 528 | appended and used as the dynamic portion of the filename. | - | ||||||||||||
| 529 | - | |||||||||||||
| 530 | If \a templateName is a relative path, the path will be relative to the | - | ||||||||||||
| 531 | current working directory. You can use QDir::tempPath() to construct \a | - | ||||||||||||
| 532 | templateName if you want use the system's temporary directory. | - | ||||||||||||
| 533 | - | |||||||||||||
| 534 | \sa open(), fileTemplate() | - | ||||||||||||
| 535 | */ | - | ||||||||||||
| 536 | QTemporaryFile::QTemporaryFile(const QString &templateName) | - | ||||||||||||
| 537 | : QFile(*new QTemporaryFilePrivate, 0) | - | ||||||||||||
| 538 | { | - | ||||||||||||
| 539 | Q_D(QTemporaryFile); | - | ||||||||||||
| 540 | d->templateName = templateName; | - | ||||||||||||
| 541 | } executed 453 times by 19 tests: end of blockExecuted by:
| 453 | ||||||||||||
| 542 | - | |||||||||||||
| 543 | /*! | - | ||||||||||||
| 544 | Constructs a QTemporaryFile (with the given \a parent) | - | ||||||||||||
| 545 | using as file template the application name returned by QCoreApplication::applicationName() | - | ||||||||||||
| 546 | (otherwise \c qt_temp) followed by ".XXXXXX". | - | ||||||||||||
| 547 | The file is stored in the system's temporary directory, QDir::tempPath(). | - | ||||||||||||
| 548 | - | |||||||||||||
| 549 | \sa setFileTemplate() | - | ||||||||||||
| 550 | */ | - | ||||||||||||
| 551 | QTemporaryFile::QTemporaryFile(QObject *parent) | - | ||||||||||||
| 552 | : QFile(*new QTemporaryFilePrivate, parent) | - | ||||||||||||
| 553 | { | - | ||||||||||||
| 554 | Q_D(QTemporaryFile); | - | ||||||||||||
| 555 | d->templateName = QTemporaryFilePrivate::defaultTemplateName(); | - | ||||||||||||
| 556 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||
| 557 | - | |||||||||||||
| 558 | /*! | - | ||||||||||||
| 559 | Constructs a QTemporaryFile with a template filename of \a | - | ||||||||||||
| 560 | templateName and the specified \a parent. | - | ||||||||||||
| 561 | Upon opening the temporary file this will be used to | - | ||||||||||||
| 562 | create a unique filename. | - | ||||||||||||
| 563 | - | |||||||||||||
| 564 | If the \a templateName does not contain XXXXXX it will automatically be | - | ||||||||||||
| 565 | appended and used as the dynamic portion of the filename. | - | ||||||||||||
| 566 | - | |||||||||||||
| 567 | If \a templateName is a relative path, the path will be relative to the | - | ||||||||||||
| 568 | current working directory. You can use QDir::tempPath() to construct \a | - | ||||||||||||
| 569 | templateName if you want use the system's temporary directory. | - | ||||||||||||
| 570 | - | |||||||||||||
| 571 | \sa open(), fileTemplate() | - | ||||||||||||
| 572 | */ | - | ||||||||||||
| 573 | QTemporaryFile::QTemporaryFile(const QString &templateName, QObject *parent) | - | ||||||||||||
| 574 | : QFile(*new QTemporaryFilePrivate, parent) | - | ||||||||||||
| 575 | { | - | ||||||||||||
| 576 | Q_D(QTemporaryFile); | - | ||||||||||||
| 577 | d->templateName = templateName; | - | ||||||||||||
| 578 | } executed 87 times by 3 tests: end of blockExecuted by:
| 87 | ||||||||||||
| 579 | #endif | - | ||||||||||||
| 580 | - | |||||||||||||
| 581 | /*! | - | ||||||||||||
| 582 | Destroys the temporary file object, the file is automatically | - | ||||||||||||
| 583 | closed if necessary and if in auto remove mode it will | - | ||||||||||||
| 584 | automatically delete the file. | - | ||||||||||||
| 585 | - | |||||||||||||
| 586 | \sa autoRemove() | - | ||||||||||||
| 587 | */ | - | ||||||||||||
| 588 | QTemporaryFile::~QTemporaryFile() | - | ||||||||||||
| 589 | { | - | ||||||||||||
| 590 | Q_D(QTemporaryFile); | - | ||||||||||||
| 591 | close(); | - | ||||||||||||
| 592 | if (!d->fileName.isEmpty() && d->autoRemove)
| 12-1568 | ||||||||||||
| 593 | remove(); executed 348 times by 22 tests: remove();Executed by:
| 348 | ||||||||||||
| 594 | } executed 1580 times by 25 tests: end of blockExecuted by:
| 1580 | ||||||||||||
| 595 | - | |||||||||||||
| 596 | /*! | - | ||||||||||||
| 597 | \fn bool QTemporaryFile::open() | - | ||||||||||||
| 598 | - | |||||||||||||
| 599 | A QTemporaryFile will always be opened in QIODevice::ReadWrite mode, | - | ||||||||||||
| 600 | this allows easy access to the data in the file. This function will | - | ||||||||||||
| 601 | return true upon success and will set the fileName() to the unique | - | ||||||||||||
| 602 | filename used. | - | ||||||||||||
| 603 | - | |||||||||||||
| 604 | \sa fileName() | - | ||||||||||||
| 605 | */ | - | ||||||||||||
| 606 | - | |||||||||||||
| 607 | /*! | - | ||||||||||||
| 608 | Returns \c true if the QTemporaryFile is in auto remove | - | ||||||||||||
| 609 | mode. Auto-remove mode will automatically delete the filename from | - | ||||||||||||
| 610 | disk upon destruction. This makes it very easy to create your | - | ||||||||||||
| 611 | QTemporaryFile object on the stack, fill it with data, read from | - | ||||||||||||
| 612 | it, and finally on function return it will automatically clean up | - | ||||||||||||
| 613 | after itself. | - | ||||||||||||
| 614 | - | |||||||||||||
| 615 | Auto-remove is on by default. | - | ||||||||||||
| 616 | - | |||||||||||||
| 617 | \sa setAutoRemove(), remove() | - | ||||||||||||
| 618 | */ | - | ||||||||||||
| 619 | bool QTemporaryFile::autoRemove() const | - | ||||||||||||
| 620 | { | - | ||||||||||||
| 621 | Q_D(const QTemporaryFile); | - | ||||||||||||
| 622 | return d->autoRemove; executed 2 times by 1 test: return d->autoRemove;Executed by:
| 2 | ||||||||||||
| 623 | } | - | ||||||||||||
| 624 | - | |||||||||||||
| 625 | /*! | - | ||||||||||||
| 626 | Sets the QTemporaryFile into auto-remove mode if \a b is true. | - | ||||||||||||
| 627 | - | |||||||||||||
| 628 | Auto-remove is on by default. | - | ||||||||||||
| 629 | - | |||||||||||||
| 630 | \sa autoRemove(), remove() | - | ||||||||||||
| 631 | */ | - | ||||||||||||
| 632 | void QTemporaryFile::setAutoRemove(bool b) | - | ||||||||||||
| 633 | { | - | ||||||||||||
| 634 | Q_D(QTemporaryFile); | - | ||||||||||||
| 635 | d->autoRemove = b; | - | ||||||||||||
| 636 | } executed 1243 times by 9 tests: end of blockExecuted by:
| 1243 | ||||||||||||
| 637 | - | |||||||||||||
| 638 | /*! | - | ||||||||||||
| 639 | Returns the complete unique filename backing the QTemporaryFile | - | ||||||||||||
| 640 | object. This string is null before the QTemporaryFile is opened, | - | ||||||||||||
| 641 | afterwards it will contain the fileTemplate() plus | - | ||||||||||||
| 642 | additional characters to make it unique. | - | ||||||||||||
| 643 | - | |||||||||||||
| 644 | \sa fileTemplate() | - | ||||||||||||
| 645 | */ | - | ||||||||||||
| 646 | - | |||||||||||||
| 647 | QString QTemporaryFile::fileName() const | - | ||||||||||||
| 648 | { | - | ||||||||||||
| 649 | Q_D(const QTemporaryFile); | - | ||||||||||||
| 650 | if(d->fileName.isEmpty())
| 6-2314 | ||||||||||||
| 651 | return QString(); executed 6 times by 1 test: return QString();Executed by:
| 6 | ||||||||||||
| 652 | return d->engine()->fileName(QAbstractFileEngine::DefaultName); executed 2314 times by 15 tests: return d->engine()->fileName(QAbstractFileEngine::DefaultName);Executed by:
| 2314 | ||||||||||||
| 653 | } | - | ||||||||||||
| 654 | - | |||||||||||||
| 655 | /*! | - | ||||||||||||
| 656 | Returns the set file template. The default file template will be | - | ||||||||||||
| 657 | called qcoreappname.XXXXXX and be placed in QDir::tempPath(). | - | ||||||||||||
| 658 | - | |||||||||||||
| 659 | \sa setFileTemplate() | - | ||||||||||||
| 660 | */ | - | ||||||||||||
| 661 | QString QTemporaryFile::fileTemplate() const | - | ||||||||||||
| 662 | { | - | ||||||||||||
| 663 | Q_D(const QTemporaryFile); | - | ||||||||||||
| 664 | return d->templateName; executed 10 times by 1 test: return d->templateName;Executed by:
| 10 | ||||||||||||
| 665 | } | - | ||||||||||||
| 666 | - | |||||||||||||
| 667 | /*! | - | ||||||||||||
| 668 | Sets the static portion of the file name to \a name. If the file | - | ||||||||||||
| 669 | template contains XXXXXX that will automatically be replaced with | - | ||||||||||||
| 670 | the unique part of the filename, otherwise a filename will be | - | ||||||||||||
| 671 | determined automatically based on the static portion specified. | - | ||||||||||||
| 672 | - | |||||||||||||
| 673 | If \a name contains a relative file path, the path will be relative to the | - | ||||||||||||
| 674 | current working directory. You can use QDir::tempPath() to construct \a | - | ||||||||||||
| 675 | name if you want use the system's temporary directory. | - | ||||||||||||
| 676 | - | |||||||||||||
| 677 | \sa fileTemplate() | - | ||||||||||||
| 678 | */ | - | ||||||||||||
| 679 | void QTemporaryFile::setFileTemplate(const QString &name) | - | ||||||||||||
| 680 | { | - | ||||||||||||
| 681 | Q_D(QTemporaryFile); | - | ||||||||||||
| 682 | d->templateName = name; | - | ||||||||||||
| 683 | if (d->fileEngine)
| 2-7 | ||||||||||||
| 684 | static_cast<QTemporaryFileEngine*>(d->fileEngine)->setFileTemplate(name); executed 2 times by 1 test: static_cast<QTemporaryFileEngine*>(d->fileEngine)->setFileTemplate(name);Executed by:
| 2 | ||||||||||||
| 685 | } executed 9 times by 2 tests: end of blockExecuted by:
| 9 | ||||||||||||
| 686 | - | |||||||||||||
| 687 | /*! | - | ||||||||||||
| 688 | \fn QTemporaryFile *QTemporaryFile::createLocalFile(const QString &fileName) | - | ||||||||||||
| 689 | \overload | - | ||||||||||||
| 690 | \obsolete | - | ||||||||||||
| 691 | - | |||||||||||||
| 692 | Use QTemporaryFile::createNativeFile(const QString &fileName) instead. | - | ||||||||||||
| 693 | */ | - | ||||||||||||
| 694 | - | |||||||||||||
| 695 | /*! | - | ||||||||||||
| 696 | \fn QTemporaryFile *QTemporaryFile::createLocalFile(QFile &file) | - | ||||||||||||
| 697 | \obsolete | - | ||||||||||||
| 698 | - | |||||||||||||
| 699 | Use QTemporaryFile::createNativeFile(QFile &file) instead. | - | ||||||||||||
| 700 | */ | - | ||||||||||||
| 701 | - | |||||||||||||
| 702 | /*! | - | ||||||||||||
| 703 | \fn QTemporaryFile *QTemporaryFile::createNativeFile(const QString &fileName) | - | ||||||||||||
| 704 | \overload | - | ||||||||||||
| 705 | - | |||||||||||||
| 706 | Works on the given \a fileName rather than an existing QFile | - | ||||||||||||
| 707 | object. | - | ||||||||||||
| 708 | */ | - | ||||||||||||
| 709 | - | |||||||||||||
| 710 | - | |||||||||||||
| 711 | /*! | - | ||||||||||||
| 712 | If \a file is not already a native file, then a QTemporaryFile is created | - | ||||||||||||
| 713 | in QDir::tempPath(), the contents of \a file is copied into it, and a pointer | - | ||||||||||||
| 714 | to the temporary file is returned. Does nothing and returns \c 0 if \a file | - | ||||||||||||
| 715 | is already a native file. | - | ||||||||||||
| 716 | - | |||||||||||||
| 717 | For example: | - | ||||||||||||
| 718 | - | |||||||||||||
| 719 | \code | - | ||||||||||||
| 720 | QFile f(":/resources/file.txt"); | - | ||||||||||||
| 721 | QTemporaryFile::createNativeFile(f); // Returns a pointer to a temporary file | - | ||||||||||||
| 722 | - | |||||||||||||
| 723 | QFile f("/users/qt/file.txt"); | - | ||||||||||||
| 724 | QTemporaryFile::createNativeFile(f); // Returns 0 | - | ||||||||||||
| 725 | \endcode | - | ||||||||||||
| 726 | - | |||||||||||||
| 727 | \sa QFileInfo::isNativePath() | - | ||||||||||||
| 728 | */ | - | ||||||||||||
| 729 | - | |||||||||||||
| 730 | QTemporaryFile *QTemporaryFile::createNativeFile(QFile &file) | - | ||||||||||||
| 731 | { | - | ||||||||||||
| 732 | if (QAbstractFileEngine *engine = file.d_func()->engine()) {
| 0-4 | ||||||||||||
| 733 | if(engine->fileFlags(QAbstractFileEngine::FlagsMask) & QAbstractFileEngine::LocalDiskFlag)
| 2 | ||||||||||||
| 734 | return 0; //native already executed 2 times by 1 test: return 0;Executed by:
| 2 | ||||||||||||
| 735 | //cache | - | ||||||||||||
| 736 | bool wasOpen = file.isOpen(); | - | ||||||||||||
| 737 | qint64 old_off = 0; | - | ||||||||||||
| 738 | if(wasOpen)
| 1 | ||||||||||||
| 739 | old_off = file.pos(); executed 1 time by 1 test: old_off = file.pos();Executed by:
| 1 | ||||||||||||
| 740 | else | - | ||||||||||||
| 741 | file.open(QIODevice::ReadOnly); executed 1 time by 1 test: file.open(QIODevice::ReadOnly);Executed by:
| 1 | ||||||||||||
| 742 | //dump data | - | ||||||||||||
| 743 | QTemporaryFile *ret = new QTemporaryFile; | - | ||||||||||||
| 744 | ret->open(); | - | ||||||||||||
| 745 | file.seek(0); | - | ||||||||||||
| 746 | char buffer[1024]; | - | ||||||||||||
| 747 | while(true) { | - | ||||||||||||
| 748 | qint64 len = file.read(buffer, 1024); | - | ||||||||||||
| 749 | if(len < 1)
| 2 | ||||||||||||
| 750 | break; executed 2 times by 1 test: break;Executed by:
| 2 | ||||||||||||
| 751 | ret->write(buffer, len); | - | ||||||||||||
| 752 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||
| 753 | ret->seek(0); | - | ||||||||||||
| 754 | //restore | - | ||||||||||||
| 755 | if(wasOpen)
| 1 | ||||||||||||
| 756 | file.seek(old_off); executed 1 time by 1 test: file.seek(old_off);Executed by:
| 1 | ||||||||||||
| 757 | else | - | ||||||||||||
| 758 | file.close(); executed 1 time by 1 test: file.close();Executed by:
| 1 | ||||||||||||
| 759 | //done | - | ||||||||||||
| 760 | return ret; executed 2 times by 1 test: return ret;Executed by:
| 2 | ||||||||||||
| 761 | } | - | ||||||||||||
| 762 | return 0; never executed: return 0; | 0 | ||||||||||||
| 763 | } | - | ||||||||||||
| 764 | - | |||||||||||||
| 765 | /*! | - | ||||||||||||
| 766 | \reimp | - | ||||||||||||
| 767 | - | |||||||||||||
| 768 | Creates a unique file name for the temporary file, and opens it. You can | - | ||||||||||||
| 769 | get the unique name later by calling fileName(). The file is guaranteed to | - | ||||||||||||
| 770 | have been created by this function (i.e., it has never existed before). | - | ||||||||||||
| 771 | */ | - | ||||||||||||
| 772 | bool QTemporaryFile::open(OpenMode flags) | - | ||||||||||||
| 773 | { | - | ||||||||||||
| 774 | Q_D(QTemporaryFile); | - | ||||||||||||
| 775 | if (!d->fileName.isEmpty()) {
| 25-1576 | ||||||||||||
| 776 | if (static_cast<QTemporaryFileEngine*>(d->engine())->isReallyOpen()) {
| 3-22 | ||||||||||||
| 777 | setOpenMode(flags); | - | ||||||||||||
| 778 | return true; executed 22 times by 3 tests: return true;Executed by:
| 22 | ||||||||||||
| 779 | } | - | ||||||||||||
| 780 | } executed 3 times by 1 test: end of blockExecuted by:
| 3 | ||||||||||||
| 781 | - | |||||||||||||
| 782 | // reset the engine state so it creates a new, unique file name from the template; | - | ||||||||||||
| 783 | // equivalent to: | - | ||||||||||||
| 784 | // delete d->fileEngine; | - | ||||||||||||
| 785 | // d->fileEngine = 0; | - | ||||||||||||
| 786 | // d->engine(); | - | ||||||||||||
| 787 | d->resetFileEngine(); | - | ||||||||||||
| 788 | - | |||||||||||||
| 789 | if (QFile::open(flags)) {
| 8-1571 | ||||||||||||
| 790 | d->fileName = d->fileEngine->fileName(QAbstractFileEngine::DefaultName); | - | ||||||||||||
| 791 | return true; executed 1571 times by 24 tests: return true;Executed by:
| 1571 | ||||||||||||
| 792 | } | - | ||||||||||||
| 793 | return false; executed 8 times by 1 test: return false;Executed by:
| 8 | ||||||||||||
| 794 | } | - | ||||||||||||
| 795 | - | |||||||||||||
| 796 | QT_END_NAMESPACE | - | ||||||||||||
| 797 | - | |||||||||||||
| 798 | #endif // QT_NO_TEMPORARYFILE | - | ||||||||||||
| 799 | - | |||||||||||||
| 800 | - | |||||||||||||
| Source code | Switch to Preprocessed file |