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