]> git.ipfire.org Git - pakfire.git/commitdiff
packager: Compute digests when packaging files
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 24 Aug 2022 17:28:31 +0000 (17:28 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 24 Aug 2022 17:28:31 +0000 (17:28 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/digest.c
src/libpakfire/file.c
src/libpakfire/include/pakfire/digest.h
src/libpakfire/include/pakfire/file.h
src/libpakfire/packager.c

index 69800aca7beca765b08ed3755f4abe549af3019c..00ef6d2716ec5614577659966861865f10ec5d38 100644 (file)
@@ -57,6 +57,19 @@ int __pakfire_digest_set(const unsigned char* digest, const size_t length) {
        return 0;
 }
 
+void pakfire_digests_reset(struct pakfire_digests* digests, int types) {
+       if (!types)
+               types = ~types;
+
+       // Reset SHA-512
+       if (types & PAKFIRE_DIGEST_SHA512)
+               memset(digests->sha512, 0, sizeof(digests->sha512));
+
+       // Reset SHA-256
+       if (types & PAKFIRE_DIGEST_SHA256)
+               memset(digests->sha256, 0, sizeof(digests->sha256));
+}
+
 static int pakfire_digests_check_length(struct pakfire* pakfire,
                const enum pakfire_digest_types type, const size_t length) {
        const size_t l = pakfire_digest_length(type);
index 6a5410de97a58780374c4af774b99264ab8b1c75..0f439b6b572700e2b476ae02dae1d1c237789f87 100644 (file)
@@ -75,6 +75,7 @@ struct pakfire_file {
 
        // Digests
        struct pakfire_digests digests;
+       unsigned int digests_computed:1;
 
        // Verification Status
        int verify_status;
@@ -550,6 +551,53 @@ FILE* pakfire_file_open(struct pakfire_file* file) {
        return f;
 }
 
+static int __pakfire_file_compute_digests(struct pakfire_file* file,
+               struct pakfire_digests* digests, const int types) {
+       FILE* f = NULL;
+       int r = 1;
+
+       // Skip this for anything that isn't a regular file
+       if (!S_ISREG(file->st.st_mode))
+               return 0;
+
+       // Reset digests
+       pakfire_digests_reset(digests, types);
+
+       // Open the file
+       f = pakfire_file_open(file);
+       if (!f)
+               goto ERROR;
+
+       // Compute digests
+       r = pakfire_digests_compute_from_file(file->pakfire, digests, types, f);
+       if (r)
+               goto ERROR;
+
+ERROR:
+       if (f)
+               fclose(f);
+
+       return r;
+}
+
+int pakfire_file_compute_digests(struct pakfire_file* file, const int types) {
+       int r;
+
+       // Don't recompute once done
+       if (file->digests_computed)
+               return 0;
+
+       // Compute digests
+       r = __pakfire_file_compute_digests(file, &file->digests, types);
+       if (r)
+               return r;
+
+       // Mark as computed
+       file->digests_computed = 1;
+
+       return 0;
+}
+
 int pakfire_file_remove(struct pakfire_file* file) {
        if (!*file->abspath) {
                errno = EINVAL;
@@ -718,7 +766,6 @@ static int pakfire_file_verify_timestamps(struct pakfire_file* file, const struc
 }
 
 static int pakfire_file_verify_payload(struct pakfire_file* file, const struct stat* st) {
-       FILE* f = NULL;
        int r;
 
        struct pakfire_digests computed_digests;
@@ -746,15 +793,8 @@ static int pakfire_file_verify_payload(struct pakfire_file* file, const struct s
                return 0;
        }
 
-       // Open the file
-       f = pakfire_file_open(file);
-       if (!f) {
-               ERROR(file->pakfire, "Could not open %s: %m\n", file->path);
-               goto ERROR;
-       }
-
        // Compute digests
-       r = pakfire_digests_compute_from_file(file->pakfire, &computed_digests, digest_types, f);
+       r = __pakfire_file_compute_digests(file, &computed_digests, digest_types);
        if (r)
                goto ERROR;
 
@@ -767,9 +807,6 @@ static int pakfire_file_verify_payload(struct pakfire_file* file, const struct s
        }
 
 ERROR:
-       if (f)
-               fclose(f);
-
        return r;
 }
 
index 2af4451544f69a693c638f10d0f9631ca0facb7c..82adb7f112fb7674e667c59d62d8c023aefbecda 100644 (file)
@@ -28,6 +28,8 @@ enum pakfire_digest_types {
        PAKFIRE_DIGEST_SHA512    = (1 << 1),
 };
 
+#define PAKFIRE_DIGESTS_ALL  (PAKFIRE_DIGEST_SHA512 | PAKFIRE_DIGEST_SHA256)
+
 #ifdef PAKFIRE_PRIVATE
 
 #include <stdio.h>
@@ -48,11 +50,19 @@ struct pakfire_digests {
        unsigned char sha256[SHA256_DIGEST_LENGTH];
 };
 
+#define PAKFIRE_DIGESTS_INIT \
+       { \
+               .sha512 = 0, \
+               .sha256 = 0, \
+       }
+
 size_t pakfire_digest_length(const enum pakfire_digest_types digest);
 
 #define pakfire_digest_set(digest) __pakfire_digest_set(digest, sizeof(digest))
 int __pakfire_digest_set(const unsigned char* digest, const size_t length);
 
+void pakfire_digests_reset(struct pakfire_digests* digests, int types);
+
 int pakfire_digests_compute_from_file(struct pakfire* pakfire,
        struct pakfire_digests* digests, int flags, FILE* f);
 
index 1b7a89a9490d61e4be2936b53feeec6c540f67a5..5b41bebfa2f399ebc9ff4dcea9b69ebda8842158 100644 (file)
@@ -93,6 +93,9 @@ const char* pakfire_file_get_abspath(struct pakfire_file* file);
 int pakfire_file_set_abspath(struct pakfire_file* file, const char* path);
 
 FILE* pakfire_file_open(struct pakfire_file* file);
+
+int pakfire_file_compute_digests(struct pakfire_file* file, const int types);
+
 int pakfire_file_remove(struct pakfire_file* file);
 int pakfire_file_cleanup(struct pakfire_file* file);
 
index 5189fd40cd2be9587cded9017159651dedbeedaf..029769359e22934b2b3fe5991f54bb737f3a70ae 100644 (file)
@@ -694,19 +694,12 @@ int pakfire_packager_add_file(struct pakfire_packager* packager, struct pakfire_
        if (!f)
                goto ERROR;
 
-#if 0
        // Add digests for regular files
-       if (filetype == AE_IFREG) {
-               r = pakfire_packager_compute_digests(packager, entry, f);
-               if (r) {
-                       ERROR(packager->pakfire, "Could not compute digests: %m\n")
-                       goto ERROR;
-               }
-
-               // Rewind the file descriptor
-               rewind(f);
+       r = pakfire_file_compute_digests(file, PAKFIRE_DIGESTS_ALL);
+       if (r) {
+               ERROR(packager->pakfire, "Could not compute digests: %m\n");
+               goto ERROR;
        }
-#endif
 
        // Generate file metadata into an archive entry
        entry = pakfire_file_archive_entry(file);