From bb06d5488d0bc12aefc4822820aac39005422397 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 3 Jun 2021 13:29:00 +0000 Subject: [PATCH] packager: Refactor writing packages to (sub-)directories Signed-off-by: Michael Tremer --- src/libpakfire/packager.c | 85 +++++++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 34 deletions(-) diff --git a/src/libpakfire/packager.c b/src/libpakfire/packager.c index b002e9829..1850dead0 100644 --- a/src/libpakfire/packager.c +++ b/src/libpakfire/packager.c @@ -18,7 +18,6 @@ # # #############################################################################*/ -#include #include #include #include @@ -50,6 +49,7 @@ struct pakfire_packager { time_t time_created; PakfirePackage pkg; + char filename[PATH_MAX]; // Reader struct archive* reader; @@ -301,7 +301,20 @@ PAKFIRE_EXPORT struct pakfire_packager* pakfire_packager_unref( } PAKFIRE_EXPORT const char* pakfire_packager_filename(struct pakfire_packager* packager) { - return pakfire_package_get_filename(packager->pkg); + if (!*packager->filename) { + const char* filename = pakfire_package_get_filename(packager->pkg); + + // Add arch + if (pakfire_package_is_source(packager->pkg)) + pakfire_string_set(packager->filename, filename); + else { + const char* arch = pakfire_package_get_arch(packager->pkg); + + pakfire_string_format(packager->filename, "%s/%s", arch, filename); + } + } + + return packager->filename; } static int pakfire_packager_copy_data(struct pakfire_packager* packager, @@ -805,7 +818,7 @@ PAKFIRE_EXPORT int pakfire_packager_finish(struct pakfire_packager* packager, FI goto ERROR; } - // Write the payload + // Write checksums r = pakfire_packager_write_archive(packager, a, NULL, PAKFIRE_ARCHIVE_FN_CHECKSUMS, &mtree, fmtree); if (r) { @@ -830,8 +843,8 @@ ERROR: PAKFIRE_EXPORT int pakfire_packager_finish_to_directory(struct pakfire_packager* packager, const char* target) { - DIR* d = NULL; char path[PATH_MAX]; + char tmppath[PATH_MAX]; int r = 1; // target cannot be empty @@ -840,60 +853,64 @@ PAKFIRE_EXPORT int pakfire_packager_finish_to_directory(struct pakfire_packager* return 1; } + // Get the filename of the package + const char* filename = pakfire_packager_filename(packager); + if (!filename) { + ERROR(packager->pakfire, "Could not generate filename for package: %s\n", + strerror(errno)); + r = 1; + goto ERROR; + } + + // Make the package path + r = pakfire_string_format(path, "%s/%s", target, filename); + if (r < 0) + goto ERROR; + + // Create the parent directory + r = pakfire_mkparentdir(path, 0); + if (r) + goto ERROR; + // Create a temporary file in the target directory - pakfire_string_format(path, "%s/.pakfire-package.XXXXXX", target); + r = pakfire_string_format(tmppath, "%s.XXXXXX", path); + if (r < 0) + goto ERROR; // Create a temporary result file - FILE* f = pakfire_mktemp(path); + FILE* f = pakfire_mktemp(tmppath); if (!f) goto ERROR; // Write the finished package r = pakfire_packager_finish(packager, f); + fclose(f); + if (r) { ERROR(packager->pakfire, "pakfire_packager_finish() failed: %s\n", strerror(errno)); goto ERROR; } - // Close the file - fclose(f); - f = NULL; - - // Get the filename of the package - const char* filename = pakfire_packager_filename(packager); - if (!filename) { - r = 1; - goto ERROR; - } - - // Open the target directory - d = opendir(target); - if (!d) { - r = 1; - goto ERROR; - } - // Move the temporary file to destination - r = renameat(AT_FDCWD, path, dirfd(d), filename); - if (r) + r = rename(tmppath, path); + if (r) { + ERROR(packager->pakfire, "Could not move %s to %s: %s\n", + tmppath, path, strerror(errno)); goto ERROR; + } - DEBUG(packager->pakfire, "Package written to %s/%s\n", target, filename); + INFO(packager->pakfire, "Package written to %s\n", path); // Success r = 0; ERROR: // Remove temporary file - if (r && *path) - unlink(path); - if (f) - fclose(f); - if (d) - closedir(d); + if (r && *tmppath) + unlink(tmppath); - return 0; + return r; } PAKFIRE_EXPORT int pakfire_packager_add(struct pakfire_packager* packager, -- 2.47.2