From 15d3b07f3439906ed558916334e2c2ed625887f6 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Thu, 10 Mar 2011 18:34:28 +0000 Subject: [PATCH] Improve folder removal behavior --- src/misc.cpp | 10 ++++++++++ src/misc.h | 2 ++ src/qtlibtorrent/qbtsession.cpp | 19 +++++++++---------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/misc.cpp b/src/misc.cpp index 85a79927e..bcf528732 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -799,3 +799,13 @@ QString misc::fileName(QString file_path) return file_path; return file_path.mid(slash_index+1); } + +bool misc::removeEmptyFolder(const QString &dirpath) +{ + QDir savedir(dirpath); + const QString dirname = savedir.dirName(); + if(savedir.exists() && savedir.cdUp()) { + return savedir.rmdir(dirname); + } + return false; +} diff --git a/src/misc.h b/src/misc.h index c06b2a65f..e96c2d1f6 100644 --- a/src/misc.h +++ b/src/misc.h @@ -123,6 +123,8 @@ public: return MyFile.remove(); } + static bool removeEmptyFolder(const QString &dirpath); + static quint64 computePathSize(QString path); static QString truncateRootFolder(boost::intrusive_ptr t); diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp index 05a68ca37..69ff5f036 100644 --- a/src/qtlibtorrent/qbtsession.cpp +++ b/src/qtlibtorrent/qbtsession.cpp @@ -2176,22 +2176,21 @@ void QBtSession::readAlerts() { #endif if(!hash.isEmpty()) { if(savePathsToRemove.contains(hash)) { - QDir().rmpath(savePathsToRemove.take(hash)); + const QString dirpath = savePathsToRemove.take(hash); + qDebug() << "Removing save path: " << dirpath << "..."; + bool ok = misc::removeEmptyFolder(dirpath); + Q_UNUSED(ok); + qDebug() << "Folder was removed: " << ok; } } else { - // XXX: Fallback - QStringList hashes_deleted; + // Fallback + qDebug() << "hash is empty, use fallback to remove save path"; foreach(const QString& key, savePathsToRemove.keys()) { // Attempt to delete - QDir().rmpath(savePathsToRemove[key]); - if(!QDir(savePathsToRemove[key]).exists()) { - hashes_deleted << key; + if(misc::removeEmptyFolder(savePathsToRemove[key])) { + savePathsToRemove.remove(key); } } - // Clean up - foreach(const QString& key, hashes_deleted) { - savePathsToRemove.remove(key); - } } } else if (storage_moved_alert* p = dynamic_cast(a.get())) {