Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
#371: better cleanup on cancelling unpack
Browse files Browse the repository at this point in the history
Deleting destination if it contains only hidden files.
  • Loading branch information
hugbug committed May 18, 2017
1 parent f6adbe8 commit 015b3be
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion daemon/postprocess/DirectUnpack.cpp
Expand Up @@ -493,7 +493,7 @@ void DirectUnpack::Cleanup()

if (m_finalDirCreated)
{
FileSystem::RemoveDirectory(m_finalDir);
FileSystem::DeleteDirectory(m_finalDir);
}
}

Expand Down
5 changes: 1 addition & 4 deletions daemon/postprocess/PrePostProcessor.cpp
Expand Up @@ -438,10 +438,7 @@ void PrePostProcessor::DeleteCleanup(NzbInfo* nzbInfo)
}

// delete old directory (if empty)
if (FileSystem::DirEmpty(nzbInfo->GetDestDir()))
{
FileSystem::RemoveDirectory(nzbInfo->GetDestDir());
}
FileSystem::DeleteDirectory(nzbInfo->GetDestDir());
}
}

Expand Down
2 changes: 1 addition & 1 deletion daemon/postprocess/Unpack.cpp
Expand Up @@ -731,7 +731,7 @@ bool UnpackController::Cleanup()

if (!m_unpackOk && m_finalDirCreated)
{
FileSystem::RemoveDirectory(m_finalDir);
FileSystem::DeleteDirectory(m_finalDir);
}

if (m_unpackOk && ok && g_Options->GetUnpackCleanupDisk())
Expand Down
33 changes: 32 additions & 1 deletion daemon/util/FileSystem.cpp
@@ -1,7 +1,7 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2007-2016 Andrey Prygunkov <hugbug@users.sourceforge.net>
* Copyright (C) 2007-2017 Andrey Prygunkov <hugbug@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -573,6 +573,37 @@ bool FileSystem::RemoveDirectory(const char* dirFilename)
#endif
}

/* Delete directory which is empty or contains only hidden files or directories (whose names start with dot) */
bool FileSystem::DeleteDirectory(const char* dirFilename)
{
if (RemoveDirectory(dirFilename))
{
return true;
}

// check if directory contains only hidden files (whose names start with dot)
{
DirBrowser dir(dirFilename);
while (const char* filename = dir.Next())
{
if (*filename != '.')
{
// calling RemoveDirectory to set correct errno
return RemoveDirectory(dirFilename);
}
}
} // make sure "DirBrowser dir" is destroyed (and has closed its handle) before we trying to delete the directory

CString errmsg;
if (!DeleteDirectoryWithContent(dirFilename, errmsg))
{
// calling RemoveDirectory to set correct errno
return RemoveDirectory(dirFilename);
}

return true;
}

bool FileSystem::DeleteDirectoryWithContent(const char* dirFilename, CString& errmsg)
{
errmsg.Clear();
Expand Down
8 changes: 7 additions & 1 deletion daemon/util/FileSystem.h
@@ -1,7 +1,7 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2007-2016 Andrey Prygunkov <hugbug@users.sourceforge.net>
* Copyright (C) 2007-2017 Andrey Prygunkov <hugbug@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -43,7 +43,13 @@ class FileSystem
static bool FileExists(const char* filename);
static bool DirectoryExists(const char* dirFilename);
static bool CreateDirectory(const char* dirFilename);

/* Delete empty directory */
static bool RemoveDirectory(const char* dirFilename);

/* Delete directory which is empty or contains only hidden files or directories */
static bool DeleteDirectory(const char* dirFilename);

static bool DeleteDirectoryWithContent(const char* dirFilename, CString& errmsg);
static bool ForceDirectories(const char* path, CString& errmsg);
static CString GetCurrentDirectory();
Expand Down

0 comments on commit 015b3be

Please sign in to comment.