]> git.ipfire.org Git - pakfire.git/commitdiff
packager: Append payload to archive
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 7 Mar 2021 17:06:32 +0000 (17:06 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 7 Mar 2021 17:06:32 +0000 (17:06 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/packager.c

index e7e32fbe4df2709dac35beb3ca28a0968aab7b26..ddbfe9f84cb5a862d10b76a27b45e3663e35c66a 100644 (file)
@@ -29,6 +29,7 @@
 #include <archive.h>
 #include <archive_entry.h>
 
+#include <pakfire/archive.h>
 #include <pakfire/constants.h>
 #include <pakfire/logging.h>
 #include <pakfire/package.h>
@@ -209,7 +210,7 @@ static int pakfire_packager_write_format(struct pakfire_packager* packager,
     archive_entry_set_perm(entry, 0644);
 
        // Set length
-    archive_entry_set_size(entry, strlen(buffer));
+       archive_entry_set_size(entry, strlen(buffer));
 
        // This is the end of the header
        int r = archive_write_header(a, entry);
@@ -232,6 +233,64 @@ static int pakfire_packager_write_format(struct pakfire_packager* packager,
        return 0;
 }
 
+static int pakfire_packager_write_payload(struct pakfire_packager* packager,
+               struct archive* a) {
+       struct stat st;
+
+       // Close the payload
+       if (packager->payload) {
+               archive_write_free(packager->payload);
+               packager->payload = NULL;
+       }
+
+       // Reset fd to beginning of the file
+       rewind(packager->fpayload);
+
+       int fd = fileno(packager->fpayload);
+
+       // Stat the payload file
+       int r = fstat(fd, &st);
+       if (r) {
+               ERROR(packager->pakfire, "stat() on fd %d failed: %s\n", fd, strerror(errno));
+               return 1;
+       }
+
+       // Create a new file entry
+       struct archive_entry* entry = archive_entry_new();
+       if (!entry)
+               return 1;
+
+       // Set filename
+       archive_entry_set_pathname(entry, PAKFIRE_ARCHIVE_FN_PAYLOAD);
+
+       // This is a regular file
+       archive_entry_set_filetype(entry, AE_IFREG);
+       archive_entry_set_perm(entry, 0644);
+
+       // Set the file size
+       archive_entry_set_size(entry, st.st_size);
+
+       // This is the end of the header
+       r = archive_write_header(a, entry);
+       if (r) {
+               ERROR(packager->pakfire, "Error writing header: %s\n", archive_error_string(a));
+               goto ERROR;
+       }
+
+       // Copy data
+       r = pakfire_packager_copy_data(packager, a, packager->fpayload);
+       if (r)
+               goto ERROR;
+
+       // Success
+       r = 0;
+
+ERROR:
+       archive_entry_free(entry);
+
+       return r;
+}
+
 /*
        This function is being called at the end when all data has been added to the package.
 
@@ -268,6 +327,11 @@ PAKFIRE_EXPORT char* pakfire_packager_finish(struct pakfire_packager* packager,
        if (r)
                goto ERROR;
 
+       // Write the payload
+       r = pakfire_packager_write_payload(packager, a);
+       if (r)
+               goto ERROR;
+
        // XXX set filename
 
 ERROR: