From ce27cb11a52e7548a21951c68d85f00617417514 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 11 Dec 2023 21:29:30 +0100 Subject: [PATCH] impr: Delete old backup files --- main/gui/source/init/tasks.cpp | 43 +++++++++++-------- .../source/content/views/view_about.cpp | 37 ++++++++-------- 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/main/gui/source/init/tasks.cpp b/main/gui/source/init/tasks.cpp index e91f8846b..d15e66ff0 100644 --- a/main/gui/source/init/tasks.cpp +++ b/main/gui/source/init/tasks.cpp @@ -1,5 +1,4 @@ #include "init/tasks.hpp" -#include "misc/freetype/imgui_freetype.h" #include @@ -246,29 +245,35 @@ namespace hex::init { return true; } - bool clearOldLogs() { + bool deleteOldFiles() { bool result = true; - for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Logs)) { - try { - std::vector files; - for (const auto& file : std::filesystem::directory_iterator(path)) - files.push_back(file); + auto keepNewest = [&](u32 count, fs::ImHexPath pathType) { + for (const auto &path : fs::getDefaultPaths(pathType)) { + try { + std::vector files; - if (files.size() <= 10) - return true; + for (const auto& file : std::filesystem::directory_iterator(path)) + files.push_back(file); - std::sort(files.begin(), files.end(), [](const auto& a, const auto& b) { - return std::filesystem::last_write_time(a) > std::filesystem::last_write_time(b); - }); + if (files.size() <= count) + return; - for (auto it = files.begin() + 10; it != files.end(); it += 1) - std::filesystem::remove(it->path()); - } catch (std::filesystem::filesystem_error &e) { - log::error("Failed to clear old log! {}", e.what()); - result = false; + std::sort(files.begin(), files.end(), [](const auto& a, const auto& b) { + return std::filesystem::last_write_time(a) > std::filesystem::last_write_time(b); + }); + + for (auto it = files.begin() + count; it != files.end(); it += 1) + std::filesystem::remove(it->path()); + } catch (std::filesystem::filesystem_error &e) { + log::error("Failed to clear old file! {}", e.what()); + result = false; + } } - } + }; + + keepNewest(10, fs::ImHexPath::Logs); + keepNewest(25, fs::ImHexPath::Backups); return result; } @@ -329,7 +334,7 @@ namespace hex::init { { "Saving settings", storeSettings, false }, { "Cleaning up shared data", deleteSharedData, false }, { "Unloading plugins", unloadPlugins, false }, - { "Clearing old logs", clearOldLogs, false }, + { "Deleting old files", deleteOldFiles, false }, }; } diff --git a/plugins/builtin/source/content/views/view_about.cpp b/plugins/builtin/source/content/views/view_about.cpp index ddad932e3..f94da7f73 100644 --- a/plugins/builtin/source/content/views/view_about.cpp +++ b/plugins/builtin/source/content/views/view_about.cpp @@ -270,24 +270,27 @@ namespace hex::plugin::builtin { } void ViewAbout::drawPathsPage() { - constexpr static std::array, size_t(fs::ImHexPath::END) - 1U> PathTypes = { + constexpr static std::array, size_t(fs::ImHexPath::END)> PathTypes = { { - { "Patterns", fs::ImHexPath::Patterns }, - { "Patterns Includes", fs::ImHexPath::PatternsInclude }, - { "Magic", fs::ImHexPath::Magic }, - { "Plugins", fs::ImHexPath::Plugins }, - { "Libraries", fs::ImHexPath::Libraries }, - { "Yara Patterns", fs::ImHexPath::Yara }, - { "Config", fs::ImHexPath::Config }, - { "Resources", fs::ImHexPath::Resources }, - { "Constants lists", fs::ImHexPath::Constants }, - { "Custom encodings", fs::ImHexPath::Encodings }, - { "Logs", fs::ImHexPath::Logs }, - { "Recent files", fs::ImHexPath::Recent }, - { "Scripts", fs::ImHexPath::Scripts }, - { "Themes", fs::ImHexPath::Themes }, - { "Data inspector scripts", fs::ImHexPath::Inspectors }, - { "Custom data processor nodes", fs::ImHexPath::Nodes }, + { "Patterns", fs::ImHexPath::Patterns }, + { "Patterns Includes", fs::ImHexPath::PatternsInclude }, + { "Magic", fs::ImHexPath::Magic }, + { "Plugins", fs::ImHexPath::Plugins }, + { "Libraries", fs::ImHexPath::Libraries }, + { "Yara Patterns", fs::ImHexPath::Yara }, + { "Config", fs::ImHexPath::Config }, + { "Resources", fs::ImHexPath::Resources }, + { "Constants lists", fs::ImHexPath::Constants }, + { "Custom encodings", fs::ImHexPath::Encodings }, + { "Logs", fs::ImHexPath::Logs }, + { "Recent files", fs::ImHexPath::Recent }, + { "Scripts", fs::ImHexPath::Scripts }, + { "Themes", fs::ImHexPath::Themes }, + { "Layouts", fs::ImHexPath::Layouts }, + { "Workspaces", fs::ImHexPath::Workspaces }, + { "Backups", fs::ImHexPath::Backups }, + { "Data inspector scripts", fs::ImHexPath::Inspectors }, + { "Custom data processor nodes", fs::ImHexPath::Nodes } } };