Add DeleteFilesSmallerThan
This commit is contained in:
@@ -259,4 +259,49 @@ namespace OpenVulkano
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t FsUtils::DeleteFilesSmallerThan(const std::filesystem::path& dir, const size_t minSize, const bool recursive) try
|
||||
{
|
||||
if (!fs::exists(dir) || !fs::is_directory(dir))
|
||||
{
|
||||
Logger::FILESYS->error("Directory does not exist or is not a directory: {}", dir);
|
||||
return 0;
|
||||
}
|
||||
size_t count = 0;
|
||||
for (const auto& entry : fs::directory_iterator(dir))
|
||||
{
|
||||
if (entry.is_regular_file())
|
||||
{
|
||||
try
|
||||
{
|
||||
std::uintmax_t fileSize = fs::file_size(entry);
|
||||
if (fileSize < minSize)
|
||||
{
|
||||
if (fs::remove(entry.path())) count++;
|
||||
else Logger::FILESYS->error("Failed to delete: {}", entry.path());
|
||||
}
|
||||
}
|
||||
catch (const fs::filesystem_error& ex)
|
||||
{
|
||||
Logger::FILESYS->error("Error accessing file {}: {}", entry.path(), ex.what());
|
||||
}
|
||||
}
|
||||
else if (recursive && entry.is_directory())
|
||||
{
|
||||
count += DeleteFilesSmallerThan(entry.path(), minSize, recursive);
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
catch (const fs::filesystem_error& ex)
|
||||
{
|
||||
Logger::FILESYS->error("Filesystem error: {}", ex.what());
|
||||
return 0;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
Logger::FILESYS->error("Error: {}", ex.what());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +52,8 @@ namespace OpenVulkano
|
||||
|
||||
static bool IsTreeEmpty(const std::filesystem::path& dir);
|
||||
|
||||
static size_t DeleteFilesSmallerThan(const std::filesystem::path& dir, size_t minSize, bool recursive = false);
|
||||
|
||||
[[nodiscard]] static ByteSize GetDirSize(const std::filesystem::path& dir);
|
||||
|
||||
[[nodiscard]] static FsCount GetDirFileAndDirCount(const std::filesystem::path& dir);
|
||||
|
||||
Reference in New Issue
Block a user