]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
util: Unify copying libarchive payload from file
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 12 Aug 2022 10:27:31 +0000 (10:27 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 12 Aug 2022 10:27:31 +0000 (10:27 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/util.h
src/libpakfire/packager.c
src/libpakfire/pakfire.c
src/libpakfire/util.c

index 1c8df8d4ddf5ac7cac3de6994786f2f977cf736c..f20ab956f7f9f2c7e7ad8da9693e5e195587d7a3 100644 (file)
@@ -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);
 
index 1581984b5a0ec375f4b05193851e273dce8d78d3..fc98f4b0c6cd177a218cac65c6b0980d898c1ba4 100644 (file)
@@ -41,8 +41,6 @@
 #include <pakfire/pwd.h>
 #include <pakfire/util.h>
 
-#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;
        }
index d05972657bf05e69685715663afb6a6092830485..b29d6d7893b08e0fae4d6b4a01044cbcd629b55b 100644 (file)
@@ -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:
index fe4bd69c20885a72ab19675e772053991f8915c9..62fc32c3a76e398ebe7711e0ada285669914ee10 100644 (file)
@@ -45,6 +45,7 @@
 #include <pakfire/package.h>
 #include <pakfire/util.h>
 
+#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;