1
0
mirror of synced 2024-09-24 11:38:26 +02:00

fix: Crash when cleaning log files that are open in another program

This commit is contained in:
WerWolv 2023-07-24 15:36:29 +02:00
parent 8b3cd2d76d
commit a4e4e01d2d

View File

@ -521,18 +521,23 @@ namespace hex::init {
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Logs)) {
std::vector<std::filesystem::directory_entry> 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;