From f028c0dcc865933703a3db31f526bfe7b53d7f1b Mon Sep 17 00:00:00 2001 From: Tim Jenssen Date: Thu, 13 Jun 2013 16:30:32 +0200 Subject: Fix missing removal of directories for MkDirOperation * use removeDirectory instead of rmdir * rmdir is not able to delete subdirectories * pass force parameter to removeDirectory * adds unit test Change-Id: I1f52aefdb5d13793321fe6001809362d34ad8941 Reviewed-by: Niels Weber Reviewed-by: Tim Jenssen --- src/libs/kdtools/kdupdaterupdateoperations.cpp | 32 +++++++++++++------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'src/libs/kdtools') diff --git a/src/libs/kdtools/kdupdaterupdateoperations.cpp b/src/libs/kdtools/kdupdaterupdateoperations.cpp index 8270bba03..b67026970 100644 --- a/src/libs/kdtools/kdupdaterupdateoperations.cpp +++ b/src/libs/kdtools/kdupdaterupdateoperations.cpp @@ -58,20 +58,27 @@ static QString errnoToQString(int error) #endif } -static bool removeDirectory(const QString &path, QString *errorString) +static bool removeDirectory(const QString &path, QString *errorString, bool force = true) { Q_ASSERT(errorString); const QFileInfoList entries = QDir(path).entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries | QDir::Hidden); for (QFileInfoList::const_iterator it = entries.constBegin(); it != entries.constEnd(); ++it) { if (it->isDir() && !it->isSymLink()) { - removeDirectory(it->filePath(), errorString); - } else { + removeDirectory(it->filePath(), errorString, force); + } else if (force) { QFile f(it->filePath()); if (!f.remove()) return false; } } + // even remove some hidden, OS-created files in there +#if defined Q_OS_MAC + QFile::remove(path + QLatin1String("/.DS_Store")); +#elif defined Q_OS_WIN + QFile::remove(path + QLatin1String("/Thumbs.db")); +#endif + errno = 0; const bool success = QDir().rmdir(path); if (errno) @@ -446,8 +453,7 @@ void MkdirOperation::backup() bool MkdirOperation::performOperation() { - // Requires only one parameter. That is the name of - // the file to remove. + // Requires only one parameter. That is the path which should be created QStringList args = this->arguments(); if (args.count() != 1) { setError(InvalidArguments); @@ -467,7 +473,10 @@ bool MkdirOperation::undoOperation() { Q_ASSERT(arguments().count() == 1); - QDir createdDir = QDir(value(QLatin1String("createddir")).toString()); + QString createdDirValue = value(QLatin1String("createddir")).toString(); + if (createdDirValue.isEmpty()) + createdDirValue = arguments().first(); + QDir createdDir = QDir(createdDirValue); const bool forceremoval = QVariant(value(QLatin1String("forceremoval"))).toBool(); // Since refactoring we know the mkdir operation which is creating the target path. If we do a full @@ -482,18 +491,9 @@ bool MkdirOperation::undoOperation() return true; QString errorString; - if (forceremoval) - return removeDirectory(createdDir.path(), &errorString); - // even remove some hidden, OS-created files in there -#if defined Q_OS_MAC - QFile::remove(createdDir.path() + QLatin1String("/.DS_Store")); -#elif defined Q_OS_WIN - QFile::remove(createdDir.path() + QLatin1String("/Thumbs.db")); -#endif + const bool result = removeDirectory(createdDir.path(), &errorString, forceremoval); - errno = 0; - const bool result = QDir::root().rmdir(createdDir.path()); if (!result) { if (errorString.isEmpty()) setError(UserDefinedError, tr("Cannot remove directory %1: %2").arg(createdDir.path(), errorString)); -- cgit v1.2.3