]> git.ipfire.org Git - pakfire.git/commitdiff
repo: Do not refresh recently downloaded metadata
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 13 Mar 2021 15:00:16 +0000 (15:00 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 13 Mar 2021 15:00:16 +0000 (15:00 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/util.h
src/libpakfire/repo.c
src/libpakfire/util.c

index 75e6a7e27de54c381e7e8e16dc80259a0700c411..d63f47cc58efc3b24c4364a852dd027318a2959e 100644 (file)
@@ -62,6 +62,7 @@ int pakfire_string_endswith(const char* s, const char* suffix);
 char* pakfire_lstrip(const char* s);
 
 int pakfire_path_exists(const char* path);
+time_t pakfire_path_age(const char* path);
 
 char* pakfire_hexlify(const char* digest, const size_t length);
 
index 702c44e820b1bd73c78eee7d605c4702e7117108..e8e2aa2f273a1bc4357a1d4b7253e955f5024d35 100644 (file)
 #include <pakfire/types.h>
 #include <pakfire/util.h>
 
+// Refresh mirrorlists once a day
+#define REFRESH_AGE_MIRRORLIST                 24 * 3600
+
+// Refresh repository metadata every 10 minutes
+#define REFRESH_AGE_METADATA                   600
+
 const uint8_t XZ_HEADER_MAGIC[] = { 0xFD, '7', 'z', 'X', 'Z', 0x00 };
 const size_t XZ_HEADER_LENGTH = sizeof(XZ_HEADER_MAGIC);
 
@@ -192,6 +198,16 @@ static int pakfire_repo_refresh_mirrorlist(PakfireRepo repo, const int force) {
        if (!mirrorlist_url || !*mirrorlist)
                return 0;
 
+       // Check if this needs to be refreshed
+       if (!force) {
+               time_t age = pakfire_path_age(mirrorlist);
+
+               if (age > 0 && age < REFRESH_AGE_MIRRORLIST) {
+                       DEBUG(repo->pakfire, "Skip refreshing mirrorlist which is %lds old\n", age);
+                       return 0;
+               }
+       }
+
        // Get the downloader
        struct pakfire_downloader* downloader = pakfire_repo_downloader(repo);
        if (!downloader)
@@ -234,6 +250,16 @@ ERROR:
 }
 
 static int pakfire_repo_refresh_metadata(PakfireRepo repo, const int force) {
+       // Check if this needs to be refreshed
+       if (!force) {
+               time_t age = pakfire_path_age(repo->appdata->metadata);
+
+               if (age > 0 && age < REFRESH_AGE_METADATA) {
+                       DEBUG(repo->pakfire, "Skip refreshing metadata which is %lds old\n", age);
+                       return 0;
+               }
+       }
+
        // Get the downloader
        struct pakfire_downloader* downloader = pakfire_repo_downloader(repo);
        if (!downloader)
index f657aa4bb64ff605452b23ba3b53d9979b5a4568..28f5dad526a7463c9eda3135229e8896c4cea658 100644 (file)
@@ -236,6 +236,21 @@ int pakfire_path_exists(const char* path) {
        return !access(path, F_OK);
 }
 
+time_t pakfire_path_age(const char* path) {
+       struct stat st;
+
+       int r = stat(path, &st);
+       if (r == 0) {
+               // Get current timestamp
+               time_t now = time(NULL);
+
+               // Return the difference since the file has been created and now
+               return now - st.st_ctime;
+       }
+
+       return -1;
+}
+
 PAKFIRE_EXPORT int pakfire_path_isdir(const char* path) {
        struct stat s;