From 87d75f91e17d297e4b2fb76bd51f3fff000996de Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 25 Oct 2024 11:47:04 +0000 Subject: [PATCH] packager: Refactor how we read metadata from the file system Signed-off-by: Michael Tremer --- src/libpakfire/file.c | 20 ------------- src/libpakfire/include/pakfire/file.h | 2 -- src/libpakfire/packager.c | 41 +++++++++++++++++++-------- tests/libpakfire/packager.c | 2 +- 4 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/libpakfire/file.c b/src/libpakfire/file.c index d09512e9b..cd0970b15 100644 --- a/src/libpakfire/file.c +++ b/src/libpakfire/file.c @@ -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; diff --git a/src/libpakfire/include/pakfire/file.h b/src/libpakfire/include/pakfire/file.h index ce3fef8c2..c21672198 100644 --- a/src/libpakfire/include/pakfire/file.h +++ b/src/libpakfire/include/pakfire/file.h @@ -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, diff --git a/src/libpakfire/packager.c b/src/libpakfire/packager.c index 4c3f5e890..e1998c7a8 100644 --- a/src/libpakfire/packager.c +++ b/src/libpakfire/packager.c @@ -27,6 +27,7 @@ #include #include +// libarchive #include #include @@ -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; } diff --git a/tests/libpakfire/packager.c b/tests/libpakfire/packager.c index b008f75ce..5a22c6641 100644 --- a/tests/libpakfire/packager.c +++ b/tests/libpakfire/packager.c @@ -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); -- 2.39.5