From 53fa4743f05062a5275e6187d8266bdbfe9eb3f2 Mon Sep 17 00:00:00 2001 From: Klaas de Waal Date: Sat, 28 Jan 2023 23:54:46 +0100 Subject: [PATCH] Video Gallery: Remove directory structures, e.g. DVD folder The functionality introduced in commit 0d21b2ba434493300f717f3840973f2940e06669 is not changed but the implementation now uses the already existing function MythRemoveDirectory to do the deletion, replacing the local implementations of removeDir. Thanks to linuxdude42 for pointing out this optimization possibility. Refs #176 --- .../requesthandler/deletethread.cpp | 48 ++++--------------- .../requesthandler/deletethread.h | 1 - mythtv/programs/mythbackend/mainserver.cpp | 32 +------------ mythtv/programs/mythbackend/mainserver.h | 1 - 4 files changed, 12 insertions(+), 70 deletions(-) diff --git a/mythtv/libs/libmythprotoserver/requesthandler/deletethread.cpp b/mythtv/libs/libmythprotoserver/requesthandler/deletethread.cpp index 6121bc88959..809b100a503 100644 --- a/mythtv/libs/libmythprotoserver/requesthandler/deletethread.cpp +++ b/mythtv/libs/libmythprotoserver/requesthandler/deletethread.cpp @@ -85,35 +85,6 @@ bool DeleteThread::AddFile(DeleteHandler *handler) return true; } -bool DeleteThread::removeDir(const QString &dirname) -{ - QDir dir(dirname); - - if (!dir.exists()) - return false; - - dir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot); - QFileInfoList list = dir.entryInfoList(); - QFileInfoList::const_iterator it = list.begin(); - const QFileInfo *fi; - - while (it != list.end()) - { - fi = &(*it++); - if (fi->isFile() && !fi->isSymLink()) - { - QFile::remove(fi->absoluteFilePath()); - } - else if (fi->isDir() && !fi->isSymLink()) - { - if(!removeDir(fi->absoluteFilePath())) return false; - } - } - - dir.rmdir(dirname); - return true; -} - void DeleteThread::ProcessNew(void) { // loop through new files, unlinking and adding for deletion @@ -200,15 +171,16 @@ void DeleteThread::ProcessNew(void) { LOG(VB_FILE, LOG_INFO, QString("About to unlink/delete file")); - if(!removeDir(cpath)) - { - LOG(VB_GENERAL, LOG_ERR, - QString("Error deleting '%1': is no directory ") - .arg(cpath) + ENO); - handler->DeleteFailed(); - handler->DecrRef(); - continue; - } + QDir dir(cpath); + if(MythRemoveDirectory(dir)) + { + LOG(VB_GENERAL, LOG_ERR, + QString("Error deleting '%1': is no directory ") + .arg(cpath) + ENO); + handler->DeleteFailed(); + handler->DecrRef(); + continue; + } } // unlink the file so as soon as it is closed, the system will // delete it from the filesystem diff --git a/mythtv/libs/libmythprotoserver/requesthandler/deletethread.h b/mythtv/libs/libmythprotoserver/requesthandler/deletethread.h index 5b655b6306c..df32514d008 100644 --- a/mythtv/libs/libmythprotoserver/requesthandler/deletethread.h +++ b/mythtv/libs/libmythprotoserver/requesthandler/deletethread.h @@ -27,7 +27,6 @@ class DeleteThread : public QObject, public MThread void Stop(void) { m_run = false; } private: - bool removeDir(const QString &dirname); void ProcessNew(void); void ProcessOld(void); diff --git a/mythtv/programs/mythbackend/mainserver.cpp b/mythtv/programs/mythbackend/mainserver.cpp index 3a738fe4d0d..ba4b11e71fc 100644 --- a/mythtv/programs/mythbackend/mainserver.cpp +++ b/mythtv/programs/mythbackend/mainserver.cpp @@ -2659,35 +2659,6 @@ void MainServer::DoDeleteInDB(DeleteStruct *ds) } } -bool MainServer::removeDir(const QString &dirname) -{ - QDir dir(dirname); - - if (!dir.exists()) - return false; - - dir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot); - QFileInfoList list = dir.entryInfoList(); - QFileInfoList::const_iterator it = list.begin(); - const QFileInfo *fi; - - while (it != list.end()) - { - fi = &(*it++); - if (fi->isFile() && !fi->isSymLink()) - { - QFile::remove(fi->absoluteFilePath()); - } - else if (fi->isDir() && !fi->isSymLink()) - { - if(!removeDir(fi->absoluteFilePath())) return false; - } - } - - dir.rmdir(dirname); - return true; -} - /** * \brief Deletes links and unlinks the main file and returns the descriptor. * @@ -2764,7 +2735,8 @@ int MainServer::OpenAndUnlink(const QString &filename) { if (errno == EISDIR) { - if(!removeDir(filename)) + QDir dir(filename); + if(MythRemoveDirectory(dir)) { LOG(VB_GENERAL, LOG_ERR, msg + " could not delete directory " + ENO); return -1; diff --git a/mythtv/programs/mythbackend/mainserver.h b/mythtv/programs/mythbackend/mainserver.h index 43217c0999e..8440bba3dce 100644 --- a/mythtv/programs/mythbackend/mainserver.h +++ b/mythtv/programs/mythbackend/mainserver.h @@ -302,7 +302,6 @@ class MainServer : public QObject, public MythSocketCBs void SetExitCode(int exitCode, bool closeApplication); - static bool removeDir(const QString &dirname); static int DeleteFile(const QString &filename, bool followLinks, bool deleteBrokenSymlinks = false); static int OpenAndUnlink(const QString &filename);