From: Michael Tremer Date: Sat, 8 Feb 2025 14:05:18 +0000 (+0000) Subject: archive writer: Split off copying payload X-Git-Tag: 0.9.30~71 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f82ba9620b6c5d712983d8a5d5b7b59d43ae6495;p=pakfire.git archive writer: Split off copying payload Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/archive_writer.c b/src/pakfire/archive_writer.c index 0e7d40fd..a097826f 100644 --- a/src/pakfire/archive_writer.c +++ b/src/pakfire/archive_writer.c @@ -357,23 +357,12 @@ int pakfire_archive_writer_set_title(struct pakfire_archive_writer* self, return pakfire_progress_set_title(self->progress, "%s", buffer); } -static int pakfire_archive_writer_write_payload(struct pakfire_archive_writer* self, - struct pakfire_file* file, struct archive_entry* entry) { +static int pakfire_archive_writer_write_payload( + struct pakfire_archive_writer* self, FILE* f) { ssize_t bytes_written = 0; ssize_t bytes_read = 0; char buffer[64 * 1024]; - FILE* f = NULL; - int r = 0; - - // Fetch the path - const char* path = pakfire_file_get_path(file); - - // Open the file - f = pakfire_file_fopen(file, "r"); - if (!f) { - r = -errno; - goto ERROR; - } + int r; // Loop through the entire length of the file while (!feof(f)) { @@ -381,36 +370,26 @@ static int pakfire_archive_writer_write_payload(struct pakfire_archive_writer* s bytes_read = fread(buffer, 1, sizeof(buffer), f); // Check if any error occured - if (ferror(f)) { - ERROR(self->ctx, "Failed to read %s: %m\n", path); - r = -errno; - goto ERROR; - } + if (ferror(f)) + return -errno; // Write the block to the archive bytes_written = archive_write_data(self->archive, buffer, bytes_read); - if (bytes_written < bytes_read) { - ERROR(self->ctx, "Failed to write %s: %s\n", - path, archive_error_string(self->archive)); - r = -errno; - goto ERROR; - } + if (bytes_written < bytes_read) + return -errno; // Update progress r = pakfire_progress_increment(self->progress, bytes_written); if (r < 0) - goto ERROR; + return r; } -ERROR: - if (f) - fclose(f); - - return r; + return 0; } static int pakfire_archive_writer_write_entry(struct pakfire_archive_writer* self, struct pakfire_file* file, struct archive_entry* entry) { + FILE* f = NULL; int r; // Fetch the path of the file in the archive @@ -436,9 +415,19 @@ static int pakfire_archive_writer_write_entry(struct pakfire_archive_writer* sel // It will set the size of the entry to zero if it does not need the payload, // for example when we are writing a hardlink. if (archive_entry_size(entry)) { - r = pakfire_archive_writer_write_payload(self, file, entry); - if (r < 0) + // Open the file + f = pakfire_file_fopen(file, "r"); + if (!f) { + r = -errno; + goto ERROR; + } + + // Copy everything + r = pakfire_archive_writer_write_payload(self, f); + if (r < 0) { + ERROR(self->ctx, "Failed to write %s: %s\n", path, strerror(-r)); goto ERROR; + } } // Write trailer @@ -451,6 +440,8 @@ static int pakfire_archive_writer_write_entry(struct pakfire_archive_writer* sel } ERROR: + if (f) + fclose(f); return r; }