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