From 467fc12440e4cf99d1471664581c6e97f6089390 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Mon, 3 Feb 2025 14:00:59 +0000 Subject: [PATCH] repo: Store the downloaded metadata if it is more recent Signed-off-by: Michael Tremer --- src/pakfire/json.c | 23 +++++++++++++++++++++++ src/pakfire/json.h | 2 ++ src/pakfire/repo.c | 23 +++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/src/pakfire/json.c b/src/pakfire/json.c index f54f4ae1..ee3177eb 100644 --- a/src/pakfire/json.c +++ b/src/pakfire/json.c @@ -271,3 +271,26 @@ int pakfire_json_get_object(struct json_object* json, const char* key, struct js int pakfire_json_get_array(struct json_object* json, const char* key, struct json_object** array) { return __pakfire_json_get_object(json, key, json_type_array, array); } + +int pakfire_json_write(struct json_object* json, const char* path) { + FILE* f = NULL; + int r; + + // Open the file for writing + f = fopen(path, "w"); + if (!f) + return -errno; + + // Write the object to file + r = json_object_to_fd(fileno(f), json, 0); + if (r < 0) { + r = -errno; + goto ERROR; + } + +ERROR: + if (f) + fclose(f); + + return r; +} diff --git a/src/pakfire/json.h b/src/pakfire/json.h index dcd6df47..7ea0886e 100644 --- a/src/pakfire/json.h +++ b/src/pakfire/json.h @@ -50,4 +50,6 @@ int pakfire_json_get_int64(struct json_object* json, const char* key, int64_t* v int pakfire_json_get_object(struct json_object* json, const char* key, struct json_object** object); int pakfire_json_get_array(struct json_object* json, const char* key, struct json_object** array); +int pakfire_json_write(struct json_object* json, const char* path); + #endif /* PAKFIRE_JSON_H */ diff --git a/src/pakfire/repo.c b/src/pakfire/repo.c index 41aef5d7..3ea48370 100644 --- a/src/pakfire/repo.c +++ b/src/pakfire/repo.c @@ -1021,6 +1021,29 @@ static int pakfire_repo_download_metadata(struct pakfire_repo* repo, const char* if (r < 0) goto ERROR; + // Check if the metadata is more recent than what we had + if (repo->appdata->repomd.revision > repomd.revision) { + WARN(repo->ctx, "Downloaded metadata is older than what we have. Ignoring.\n"); + + // If we have downloaded more recent data, we want to import it + } else if (repo->appdata->repomd.revision < repomd.revision) { + // Copy the object + memcpy(&repo->appdata->repomd, &repomd, sizeof(repo->appdata->repomd)); + + // Ensure the parent directory exists + r = pakfire_mkparentdir(path, 0755); + if (r < 0) + goto ERROR; + + // Write the metadata to disk for next time + r = pakfire_json_write(json, path); + if (r < 0) { + ERROR(repo->ctx, "Failed to store repository metadata in %s: %s\n", + path, strerror(-r)); + goto ERROR; + } + } + ERROR: if (xfer) pakfire_xfer_unref(xfer); -- 2.39.5