Add DeleteFilesSmallerThan

This commit is contained in:
Georg Hagen
2025-06-09 10:16:34 +02:00
parent fb504e8c51
commit 19a2b35bd3
2 changed files with 47 additions and 0 deletions

View File

@@ -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;
}
}

View File

@@ -51,6 +51,8 @@ namespace OpenVulkano
static bool DeleteEmptySubTrees(const std::filesystem::path& dir);
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);