impr: Delete old backup files

This commit is contained in:
WerWolv
2023-12-11 21:29:30 +01:00
parent b84b82c416
commit ce27cb11a5
2 changed files with 44 additions and 36 deletions

View File

@@ -1,5 +1,4 @@
#include "init/tasks.hpp"
#include "misc/freetype/imgui_freetype.h"
#include <imgui.h>
@@ -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<std::filesystem::directory_entry> 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<std::filesystem::directory_entry> 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 },
};
}

View File

@@ -270,24 +270,27 @@ namespace hex::plugin::builtin {
}
void ViewAbout::drawPathsPage() {
constexpr static std::array<std::pair<const char *, fs::ImHexPath>, size_t(fs::ImHexPath::END) - 1U> PathTypes = {
constexpr static std::array<std::pair<const char *, fs::ImHexPath>, 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 }
}
};