Skip to content

Commit e7ddb4d

Browse files
committed
fix fs exceptions in deleteOldLogs
1 parent 1e98811 commit e7ddb4d

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

loader/src/loader/Log.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,22 +313,31 @@ void Logger::deleteOldLogs(size_t maxAgeHours) {
313313

314314
std::error_code ec;
315315
auto iterator = std::filesystem::directory_iterator(logDir, ec);
316+
std::filesystem::directory_iterator end;
317+
316318
if (ec != std::error_code{}) {
317319
log::error("Failed to delete old logs: {}", ec.message());
318320
return;
319321
}
320322

321-
for (auto const& entry : iterator) {
322-
if (entry.is_regular_file() && entry.path().extension() == ".log") {
323+
while (iterator != end) {
324+
auto& entry = *iterator;
325+
if (entry.is_regular_file(ec) && entry.path().extension() == ".log") {
323326
auto time = std::filesystem::last_write_time(entry, ec);
324-
if (ec != std::error_code{}) {
325-
continue;
327+
if (ec == std::error_code{}) {
328+
auto diff = now - time;
329+
if (diff > std::chrono::hours(maxAgeHours)) {
330+
std::filesystem::remove(entry, ec);
331+
}
326332
}
327333

328-
auto diff = now - time;
329-
if (diff > std::chrono::hours(maxAgeHours)) {
330-
std::filesystem::remove(entry, ec);
331-
}
334+
}
335+
336+
ec.clear();
337+
iterator.increment(ec);
338+
if (ec != std::error_code{}) {
339+
log::error("Failed to iterate log directory: {}", ec.message());
340+
return;
332341
}
333342
}
334343
}

0 commit comments

Comments
 (0)