]> git.ipfire.org Git - pakfire.git/commitdiff
file: Add function to create file from path
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 24 Aug 2022 09:26:08 +0000 (09:26 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 24 Aug 2022 09:26:08 +0000 (09:26 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/file.c
src/libpakfire/include/pakfire/file.h
src/libpakfire/packager.c

index a46e0a38054c6c1458aa1bee6cc632002f897950..43399126801e8f82d4fbb78134f686095982f7ac 100644 (file)
@@ -121,6 +121,48 @@ PAKFIRE_EXPORT int pakfire_file_create(struct pakfire_file** file, struct pakfir
        return 0;
 }
 
+int pakfire_file_create_from_path(struct pakfire_file** file,
+               struct pakfire* pakfire, const char* path) {
+       struct archive* reader = NULL;
+       struct archive_entry* entry = NULL;
+       int r = 1;
+
+       // Allocate a reader
+       reader = pakfire_make_archive_disk_reader(pakfire, 0);
+       if (!reader)
+               goto ERROR;
+
+       // Allocate a new archive entry
+       entry = archive_entry_new();
+       if (!entry)
+               goto ERROR;
+
+       // Set source path
+       archive_entry_copy_sourcepath(entry, path);
+
+       // Read all file attributes from disk
+       r = archive_read_disk_entry_from_file(reader, entry, -1, NULL);
+       if (r) {
+               ERROR(pakfire, "Could not read from %s: %m\n", path);
+               goto ERROR;
+       }
+
+       // Create file
+       r = pakfire_file_create_from_archive_entry(file, pakfire, entry);
+       if (r)
+               goto ERROR;
+
+ERROR:
+       if (r)
+               ERROR(pakfire, "Could not create file from path %s: %m\n", path);
+       if (entry)
+               archive_entry_free(entry);
+       if (reader)
+               archive_read_free(reader);
+
+       return r;
+}
+
 int pakfire_file_create_from_archive_entry(struct pakfire_file** file, struct pakfire* pakfire,
                struct archive_entry* entry) {
        int r = pakfire_file_create(file, pakfire);
index 1df7d3727b39b30b2980ae0f9cf6d84269476c5a..5411a7a7df2602c7255da91638f1a329143ad1dc 100644 (file)
@@ -82,8 +82,10 @@ struct pakfire_file* pakfire_file_parse_from_file(const char* list, unsigned int
 
 #include <archive_entry.h>
 
+int pakfire_file_create_from_path(struct pakfire_file** file,
+       struct pakfire* pakfire, const char* path);
 int pakfire_file_create_from_archive_entry(struct pakfire_file** file, struct pakfire* pakfire,
-               struct archive_entry* entry);
+       struct archive_entry* entry);
 int pakfire_file_copy_archive_entry(struct pakfire_file* file, struct archive_entry* entry);
 struct archive_entry* pakfire_file_archive_entry(struct pakfire_file* file);
 
index be01ed2956c3600e75fccea61b2409b18ba78a84..8bafffc0e4d51374bbb8092fc5b6e612fc69079b 100644 (file)
@@ -749,43 +749,18 @@ int pakfire_packager_add(struct pakfire_packager* packager,
        struct pakfire_file* file = NULL;
        int r = 1;
 
-       // Check if path is set
-       if (!sourcepath) {
-               errno = EINVAL;
-               return 1;
-       }
-
-       // Use basename if path isn't set
-       if (!path) {
-               path = strrchr(sourcepath, '/');
-               if (path)
-                       path++;
-       }
-
-       // Create a new file entry
-       struct archive_entry* entry = archive_entry_new();
-       if (!entry)
-               return 1;
-
-       // Set the source path
-       archive_entry_copy_sourcepath(entry, sourcepath);
-
-       // Set path in archive
-       if (path)
-               archive_entry_set_pathname(entry, path);
-
-       // Read all attributes from file
-       r = archive_read_disk_entry_from_file(packager->reader, entry, -1, NULL);
-       if (r) {
-               ERROR(packager->pakfire, "Could not read attributes from %s: %m\n", path);
-               goto ERROR;
-       }
-
-       // Convert to file
-       r = pakfire_file_create_from_archive_entry(&file, packager->pakfire, entry);
+       // Create file
+       r = pakfire_file_create_from_path(&file, packager->pakfire, sourcepath);
        if (r)
                goto ERROR;
 
+       // Assign a new path for inside the archive
+       if (path) {
+               r = pakfire_file_set_path(file, path);
+               if (r)
+                       goto ERROR;
+       }
+
        // Call the main function
        r = pakfire_packager_add_file(packager, file);