]> git.ipfire.org Git - pakfire.git/commitdiff
archive writer: Split off creating a new file entry
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Feb 2025 14:06:21 +0000 (14:06 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Feb 2025 14:06:21 +0000 (14:06 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/archive_writer.c

index a097826f2d3cd0fa79d94b8e5cefecd6315ac518..accc704567f29809d0ea6ce577a80517dc754ecb 100644 (file)
@@ -564,42 +564,63 @@ ERROR:
        return r;
 }
 
-/*
- * Creates a new file and writes it to the archive.
- */
-int pakfire_archive_writer_create_file(struct pakfire_archive_writer* self,
-               const char* filename, mode_t mode, const char* payload, const size_t length) {
-       struct archive_entry* entry = NULL;
+static int pakfire_archive_writer_create_entry(struct pakfire_archive_writer* self,
+               struct archive_entry** entry, const char* path, const mode_t mode, const size_t length) {
+       struct archive_entry* e = NULL;
        int r;
 
        // Create a new file entry
-       entry = archive_entry_new();
-       if (!entry) {
+       e = archive_entry_new();
+       if (!e) {
                r = -errno;
                goto ERROR;
        }
 
-       // Set filename
-       archive_entry_set_pathname(entry, filename);
+       // Set path
+       archive_entry_set_pathname(e, path);
 
        // This is a regular file
-    archive_entry_set_filetype(entry, AE_IFREG);
-    archive_entry_set_perm(entry, mode);
+       archive_entry_set_filetype(e, AE_IFREG);
+       archive_entry_set_perm(e, mode);
 
        // Set length
-       archive_entry_set_size(entry, length);
+       archive_entry_set_size(e, length);
 
        // Set ownership
-       archive_entry_set_uname(entry, "root");
-       archive_entry_set_uid(entry, 0);
-       archive_entry_set_gname(entry, "root");
-       archive_entry_set_gid(entry, 0);
+       archive_entry_set_uname(e, "root");
+       archive_entry_set_uid(e, 0);
+       archive_entry_set_gname(e, "root");
+       archive_entry_set_gid(e, 0);
 
        // Set times
-       archive_entry_set_birthtime(entry, self->time_created, 0);
-       archive_entry_set_ctime(entry, self->time_created, 0);
-       archive_entry_set_mtime(entry, self->time_created, 0);
-       archive_entry_set_atime(entry, self->time_created, 0);
+       archive_entry_set_birthtime(e, self->time_created, 0);
+       archive_entry_set_ctime(e, self->time_created, 0);
+       archive_entry_set_mtime(e, self->time_created, 0);
+       archive_entry_set_atime(e, self->time_created, 0);
+
+       // Return the pointer
+       *entry = e;
+       return 0;
+
+ERROR:
+       if (e)
+               archive_entry_free(e);
+
+       return r;
+}
+
+/*
+ * Creates a new file and writes it to the archive.
+ */
+int pakfire_archive_writer_create_file(struct pakfire_archive_writer* self,
+               const char* path, mode_t mode, const char* payload, const size_t length) {
+       struct archive_entry* entry = NULL;
+       int r;
+
+       // Create a new file entry
+       r = pakfire_archive_writer_create_entry(self, &entry, path, mode, length);
+       if (r < 0)
+               goto ERROR;
 
        // Write the header
        r = archive_write_header(self->archive, entry);