From: Michael Tremer Date: Fri, 20 Oct 2023 12:34:43 +0000 (+0000) Subject: compress: Move function to read payload from util X-Git-Tag: 0.9.30~1412 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7ce5af3a3913a5a116e448d932d2e168ba1ad2f9;p=pakfire.git compress: Move function to read payload from util Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/compress.c b/src/libpakfire/compress.c index 316a2139d..c9ebce248 100644 --- a/src/libpakfire/compress.c +++ b/src/libpakfire/compress.c @@ -890,6 +890,38 @@ struct pakfire_compress { int digests; }; +static int pakfire_copy_data_from_file(struct pakfire* pakfire, + struct archive* archive, FILE* f) { + char buffer[BUFFER_SIZE]; + + ssize_t bytes_read = 0; + ssize_t bytes_written = 0; + + // Read file from the very beginning - also allows calling this multiple times + rewind(f); + + // Loop through the entire length of the file + while (!feof(f)) { + // Read a block from file + bytes_read = fread(buffer, 1, sizeof(buffer), f); + + // Check if any error occured + if (ferror(f)) { + ERROR(pakfire, "Read error: %m\n"); + return -errno; + } + + // Write the block to the archive + bytes_written = archive_write_data(archive, buffer, bytes_read); + if (bytes_written < bytes_read) { + ERROR(pakfire, "Write error: %s\n", archive_error_string(archive)); + return -errno; + } + } + + return 0; +} + static int __pakfire_compress_entry(struct pakfire* pakfire, struct pakfire_file* file, struct pakfire_compress* data, struct archive_entry* entry) { FILE* f = NULL; @@ -921,7 +953,7 @@ static int __pakfire_compress_entry(struct pakfire* pakfire, struct pakfire_file } // Copy the payload into the archive - r = pakfire_archive_copy_data_from_file(pakfire, data->archive, f); + r = pakfire_copy_data_from_file(pakfire, data->archive, f); if (r) goto ERROR; } diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index 14b54dfc1..bcc87ad36 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -26,8 +26,6 @@ #include #include -#include - #define PCRE2_CODE_UNIT_WIDTH 8 #include @@ -86,11 +84,6 @@ char* pakfire_generate_uuid(void); int pakfire_tty_is_noninteractive(void); -// Archive Stuff - -int pakfire_archive_copy_data_from_file(struct pakfire* pakfire, - struct archive* archive, FILE* f); - // JSON Stuff struct json_object* pakfire_json_parse(struct pakfire_ctx* ctx, diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index 9aa1993f4..b2b07bd3f 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -33,8 +33,6 @@ #include #include -#include -#include #include #include @@ -643,40 +641,6 @@ int __pakfire_which(struct pakfire* pakfire, char* path, const size_t length, return 0; } -// Archive Stuff - -int pakfire_archive_copy_data_from_file(struct pakfire* pakfire, - struct archive* archive, FILE* f) { - char buffer[BUFFER_SIZE]; - - size_t bytes_read = 0; - ssize_t bytes_written = 0; - - // Read file from the very beginning - also allows calling this multiple times - rewind(f); - - // Loop through the entire length of the file - while (!feof(f)) { - // Read a block from file - bytes_read = fread(buffer, 1, sizeof(buffer), f); - - // Check if any error occured - if (ferror(f)) { - ERROR(pakfire, "Read error: %m\n"); - return 1; - } - - // Write the block to the archive - bytes_written = archive_write_data(archive, buffer, bytes_read); - if (bytes_written < 0) { - ERROR(pakfire, "Write error: %s\n", archive_error_string(archive)); - return 1; - } - } - - return 0; -} - // JSON Stuff struct json_object* pakfire_json_parse(struct pakfire_ctx* ctx,