From: Michael Tremer Date: Wed, 24 Aug 2022 09:26:08 +0000 (+0000) Subject: file: Add function to create file from path X-Git-Tag: 0.9.28~382 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=61b0856b9548136dbea52deb9b86fd66dfac54ae;p=pakfire.git file: Add function to create file from path Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/file.c b/src/libpakfire/file.c index a46e0a380..433991268 100644 --- a/src/libpakfire/file.c +++ b/src/libpakfire/file.c @@ -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); diff --git a/src/libpakfire/include/pakfire/file.h b/src/libpakfire/include/pakfire/file.h index 1df7d3727..5411a7a7d 100644 --- a/src/libpakfire/include/pakfire/file.h +++ b/src/libpakfire/include/pakfire/file.h @@ -82,8 +82,10 @@ struct pakfire_file* pakfire_file_parse_from_file(const char* list, unsigned int #include +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); diff --git a/src/libpakfire/packager.c b/src/libpakfire/packager.c index be01ed295..8bafffc0e 100644 --- a/src/libpakfire/packager.c +++ b/src/libpakfire/packager.c @@ -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);