From a4e4e01d2dd732f2e9070ee7ca5c90a140e7ea02 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 24 Jul 2023 15:36:29 +0200 Subject: [PATCH] fix: Crash when cleaning log files that are open in another program --- main/source/init/tasks.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/main/source/init/tasks.cpp b/main/source/init/tasks.cpp index 60c958c21..579d27c43 100644 --- a/main/source/init/tasks.cpp +++ b/main/source/init/tasks.cpp @@ -521,18 +521,23 @@ namespace hex::init { for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Logs)) { std::vector files; - for (const auto& file : std::filesystem::directory_iterator(path)) - files.push_back(file); + try { + for (const auto& file : std::filesystem::directory_iterator(path)) + files.push_back(file); - if (files.size() <= 10) - return true; + if (files.size() <= 10) + return true; - 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); - }); + 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() + 10; it != files.end(); it++) - std::filesystem::remove(it->path()); + for (auto it = files.begin() + 10; it != files.end(); it++) + std::filesystem::remove(it->path()); + } catch (std::filesystem::filesystem_error &e) { + log::error("Failed to clear old log! {}", e.what()); + continue; + } } return true;