From: Michael Tremer Date: Mon, 24 May 2021 13:40:13 +0000 (+0000) Subject: build: Write out packages X-Git-Tag: 0.9.28~1285^2~82 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=48c6f2e7ffc06f1049c181da77fd4c6e1cad7c53;p=pakfire.git build: Write out packages Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/build.c b/src/libpakfire/build.c index d955dfe16..5ea118fa8 100644 --- a/src/libpakfire/build.c +++ b/src/libpakfire/build.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -49,9 +50,11 @@ static const char* stages[] = { "\n" \ "exit 0\n" -static int pakfire_build_package(Pakfire pakfire, PakfireParser makefile, const char* namespace) { +static int pakfire_build_package(Pakfire pakfire, PakfireParser makefile, + const char* namespace, const char* target) { PakfireRepo repo = NULL; PakfirePackage pkg = NULL; + struct pakfire_packager* packager = NULL; int r = 1; // Expand the handle into the package name @@ -80,6 +83,11 @@ static int pakfire_build_package(Pakfire pakfire, PakfireParser makefile, const goto ERROR; } + // Create a packager + r = pakfire_packager_create(&packager, pkg); + if (r) + goto ERROR; + #ifdef ENABLE_DEBUG char* dump = pakfire_package_dump(pkg, PAKFIRE_PKG_DUMP_LONG); if (dump) { @@ -88,10 +96,19 @@ static int pakfire_build_package(Pakfire pakfire, PakfireParser makefile, const } #endif + // Write the finished package + r = pakfire_packager_finish_to_directory(packager, target); + if (r) { + ERROR(pakfire, "pakfire_packager_finish() failed: %s\n", strerror(errno)); + goto ERROR; + } + // Success r = 0; ERROR: + if (packager) + pakfire_packager_unref(packager); if (repo) pakfire_repo_unref(repo); if (pkg) @@ -102,7 +119,7 @@ ERROR: return r; } -static int pakfire_build_packages(Pakfire pakfire, PakfireParser makefile) { +static int pakfire_build_packages(Pakfire pakfire, PakfireParser makefile, const char* target) { DEBUG(pakfire, "Creating packages..."); int r = 1; @@ -123,7 +140,7 @@ static int pakfire_build_packages(Pakfire pakfire, PakfireParser makefile) { // Build packages in reverse order for (int i = num_packages - 1; i >= 0; i--) { - r = pakfire_build_package(pakfire, makefile, packages[i]); + r = pakfire_build_package(pakfire, makefile, packages[i], target); if (r) goto ERROR; } @@ -176,6 +193,24 @@ PAKFIRE_EXPORT int pakfire_build(Pakfire pakfire, const char* path, PakfireParser makefile = NULL; struct pakfire_parser_error* error = NULL; + // Check for valid input + if (!path) { + errno = EINVAL; + return 1; + } + + // The default target is the local repository path + if (!target) { + PakfireRepo repo = pakfire_get_repo(pakfire, "local"); + if (!repo) { + errno = EINVAL; + return 1; + } + + target = pakfire_repo_get_path(repo); + pakfire_repo_unref(repo); + } + // Read makefile int r = pakfire_read_makefile(&makefile, pakfire, path, &error); if (r) { @@ -204,7 +239,7 @@ PAKFIRE_EXPORT int pakfire_build(Pakfire pakfire, const char* path, } // Create the packages - r = pakfire_build_packages(pakfire, makefile); + r = pakfire_build_packages(pakfire, makefile, target); if (r) { ERROR(pakfire, "Could not create packages: %s\n", strerror(errno)); goto ERROR; diff --git a/src/libpakfire/dist.c b/src/libpakfire/dist.c index db3a716b7..1dbd37590 100644 --- a/src/libpakfire/dist.c +++ b/src/libpakfire/dist.c @@ -20,7 +20,6 @@ #include #include -#include #include #include #include @@ -366,15 +365,6 @@ PAKFIRE_EXPORT int pakfire_dist(Pakfire pakfire, const char* path, const char* t PakfirePackage pkg = NULL; PakfireRepo repo = NULL; - char tempfile[PATH_MAX]; - - // Open the target directory - DIR* d = opendir(target); - if (!d) - return 1; - - int dfd = dirfd(d); - // Load makefile int r = pakfire_read_makefile(&makefile, pakfire, path, &error); if (r) { @@ -413,38 +403,12 @@ PAKFIRE_EXPORT int pakfire_dist(Pakfire pakfire, const char* path, const char* t if (r) goto ERROR; - pakfire_string_format(tempfile, "%s/.pakfire-dist.XXXXXX", target); - - // Create a temporary result file - FILE* f = pakfire_mktemp(tempfile); - if (!f) - goto ERROR; - // Write the finished package - r = pakfire_packager_finish(packager, f); - if (r) { - ERROR(pakfire, "pakfire_packager_finish() failed: %s\n", strerror(errno)); - goto ERROR; - } - - // Close the file - fclose(f); - - const char* filename = pakfire_packager_filename(packager); - if (!filename) - goto ERROR; - - // Move the temporary file to destination - r = renameat(AT_FDCWD, tempfile, dfd, filename); + r = pakfire_packager_finish_to_directory(packager, target); if (r) goto ERROR; ERROR: - // Remove the temporary file - if (*tempfile) - unlink(tempfile); - closedir(d); - if (packager) pakfire_packager_unref(packager); if (pkg) diff --git a/src/libpakfire/include/pakfire/packager.h b/src/libpakfire/include/pakfire/packager.h index 8c7c752cb..811f3ee8f 100644 --- a/src/libpakfire/include/pakfire/packager.h +++ b/src/libpakfire/include/pakfire/packager.h @@ -33,6 +33,8 @@ struct pakfire_packager* pakfire_packager_unref(struct pakfire_packager* package const char* pakfire_packager_filename(struct pakfire_packager* packager); int pakfire_packager_finish(struct pakfire_packager* packager, FILE* f); +int pakfire_packager_finish_to_directory(struct pakfire_packager* packager, + const char* target); int pakfire_packager_add(struct pakfire_packager* packager, const char* sourcepath, const char* path); diff --git a/src/libpakfire/include/pakfire/repo.h b/src/libpakfire/include/pakfire/repo.h index a3f1a7e67..2020944ad 100644 --- a/src/libpakfire/include/pakfire/repo.h +++ b/src/libpakfire/include/pakfire/repo.h @@ -93,6 +93,7 @@ int pakfire_repo_refresh(PakfireRepo repo, const int force); #include int pakfire_repo_import(Pakfire pakfire, struct pakfire_config* config); +const char* pakfire_repo_get_path(PakfireRepo repo); void pakfire_repo_internalize(PakfireRepo repo); Id pakfire_repo_add_solvable(PakfireRepo repo); diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index 48d51b9f3..bb69a44d9 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -225,6 +225,7 @@ global: pakfire_packager_add; pakfire_packager_create; pakfire_packager_finish; + pakfire_packager_finish_to_directory; pakfire_packager_ref; pakfire_packager_unref; diff --git a/src/libpakfire/packager.c b/src/libpakfire/packager.c index 948aa360b..b002e9829 100644 --- a/src/libpakfire/packager.c +++ b/src/libpakfire/packager.c @@ -18,6 +18,7 @@ # # #############################################################################*/ +#include #include #include #include @@ -827,6 +828,74 @@ ERROR: return r; } +PAKFIRE_EXPORT int pakfire_packager_finish_to_directory(struct pakfire_packager* packager, + const char* target) { + DIR* d = NULL; + char path[PATH_MAX]; + int r = 1; + + // target cannot be empty + if (!target) { + errno = EINVAL; + return 1; + } + + // Create a temporary file in the target directory + pakfire_string_format(path, "%s/.pakfire-package.XXXXXX", target); + + // Create a temporary result file + FILE* f = pakfire_mktemp(path); + if (!f) + goto ERROR; + + // Write the finished package + r = pakfire_packager_finish(packager, 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) + goto ERROR; + + DEBUG(packager->pakfire, "Package written to %s/%s\n", target, filename); + + // Success + r = 0; + +ERROR: + // Remove temporary file + if (r && *path) + unlink(path); + if (f) + fclose(f); + if (d) + closedir(d); + + return 0; +} + PAKFIRE_EXPORT int pakfire_packager_add(struct pakfire_packager* packager, const char* sourcepath, const char* path) { FILE* f = NULL; diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index 91504683e..11ad2664b 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -644,7 +644,7 @@ static int pakfire_repo_is_local(PakfireRepo repo) { return pakfire_string_startswith(repo->appdata->baseurl, "file://"); } -static const char* pakfire_repo_get_path(PakfireRepo repo) { +const char* pakfire_repo_get_path(PakfireRepo repo) { if (!pakfire_repo_is_local(repo)) return NULL;