| Line | Source Code | Coverage |
|---|
| 1 | /**************************************************************************** | - |
| 2 | ** | - |
| 3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | - |
| 4 | ** Contact: http://www.qt-project.org/legal | - |
| 5 | ** | - |
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | - |
| 7 | ** | - |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
| 9 | ** Commercial License Usage | - |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - |
| 11 | ** accordance with the commercial license agreement provided with the | - |
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - |
| 13 | ** a written agreement between you and Digia. For licensing terms and | - |
| 14 | ** conditions see http://qt.digia.com/licensing. For further information | - |
| 15 | ** use the contact form at http://qt.digia.com/contact-us. | - |
| 16 | ** | - |
| 17 | ** GNU Lesser General Public License Usage | - |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - |
| 19 | ** General Public License version 2.1 as published by the Free Software | - |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL included in the | - |
| 21 | ** packaging of this file. Please review the following information to | - |
| 22 | ** ensure the GNU Lesser General Public License version 2.1 requirements | - |
| 23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - |
| 24 | ** | - |
| 25 | ** In addition, as a special exception, Digia gives you certain additional | - |
| 26 | ** rights. These rights are described in the Digia Qt LGPL Exception | - |
| 27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - |
| 28 | ** | - |
| 29 | ** GNU General Public License Usage | - |
| 30 | ** Alternatively, this file may be used under the terms of the GNU | - |
| 31 | ** General Public License version 3.0 as published by the Free Software | - |
| 32 | ** Foundation and appearing in the file LICENSE.GPL included in the | - |
| 33 | ** packaging of this file. Please review the following information to | - |
| 34 | ** ensure the GNU General Public License version 3.0 requirements will be | - |
| 35 | ** met: http://www.gnu.org/copyleft/gpl.html. | - |
| 36 | ** | - |
| 37 | ** | - |
| 38 | ** $QT_END_LICENSE$ | - |
| 39 | ** | - |
| 40 | ****************************************************************************/ | - |
| 41 | | - |
| 42 | #include "qtemporarydir.h" | - |
| 43 | | - |
| 44 | #ifndef QT_NO_TEMPORARYFILE | - |
| 45 | | - |
| 46 | #include "qdiriterator.h" | - |
| 47 | #include "qplatformdefs.h" | - |
| 48 | #include <QDebug> | - |
| 49 | | - |
| 50 | #if defined(QT_BUILD_CORE_LIB) | - |
| 51 | #include "qcoreapplication.h" | - |
| 52 | #endif | - |
| 53 | | - |
| 54 | #include <stdlib.h> // mkdtemp | - |
| 55 | #if defined(Q_OS_QNX) || defined(Q_OS_WIN) | - |
| 56 | #include <private/qfilesystemengine_p.h> | - |
| 57 | #endif | - |
| 58 | | - |
| 59 | QT_BEGIN_NAMESPACE | - |
| 60 | | - |
| 61 | //************* QTemporaryDirPrivate | - |
| 62 | class QTemporaryDirPrivate | - |
| 63 | { | - |
| 64 | public: | - |
| 65 | QTemporaryDirPrivate(); | - |
| 66 | ~QTemporaryDirPrivate(); | - |
| 67 | | - |
| 68 | void create(const QString &templateName); | - |
| 69 | | - |
| 70 | QString path; | - |
| 71 | bool autoRemove; | - |
| 72 | bool success; | - |
| 73 | }; | - |
| 74 | | - |
| 75 | QTemporaryDirPrivate::QTemporaryDirPrivate() | - |
| 76 | : autoRemove(true), | - |
| 77 | success(false) | - |
| 78 | { | - |
| 79 | } executed: }Execution Count:1148 | 1148 |
| 80 | | - |
| 81 | QTemporaryDirPrivate::~QTemporaryDirPrivate() | - |
| 82 | { | - |
| 83 | } | - |
| 84 | | - |
| 85 | static QString defaultTemplateName() | - |
| 86 | { | - |
| 87 | QString baseName; executed (the execution status of this line is deduced): QString baseName; | - |
| 88 | #if defined(QT_BUILD_CORE_LIB) | - |
| 89 | baseName = QCoreApplication::applicationName(); executed (the execution status of this line is deduced): baseName = QCoreApplication::applicationName(); | - |
| 90 | if (baseName.isEmpty()) partially evaluated: baseName.isEmpty()| no Evaluation Count:0 | yes Evaluation Count:1035 |
| 0-1035 |
| 91 | #endif | - |
| 92 | baseName = QLatin1String("qt_temp"); never executed: baseName = QLatin1String("qt_temp"); | 0 |
| 93 | | - |
| 94 | return QDir::tempPath() + QLatin1Char('/') + baseName + QLatin1String("-XXXXXX"); executed: return QDir::tempPath() + QLatin1Char('/') + baseName + QLatin1String("-XXXXXX");Execution Count:1035 | 1035 |
| 95 | } | - |
| 96 | | - |
| 97 | #if defined(Q_OS_QNX ) || defined(Q_OS_WIN) | - |
| 98 | static char *mkdtemp(char *templateName) | - |
| 99 | { | - |
| 100 | static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | - |
| 101 | | - |
| 102 | const size_t length = strlen(templateName); | - |
| 103 | | - |
| 104 | char *XXXXXX = templateName + length - 6; | - |
| 105 | | - |
| 106 | if ((length < 6u) || strncmp(XXXXXX, "XXXXXX", 6)) | - |
| 107 | return 0; | - |
| 108 | | - |
| 109 | for (int i = 0; i < 256; ++i) { | - |
| 110 | int v = qrand(); | - |
| 111 | | - |
| 112 | /* Fill in the random bits. */ | - |
| 113 | XXXXXX[0] = letters[v % 62]; | - |
| 114 | v /= 62; | - |
| 115 | XXXXXX[1] = letters[v % 62]; | - |
| 116 | v /= 62; | - |
| 117 | XXXXXX[2] = letters[v % 62]; | - |
| 118 | v /= 62; | - |
| 119 | XXXXXX[3] = letters[v % 62]; | - |
| 120 | v /= 62; | - |
| 121 | XXXXXX[4] = letters[v % 62]; | - |
| 122 | v /= 62; | - |
| 123 | XXXXXX[5] = letters[v % 62]; | - |
| 124 | | - |
| 125 | QString templateNameStr = QFile::decodeName(templateName); | - |
| 126 | | - |
| 127 | QFileSystemEntry fileSystemEntry(templateNameStr); | - |
| 128 | if (QFileSystemEngine::createDirectory(fileSystemEntry, false)) { | - |
| 129 | QSystemError error; | - |
| 130 | QFileSystemEngine::setPermissions(fileSystemEntry, | - |
| 131 | QFile::ReadOwner | | - |
| 132 | QFile::WriteOwner | | - |
| 133 | QFile::ExeOwner, error); | - |
| 134 | if (error.error() != 0) | - |
| 135 | continue; | - |
| 136 | return templateName; | - |
| 137 | } | - |
| 138 | } | - |
| 139 | return 0; | - |
| 140 | } | - |
| 141 | #elif defined(Q_OS_LINUX_ANDROID) | - |
| 142 | extern char *mkdtemp(char *); | - |
| 143 | #endif | - |
| 144 | | - |
| 145 | void QTemporaryDirPrivate::create(const QString &templateName) | - |
| 146 | { | - |
| 147 | QByteArray buffer = QFile::encodeName(templateName); executed (the execution status of this line is deduced): QByteArray buffer = QFile::encodeName(templateName); | - |
| 148 | if (!buffer.endsWith("XXXXXX")) evaluated: !buffer.endsWith("XXXXXX")| yes Evaluation Count:34 | yes Evaluation Count:1114 |
| 34-1114 |
| 149 | buffer += "XXXXXX"; executed: buffer += "XXXXXX";Execution Count:34 | 34 |
| 150 | if (mkdtemp(buffer.data())) { // modifies buffer evaluated: mkdtemp(buffer.data())| yes Evaluation Count:1141 | yes Evaluation Count:7 |
| 7-1141 |
| 151 | success = true; executed (the execution status of this line is deduced): success = true; | - |
| 152 | path = QFile::decodeName(buffer.constData()); executed (the execution status of this line is deduced): path = QFile::decodeName(buffer.constData()); | - |
| 153 | } executed: }Execution Count:1141 | 1141 |
| 154 | } executed: }Execution Count:1148 | 1148 |
| 155 | | - |
| 156 | //************* QTemporaryDir | - |
| 157 | | - |
| 158 | /*! | - |
| 159 | \class QTemporaryDir | - |
| 160 | \inmodule QtCore | - |
| 161 | \reentrant | - |
| 162 | \brief The QTemporaryDir class creates a unique directory for temporary use. | - |
| 163 | | - |
| 164 | \ingroup io | - |
| 165 | | - |
| 166 | | - |
| 167 | QTemporaryDir is used to create unique temporary dirs safely. | - |
| 168 | The dir itself is created by the constructor. The name of the | - |
| 169 | temporary directory is guaranteed to be unique (i.e., you are | - |
| 170 | guaranteed to not overwrite an existing dir), and the directory will | - |
| 171 | subsequently be removed upon destruction of the QTemporaryDir | - |
| 172 | object. The directory name is either auto-generated, or created based | - |
| 173 | on a template, which is passed to QTemporaryDir's constructor. | - |
| 174 | | - |
| 175 | Example: | - |
| 176 | | - |
| 177 | \snippet code/src_corelib_io_qtemporarydir.cpp 0 | - |
| 178 | | - |
| 179 | It is very important to test that the temporary directory could be | - |
| 180 | created, using isValid(). Do not use exists(), since a default-constructed | - |
| 181 | QDir represents the current directory, which exists. | - |
| 182 | | - |
| 183 | The path to the temporary dir can be found by calling path(). | - |
| 184 | | - |
| 185 | A temporary directory will have some static part of the name and some | - |
| 186 | part that is calculated to be unique. The default path will be | - |
| 187 | determined from QCoreApplication::applicationName() (otherwise \c qt_temp) and will | - |
| 188 | be placed into the temporary path as returned by QDir::tempPath(). | - |
| 189 | If you specify your own path, a relative path will not be placed in the | - |
| 190 | temporary directory by default, but be relative to the current working directory. | - |
| 191 | In all cases, a random string will be appended to the path in order to make it unique. | - |
| 192 | | - |
| 193 | \sa QDir::tempPath(), QDir, QTemporaryFile | - |
| 194 | */ | - |
| 195 | | - |
| 196 | /*! | - |
| 197 | Constructs a QTemporaryDir using as template the application name | - |
| 198 | returned by QCoreApplication::applicationName() (otherwise \c qt_temp). | - |
| 199 | The directory is stored in the system's temporary directory, QDir::tempPath(). | - |
| 200 | | - |
| 201 | \sa QDir::tempPath() | - |
| 202 | */ | - |
| 203 | QTemporaryDir::QTemporaryDir() | - |
| 204 | : d_ptr(new QTemporaryDirPrivate) | - |
| 205 | { | - |
| 206 | d_ptr->create(defaultTemplateName()); executed (the execution status of this line is deduced): d_ptr->create(defaultTemplateName()); | - |
| 207 | } executed: }Execution Count:1034 | 1034 |
| 208 | | - |
| 209 | /*! | - |
| 210 | Constructs a QTemporaryFile with a template name of \a templateName. | - |
| 211 | | - |
| 212 | If \a templateName is a relative path, the path will be relative to the | - |
| 213 | current working directory. You can use QDir::tempPath() to construct \a | - |
| 214 | templateName if you want use the system's temporary directory. | - |
| 215 | | - |
| 216 | If the \a templateName ends with XXXXXX it will be used as the dynamic portion | - |
| 217 | of the directory name, otherwise it will be appended. | - |
| 218 | Unlike QTemporaryFile, XXXXXX in the middle of the template string is not supported. | - |
| 219 | | - |
| 220 | \sa QDir::tempPath() | - |
| 221 | */ | - |
| 222 | QTemporaryDir::QTemporaryDir(const QString &templateName) | - |
| 223 | : d_ptr(new QTemporaryDirPrivate) | - |
| 224 | { | - |
| 225 | if (templateName.isEmpty()) evaluated: templateName.isEmpty()| yes Evaluation Count:1 | yes Evaluation Count:113 |
| 1-113 |
| 226 | d_ptr->create(defaultTemplateName()); executed: d_ptr->create(defaultTemplateName());Execution Count:1 | 1 |
| 227 | else | - |
| 228 | d_ptr->create(templateName); executed: d_ptr->create(templateName);Execution Count:113 | 113 |
| 229 | } | - |
| 230 | | - |
| 231 | /*! | - |
| 232 | Destroys the temporary directory object. | - |
| 233 | If auto remove mode was set, it will automatically delete the directory | - |
| 234 | including all its contents. | - |
| 235 | | - |
| 236 | \sa autoRemove() | - |
| 237 | */ | - |
| 238 | QTemporaryDir::~QTemporaryDir() | - |
| 239 | { | - |
| 240 | if (d_ptr->autoRemove) evaluated: d_ptr->autoRemove| yes Evaluation Count:161 | yes Evaluation Count:1001 |
| 161-1001 |
| 241 | remove(); executed: remove();Execution Count:161 | 161 |
| 242 | } executed: }Execution Count:1162 | 1162 |
| 243 | | - |
| 244 | /*! | - |
| 245 | Returns true if the QTemporaryDir was created successfully. | - |
| 246 | */ | - |
| 247 | bool QTemporaryDir::isValid() const | - |
| 248 | { | - |
| 249 | return d_ptr->success; executed: return d_ptr->success;Execution Count:1149 | 1149 |
| 250 | } | - |
| 251 | | - |
| 252 | /*! | - |
| 253 | Returns the path to the temporary directory. | - |
| 254 | Empty if the QTemporaryDir could not be created. | - |
| 255 | */ | - |
| 256 | QString QTemporaryDir::path() const | - |
| 257 | { | - |
| 258 | return d_ptr->path; executed: return d_ptr->path;Execution Count:3756 | 3756 |
| 259 | } | - |
| 260 | | - |
| 261 | /*! | - |
| 262 | Returns true if the QTemporaryDir is in auto remove | - |
| 263 | mode. Auto-remove mode will automatically delete the directory from | - |
| 264 | disk upon destruction. This makes it very easy to create your | - |
| 265 | QTemporaryDir object on the stack, fill it with files, do something with | - |
| 266 | the files, and finally on function return it will automatically clean up | - |
| 267 | after itself. | - |
| 268 | | - |
| 269 | Auto-remove is on by default. | - |
| 270 | | - |
| 271 | \sa setAutoRemove(), remove() | - |
| 272 | */ | - |
| 273 | bool QTemporaryDir::autoRemove() const | - |
| 274 | { | - |
| 275 | return d_ptr->autoRemove; executed: return d_ptr->autoRemove;Execution Count:2 | 2 |
| 276 | } | - |
| 277 | | - |
| 278 | /*! | - |
| 279 | Sets the QTemporaryDir into auto-remove mode if \a b is true. | - |
| 280 | | - |
| 281 | Auto-remove is on by default. | - |
| 282 | | - |
| 283 | \sa autoRemove(), remove() | - |
| 284 | */ | - |
| 285 | void QTemporaryDir::setAutoRemove(bool b) | - |
| 286 | { | - |
| 287 | d_ptr->autoRemove = b; executed (the execution status of this line is deduced): d_ptr->autoRemove = b; | - |
| 288 | } executed: }Execution Count:1007 | 1007 |
| 289 | | - |
| 290 | /*! | - |
| 291 | Removes the temporary directory, including all its contents. | - |
| 292 | | - |
| 293 | Returns true if removing was successful. | - |
| 294 | */ | - |
| 295 | bool QTemporaryDir::remove() | - |
| 296 | { | - |
| 297 | if (!d_ptr->success) evaluated: !d_ptr->success| yes Evaluation Count:7 | yes Evaluation Count:156 |
| 7-156 |
| 298 | return false; executed: return false;Execution Count:7 | 7 |
| 299 | Q_ASSERT(!path().isEmpty()); executed (the execution status of this line is deduced): qt_noop(); | - |
| 300 | Q_ASSERT(path() != QLatin1String(".")); executed (the execution status of this line is deduced): qt_noop(); | - |
| 301 | | - |
| 302 | return QDir(path()).removeRecursively(); executed: return QDir(path()).removeRecursively();Execution Count:156 | 156 |
| 303 | } | - |
| 304 | | - |
| 305 | QT_END_NAMESPACE | - |
| 306 | | - |
| 307 | #endif // QT_NO_TEMPORARYFILE | - |
| 308 | | - |
| | |