Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtemporarydir.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// Copyright (C) 2017 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include "qtemporarydir.h"
6
7#if QT_CONFIG(temporaryfile)
8
9#include "qdebug.h"
10#include "qplatformdefs.h"
11#include "qrandom.h"
12#include "private/qtemporaryfile_p.h"
13
14#if defined(QT_BUILD_CORE_LIB)
15#include "qcoreapplication.h"
16#endif
17
18#if !defined(Q_OS_WIN)
19#include <errno.h>
20#endif
21
22#include <type_traits>
23
25
26using namespace Qt::StringLiterals;
27
28static_assert(std::is_nothrow_move_constructible_v<QTemporaryDir>);
29static_assert(std::is_nothrow_move_assignable_v<QTemporaryDir>);
30
31//************* QTemporaryDirPrivate
32class QTemporaryDirPrivate
33{
34public:
35 QTemporaryDirPrivate();
36 ~QTemporaryDirPrivate();
37
38 void create(const QString &templateName);
39
40 QString pathOrError;
41 bool autoRemove;
42 bool success;
43};
44
45QTemporaryDirPrivate::QTemporaryDirPrivate()
46 : autoRemove(true),
47 success(false)
48{
49}
50
51QTemporaryDirPrivate::~QTemporaryDirPrivate()
52{
53}
54
55static QString defaultTemplateName()
56{
57 QString baseName;
58#if defined(QT_BUILD_CORE_LIB)
60 if (baseName.isEmpty())
61#endif
62 baseName = "qt_temp"_L1;
63
64 return QDir::tempPath() + u'/' + baseName + "-XXXXXX"_L1;
65}
66
67void QTemporaryDirPrivate::create(const QString &templateName)
68{
69 QTemporaryFileName tfn(templateName);
70 for (int i = 0; i < 256; ++i) {
71 tfn.generateNext();
72 QFileSystemEntry fileSystemEntry(tfn.path, QFileSystemEntry::FromNativePath());
73 if (QFileSystemEngine::createDirectory(fileSystemEntry, false,
75 | QFile::ExeOwner)) {
76 success = true;
77 pathOrError = fileSystemEntry.filePath();
78 return;
79 }
80# ifdef Q_OS_WIN
81 const int exists = ERROR_ALREADY_EXISTS;
82 int code = GetLastError();
83# else
84 const int exists = EEXIST;
85 int code = errno;
86# endif
87 if (code != exists)
88 break;
89 }
90 pathOrError = qt_error_string();
91 success = false;
92}
93
94//************* QTemporaryDir
95
141QTemporaryDir::QTemporaryDir()
142 : d_ptr(new QTemporaryDirPrivate)
143{
144 d_ptr->create(defaultTemplateName());
145}
146
160QTemporaryDir::QTemporaryDir(const QString &templatePath)
161 : d_ptr(new QTemporaryDirPrivate)
162{
163 if (templatePath.isEmpty())
164 d_ptr->create(defaultTemplateName());
165 else
166 d_ptr->create(templatePath);
167}
168
209QTemporaryDir::~QTemporaryDir()
210{
211 if (d_ptr) {
212 if (d_ptr->autoRemove)
213 remove();
214
215 delete d_ptr;
216 }
217}
218
222bool QTemporaryDir::isValid() const
223{
224 return d_ptr->success;
225}
226
234QString QTemporaryDir::errorString() const
235{
236 return d_ptr->success ? QString() : d_ptr->pathOrError;
237}
238
243QString QTemporaryDir::path() const
244{
245 return d_ptr->success ? d_ptr->pathOrError : QString();
246}
247
257QString QTemporaryDir::filePath(const QString &fileName) const
258{
260 qWarning("QTemporaryDir::filePath: Absolute paths are not allowed: %s", qUtf8Printable(fileName));
261 return QString();
262 }
263
264 if (!d_ptr->success)
265 return QString();
266
267 QString ret = d_ptr->pathOrError;
268 if (!fileName.isEmpty()) {
269 ret += u'/';
270 ret += fileName;
271 }
272 return ret;
273}
274
287bool QTemporaryDir::autoRemove() const
288{
289 return d_ptr->autoRemove;
290}
291
299void QTemporaryDir::setAutoRemove(bool b)
300{
301 d_ptr->autoRemove = b;
302}
303
309bool QTemporaryDir::remove()
310{
311 if (!d_ptr->success)
312 return false;
313 Q_ASSERT(!path().isEmpty());
314 Q_ASSERT(path() != "."_L1);
315
316 const bool result = QDir(path()).removeRecursively();
317 if (!result) {
318 qWarning() << "QTemporaryDir: Unable to remove"
320 << "most likely due to the presence of read-only files.";
321 }
322 return result;
323}
324
326
327#endif // QT_CONFIG(temporaryfile)
QString applicationName
the name of this application
\inmodule QtCore
Definition qdir.h:20
static bool isAbsolutePath(const QString &path)
Returns true if path is absolute; returns false if it is relative.
Definition qdir.h:184
bool removeRecursively()
Definition qdir.cpp:1640
static QString tempPath()
Returns the absolute canonical path of the system's temporary directory.
Definition qdir.cpp:2133
static QString toNativeSeparators(const QString &pathName)
Definition qdir.cpp:929
static bool createDirectory(const QFileSystemEntry &entry, bool createParents, std::optional< QFile::Permissions > permissions=std::nullopt)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
Combined button and popup list for selecting options.
Q_DECL_COLD_FUNCTION Q_CORE_EXPORT QString qt_error_string(int errorCode=-1)
#define qWarning
Definition qlogging.h:166
return ret
GLboolean GLboolean GLboolean b
GLsizei const GLchar *const * path
GLuint64EXT * result
[6]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define qUtf8Printable(string)
Definition qstring.h:1535
settings remove("monkey")
view create()