fix: Crash when cleaning log files that are open in another program
This commit is contained in:
parent
8b3cd2d76d
commit
a4e4e01d2d
@ -521,18 +521,23 @@ namespace hex::init {
|
|||||||
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Logs)) {
|
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Logs)) {
|
||||||
std::vector<std::filesystem::directory_entry> files;
|
std::vector<std::filesystem::directory_entry> files;
|
||||||
|
|
||||||
for (const auto& file : std::filesystem::directory_iterator(path))
|
try {
|
||||||
files.push_back(file);
|
for (const auto& file : std::filesystem::directory_iterator(path))
|
||||||
|
files.push_back(file);
|
||||||
|
|
||||||
if (files.size() <= 10)
|
if (files.size() <= 10)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
std::sort(files.begin(), files.end(), [](const auto& a, const auto& 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);
|
return std::filesystem::last_write_time(a) > std::filesystem::last_write_time(b);
|
||||||
});
|
});
|
||||||
|
|
||||||
for (auto it = files.begin() + 10; it != files.end(); it++)
|
for (auto it = files.begin() + 10; it != files.end(); it++)
|
||||||
std::filesystem::remove(it->path());
|
std::filesystem::remove(it->path());
|
||||||
|
} catch (std::filesystem::filesystem_error &e) {
|
||||||
|
log::error("Failed to clear old log! {}", e.what());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user