]> git.ipfire.org Git - pakfire.git/commitdiff
repo: Add checksums for files
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 2 Feb 2025 14:46:52 +0000 (14:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 2 Feb 2025 14:46:52 +0000 (14:46 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/digest.c
src/pakfire/digest.h
src/pakfire/repo.c

index acbe87eecad864c8f8d289cc4a834bb467e344a6..beea8a00f968a01ecc2838a69b553f227a8e51ff 100644 (file)
@@ -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);
index a4789ee998f8da1ec59bfbc27c79f12f67bb7935..0da130130107765db792f281d2efcd6772d09d8b 100644 (file)
@@ -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 <pakfire/ctx.h>
 
@@ -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);
index a5a0753726843fd6088dfebdb09924f072cc711d..fce57b686e8bb23f343d9d4bc37437bb43e6c561 100644 (file)
@@ -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;