From: Michael Tremer Date: Wed, 24 Aug 2022 17:28:31 +0000 (+0000) Subject: packager: Compute digests when packaging files X-Git-Tag: 0.9.28~367 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1e76689ac9d8ff8ba82932c66ec8085f0c20f168;p=pakfire.git packager: Compute digests when packaging files Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/digest.c b/src/libpakfire/digest.c index 69800aca7..00ef6d271 100644 --- a/src/libpakfire/digest.c +++ b/src/libpakfire/digest.c @@ -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); diff --git a/src/libpakfire/file.c b/src/libpakfire/file.c index 6a5410de9..0f439b6b5 100644 --- a/src/libpakfire/file.c +++ b/src/libpakfire/file.c @@ -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; } diff --git a/src/libpakfire/include/pakfire/digest.h b/src/libpakfire/include/pakfire/digest.h index 2af445154..82adb7f11 100644 --- a/src/libpakfire/include/pakfire/digest.h +++ b/src/libpakfire/include/pakfire/digest.h @@ -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 @@ -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); diff --git a/src/libpakfire/include/pakfire/file.h b/src/libpakfire/include/pakfire/file.h index 1b7a89a94..5b41bebfa 100644 --- a/src/libpakfire/include/pakfire/file.h +++ b/src/libpakfire/include/pakfire/file.h @@ -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); diff --git a/src/libpakfire/packager.c b/src/libpakfire/packager.c index 5189fd40c..029769359 100644 --- a/src/libpakfire/packager.c +++ b/src/libpakfire/packager.c @@ -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);