8#if QT_CONFIG(temporaryfile)
11#include "qplatformdefs.h"
13#include "private/qtemporaryfile_p.h"
15#if defined(QT_BUILD_CORE_LIB)
16#include "qcoreapplication.h"
27using namespace Qt::StringLiterals;
29static_assert(std::is_nothrow_move_constructible_v<QTemporaryDir>);
30static_assert(std::is_nothrow_move_assignable_v<QTemporaryDir>);
33class QTemporaryDirPrivate
36 QTemporaryDirPrivate();
37 ~QTemporaryDirPrivate();
39 void create(
const QString &templateName);
46QTemporaryDirPrivate::QTemporaryDirPrivate()
52QTemporaryDirPrivate::~QTemporaryDirPrivate()
56static QString defaultTemplateName()
59#if defined(QT_BUILD_CORE_LIB)
60 baseName = QCoreApplication::applicationName();
61 if (baseName.isEmpty())
63 baseName =
"qt_temp"_L1;
65 return QDir::tempPath() + u'/' + baseName +
"-XXXXXX"_L1;
68void QTemporaryDirPrivate::create(
const QString &templateName)
70 QTemporaryFileName tfn(templateName);
71 constexpr auto perms = QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner;
72 for (
int i = 0; i < 256; ++i) {
74 QFileSystemEntry fileSystemEntry(tfn.path, QFileSystemEntry::FromNativePath());
75 if (QFileSystemEngine::mkdir(fileSystemEntry, perms)) {
77 pathOrError = fileSystemEntry.filePath();
81 const int exists = ERROR_ALREADY_EXISTS;
82 int code = GetLastError();
84 const int exists = EEXIST;
90 pathOrError = qt_error_string();
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
135
136
137
138
139
140
141QTemporaryDir::QTemporaryDir()
142 : d_ptr(
new QTemporaryDirPrivate)
144 d_ptr->create(defaultTemplateName());
148
149
150
151
152
153
154
155
156
157
158
159
160QTemporaryDir::QTemporaryDir(
const QString &templatePath)
161 : d_ptr(
new QTemporaryDirPrivate)
163 if (templatePath.isEmpty())
164 d_ptr->create(defaultTemplateName());
166 d_ptr->create(templatePath);
170
171
172
173
174
175
176
177
178
179
182
183
184
185
186
187
188
189
190
191
194
195
196
197
200
201
202
203
204
205
206QTemporaryDir::~QTemporaryDir()
209 if (d_ptr->autoRemove)
217
218
219bool QTemporaryDir::isValid()
const
221 return d_ptr->success;
225
226
227
228
229
230
231QString QTemporaryDir::errorString()
const
233 return d_ptr->success ? QString() : d_ptr->pathOrError;
237
238
239
240
241
242
243
244
245
246
247
248QString QTemporaryDir::path()
const
250 return d_ptr->success ? d_ptr->pathOrError : QString();
254
255
256
257
258
259
260
261
262
263
264QString QTemporaryDir::filePath(
const QString &fileName)
const
266 if (QDir::isAbsolutePath(fileName)) {
267 qWarning(
"QTemporaryDir::filePath: Absolute paths are not allowed: %s", qUtf8Printable(fileName));
274 QString ret = d_ptr->pathOrError;
275 if (!fileName.isEmpty()) {
283
284
285
286
287
288
289
290
291
292
293
294bool QTemporaryDir::autoRemove()
const
296 return d_ptr->autoRemove;
300
301
302
303
304
305
306void QTemporaryDir::setAutoRemove(
bool b)
308 d_ptr->autoRemove = b;
312
313
314
315
316bool QTemporaryDir::remove()
320 Q_ASSERT(!path().isEmpty());
321 Q_ASSERT(path() !=
"."_L1);
323 const bool result = QDir(path()).removeRecursively();
325 qWarning() <<
"QTemporaryDir: Unable to remove"
326 << QDir::toNativeSeparators(path())
327 <<
"most likely due to the presence of read-only files.";