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 <marc.mutz@kdab.com>
Reviewed-by: David Faure <david.faure@kdab.com>
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
This commit is contained in:
Thiago Macieira 2015-07-13 14:55:27 -07:00
parent 5af7f7ec46
commit 7331b15525
5 changed files with 37 additions and 16 deletions

View File

@ -38,6 +38,7 @@
#include "qdiriterator.h"
#include "qplatformdefs.h"
#include <QDebug>
#include <QPair>
#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<QString, bool> 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<QString, bool> 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<QString, bool> 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();
}
/*!

View File

@ -52,6 +52,7 @@ public:
~QTemporaryDir();
bool isValid() const;
QString errorString() const;
bool autoRemove() const;
void setAutoRemove(bool b);

View File

@ -281,7 +281,8 @@ void tst_QFileInfo::initTestCase()
m_resourcesDir = dataPath + QLatin1String("/resources");
m_proFile = dataPath + QLatin1String("/tst_qfileinfo.pro");
QVERIFY(m_dir.isValid());
QVERIFY2(m_dir.isValid(),
("Failed to create temporary dir: " + m_dir.errorString()).toUtf8());
QVERIFY(QDir::setCurrent(m_dir.path()));
}

View File

@ -93,6 +93,7 @@ void tst_QTemporaryDir::construction()
QCOMPARE(dir.path().left(tmp.size()), tmp);
QVERIFY(dir.path().contains("tst_qtemporarydir"));
QVERIFY(QFileInfo(dir.path()).isDir());
QCOMPARE(dir.errorString(), QString());
}
// Testing get/set functions
@ -251,6 +252,7 @@ void tst_QTemporaryDir::nonWritableCurrentDir()
QTemporaryDir dir("tempXXXXXX");
dir.setAutoRemove(true);
QVERIFY(!dir.isValid());
QVERIFY(!dir.errorString().isEmpty());
QVERIFY(dir.path().isEmpty());
#endif
}
@ -287,7 +289,11 @@ void tst_QTemporaryDir::stressTest()
for (int i = 0; i < iterations; ++i) {
QTemporaryDir dir(pattern);
dir.setAutoRemove(false);
QVERIFY2(dir.isValid(), qPrintable(QString::fromLatin1("Failed to create #%1 under %2.").arg(i).arg(QDir::toNativeSeparators(pattern))));
QVERIFY2(dir.isValid(),
qPrintable(QString::fromLatin1("Failed to create #%1 under %2: %3.")
.arg(i)
.arg(QDir::toNativeSeparators(pattern))
.arg(dir.errorString())));
QVERIFY(!names.contains(dir.path()));
names.insert(dir.path());
}

View File

@ -82,7 +82,8 @@ tst_QMimeDatabase::tst_QMimeDatabase()
void tst_QMimeDatabase::initTestCase()
{
QVERIFY(m_temporaryDir.isValid());
QVERIFY2(m_temporaryDir.isValid(),
("Could not create temporary subdir: " + m_temporaryDir.errorString()).toUtf8());
// Create a "global" and a "local" XDG data dir, right here.
// The local dir will be empty initially, while the global dir will contain a copy of freedesktop.org.xml