]> git.ipfire.org Git - pakfire.git/commitdiff
snapshots: Only use snapshots when they are not older than 24 hrs
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 18:26:17 +0000 (18:26 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 18:26:17 +0000 (18:26 +0000)
The chosen age might be a little bit short, but we can try...

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/snapshot.c

index cfe85b054f309af53b7656a9333da0d072fdab05..5b1b4014fa31f53d2f0231e5e97d24b3c9eec716 100644 (file)
 #include <pakfire/string.h>
 #include <pakfire/util.h>
 
+#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;