Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/io/qtemporarydir.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||
4 | ** Copyright (C) 2016 Intel Corporation. | - | ||||||
5 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
6 | ** | - | ||||||
7 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||
8 | ** | - | ||||||
9 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||
10 | ** Commercial License Usage | - | ||||||
11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
12 | ** accordance with the commercial license agreement provided with the | - | ||||||
13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
15 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
16 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||
17 | ** | - | ||||||
18 | ** GNU Lesser General Public License Usage | - | ||||||
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
20 | ** General Public License version 3 as published by the Free Software | - | ||||||
21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
22 | ** packaging of this file. Please review the following information to | - | ||||||
23 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
25 | ** | - | ||||||
26 | ** GNU General Public License Usage | - | ||||||
27 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
28 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
29 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
30 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
32 | ** included in the packaging of this file. Please review the following | - | ||||||
33 | ** information to ensure the GNU General Public License requirements will | - | ||||||
34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
35 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
36 | ** | - | ||||||
37 | ** $QT_END_LICENSE$ | - | ||||||
38 | ** | - | ||||||
39 | ****************************************************************************/ | - | ||||||
40 | - | |||||||
41 | #include "qtemporarydir.h" | - | ||||||
42 | - | |||||||
43 | #ifndef QT_NO_TEMPORARYFILE | - | ||||||
44 | - | |||||||
45 | #include "qdiriterator.h" | - | ||||||
46 | #include "qplatformdefs.h" | - | ||||||
47 | #include <QDebug> | - | ||||||
48 | #include <QPair> | - | ||||||
49 | - | |||||||
50 | #if defined(QT_BUILD_CORE_LIB) | - | ||||||
51 | #include "qcoreapplication.h" | - | ||||||
52 | #endif | - | ||||||
53 | - | |||||||
54 | #if !defined(Q_OS_QNX) && !defined(Q_OS_WIN) && !defined(Q_OS_ANDROID) && !defined(Q_OS_INTEGRITY) | - | ||||||
55 | # define USE_SYSTEM_MKDTEMP | - | ||||||
56 | #endif | - | ||||||
57 | - | |||||||
58 | #include <stdlib.h> // mkdtemp | - | ||||||
59 | #ifndef USE_SYSTEM_MKDTEMP | - | ||||||
60 | #include <private/qfilesystemengine_p.h> | - | ||||||
61 | #endif | - | ||||||
62 | - | |||||||
63 | #if !defined(Q_OS_WIN) | - | ||||||
64 | #include <errno.h> | - | ||||||
65 | #endif | - | ||||||
66 | - | |||||||
67 | QT_BEGIN_NAMESPACE | - | ||||||
68 | - | |||||||
69 | //************* QTemporaryDirPrivate | - | ||||||
70 | class QTemporaryDirPrivate | - | ||||||
71 | { | - | ||||||
72 | public: | - | ||||||
73 | QTemporaryDirPrivate(); | - | ||||||
74 | ~QTemporaryDirPrivate(); | - | ||||||
75 | - | |||||||
76 | void create(const QString &templateName); | - | ||||||
77 | - | |||||||
78 | QString pathOrError; | - | ||||||
79 | bool autoRemove; | - | ||||||
80 | bool success; | - | ||||||
81 | }; | - | ||||||
82 | - | |||||||
83 | QTemporaryDirPrivate::QTemporaryDirPrivate() | - | ||||||
84 | : autoRemove(true), | - | ||||||
85 | success(false) | - | ||||||
86 | { | - | ||||||
87 | } executed 1210 times by 29 tests: end of block Executed by:
| 1210 | ||||||
88 | - | |||||||
89 | QTemporaryDirPrivate::~QTemporaryDirPrivate() | - | ||||||
90 | { | - | ||||||
91 | } | - | ||||||
92 | - | |||||||
93 | static QString defaultTemplateName() | - | ||||||
94 | { | - | ||||||
95 | QString baseName; | - | ||||||
96 | #if defined(QT_BUILD_CORE_LIB) | - | ||||||
97 | baseName = QCoreApplication::applicationName(); | - | ||||||
98 | if (baseName.isEmpty())
| 2-81 | ||||||
99 | #endif | - | ||||||
100 | baseName = QLatin1String("qt_temp"); executed 2 times by 2 tests: baseName = QLatin1String("qt_temp"); Executed by:
| 2 | ||||||
101 | - | |||||||
102 | return QDir::tempPath() + QLatin1Char('/') + baseName + QLatin1String("-XXXXXX"); executed 83 times by 20 tests: return QDir::tempPath() + QLatin1Char('/') + baseName + QLatin1String("-XXXXXX"); Executed by:
| 83 | ||||||
103 | } | - | ||||||
104 | - | |||||||
105 | #ifndef USE_SYSTEM_MKDTEMP | - | ||||||
106 | static int nextRand(int &v) | - | ||||||
107 | { | - | ||||||
108 | int r = v % 62; | - | ||||||
109 | v /= 62; | - | ||||||
110 | if (v < 62) | - | ||||||
111 | v = qrand(); | - | ||||||
112 | return r; | - | ||||||
113 | } | - | ||||||
114 | - | |||||||
115 | QPair<QString, bool> q_mkdtemp(QString templateName) | - | ||||||
116 | { | - | ||||||
117 | Q_ASSERT(templateName.endsWith(QLatin1String("XXXXXX"))); | - | ||||||
118 | - | |||||||
119 | static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | - | ||||||
120 | - | |||||||
121 | const int length = templateName.size(); | - | ||||||
122 | - | |||||||
123 | QChar *XXXXXX = templateName.data() + length - 6; | - | ||||||
124 | - | |||||||
125 | for (int i = 0; i < 256; ++i) { | - | ||||||
126 | int v = qrand(); | - | ||||||
127 | - | |||||||
128 | /* Fill in the random bits. */ | - | ||||||
129 | XXXXXX[0] = QLatin1Char(letters[nextRand(v)]); | - | ||||||
130 | XXXXXX[1] = QLatin1Char(letters[nextRand(v)]); | - | ||||||
131 | XXXXXX[2] = QLatin1Char(letters[nextRand(v)]); | - | ||||||
132 | XXXXXX[3] = QLatin1Char(letters[nextRand(v)]); | - | ||||||
133 | XXXXXX[4] = QLatin1Char(letters[nextRand(v)]); | - | ||||||
134 | XXXXXX[5] = QLatin1Char(letters[v % 62]); | - | ||||||
135 | - | |||||||
136 | QFileSystemEntry fileSystemEntry(templateName); | - | ||||||
137 | if (QFileSystemEngine::createDirectory(fileSystemEntry, false)) { | - | ||||||
138 | QSystemError error; | - | ||||||
139 | QFileSystemEngine::setPermissions(fileSystemEntry, | - | ||||||
140 | QFile::ReadOwner | | - | ||||||
141 | QFile::WriteOwner | | - | ||||||
142 | QFile::ExeOwner, error); | - | ||||||
143 | if (error.error() != 0) { | - | ||||||
144 | if (!QFileSystemEngine::removeDirectory(fileSystemEntry, false)) | - | ||||||
145 | qWarning() << "Unable to remove unused directory" << templateName; | - | ||||||
146 | continue; | - | ||||||
147 | } | - | ||||||
148 | return qMakePair(templateName, true); | - | ||||||
149 | } | - | ||||||
150 | # ifdef Q_OS_WIN | - | ||||||
151 | const int exists = ERROR_ALREADY_EXISTS; | - | ||||||
152 | int code = GetLastError(); | - | ||||||
153 | # else | - | ||||||
154 | const int exists = EEXIST; | - | ||||||
155 | int code = errno; | - | ||||||
156 | # endif | - | ||||||
157 | if (code != exists) | - | ||||||
158 | return qMakePair(qt_error_string(code), false); | - | ||||||
159 | } | - | ||||||
160 | return qMakePair(qt_error_string(), false); | - | ||||||
161 | } | - | ||||||
162 | - | |||||||
163 | #else // !USE_SYSTEM_MKDTEMP | - | ||||||
164 | - | |||||||
165 | QPair<QString, bool> q_mkdtemp(char *templateName) | - | ||||||
166 | { | - | ||||||
167 | bool ok = (mkdtemp(templateName) != 0); | - | ||||||
168 | return qMakePair(ok ? QFile::decodeName(templateName) : qt_error_string(), ok); executed 1210 times by 29 tests: return qMakePair(ok ? QFile::decodeName(templateName) : qt_error_string(), ok); Executed by:
| 1210 | ||||||
169 | } | - | ||||||
170 | - | |||||||
171 | #endif // USE_SYSTEM_MKDTEMP | - | ||||||
172 | - | |||||||
173 | void QTemporaryDirPrivate::create(const QString &templateName) | - | ||||||
174 | { | - | ||||||
175 | #ifndef USE_SYSTEM_MKDTEMP | - | ||||||
176 | QString buffer = templateName; | - | ||||||
177 | if (!buffer.endsWith(QLatin1String("XXXXXX"))) | - | ||||||
178 | buffer += QLatin1String("XXXXXX"); | - | ||||||
179 | const QPair<QString, bool> result = q_mkdtemp(buffer); | - | ||||||
180 | #else // !USE_SYSTEM_MKDTEMP | - | ||||||
181 | QByteArray buffer = QFile::encodeName(templateName); | - | ||||||
182 | if (!buffer.endsWith("XXXXXX"))
| 35-1175 | ||||||
183 | buffer += "XXXXXX"; executed 35 times by 3 tests: buffer += "XXXXXX"; Executed by:
| 35 | ||||||
184 | QPair<QString, bool> result = q_mkdtemp(buffer.data()); // modifies buffer | - | ||||||
185 | #endif // USE_SYSTEM_MKDTEMP | - | ||||||
186 | pathOrError = result.first; | - | ||||||
187 | success = result.second; | - | ||||||
188 | } executed 1210 times by 29 tests: end of block Executed by:
| 1210 | ||||||
189 | - | |||||||
190 | //************* QTemporaryDir | - | ||||||
191 | - | |||||||
192 | /*! | - | ||||||
193 | \class QTemporaryDir | - | ||||||
194 | \inmodule QtCore | - | ||||||
195 | \reentrant | - | ||||||
196 | \brief The QTemporaryDir class creates a unique directory for temporary use. | - | ||||||
197 | - | |||||||
198 | \ingroup io | - | ||||||
199 | - | |||||||
200 | - | |||||||
201 | QTemporaryDir is used to create unique temporary dirs safely. | - | ||||||
202 | The dir itself is created by the constructor. The name of the | - | ||||||
203 | temporary directory is guaranteed to be unique (i.e., you are | - | ||||||
204 | guaranteed to not overwrite an existing dir), and the directory will | - | ||||||
205 | subsequently be removed upon destruction of the QTemporaryDir | - | ||||||
206 | object. The directory name is either auto-generated, or created based | - | ||||||
207 | on a template, which is passed to QTemporaryDir's constructor. | - | ||||||
208 | - | |||||||
209 | Example: | - | ||||||
210 | - | |||||||
211 | \snippet code/src_corelib_io_qtemporarydir.cpp 0 | - | ||||||
212 | - | |||||||
213 | It is very important to test that the temporary directory could be | - | ||||||
214 | created, using isValid(). Do not use \l {QDir::exists()}{exists()}, since a default-constructed | - | ||||||
215 | QDir represents the current directory, which exists. | - | ||||||
216 | - | |||||||
217 | The path to the temporary dir can be found by calling path(). | - | ||||||
218 | - | |||||||
219 | A temporary directory will have some static part of the name and some | - | ||||||
220 | part that is calculated to be unique. The default path will be | - | ||||||
221 | determined from QCoreApplication::applicationName() (otherwise \c qt_temp) and will | - | ||||||
222 | be placed into the temporary path as returned by QDir::tempPath(). | - | ||||||
223 | If you specify your own path, a relative path will not be placed in the | - | ||||||
224 | temporary directory by default, but be relative to the current working directory. | - | ||||||
225 | In all cases, a random string will be appended to the path in order to make it unique. | - | ||||||
226 | - | |||||||
227 | \sa QDir::tempPath(), QDir, QTemporaryFile | - | ||||||
228 | */ | - | ||||||
229 | - | |||||||
230 | /*! | - | ||||||
231 | Constructs a QTemporaryDir using as template the application name | - | ||||||
232 | returned by QCoreApplication::applicationName() (otherwise \c qt_temp). | - | ||||||
233 | The directory is stored in the system's temporary directory, QDir::tempPath(). | - | ||||||
234 | - | |||||||
235 | \sa QDir::tempPath() | - | ||||||
236 | */ | - | ||||||
237 | QTemporaryDir::QTemporaryDir() | - | ||||||
238 | : d_ptr(new QTemporaryDirPrivate) | - | ||||||
239 | { | - | ||||||
240 | d_ptr->create(defaultTemplateName()); | - | ||||||
241 | } executed 82 times by 20 tests: end of block Executed by:
| 82 | ||||||
242 | - | |||||||
243 | /*! | - | ||||||
244 | Constructs a QTemporaryDir with a template of \a templatePath. | - | ||||||
245 | - | |||||||
246 | If \a templatePath is a relative path, the path will be relative to the | - | ||||||
247 | current working directory. You can use QDir::tempPath() to construct \a | - | ||||||
248 | templatePath if you want use the system's temporary directory. | - | ||||||
249 | - | |||||||
250 | If the \a templatePath ends with XXXXXX it will be used as the dynamic portion | - | ||||||
251 | of the directory name, otherwise it will be appended. | - | ||||||
252 | Unlike QTemporaryFile, XXXXXX in the middle of the template string is not supported. | - | ||||||
253 | - | |||||||
254 | \sa QDir::tempPath() | - | ||||||
255 | */ | - | ||||||
256 | QTemporaryDir::QTemporaryDir(const QString &templatePath) | - | ||||||
257 | : d_ptr(new QTemporaryDirPrivate) | - | ||||||
258 | { | - | ||||||
259 | if (templatePath.isEmpty())
| 1-1127 | ||||||
260 | d_ptr->create(defaultTemplateName()); executed 1 time by 1 test: d_ptr->create(defaultTemplateName()); Executed by:
| 1 | ||||||
261 | else | - | ||||||
262 | d_ptr->create(templatePath); executed 1127 times by 11 tests: d_ptr->create(templatePath); Executed by:
| 1127 | ||||||
263 | } | - | ||||||
264 | - | |||||||
265 | /*! | - | ||||||
266 | Destroys the temporary directory object. | - | ||||||
267 | If auto remove mode was set, it will automatically delete the directory | - | ||||||
268 | including all its contents. | - | ||||||
269 | - | |||||||
270 | \sa autoRemove() | - | ||||||
271 | */ | - | ||||||
272 | QTemporaryDir::~QTemporaryDir() | - | ||||||
273 | { | - | ||||||
274 | if (d_ptr->autoRemove)
| 230-1001 | ||||||
275 | remove(); executed 230 times by 47 tests: remove(); Executed by:
| 230 | ||||||
276 | } executed 1231 times by 47 tests: end of block Executed by:
| 1231 | ||||||
277 | - | |||||||
278 | /*! | - | ||||||
279 | Returns \c true if the QTemporaryDir was created successfully. | - | ||||||
280 | */ | - | ||||||
281 | bool QTemporaryDir::isValid() const | - | ||||||
282 | { | - | ||||||
283 | return d_ptr->success; executed 1237 times by 37 tests: return d_ptr->success; Executed by:
| 1237 | ||||||
284 | } | - | ||||||
285 | - | |||||||
286 | /*! | - | ||||||
287 | \since 5.6 | - | ||||||
288 | - | |||||||
289 | If isValid() returns \c false, this function returns the error string that | - | ||||||
290 | explains why the creation of the temporary directory failed. Otherwise, this | - | ||||||
291 | function return an empty string. | - | ||||||
292 | */ | - | ||||||
293 | QString QTemporaryDir::errorString() const | - | ||||||
294 | { | - | ||||||
295 | return d_ptr->success ? QString() : d_ptr->pathOrError; executed 1100 times by 23 tests: return d_ptr->success ? QString() : d_ptr->pathOrError; Executed by:
| 1100 | ||||||
296 | } | - | ||||||
297 | - | |||||||
298 | /*! | - | ||||||
299 | Returns the path to the temporary directory. | - | ||||||
300 | Empty if the QTemporaryDir could not be created. | - | ||||||
301 | */ | - | ||||||
302 | QString QTemporaryDir::path() const | - | ||||||
303 | { | - | ||||||
304 | return d_ptr->success ? d_ptr->pathOrError : QString(); executed 5034 times by 69 tests: return d_ptr->success ? d_ptr->pathOrError : QString(); Executed by:
| 5034 | ||||||
305 | } | - | ||||||
306 | - | |||||||
307 | /*! | - | ||||||
308 | Returns \c true if the QTemporaryDir is in auto remove | - | ||||||
309 | mode. Auto-remove mode will automatically delete the directory from | - | ||||||
310 | disk upon destruction. This makes it very easy to create your | - | ||||||
311 | QTemporaryDir object on the stack, fill it with files, do something with | - | ||||||
312 | the files, and finally on function return it will automatically clean up | - | ||||||
313 | after itself. | - | ||||||
314 | - | |||||||
315 | Auto-remove is on by default. | - | ||||||
316 | - | |||||||
317 | \sa setAutoRemove(), remove() | - | ||||||
318 | */ | - | ||||||
319 | bool QTemporaryDir::autoRemove() const | - | ||||||
320 | { | - | ||||||
321 | return d_ptr->autoRemove; executed 2 times by 1 test: return d_ptr->autoRemove; Executed by:
| 2 | ||||||
322 | } | - | ||||||
323 | - | |||||||
324 | /*! | - | ||||||
325 | Sets the QTemporaryDir into auto-remove mode if \a b is true. | - | ||||||
326 | - | |||||||
327 | Auto-remove is on by default. | - | ||||||
328 | - | |||||||
329 | \sa autoRemove(), remove() | - | ||||||
330 | */ | - | ||||||
331 | void QTemporaryDir::setAutoRemove(bool b) | - | ||||||
332 | { | - | ||||||
333 | d_ptr->autoRemove = b; | - | ||||||
334 | } executed 1009 times by 4 tests: end of block Executed by:
| 1009 | ||||||
335 | - | |||||||
336 | /*! | - | ||||||
337 | Removes the temporary directory, including all its contents. | - | ||||||
338 | - | |||||||
339 | Returns \c true if removing was successful. | - | ||||||
340 | */ | - | ||||||
341 | bool QTemporaryDir::remove() | - | ||||||
342 | { | - | ||||||
343 | if (!d_ptr->success)
| 7-226 | ||||||
344 | return false; executed 7 times by 1 test: return false; Executed by:
| 7 | ||||||
345 | Q_ASSERT(!path().isEmpty()); | - | ||||||
346 | Q_ASSERT(path() != QLatin1String(".")); | - | ||||||
347 | - | |||||||
348 | const bool result = QDir(path()).removeRecursively(); | - | ||||||
349 | if (!result) {
| 0-226 | ||||||
350 | qWarning() << "QTemporaryDir: Unable to remove" | - | ||||||
351 | << QDir::toNativeSeparators(path()) | - | ||||||
352 | << "most likely due to the presence of read-only files."; | - | ||||||
353 | } never executed: end of block | 0 | ||||||
354 | return result; executed 226 times by 47 tests: return result; Executed by:
| 226 | ||||||
355 | } | - | ||||||
356 | - | |||||||
357 | QT_END_NAMESPACE | - | ||||||
358 | - | |||||||
359 | #endif // QT_NO_TEMPORARYFILE | - | ||||||
Source code | Switch to Preprocessed file |