Skip to content

Commit

Permalink
Video Gallery: Remove directory structures, e.g. DVD folder
Browse files Browse the repository at this point in the history
The Video Gallery correctly recognizes and handles DVD folders (Video_TS structure)
but it can not be deleted from within Mythfrontend. This commit adds the functionality
that when pressing delete in the menu the whole DVD folder is deleted.

Thanks to Lomion0815 for contributing this fix.

Refs #176
  • Loading branch information
kmdewaal committed Jan 24, 2023
1 parent c30fa29 commit 0d21b2b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 13 deletions.
49 changes: 41 additions & 8 deletions mythtv/libs/libmythprotoserver/requesthandler/deletethread.cpp
Expand Up @@ -85,6 +85,35 @@ 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;
}

This comment has been minimized.

Copy link
@linuxdude42

linuxdude42 Jan 25, 2023

Contributor

This appears to duplicate the functionality of MythRemoveDirectory from libmythbase/mythmiscutil.cpp.

void DeleteThread::ProcessNew(void)
{
// loop through new files, unlinking and adding for deletion
Expand Down Expand Up @@ -169,17 +198,21 @@ void DeleteThread::ProcessNew(void)
int fd = open(cpath, O_WRONLY);
if (fd == -1)
{
LOG(VB_GENERAL, LOG_ERR,
QString("Error deleting '%1': could not open ")
.arg(handler->m_path) + ENO);
handler->DeleteFailed();
handler->DecrRef();
continue;
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;
}
}

// unlink the file so as soon as it is closed, the system will
// delete it from the filesystem
if (unlink(cpath))
else if (unlink(cpath))
{
LOG(VB_GENERAL, LOG_ERR,
QString("Error deleting '%1': could not unlink ")
Expand Down
Expand Up @@ -27,6 +27,7 @@ class DeleteThread : public QObject, public MThread
void Stop(void) { m_run = false; }

private:
bool removeDir(const QString &dirname);
void ProcessNew(void);
void ProcessOld(void);

Expand Down
49 changes: 44 additions & 5 deletions mythtv/programs/mythbackend/mainserver.cpp
Expand Up @@ -2659,6 +2659,35 @@ 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;
}

This comment has been minimized.

Copy link
@linuxdude42

linuxdude42 Jan 25, 2023

Contributor

This is identical to the other copy of removeDir, and also duplicates the functionality of MythRemoveDirectory from libmythbase/mythmiscutil.cpp.

/**
* \brief Deletes links and unlinks the main file and returns the descriptor.
*
Expand Down Expand Up @@ -2710,7 +2739,7 @@ int MainServer::DeleteFile(const QString &filename, bool followLinks,
return -2; // valid result, not an error condition
}

if (fd < 0)
if (fd < 0 && errno != EISDIR)
LOG(VB_GENERAL, LOG_ERR, LOC + errmsg + ENO);

return fd;
Expand All @@ -2733,11 +2762,21 @@ int MainServer::OpenAndUnlink(const QString &filename)

if (fd == -1)
{
LOG(VB_GENERAL, LOG_ERR, LOC + msg + " could not open " + ENO);
return -1;
if (errno == EISDIR)
{
if(!removeDir(filename))
{
LOG(VB_GENERAL, LOG_ERR, msg + " could not delete directory " + ENO);
return -1;
}
}
else
{
LOG(VB_GENERAL, LOG_ERR, msg + " could not open " + ENO);
return -1;
}
}

if (unlink(fname.constData()))
else if (unlink(fname.constData()))
{
LOG(VB_GENERAL, LOG_ERR, LOC + msg + " could not unlink " + ENO);
close(fd);
Expand Down
1 change: 1 addition & 0 deletions mythtv/programs/mythbackend/mainserver.h
Expand Up @@ -302,6 +302,7 @@ 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);
Expand Down

1 comment on commit 0d21b2b

@kmdewaal
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi David, thanks for your comments. I will have a look at it.

Please sign in to comment.