]> git.ipfire.org Git - pakfire.git/commitdiff
pakfire: Add convenience functions to install packages
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 14 Jun 2021 16:46:42 +0000 (16:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 14 Jun 2021 16:46:42 +0000 (16:46 +0000)
This is being used by the builder to install the source package

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/build.c
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/libpakfire.sym
src/libpakfire/pakfire.c

index e4e9797b2bc5883fe7b4aeb91bc88058ad21e2e9..464e8d4104117ab3fbc5d8c2fe9bfe9084fa545d 100644 (file)
@@ -816,17 +816,28 @@ PAKFIRE_EXPORT int pakfire_build(Pakfire pakfire, const char* path,
                pakfire_repo_unref(repo);
        }
 
+       const char* packages[] = {
+               path, NULL
+       };
+
+       // Install the package into the build environment
+       r = pakfire_install(pakfire, packages, 0);
+       if (r) {
+               ERROR(pakfire, "Could not install %s\n", path);
+               goto ERROR;
+       }
+
        const char* root = pakfire_get_path(pakfire);
 
        // Create BUILDROOT
        pakfire_make_path(pakfire, buildroot, "/tmp/.buildroot.XXXXXX");
        if (!pakfire_mkdtemp(buildroot))
-               return 1;
+               goto ERROR;
 
        // Make relative BUILDROOT path
        const char* buildroot_rel = pakfire_path_relpath(root, buildroot);
        if (!buildroot_rel)
-               return 1;
+               goto ERROR;
 
        // Read makefile
        r = pakfire_read_makefile(&makefile, pakfire, path, &error);
index 456a7361b69de6d84639074daba16d33b8461b1e..a3c19e4ac5a0b5ea940a2ce7aebadbf226207e77 100644 (file)
@@ -85,6 +85,12 @@ void pakfire_log_set_priority(Pakfire pakfire, int priority);
 int pakfire_read_makefile(PakfireParser* parser, Pakfire pakfire, const char* path,
        struct pakfire_parser_error** error);
 
+// Install/Erase/Update
+
+int pakfire_install(Pakfire pakfire, const char** packages, int flags);
+int pakfire_erase(Pakfire pakfire, const char** packages, int flags);
+int pakfire_update(Pakfire pakfire, const char** packages, int flags);
+
 #ifdef PAKFIRE_PRIVATE
 
 #include <solv/pool.h>
index a3543db6a4ada82f5e3c0a493a8ed3a9aea2254e..6f68c2f2c1bf42b91a117cf874ee056845f9e439 100644 (file)
@@ -26,6 +26,7 @@ global:
        pakfire_copy_out;
        pakfire_count_packages;
        pakfire_create;
+       pakfire_erase;
        pakfire_execute;
        pakfire_execute_command;
        pakfire_execute_script;
@@ -35,12 +36,14 @@ global:
        pakfire_get_pool;
        pakfire_get_repo;
        pakfire_get_repos;
+       pakfire_install;
        pakfire_make_cache_path;
        pakfire_read_makefile;
        pakfire_ref;
        pakfire_search;
        pakfire_set_installonly;
        pakfire_unref;
+       pakfire_update;
        pakfire_version_compare;
        pakfire_whatprovides;
 
index 0e45750cc810371f1c77c355b23c4f5295bdaa7c..38d8273c529d5dfa8dead5345d0951297206f687 100644 (file)
@@ -53,6 +53,8 @@
 #include <pakfire/private.h>
 #include <pakfire/pwd.h>
 #include <pakfire/repo.h>
+#include <pakfire/request.h>
+#include <pakfire/transaction.h>
 #include <pakfire/types.h>
 #include <pakfire/util.h>
 
@@ -1553,3 +1555,71 @@ struct archive* pakfire_make_archive_disk_writer(Pakfire pakfire) {
 
        return archive;
 }
+
+// Convenience functions to install/erase/update packages
+
+static int pakfire_perform_transaction(Pakfire pakfire,
+               int (*action)(struct pakfire_request* request, const char* what, int flags),
+               const char** packages, int flags) {
+       struct pakfire_request* request = NULL;
+       struct pakfire_transaction* transaction = NULL;
+       int r = 1;
+
+       // Packages cannot be NULL
+       if (!packages) {
+               errno = EINVAL;
+               return 1;
+       }
+
+       // Create a new request
+       r = pakfire_request_create(&request, pakfire, 0);
+       if (r)
+               goto ERROR;
+
+       // Perform action on all packages
+       for (const char** package = packages; *package; package++) {
+               r = action(request, *package, flags);
+               if (r) {
+                       ERROR(pakfire, "Could not find '%s': %m\n", *package);
+                       goto ERROR;
+               }
+       }
+
+       // Solve the request
+       r = pakfire_request_solve(request, 0);
+       if (r)
+               goto ERROR;
+
+       // Fetch the transaction
+       transaction = pakfire_request_get_transaction(request);
+       if (!transaction)
+               goto ERROR;
+
+       // Run the transaction
+       r = pakfire_transaction_run(transaction);
+       if (r)
+               goto ERROR;
+
+       // Success
+       r = 0;
+
+ERROR:
+       if (transaction)
+               pakfire_transaction_unref(transaction);
+       if (request)
+               pakfire_request_unref(request);
+
+       return r;
+}
+
+PAKFIRE_EXPORT int pakfire_install(Pakfire pakfire, const char** packages, int flags) {
+       return pakfire_perform_transaction(pakfire, pakfire_request_install, packages, flags);
+}
+
+PAKFIRE_EXPORT int pakfire_erase(Pakfire pakfire, const char** packages, int flags) {
+       return pakfire_perform_transaction(pakfire, pakfire_request_erase, packages, flags);
+}
+
+PAKFIRE_EXPORT int pakfire_update(Pakfire pakfire, const char** packages, int flags) {
+       return pakfire_perform_transaction(pakfire, pakfire_request_upgrade, packages, flags);
+}