diff options
Diffstat (limited to 'src/corelib/io/qtemporarydir.cpp')
-rw-r--r-- | src/corelib/io/qtemporarydir.cpp | 83 |
1 files changed, 13 insertions, 70 deletions
diff --git a/src/corelib/io/qtemporarydir.cpp b/src/corelib/io/qtemporarydir.cpp index ffaee9c683..2779246c5b 100644 --- a/src/corelib/io/qtemporarydir.cpp +++ b/src/corelib/io/qtemporarydir.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2017 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. +** Copyright (C) 2017 Intel Corporation. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -44,22 +44,15 @@ #include "qdiriterator.h" #include "qplatformdefs.h" +#include "private/qtemporaryfile_p.h" #include <QDebug> #include <QPair> +#include <QRandomGenerator> #if defined(QT_BUILD_CORE_LIB) #include "qcoreapplication.h" #endif -#if !defined(Q_OS_QNX) && !defined(Q_OS_WIN) && !defined(Q_OS_ANDROID) && !defined(Q_OS_INTEGRITY) -# define USE_SYSTEM_MKDTEMP -#endif - -#include <stdlib.h> // mkdtemp -#ifndef USE_SYSTEM_MKDTEMP -#include <private/qfilesystemengine_p.h> -#endif - #if !defined(Q_OS_WIN) #include <errno.h> #endif @@ -102,38 +95,12 @@ static QString defaultTemplateName() return QDir::tempPath() + QLatin1Char('/') + baseName + QLatin1String("-XXXXXX"); } -#ifndef USE_SYSTEM_MKDTEMP -static int nextRand(int &v) -{ - int r = v % 62; - v /= 62; - if (v < 62) - v = qrand(); - return r; -} - -QPair<QString, bool> q_mkdtemp(QString templateName) +void QTemporaryDirPrivate::create(const QString &templateName) { - Q_ASSERT(templateName.endsWith(QLatin1String("XXXXXX"))); - - static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - - const int length = templateName.size(); - - QChar *XXXXXX = templateName.data() + length - 6; - + QTemporaryFileName tfn(templateName); for (int i = 0; i < 256; ++i) { - int v = qrand(); - - /* Fill in the random bits. */ - XXXXXX[0] = QLatin1Char(letters[nextRand(v)]); - XXXXXX[1] = QLatin1Char(letters[nextRand(v)]); - XXXXXX[2] = QLatin1Char(letters[nextRand(v)]); - XXXXXX[3] = QLatin1Char(letters[nextRand(v)]); - XXXXXX[4] = QLatin1Char(letters[nextRand(v)]); - XXXXXX[5] = QLatin1Char(letters[v % 62]); - - QFileSystemEntry fileSystemEntry(templateName); + tfn.generateNext(); + QFileSystemEntry fileSystemEntry(tfn.path, QFileSystemEntry::FromNativePath()); if (QFileSystemEngine::createDirectory(fileSystemEntry, false)) { QSystemError error; QFileSystemEngine::setPermissions(fileSystemEntry, @@ -145,7 +112,9 @@ QPair<QString, bool> q_mkdtemp(QString templateName) qWarning() << "Unable to remove unused directory" << templateName; continue; } - return qMakePair(templateName, true); + success = true; + pathOrError = fileSystemEntry.filePath(); + return; } # ifdef Q_OS_WIN const int exists = ERROR_ALREADY_EXISTS; @@ -155,36 +124,10 @@ QPair<QString, bool> q_mkdtemp(QString templateName) int code = errno; # endif if (code != exists) - return qMakePair(qt_error_string(code), false); + break; } - return qMakePair(qt_error_string(), false); -} - -#else // !USE_SYSTEM_MKDTEMP - -QPair<QString, bool> q_mkdtemp(char *templateName) -{ - bool ok = (mkdtemp(templateName) != 0); - return qMakePair(ok ? QFile::decodeName(templateName) : qt_error_string(), ok); -} - -#endif // USE_SYSTEM_MKDTEMP - -void QTemporaryDirPrivate::create(const QString &templateName) -{ -#ifndef USE_SYSTEM_MKDTEMP - QString buffer = templateName; - if (!buffer.endsWith(QLatin1String("XXXXXX"))) - buffer += QLatin1String("XXXXXX"); - const QPair<QString, bool> result = q_mkdtemp(buffer); -#else // !USE_SYSTEM_MKDTEMP - QByteArray buffer = QFile::encodeName(templateName); - if (!buffer.endsWith("XXXXXX")) - buffer += "XXXXXX"; - QPair<QString, bool> result = q_mkdtemp(buffer.data()); // modifies buffer -#endif // USE_SYSTEM_MKDTEMP - pathOrError = result.first; - success = result.second; + pathOrError = qt_error_string(); + success = false; } //************* QTemporaryDir |