Qt 6.x
The Qt SDK
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#ifndef QT_NO_TEMPORARYFILE
8
9#include "qdebug.h"
10#include "qdiriterator.h"
11#include "qpair.h"
12#include "qplatformdefs.h"
13#include "qrandom.h"
14#include "private/qtemporaryfile_p.h"
15
16#if defined(QT_BUILD_CORE_LIB)
17#include "qcoreapplication.h"
18#endif
19
20#if !defined(Q_OS_WIN)
21#include <errno.h>
22#endif
23
24#include <type_traits>
25
27
28using namespace Qt::StringLiterals;
29
30static_assert(std::is_nothrow_move_constructible_v<QTemporaryDir>);
31static_assert(std::is_nothrow_move_assignable_v<QTemporaryDir>);
32
33//************* QTemporaryDirPrivate
35{
36public:
39
40 void create(const QString &templateName);
41
44 bool success;
45};
46
48 : autoRemove(true),
49 success(false)
50{
51}
52
54{
55}
56
58{
59 QString baseName;
60#if defined(QT_BUILD_CORE_LIB)
62 if (baseName.isEmpty())
63#endif
64 baseName = "qt_temp"_L1;
65
66 return QDir::tempPath() + u'/' + baseName + "-XXXXXX"_L1;
67}
68
69void QTemporaryDirPrivate::create(const QString &templateName)
70{
71 QTemporaryFileName tfn(templateName);
72 for (int i = 0; i < 256; ++i) {
73 tfn.generateNext();
75 if (QFileSystemEngine::createDirectory(fileSystemEntry, false,
77 | QFile::ExeOwner)) {
78 success = true;
79 pathOrError = fileSystemEntry.filePath();
80 return;
81 }
82# ifdef Q_OS_WIN
83 const int exists = ERROR_ALREADY_EXISTS;
84 int code = GetLastError();
85# else
86 const int exists = EEXIST;
87 int code = errno;
88# endif
89 if (code != exists)
90 break;
91 }
93 success = false;
94}
95
96//************* QTemporaryDir
97
144 : d_ptr(new QTemporaryDirPrivate)
145{
146 d_ptr->create(defaultTemplateName());
147}
148
163 : d_ptr(new QTemporaryDirPrivate)
164{
165 if (templatePath.isEmpty())
166 d_ptr->create(defaultTemplateName());
167 else
168 d_ptr->create(templatePath);
169}
170
212{
213 if (d_ptr) {
214 if (d_ptr->autoRemove)
215 remove();
216
217 delete d_ptr;
218 }
219}
220
225{
226 return d_ptr->success;
227}
228
237{
238 return d_ptr->success ? QString() : d_ptr->pathOrError;
239}
240
246{
247 return d_ptr->success ? d_ptr->pathOrError : QString();
248}
249
260{
262 qWarning("QTemporaryDir::filePath: Absolute paths are not allowed: %s", qUtf8Printable(fileName));
263 return QString();
264 }
265
266 if (!d_ptr->success)
267 return QString();
268
269 QString ret = d_ptr->pathOrError;
270 if (!fileName.isEmpty()) {
271 ret += u'/';
272 ret += fileName;
273 }
274 return ret;
275}
276
290{
291 return d_ptr->autoRemove;
292}
293
302{
303 d_ptr->autoRemove = b;
304}
305
312{
313 if (!d_ptr->success)
314 return false;
315 Q_ASSERT(!path().isEmpty());
316 Q_ASSERT(path() != "."_L1);
317
318 const bool result = QDir(path()).removeRecursively();
319 if (!result) {
320 qWarning() << "QTemporaryDir: Unable to remove"
322 << "most likely due to the presence of read-only files.";
323 }
324 return result;
325}
326
328
329#endif // QT_NO_TEMPORARYFILE
QString applicationName
the name of this application
\inmodule QtCore
Definition qdir.h:19
static bool isAbsolutePath(const QString &path)
Returns true if path is absolute; returns false if it is relative.
Definition qdir.h:183
bool removeRecursively()
Definition qdir.cpp:1640
static QString tempPath()
Returns the absolute canonical path of the system's temporary directory.
Definition qdir.cpp:2130
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)
Q_AUTOTEST_EXPORT QString filePath() const
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
void create(const QString &templateName)
bool remove()
Removes the temporary directory, including all its contents.
QTemporaryDir()
Constructs a QTemporaryDir using as template the application name returned by QCoreApplication::appli...
QString filePath(const QString &fileName) const
bool autoRemove() const
Returns true if the QTemporaryDir is in auto remove mode.
void setAutoRemove(bool b)
Sets the QTemporaryDir into auto-remove mode if b is true.
~QTemporaryDir()
Destroys the temporary directory object.
QString errorString() const
QString path() const
Returns the path to the temporary directory.
bool isValid() const
Returns true if the QTemporaryDir was created successfully.
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:162
return ret
GLboolean GLboolean GLboolean b
GLuint64EXT * result
[6]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define qUtf8Printable(string)
Definition qstring.h:1395
static QString defaultTemplateName()
view create()
QFileSystemEntry::NativePath generateNext()
QFileSystemEntry::NativePath path