From eeacea01e00ea1e42a41eb1896349d97bd4142e2 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 31 Aug 2023 05:38:51 +0000 Subject: [PATCH] repo: Allow storing custom refresh times for metadata Fixes: #13248 - repo: Build TTL for metadata Signed-off-by: Michael Tremer --- src/libpakfire/repo.c | 53 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index 77ca4aa67..11e2a65f1 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -58,6 +58,9 @@ struct pakfire_repo_appdata { char* description; char* baseurl; + // Refresh Interval + time_t refresh; + // Key char* key; @@ -252,6 +255,11 @@ int pakfire_repo_import(struct pakfire* pakfire, struct pakfire_config* config) if (baseurl) pakfire_repo_set_baseurl(repo, baseurl); + // Refresh Interval + const char* refresh = pakfire_config_get(config, *section, "refresh", NULL); + if (refresh) + repo->appdata->refresh = pakfire_string_parse_interval(refresh); + // mirrors const char* mirrors = pakfire_config_get(config, *section, "mirrors", NULL); if (mirrors) @@ -480,12 +488,23 @@ static int pakfire_repo_refresh_mirrorlist(struct pakfire_repo* repo, const int 0, NULL, 0, PAKFIRE_TRANSFER_NOPROGRESS); } -static int pakfire_repo_download_metadata(struct pakfire_repo* repo, const char* path, const int force) { +static int pakfire_repo_download_metadata(struct pakfire_repo* repo, const char* path, int force) { + // Fetch refresh interval + time_t refresh = repo->appdata->refresh; + + // Use a default value if unset + if (refresh < 0) + refresh = REFRESH_AGE_METADATA; + + // If the refresh interval is set to zero, we will always force a refresh + if (refresh == 0) + force = 1; + // Check if this needs to be refreshed if (!force) { time_t age = pakfire_path_age(path); - if (age > 0 && age < REFRESH_AGE_METADATA) { + if (age > 0 && age < refresh) { DEBUG(repo->pakfire, "Skip refreshing metadata which is %lds old\n", age); return 0; } @@ -561,6 +580,20 @@ void pakfire_repo_free_all(struct pakfire* pakfire) { } } +static struct pakfire_repo_appdata* pakfire_repo_setup_appdata(struct pakfire_repo* repo) { + struct pakfire_repo_appdata* appdata = NULL; + + // Allocate appdata + appdata = calloc(1, sizeof(*repo->appdata)); + if (!appdata) + return NULL; + + // Refresh Interval: Set to invalid + appdata->refresh = -1; + + return appdata; +} + PAKFIRE_EXPORT int pakfire_repo_create(struct pakfire_repo** repo, struct pakfire* pakfire, const char* name) { int r = 1; @@ -591,9 +624,9 @@ PAKFIRE_EXPORT int pakfire_repo_create(struct pakfire_repo** repo, } // Allocate repository appdata - rep->appdata = rep->repo->appdata = calloc(1, sizeof(*rep->appdata)); + rep->appdata = rep->repo->appdata = pakfire_repo_setup_appdata(rep); if (!rep->appdata) { - ERROR(rep->pakfire, "Could not allocate repo appdata\n"); + ERROR(rep->pakfire, "Could not setup repository appdata for %s\n", name); goto ERROR; } @@ -904,6 +937,18 @@ PAKFIRE_EXPORT int pakfire_repo_write_config(struct pakfire_repo* repo, FILE* f) } } + // Refresh Interval + char* refresh = pakfire_string_format_interval(repo->appdata->refresh); + if (refresh) { + r = pakfire_config_set(config, section, "refresh", refresh); + free(refresh); + + if (r) { + ERROR(repo->pakfire, "Could not set refresh interval: %m\n"); + goto ERROR; + } + } + // Mirror List const char* mirrorlist = pakfire_repo_get_mirrorlist_url(repo); if (mirrorlist) { -- 2.39.5