Extend FsUtils

This commit is contained in:
2023-10-11 09:54:44 +02:00
parent 2a49949e0c
commit 31eef79f22
2 changed files with 28 additions and 6 deletions

View File

@@ -10,25 +10,45 @@ namespace fs = std::filesystem;
namespace OpenVulkano namespace OpenVulkano
{ {
bool FsUtils::DeleteEmptyDirs(const std::filesystem::path& dir, bool recursive) bool FsUtils::DeleteEmptyDirs(const std::filesystem::path& dir, const bool recursive)
{ {
bool deleted = false;
for (auto& p: fs::directory_iterator(dir)) for (auto& p: fs::directory_iterator(dir))
{ {
if (fs::is_directory(p)) if (fs::is_directory(p))
{ {
if (recursive) if (recursive)
{ {
if (IsTreeEmpty(p)) if (DeleteEmptyDirs(p), recursive)
{ {
fs::remove_all(p); deleted = true;
} }
} }
else if (fs::is_empty(p)) if (fs::is_empty(p))
{ {
fs::remove(p); fs::remove(p);
deleted = true;
} }
} }
} }
return deleted;
}
bool FsUtils::DeleteEmptySubTrees(const std::filesystem::path& dir)
{
bool deleted = false;
for (auto& p: fs::directory_iterator(dir))
{
if (fs::is_directory(p))
{
if (IsTreeEmpty(p))
{
fs::remove_all(p);
deleted = true;
}
}
}
return deleted;
} }
bool FsUtils::IsTreeEmpty(const std::filesystem::path& dir) bool FsUtils::IsTreeEmpty(const std::filesystem::path& dir)
@@ -44,7 +64,7 @@ namespace OpenVulkano
} }
else else
{ {
return false; // found a file, so the directory tree is not empty return false;
} }
} }
return true; return true;

View File

@@ -10,10 +10,12 @@
namespace OpenVulkano namespace OpenVulkano
{ {
class FsUtils struct FsUtils
{ {
static bool DeleteEmptyDirs(const std::filesystem::path& dir, bool recursive = true); static bool DeleteEmptyDirs(const std::filesystem::path& dir, bool recursive = true);
static bool DeleteEmptySubTrees(const std::filesystem::path& dir);
static bool IsTreeEmpty(const std::filesystem::path& dir); static bool IsTreeEmpty(const std::filesystem::path& dir);
}; };
} }