Add more functions
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "FsUtils.hpp"
|
#include "FsUtils.hpp"
|
||||||
|
#include "Base/Logger.hpp"
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
|
|
||||||
@@ -12,6 +13,34 @@ namespace fs = std::filesystem;
|
|||||||
|
|
||||||
namespace OpenVulkano
|
namespace OpenVulkano
|
||||||
{
|
{
|
||||||
|
bool FsUtils::DeleteEmptyFilesAndDirs(const std::filesystem::path& dir, const bool recursive)
|
||||||
|
{
|
||||||
|
bool deleted = false;
|
||||||
|
for (auto& p: fs::directory_iterator(dir))
|
||||||
|
{
|
||||||
|
if (fs::is_directory(p))
|
||||||
|
{
|
||||||
|
if (recursive)
|
||||||
|
{
|
||||||
|
deleted |= (DeleteEmptyDirs(p), recursive);
|
||||||
|
}
|
||||||
|
if (fs::is_empty(p))
|
||||||
|
{
|
||||||
|
fs::remove(p);
|
||||||
|
Logger::FILESYS->info("Deleted empty directory: {}", p.path().string());
|
||||||
|
deleted = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (fs::is_empty(p))
|
||||||
|
{
|
||||||
|
fs::remove(p);
|
||||||
|
Logger::FILESYS->info("Deleted empty file: {}", p.path().string());
|
||||||
|
deleted = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return deleted;
|
||||||
|
}
|
||||||
|
|
||||||
bool FsUtils::DeleteEmptyDirs(const std::filesystem::path& dir, const bool recursive)
|
bool FsUtils::DeleteEmptyDirs(const std::filesystem::path& dir, const bool recursive)
|
||||||
{
|
{
|
||||||
bool deleted = false;
|
bool deleted = false;
|
||||||
@@ -21,14 +50,12 @@ namespace OpenVulkano
|
|||||||
{
|
{
|
||||||
if (recursive)
|
if (recursive)
|
||||||
{
|
{
|
||||||
if (DeleteEmptyDirs(p), recursive)
|
deleted |= (DeleteEmptyDirs(p), recursive);
|
||||||
{
|
|
||||||
deleted = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (fs::is_empty(p))
|
if (fs::is_empty(p))
|
||||||
{
|
{
|
||||||
fs::remove(p);
|
fs::remove(p);
|
||||||
|
Logger::FILESYS->info("Deleted empty directory: {}", p.path().string());
|
||||||
deleted = true;
|
deleted = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -90,6 +117,24 @@ namespace OpenVulkano
|
|||||||
|
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FsCount FsUtils::GetDirFileAndDirCount(const std::filesystem::path& dir)
|
||||||
|
{
|
||||||
|
FsCount count;
|
||||||
|
for (auto& p: fs::directory_iterator(dir))
|
||||||
|
{
|
||||||
|
if (fs::is_directory(p))
|
||||||
|
{
|
||||||
|
count += GetDirFileAndDirCount(p);
|
||||||
|
count.dirCount++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
count.fileCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
std::string FsUtils::ToValidFileName(std::string str)
|
std::string FsUtils::ToValidFileName(std::string str)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,8 +11,24 @@
|
|||||||
|
|
||||||
namespace OpenVulkano
|
namespace OpenVulkano
|
||||||
{
|
{
|
||||||
|
struct FsCount
|
||||||
|
{
|
||||||
|
size_t fileCount, dirCount;
|
||||||
|
|
||||||
|
size_t GetTotalEntryCount() const { return fileCount + dirCount; }
|
||||||
|
|
||||||
|
FsCount& operator +=(const FsCount& other)
|
||||||
|
{
|
||||||
|
fileCount += other.fileCount;
|
||||||
|
dirCount += other.dirCount;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
struct FsUtils
|
struct FsUtils
|
||||||
{
|
{
|
||||||
|
static bool DeleteEmptyFilesAndDirs(const std::filesystem::path& dir, bool recursive = true);
|
||||||
|
|
||||||
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 DeleteEmptySubTrees(const std::filesystem::path& dir);
|
||||||
@@ -20,6 +36,8 @@ namespace OpenVulkano
|
|||||||
static bool IsTreeEmpty(const std::filesystem::path& dir);
|
static bool IsTreeEmpty(const std::filesystem::path& dir);
|
||||||
|
|
||||||
static ByteSize GetDirSize(const std::filesystem::path& dir);
|
static ByteSize GetDirSize(const std::filesystem::path& dir);
|
||||||
|
|
||||||
|
static FsCount GetDirFileAndDirCount(const std::filesystem::path& dir);
|
||||||
|
|
||||||
static std::string ToValidFileName(std::string filename);
|
static std::string ToValidFileName(std::string filename);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user