]> git.ipfire.org Git - pakfire.git/commitdiff
packager: Refactor how we read metadata from the file system
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 25 Oct 2024 11:47:04 +0000 (11:47 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 25 Oct 2024 11:47:04 +0000 (11:47 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/file.c
src/libpakfire/include/pakfire/file.h
src/libpakfire/packager.c
tests/libpakfire/packager.c

index d09512e9b90ab250f9f0bbb4ba41b71977fa5706..cd0970b1554dbb3f1c56116943d21ba40207da82 100644 (file)
@@ -417,26 +417,6 @@ ERROR:
        return r;
 }
 
-int pakfire_file_read(struct pakfire_file* file, struct archive* reader, const char* path) {
-       int r;
-
-       // Check inputs
-       if (!reader || !path)
-               return -EINVAL;
-
-       // Set abspath
-       archive_entry_copy_sourcepath(file->entry, path);
-
-       // Read everything
-       r = archive_read_disk_entry_from_file(reader, file->entry, -1, NULL);
-       if (r) {
-               CTX_ERROR(file->ctx, "Could not read %s: %s\n", path, archive_error_string(reader));
-               return 1;
-       }
-
-       return 0;
-}
-
 int pakfire_file_create_from_archive_entry(struct pakfire_file** file, struct pakfire* pakfire,
                struct archive_entry* entry) {
        struct pakfire_file* f = NULL;
index ce3fef8c24268b46dad71482d476d5873fb697c5..c21672198765bba90daea31da1ab76575beadc7f 100644 (file)
@@ -137,8 +137,6 @@ int pakfire_file_set_fd(struct pakfire_file* file, int fd);
 
 int pakfire_file_has_payload(struct pakfire_file* file);
 
-int pakfire_file_read(struct pakfire_file* file, struct archive* reader, const char* path);
-
 int pakfire_file_write_fcaps(struct pakfire_file* file, struct vfs_cap_data* cap_data);
 
 int pakfire_file_create_from_archive_entry(struct pakfire_file** file, struct pakfire* pakfire,
index 4c3f5e8906f0cb86d784ae1f38ec789e0675a20d..e1998c7a88dada30ded75833a546bec85a7eb52f 100644 (file)
@@ -27,6 +27,7 @@
 #include <time.h>
 #include <unistd.h>
 
+// libarchive
 #include <archive.h>
 #include <archive_entry.h>
 
@@ -619,26 +620,40 @@ int pakfire_packager_add_file(struct pakfire_packager* packager, struct pakfire_
 
 int pakfire_packager_add(struct pakfire_packager* packager,
                const char* sourcepath, const char* path) {
+       struct archive_entry* entry = NULL;
        struct pakfire_file* file = NULL;
        int r;
 
-       if (!path)
-               path = sourcepath;
+       // Check inputs
+       if (!sourcepath || !path)
+               return -EINVAL;
 
-       // Create a new file object
-       r = pakfire_file_create(&file, packager->pakfire, path);
-       if (r < 0)
-               goto ERROR;
+       // Create a new archive entry
+       entry = archive_entry_new();
+       if (!entry)
+               return -errno;
+
+       // Set the path
+       archive_entry_copy_pathname(entry, path);
+
+       // Set the absolute path
+       archive_entry_copy_sourcepath(entry, sourcepath);
 
-       // Read the meta information
-       r = pakfire_file_read(file, packager->reader, sourcepath);
-       if (r < 0)
+       // Read everything
+       r = archive_read_disk_entry_from_file(packager->reader, entry, -1, NULL);
+       if (r) {
+               ERROR(packager->pakfire, "Could not read %s: %s\n",
+                       sourcepath, archive_error_string(packager->reader));
+               r = -errno;
                goto ERROR;
+       }
 
-       // Assign a new path for inside the archive
-       r = pakfire_file_set_path(file, path);
-       if (r < 0)
+       // Create a new file object from the archive entry
+       r = pakfire_file_create_from_archive_entry(&file, packager->pakfire, entry);
+       if (r < 0) {
+               ERROR(packager->pakfire, "Could not create file object: %s\n", strerror(-r));
                goto ERROR;
+       }
 
        // Call the main function
        r = pakfire_packager_add_file(packager, file);
@@ -646,6 +661,8 @@ int pakfire_packager_add(struct pakfire_packager* packager,
 ERROR:
        if (file)
                pakfire_file_unref(file);
+       if (entry)
+               archive_entry_free(entry);
 
        return r;
 }
index b008f75cef2ce287b8e9f733f9ff8a6645e52055..5a22c6641ebcafe262da11892de56a69895c72c4 100644 (file)
@@ -44,7 +44,7 @@ static int test_create(const struct test* t) {
        // Add a file to the package
        const char* path = TEST_SRC_PATH "data/beep-1.3-2.ip3.x86_64.pfm";
 
-       ASSERT_SUCCESS(pakfire_packager_add(packager, path, NULL));
+       ASSERT_SUCCESS(pakfire_packager_add(packager, path, "beep-1.3-2.ip3.x86_64.pfm"));
 
        // Write archive
        FILE* f = test_mktemp(NULL);