From: Michael Tremer Date: Fri, 12 Aug 2022 10:27:31 +0000 (+0000) Subject: util: Unify copying libarchive payload from file X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=619aa8114e0b71b745f2856b9223df3de5873c7b;p=people%2Fstevee%2Fpakfire.git util: Unify copying libarchive payload from file Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index 1c8df8d4..f20ab956 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -102,6 +102,8 @@ size_t pakfire_digest_length(enum pakfire_digests digest); int pakfire_archive_copy_data(struct pakfire* pakfire, struct archive* src, struct archive* dst); +int pakfire_archive_copy_data_from_file(struct pakfire* pakfire, + struct archive* archive, FILE* f); int pakfire_archive_copy_data_to_buffer(struct pakfire* pakfire, struct archive* a, struct archive_entry* entry, char** data, size_t* data_size); diff --git a/src/libpakfire/packager.c b/src/libpakfire/packager.c index 1581984b..fc98f4b0 100644 --- a/src/libpakfire/packager.c +++ b/src/libpakfire/packager.c @@ -41,8 +41,6 @@ #include #include -#define BUFFER_SIZE 64 * 1024 - struct pakfire_packager { struct pakfire* pakfire; int nrefs; @@ -339,34 +337,6 @@ const char* pakfire_packager_filename(struct pakfire_packager* packager) { return packager->filename; } -static int pakfire_packager_copy_data(struct pakfire_packager* packager, - struct archive* a, FILE* f) { - char buffer[BUFFER_SIZE]; - - rewind(f); - - while (!feof(f)) { - // Read a block from file - size_t bytes_read = fread(buffer, 1, sizeof(buffer), f); - - // Check if any error occured - if (ferror(f)) { - ERROR(packager->pakfire, "Error reading from file: %m\n"); - return 1; - } - - // Write the block to the archive - ssize_t bytes_written = archive_write_data(a, buffer, bytes_read); - if (bytes_written < 0) { - ERROR(packager->pakfire, "Error writing data to archive: %s\n", - archive_error_string(a)); - return 1; - } - } - - return 0; -} - static struct archive_entry* pakfire_packager_create_file( struct pakfire_packager* packager, const char* filename, size_t size, mode_t mode) { // Create a new file entry @@ -551,7 +521,7 @@ static int pakfire_packager_write_archive(struct pakfire_packager* packager, } // Copy data - r = pakfire_packager_copy_data(packager, a, f); + r = pakfire_archive_copy_data_from_file(packager->pakfire, a, f); if (r) { const char* error = archive_error_string(a); if (!error) @@ -570,7 +540,7 @@ static int pakfire_packager_write_archive(struct pakfire_packager* packager, goto ERROR; } - r = pakfire_packager_copy_data(packager, mtree, f); + r = pakfire_archive_copy_data_from_file(packager->pakfire, mtree, f); if (r) { ERROR(packager->pakfire, "Error copying data to mtree: %s\n", archive_error_string(mtree)); @@ -913,12 +883,12 @@ int pakfire_packager_add(struct pakfire_packager* packager, // Copy the data of regular files if (archive_entry_filetype(entry) == AE_IFREG) { // Copy the payload into the archive - r = pakfire_packager_copy_data(packager, packager->payload, f); + r = pakfire_archive_copy_data_from_file(packager->pakfire, packager->payload, f); if (r) goto ERROR; // Copy the payload into the mtree for hashing - r = pakfire_packager_copy_data(packager, packager->mtree, f); + r = pakfire_archive_copy_data_from_file(packager->pakfire, packager->mtree, f); if (r) goto ERROR; } diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index d0597265..b29d6d78 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -1033,7 +1033,6 @@ PAKFIRE_EXPORT int pakfire_refresh(struct pakfire* pakfire, int flags) { } static int pakfire_copy(struct pakfire* pakfire, const char* src, const char* dst) { - char buffer[512 * 1024]; struct archive* reader = NULL; struct archive* writer = NULL; struct archive_entry* entry = NULL; @@ -1042,6 +1041,13 @@ static int pakfire_copy(struct pakfire* pakfire, const char* src, const char* ds DEBUG(pakfire, "Copying %s to %s\n", src, dst); + // Open the source file + f = fopen(src, "r"); + if (!f) { + ERROR(pakfire, "Could not open %s: %m\n", src); + goto ERROR; + } + // Allocate reader reader = archive_read_disk_new(); if (!reader) @@ -1060,16 +1066,16 @@ static int pakfire_copy(struct pakfire* pakfire, const char* src, const char* ds // Set the source path archive_entry_copy_sourcepath(entry, src); + // Set the destination path + archive_entry_set_pathname(entry, dst); + // Read everything from source file - r = archive_read_disk_entry_from_file(reader, entry, -1, NULL); + r = archive_read_disk_entry_from_file(reader, entry, fileno(f), NULL); if (r) { ERROR(pakfire, "Could not read from %s: %m\n", src); goto ERROR; } - // Set the destination path - archive_entry_set_pathname(entry, dst); - // Write file to destination r = archive_write_header(writer, entry); if (r) { @@ -1079,29 +1085,9 @@ static int pakfire_copy(struct pakfire* pakfire, const char* src, const char* ds // Copy payload if (archive_entry_filetype(entry) == AE_IFREG) { - f = fopen(src, "r"); - if (!f) { - r = 1; + r = pakfire_archive_copy_data_from_file(pakfire, writer, f); + if (r) goto ERROR; - } - - while (!feof(f)) { - size_t bytes_read = fread(buffer, 1, sizeof(buffer), f); - - // Check if any error occured - if (ferror(f)) { - ERROR(pakfire, "Error reading from file: %m\n"); - r = 1; - goto ERROR; - } - - ssize_t bytes_written = archive_write_data(writer, buffer, bytes_read); - if (bytes_written < 0) { - ERROR(pakfire, "Error writing data: %s\n", archive_error_string(writer)); - r = 1; - goto ERROR; - } - } } ERROR: diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index fe4bd69c..62fc32c3 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -45,6 +45,7 @@ #include #include +#define BUFFER_SIZE 64 * 1024 #define NSEC_PER_SEC 1000000000 static const struct pakfire_rich_operation { @@ -1081,6 +1082,38 @@ int pakfire_archive_copy_data(struct pakfire* pakfire, } } +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; +} + int pakfire_archive_copy_data_to_buffer(struct pakfire* pakfire, struct archive* a, struct archive_entry* entry, char** data, size_t* data_size) { *data = NULL;