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)) {
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
// 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
}
ERROR:
+ if (f)
+ fclose(f);
return r;
}