From 96f07b21dee106da17ddb1a685e2936d4573ce79 Mon Sep 17 00:00:00 2001 From: Richard Frith-Macdonald Date: Fri, 3 Sep 2021 10:17:38 +0100 Subject: [PATCH] Improve deletion of older logs --- ChangeLog | 7 +++++++ EcCommand.m | 52 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5382de0..ebd3148 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2021-09-03 Richard Frith-Macdonald + + * EcCommand.m: When deleting old logs, consider directories from all + time rather than just those for the last year. This prevents logs + from being left undeleted when the DeleteLogsAfter config is changed + from a value longer than a year to a shorter value. + 2021-06-30 Richard Frith-Macdonald * Command.m: diff --git a/EcCommand.m b/EcCommand.m index 6d953ea..9d3e73a 100644 --- a/EcCommand.m +++ b/EcCommand.m @@ -5796,29 +5796,45 @@ NSLog(@"Problem %@", localException); uncompressed = logUncompressed; undeleted = logUndeleted; } - if (0.0 == undeleted) - { - undeleted = now - 365.0 * day; - } - ti = undeleted; latestDeleteAt = now - day * deleteAfter; - while (ti < latestDeleteAt) + if (undeleted < latestDeleteAt) { - NSAutoreleasePool *pool = [NSAutoreleasePool new]; + NSDirectoryEnumerator *enumerator; + NSString *file; + NSCalendarDate *when; + unsigned limit; - when = date(ti); - file = [[dir stringByAppendingPathComponent: - [when descriptionWithCalendarFormat: @"%Y-%m-%d"]] - stringByStandardizingPath]; - if ([mgr fileExistsAtPath: file]) + when = date(latestDeleteAt); + limit = ([when yearOfCommonEra] * 100 + [when monthOfYear]) * 100 + + [when dayOfMonth]; + + enumerator = [mgr enumeratorAtPath: dir]; + while ((file = [enumerator nextObject]) != nil) { - [mgr removeFileAtPath: file handler: nil]; - } - ti += day; - [pool release]; + if ([file length] == 10) + { + const char *s = [file UTF8String]; + unsigned y, m, d; + + if (sscanf(s, "%04u-%02u-%02u", &y, &m, &d) == 3) + { + int dayNumber = (y * 100 + m) * 100 + d; + + if (dayNumber < limit) + { + file = [dir stringByAppendingPathComponent: file]; + if (NO == [mgr removeFileAtPath: file handler: nil]) + { + NSLog(@"Failed to delete old logs at %@", file); + } + } + } + } + } + undeleted = latestDeleteAt; } - if (YES == deb) debUndeleted = ti; - else logUndeleted = ti; + if (YES == deb) debUndeleted = undeleted; + else logUndeleted = undeleted; if (uncompressed < undeleted) {