]> git.ipfire.org Git - pakfire.git/commitdiff
repo: Store the downloaded metadata if it is more recent
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 3 Feb 2025 14:00:59 +0000 (14:00 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 3 Feb 2025 14:00:59 +0000 (14:00 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/json.c
src/pakfire/json.h
src/pakfire/repo.c

index f54f4ae1a435d34622a9633b625ef459a01cd592..ee3177eb806af34bdc44086d48b432d7055e9ce9 100644 (file)
@@ -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;
+}
index dcd6df4785228468b89a9d3b77f196605b97a843..7ea0886e2a0fbdefaa933cfea9df56fa157a7c95 100644 (file)
@@ -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 */
index 41aef5d75a669a178e29a4bc9aaa8ceac7ccf43b..3ea4837076a6f7b81e417ee12d7bfa66f63db1bf 100644 (file)
@@ -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);