]> git.ipfire.org Git - pakfire.git/commitdiff
build: Write out packages
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 24 May 2021 13:40:13 +0000 (13:40 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 24 May 2021 13:41:48 +0000 (13:41 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/build.c
src/libpakfire/dist.c
src/libpakfire/include/pakfire/packager.h
src/libpakfire/include/pakfire/repo.h
src/libpakfire/libpakfire.sym
src/libpakfire/packager.c
src/libpakfire/repo.c

index d955dfe164dcfd6b18352a6acb7ab904f8c1fc9e..5ea118fa818c8b2591cb8c0fd6ca5aff9f957b0a 100644 (file)
@@ -26,6 +26,7 @@
 #include <pakfire/dist.h>
 #include <pakfire/logging.h>
 #include <pakfire/package.h>
+#include <pakfire/packager.h>
 #include <pakfire/parser.h>
 #include <pakfire/private.h>
 #include <pakfire/types.h>
@@ -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;
index db3a716b7e1747f875c6bc47e284ab7ded10474f..1dbd3759085918e97134ad78d9efbec6ad0b9e64 100644 (file)
@@ -20,7 +20,6 @@
 
 #include <errno.h>
 #include <fts.h>
-#include <dirent.h>
 #include <fcntl.h>
 #include <glob.h>
 #include <stddef.h>
@@ -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)
index 8c7c752cb9b9bf801d001ae27fdfff45ca62a438..811f3ee8f3336292f67d72d5a47aeb52684e2625 100644 (file)
@@ -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);
index a3f1a7e67dbef14febd33a4bb4bffa0839c8d69d..2020944ad3a0626549849d4c5ccf67297718de4b 100644 (file)
@@ -93,6 +93,7 @@ int pakfire_repo_refresh(PakfireRepo repo, const int force);
 #include <pakfire/downloader.h>
 
 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);
index 48d51b9f3694954a9c830eaca64378aa23e405bd..bb69a44d9884221c5eff1a886b07bf1dc76dabb8 100644 (file)
@@ -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;
 
index 948aa360bf787de93741a1417a3896388dd4c0c5..b002e9829f596ac11bf6e5fe635e002f89607ef8 100644 (file)
@@ -18,6 +18,7 @@
 #                                                                             #
 #############################################################################*/
 
+#include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <linux/limits.h>
@@ -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;
index 91504683e99eaa84b2c1d35e1f86d46818b1bab7..11ad2664b74e8a6cbad0670f856964a5c1163ac5 100644 (file)
@@ -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;