From f44b6159302272f4b539b41dfe7b40df5d7cb368 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 2 Feb 2025 14:46:52 +0000 Subject: [PATCH] repo: Add checksums for files Signed-off-by: Michael Tremer --- src/pakfire/digest.c | 20 ++++++++++++++++++++ src/pakfire/digest.h | 14 +++++++++++++- src/pakfire/repo.c | 44 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/pakfire/digest.c b/src/pakfire/digest.c index acbe87ee..beea8a00 100644 --- a/src/pakfire/digest.c +++ b/src/pakfire/digest.c @@ -468,6 +468,26 @@ ERROR: return r; } +int pakfire_digests_compute_from_path(struct pakfire_ctx* ctx, + struct pakfire_digests* digests, int types, const char* path) { + FILE* f = NULL; + int r; + + // Open the file + f = fopen(path, "r"); + if (!f) + return -errno; + + // Compute the digests + r = pakfire_digests_compute_from_file(ctx, digests, types, f); + + // Close the file + if (f) + fclose(f); + + return r; +} + static void pakfire_digests_compare_mismatch(struct pakfire_ctx* ctx, const char* what, const unsigned char* digest1, const unsigned char* digest2, const size_t length) { char* hexdigest1 = __pakfire_hexlify(digest1, length); diff --git a/src/pakfire/digest.h b/src/pakfire/digest.h index a4789ee9..0da13013 100644 --- a/src/pakfire/digest.h +++ b/src/pakfire/digest.h @@ -37,7 +37,17 @@ enum pakfire_digest_types { PAKFIRE_DIGEST_SHA3_512 = (1 << 5), }; -#define PAKFIRE_DIGESTS_ALL (~PAKFIRE_DIGEST_UNDEFINED) +#define PAKFIRE_DIGESTS_ALL ( \ + PAKFIRE_DIGEST_SHA2_256 | \ + PAKFIRE_DIGEST_SHA2_512 | \ + PAKFIRE_DIGEST_BLAKE2S256 | \ + PAKFIRE_DIGEST_BLAKE2B512 | \ + PAKFIRE_DIGEST_SHA3_256 | \ + PAKFIRE_DIGEST_SHA3_512 \ +) + +#define PAKFIRE_DIGESTS_FOREACH(digest) \ + for (digest = 1; digest & PAKFIRE_DIGESTS_ALL; digest <<= 1) #include @@ -94,6 +104,8 @@ void pakfire_digests_reset(struct pakfire_digests* digests, int types); int pakfire_digests_compute_from_file(struct pakfire_ctx* ctx, struct pakfire_digests* digests, int types, FILE* f); +int pakfire_digests_compute_from_path(struct pakfire_ctx* ctx, + struct pakfire_digests* digests, int types, const char* path); int pakfire_digests_compare(struct pakfire_ctx* ctx, const struct pakfire_digests* digests1, const struct pakfire_digests* digests2, const int types); diff --git a/src/pakfire/repo.c b/src/pakfire/repo.c index a5a07537..fce57b68 100644 --- a/src/pakfire/repo.c +++ b/src/pakfire/repo.c @@ -1965,12 +1965,29 @@ int pakfire_repo_refresh(struct pakfire_repo* repo, int force) { return 0; } -static int pakfire_repo_metadata_add_file( - struct pakfire_repo* self, struct json_object* repomd, const char* filename) { +static int pakfire_repo_metadata_add_file(struct pakfire_repo* self, + struct json_object* repomd, const char* path) { + struct pakfire_digests digests = {}; struct json_object* files = NULL; struct json_object* file = NULL; + struct json_object* chksums = NULL; + char filename[PATH_MAX]; + char* hexdigest = NULL; + int digest; int r; + // Make the filename + r = pakfire_path_basename(filename, path); + if (r < 0) + goto ERROR; + + // Compute the database digests + r = pakfire_digests_compute_from_path(self->ctx, &digests, PAKFIRE_DIGESTS_ALL, path); + if (r < 0) { + ERROR(self->ctx, "Failed to compute file digests: %s\n", strerror(-r)); + goto ERROR; + } + // Fetch the files array files = json_object_object_get(repomd, "files"); if (!files) { @@ -1991,6 +2008,25 @@ static int pakfire_repo_metadata_add_file( if (r < 0) goto ERROR; + // Add checksums + r = pakfire_json_add_object(file, "chksums", &chksums); + if (r < 0) + goto ERROR; + + // Add all digests + PAKFIRE_DIGESTS_FOREACH(digest) { + hexdigest = pakfire_digest_get_hex(&digests, digest); + + if (hexdigest) { + r = pakfire_json_add_string(chksums, pakfire_digest_name(digest), hexdigest); + if (r < 0) + goto ERROR; + + free(hexdigest); + hexdigest = NULL; + } + } + // Append it to the files array r = json_object_array_add(files, file); if (r < 0) { @@ -2003,6 +2039,8 @@ static int pakfire_repo_metadata_add_file( ERROR: if (file) json_object_put(file); + if (hexdigest) + free(hexdigest); return r; } @@ -2066,7 +2104,7 @@ static int pakfire_repo_write_database(struct pakfire_repo* self, } // Add the database to the filelist - r = pakfire_repo_metadata_add_file(self, repomd, filename); + r = pakfire_repo_metadata_add_file(self, repomd, database); if (r < 0) goto ERROR; -- 2.39.5