From 19a2b35bd35ead67237577ff5e7a8dea4c37007d Mon Sep 17 00:00:00 2001 From: Georg Hagen Date: Mon, 9 Jun 2025 10:16:34 +0200 Subject: [PATCH] Add DeleteFilesSmallerThan --- openVulkanoCpp/IO/FsUtils.cpp | 45 +++++++++++++++++++++++++++++++++++ openVulkanoCpp/IO/FsUtils.hpp | 2 ++ 2 files changed, 47 insertions(+) diff --git a/openVulkanoCpp/IO/FsUtils.cpp b/openVulkanoCpp/IO/FsUtils.cpp index 0490d82..c16cf13 100644 --- a/openVulkanoCpp/IO/FsUtils.cpp +++ b/openVulkanoCpp/IO/FsUtils.cpp @@ -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; + } } diff --git a/openVulkanoCpp/IO/FsUtils.hpp b/openVulkanoCpp/IO/FsUtils.hpp index f56f7fe..4da5364 100644 --- a/openVulkanoCpp/IO/FsUtils.hpp +++ b/openVulkanoCpp/IO/FsUtils.hpp @@ -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);