From: Michael Tremer Date: Sat, 11 Jan 2025 18:26:17 +0000 (+0000) Subject: snapshots: Only use snapshots when they are not older than 24 hrs X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=65783f84ffca9fcfb10c31e0d3286f9353a83bc1;p=people%2Fric9%2Fpakfire.git snapshots: Only use snapshots when they are not older than 24 hrs The chosen age might be a little bit short, but we can try... Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/snapshot.c b/src/pakfire/snapshot.c index cfe85b054..5b1b4014f 100644 --- a/src/pakfire/snapshot.c +++ b/src/pakfire/snapshot.c @@ -34,6 +34,11 @@ #include #include +#define PAKFIRE_SNAPSHOT_TIMESTAMP_FORMAT "%Y-%m-%d-%H%M%S" + +// The time after which a snapshot will be refreshed +#define PAKFIRE_SNAPSHOT_MAX_AGE 86400 // 24 hrs + struct pakfire_snapshot { struct pakfire_ctx* ctx; int nrefs; @@ -131,11 +136,50 @@ struct pakfire_snapshot* pakfire_snapshot_unref(struct pakfire_snapshot* snapsho return NULL; } +static int pakfire_snapshot_parse_timestamp(const char* name, time_t* t) { + struct tm brokentime = {}; + + // Parse the string + char* p = strptime(name, PAKFIRE_SNAPSHOT_TIMESTAMP_FORMAT, &brokentime); + if (!p) + return -errno; + + // If p is not pointing to the end of the string, + // which means that strptime could not parse the entire string. + else if (*p) + return -EINVAL; + + // Convert time into time_t + *t = mktime(&brokentime); + if (*t < 0) + return -errno; + + return 0; +} + static int pakfire_snapshot_filter(const struct dirent* dirent) { + time_t t = -1; + time_t now; + int r; + // Skip any hidden directories if (*dirent->d_name == '.') return 0; + // Parse the timestamp + r = pakfire_snapshot_parse_timestamp(dirent->d_name, &t); + if (r < 0) + return r; + + // Fetch the current time + now = time(NULL); + if (now < 0) + return -errno; + + // Ignore snapshots that are too old + if (now - t >= PAKFIRE_SNAPSHOT_MAX_AGE) + return 0; + // Select the rest return 1; } @@ -468,7 +512,7 @@ int pakfire_snapshot_make(struct pakfire_snapshot** snapshot, struct pakfire* pa struct pakfire_ctx* ctx = pakfire_ctx(pakfire); // Store the current time - r = pakfire_strftime_now(time, "%Y-%m-%d-%H%M%S"); + r = pakfire_strftime_now(time, PAKFIRE_SNAPSHOT_TIMESTAMP_FORMAT); if (r < 0) goto ERROR;