From 7331b15525996179acb93f456ca24286418eca23 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 13 Jul 2015 14:55:27 -0700 Subject: Add QTemporaryDir::errorString() [ChangeLog][QtCore][QTemporaryDir] Added errorString() method that returns the string explaining why creating the temporary directory failed. Change-Id: Ib306f8f647014b399b87ffff13f0a1f3c89e0a2c Reviewed-by: Marc Mutz Reviewed-by: David Faure Reviewed-by: Oswald Buddenhagen --- src/corelib/io/qtemporarydir.cpp | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) (limited to 'src/corelib/io/qtemporarydir.cpp') diff --git a/src/corelib/io/qtemporarydir.cpp b/src/corelib/io/qtemporarydir.cpp index 0beda22f24..32c3d92dca 100644 --- a/src/corelib/io/qtemporarydir.cpp +++ b/src/corelib/io/qtemporarydir.cpp @@ -38,6 +38,7 @@ #include "qdiriterator.h" #include "qplatformdefs.h" #include +#include #if defined(QT_BUILD_CORE_LIB) #include "qcoreapplication.h" @@ -59,7 +60,7 @@ public: void create(const QString &templateName); - QString path; + QString pathOrError; bool autoRemove; bool success; }; @@ -97,7 +98,7 @@ static int nextRand(int &v) return r; } -static char *q_mkdtemp(char *templateName) +QPair q_mkdtemp(char *templateName) { static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; @@ -105,8 +106,7 @@ static char *q_mkdtemp(char *templateName) char *XXXXXX = templateName + length - 6; - if ((length < 6u) || strncmp(XXXXXX, "XXXXXX", 6)) - return 0; + Q_ASSERT((length >= 6u) && strncmp(XXXXXX, "XXXXXX", 6) == 0); for (int i = 0; i < 256; ++i) { int v = qrand(); @@ -133,17 +133,18 @@ static char *q_mkdtemp(char *templateName) qWarning() << "Unable to remove unused directory" << templateNameStr; continue; } - return templateName; + return qMakePair(QFile::decodeName(templateName), true); } } - return 0; + return qMakePair(qt_error_string(), false); } #else // defined(Q_OS_QNX ) || defined(Q_OS_WIN) || defined(Q_OS_ANDROID) -static char *q_mkdtemp(char *templateName) +QPair q_mkdtemp(char *templateName) { - return mkdtemp(templateName); + bool ok = (mkdtemp(templateName) != 0); + return qMakePair(ok ? QFile::decodeName(templateName) : qt_error_string(), ok); } #endif @@ -153,10 +154,9 @@ void QTemporaryDirPrivate::create(const QString &templateName) QByteArray buffer = QFile::encodeName(templateName); if (!buffer.endsWith("XXXXXX")) buffer += "XXXXXX"; - if (q_mkdtemp(buffer.data())) { // modifies buffer - success = true; - path = QFile::decodeName(buffer.constData()); - } + QPair result = q_mkdtemp(buffer.data()); // modifies buffer + pathOrError = result.first; + success = result.second; } //************* QTemporaryDir @@ -255,13 +255,25 @@ bool QTemporaryDir::isValid() const return d_ptr->success; } +/*! + \since 5.6 + + If isValid() returns \c false, this function returns the error string that + explains why the creation of the temporary directory failed. Otherwise, this + function return an empty string. +*/ +QString QTemporaryDir::errorString() const +{ + return d_ptr->success ? QString() : d_ptr->pathOrError; +} + /*! Returns the path to the temporary directory. Empty if the QTemporaryDir could not be created. */ QString QTemporaryDir::path() const { - return d_ptr->path; + return d_ptr->success ? d_ptr->pathOrError : QString(); } /*! -- cgit v1.2.3