From: Michael Tremer Date: Tue, 4 Feb 2025 15:23:28 +0000 (+0000) Subject: repo: Reset metadata if parsing failed X-Git-Tag: 0.9.30~148 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1cd0f9b868487f3e29ccbf16a24d63e17c6117b8;p=pakfire.git repo: Reset metadata if parsing failed 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 --- diff --git a/src/pakfire/repo.c b/src/pakfire/repo.c index ac0bbc0f..ffbca992 100644 --- a/src/pakfire/repo.c +++ b/src/pakfire/repo.c @@ -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) {