]> git.ipfire.org Git - pakfire.git/commitdiff
repo: Reset metadata if parsing failed
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 4 Feb 2025 15:23:28 +0000 (15:23 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 4 Feb 2025 15:23:28 +0000 (15:23 +0000)
This is needed in case there were some required fields missing. We then
consider the metadata invalid and won't use it any more.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/repo.c

index ac0bbc0f4b85b4d419fcd7db2b51457e52553abc..ffbca99298d27acc73e7ad90bfb73481f7e8d36a 100644 (file)
@@ -943,6 +943,12 @@ ERROR:
        return r;
 }
 
+static void pakfire_repo_reset_metadata(struct pakfire_repo* self) {
+       // Reset version and revision
+       self->appdata->repomd.version  = 0;
+       self->appdata->repomd.revision = 0;
+}
+
 static int pakfire_repo_parse_repomd(struct pakfire_repo* self,
                struct pakfire_repomd* repomd, struct json_object* root) {
        struct json_object* files = NULL;
@@ -957,7 +963,7 @@ static int pakfire_repo_parse_repomd(struct pakfire_repo* self,
        // Parse version
        r = pakfire_json_get_int64(root, "version", &repomd->version);
        if (r < 0)
-               return r;
+               goto ERROR;
 
        // Check if we support this version
        switch (repomd->version) {
@@ -969,7 +975,8 @@ static int pakfire_repo_parse_repomd(struct pakfire_repo* self,
 
                default:
                        ERROR(self->ctx, "Unsupported version of repository metadata: %ld\n", repomd->version);
-                       return -ENOTSUP;
+                       r = -ENOTSUP;
+                       goto ERROR;
        }
 
        DEBUG(self->ctx, "Parsing repository metadata in version %ld\n", repomd->version);
@@ -977,12 +984,12 @@ static int pakfire_repo_parse_repomd(struct pakfire_repo* self,
        // Parse revision
        r = pakfire_json_get_int64(root, "revision", &repomd->revision);
        if (r < 0)
-               return r;
+               goto ERROR;
 
        // Fetch files
        r = pakfire_json_get_array(root, "files", &files);
        if (r < 0)
-               return r;
+               goto ERROR;
 
        // Iterate over all files
        for (size_t i = 0; i < json_object_array_length(files); i++) {
@@ -991,22 +998,22 @@ static int pakfire_repo_parse_repomd(struct pakfire_repo* self,
                // Read type
                r = pakfire_json_get_string(file, "type", &type);
                if (r < 0)
-                       return r;
+                       goto ERROR;
 
                // Read filename
                r = pakfire_json_get_string(file, "filename", &filename);
                if (r < 0)
-                       return r;
+                       goto ERROR;
 
                // Read size
                r = pakfire_json_get_int64(file, "size", &size);
                if (r < 0)
-                       return r;
+                       goto ERROR;
 
                // Fetch checksums
                r = pakfire_json_get_object(file, "chksums", &chksums);
                if (r < 0)
-                       return r;
+                       goto ERROR;
 
                // Parse checksums
                json_object_object_foreach(chksums, hash_name, hexdigest) {
@@ -1015,7 +1022,7 @@ static int pakfire_repo_parse_repomd(struct pakfire_repo* self,
                        // Import the hexdigest
                        r = pakfire_hashes_set_hex(&hashes, hash_type, json_object_get_string(hexdigest));
                        if (r < 0)
-                               return r;
+                               goto ERROR;
                }
 
                // We found the packages database
@@ -1023,7 +1030,7 @@ static int pakfire_repo_parse_repomd(struct pakfire_repo* self,
                        // Store the filename
                        r = pakfire_string_set(repomd->packages.path, filename);
                        if (r < 0)
-                               return r;
+                               goto ERROR;
 
                        // Store the filelist
                        repomd->packages.size = size;
@@ -1031,7 +1038,7 @@ static int pakfire_repo_parse_repomd(struct pakfire_repo* self,
                        // Store the hashes
                        r = pakfire_hashes_import(&repomd->packages.hashes, &hashes);
                        if (r < 0)
-                               return r;
+                               goto ERROR;
 
                        // Since we currently only know this one file, we can stop once we found it
                        break;
@@ -1042,6 +1049,12 @@ static int pakfire_repo_parse_repomd(struct pakfire_repo* self,
        }
 
        return 0;
+
+ERROR:
+       // If the entire metadata could not be parsed, we reset everything
+       pakfire_repo_reset_metadata(self);
+
+       return r;
 }
 
 static int pakfire_repo_read_metadata(struct pakfire_repo* repo, const char* path) {