#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;
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;
}
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;