]> git.ipfire.org Git - pakfire.git/commitdiff
build: Create and manage the Pakfire instance ourselves
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Jan 2025 13:30:03 +0000 (13:30 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Jan 2025 13:30:03 +0000 (13:30 +0000)
This allows us to pass arguments around easier and keep the
configuration closer together.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/build.c
src/cli/lib/image_create.c
src/cli/lib/shell.c
src/pakfire/build.c
src/pakfire/build.h
src/pakfire/job.c

index 239b03c3a62e8920b2d74a90c815d861c0ca61c8..464d7178563eff4b07ddb1cb0e9039b83815b340 100644 (file)
@@ -146,7 +146,7 @@ static void log_callback(void* data, int priority, const char* file, int line,
 }
 
 int cli_build(void* data, int argc, char* argv[]) {
-       struct pakfire* pakfire = NULL;
+       struct cli_config* cli_config = data;
        struct pakfire_build* build = NULL;
        struct config config = {
                .id     = NULL,
@@ -160,8 +160,6 @@ int cli_build(void* data, int argc, char* argv[]) {
        int build_flags = 0;
        int r;
 
-       struct cli_config* cli_config = data;
-
        // Parse the command line
        r = cli_parse(options, NULL, NULL, NULL, parse, 0, argc, argv, &config);
        if (r)
@@ -170,14 +168,9 @@ int cli_build(void* data, int argc, char* argv[]) {
        // Replace the logger
        pakfire_ctx_set_log_callback(cli_config->ctx, log_callback, NULL);
 
-       // Set Pakfire flags
+       // Use the snapshot?
        if (config.flags & BUILD_ENABLE_SNAPSHOT)
-               cli_config->flags |= PAKFIRE_USE_SNAPSHOT;
-
-       // Setup pakfire
-       r = cli_setup_pakfire(&pakfire, cli_config);
-       if (r)
-               goto ERROR;
+               build_flags |= PAKFIRE_BUILD_ENABLE_SNAPSHOT;
 
        // Is the build interactive?
        if (config.flags & BUILD_INTERACTIVE)
@@ -192,7 +185,7 @@ int cli_build(void* data, int argc, char* argv[]) {
                build_flags |= PAKFIRE_BUILD_DISABLE_TESTS;
 
        // Setup the build environment
-       r = pakfire_build_create(&build, pakfire, config.id, build_flags);
+       r = pakfire_build_create(&build, cli_config->ctx, cli_config->arch, config.id, build_flags);
        if (r) {
                fprintf(stderr, "Could not setup the build environment: %m\n");
                goto ERROR;
@@ -220,8 +213,6 @@ int cli_build(void* data, int argc, char* argv[]) {
 ERROR:
        if (build)
                pakfire_build_unref(build);
-       if (pakfire)
-               pakfire_unref(pakfire);
 
        return r;
 }
index 090f37eee826bc0e6528d8596e54519c28e20e71..a4fa920fbf79270a5d99d8ea543687e26f502744 100644 (file)
@@ -57,13 +57,11 @@ static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
 }
 
 int cli_image_create(void* data, int argc, char* argv[]) {
-       struct pakfire* pakfire = NULL;
+       struct cli_config* cli_config = data;
        struct pakfire_build* build = NULL;
        FILE* f = NULL;
        int r;
 
-       struct cli_config* cli_config = data;
-
        struct config config = {
                .type = NULL,
                .path = NULL,
@@ -81,13 +79,8 @@ int cli_image_create(void* data, int argc, char* argv[]) {
                goto ERROR;
        }
 
-       // Setup pakfire
-       r = cli_setup_pakfire(&pakfire, cli_config);
-       if (r)
-               goto ERROR;
-
        // Setup the build environment
-       r = pakfire_build_create(&build, pakfire, NULL, 0);
+       r = pakfire_build_create(&build, cli_config->ctx, cli_config->arch, NULL, 0);
        if (r)
                goto ERROR;
 
@@ -99,8 +92,6 @@ int cli_image_create(void* data, int argc, char* argv[]) {
 ERROR:
        if (build)
                pakfire_build_unref(build);
-       if (pakfire)
-               pakfire_unref(pakfire);
        if (f)
                fclose(f);
 
index d13972ec6ff131758e5f553c96a5b422c2b23a4a..aee039c41d6fc08052ec1c624948cb38d9cb7ee6 100644 (file)
@@ -88,12 +88,11 @@ static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
 }
 
 int cli_shell(void* data, int argc, char* argv[]) {
-       struct pakfire* pakfire = NULL;
+       struct cli_config* cli_config = data;
        struct pakfire_build* build = NULL;
+       int build_flags = PAKFIRE_BUILD_INTERACTIVE;
        int r;
 
-       struct cli_config* cli_config = data;
-
        struct config config = {
                .flags        = SHELL_ENABLE_SNAPSHOT,
                .argv         = {},
@@ -109,15 +108,10 @@ int cli_shell(void* data, int argc, char* argv[]) {
 
        // Enable snapshots?
        if (config.flags & SHELL_ENABLE_SNAPSHOT)
-               cli_config->flags |= PAKFIRE_USE_SNAPSHOT;
-
-       // Setup pakfire
-       r = cli_setup_pakfire(&pakfire, cli_config);
-       if (r)
-               goto ERROR;
+               build_flags |= PAKFIRE_BUILD_ENABLE_SNAPSHOT;
 
        // Setup the build environment
-       r = pakfire_build_create(&build, pakfire, NULL, PAKFIRE_BUILD_INTERACTIVE);
+       r = pakfire_build_create(&build, cli_config->ctx, cli_config->arch, NULL, build_flags);
        if (r) {
                fprintf(stderr, "Could not setup the build environment: %m\n");
                goto ERROR;
@@ -136,8 +130,6 @@ int cli_shell(void* data, int argc, char* argv[]) {
 ERROR:
        if (build)
                pakfire_build_unref(build);
-       if (pakfire)
-               pakfire_unref(pakfire);
 
        return r;
 }
index 47b76587ccc1b5303f33ea0cb712e275f21c7267..67effffbfcf61835336a72bb533beeaa8dbb2d77 100644 (file)
@@ -1993,8 +1993,24 @@ static int pakfire_build_set_time_start(struct pakfire_build* build) {
        return r;
 }
 
+static int pakfire_build_setup_pakfire(struct pakfire_build* build, const char* arch) {
+       int flags = PAKFIRE_FLAGS_BUILD;
+       int r;
+
+       // Enable snapshot?
+       if (build->flags & PAKFIRE_BUILD_ENABLE_SNAPSHOT)
+               flags |= PAKFIRE_USE_SNAPSHOT;
+
+       // Create a new Pakfire instance
+       r = pakfire_create(&build->pakfire, build->ctx, NULL, arch, NULL, flags);
+       if (r < 0)
+               return r;
+
+       return 0;
+}
+
 int pakfire_build_create(struct pakfire_build** build,
-               struct pakfire* pakfire, const char* id, int flags) {
+               struct pakfire_ctx* ctx, const char* arch, const char* id, int flags) {
        int r;
 
        // Allocate build object
@@ -2003,10 +2019,7 @@ int pakfire_build_create(struct pakfire_build** build,
                return 1;
 
        // Reference the context
-       b->ctx = pakfire_ctx(pakfire);
-
-       // Reference pakfire
-       b->pakfire = pakfire_ref(pakfire);
+       b->ctx = pakfire_ctx_ref(ctx);
 
        // Initialize reference counter
        b->nrefs = 1;
@@ -2014,6 +2027,11 @@ int pakfire_build_create(struct pakfire_build** build,
        // Copy flags
        b->flags = flags;
 
+       // Setup Pakfire
+       r = pakfire_build_setup_pakfire(b, arch);
+       if (r < 0)
+               goto ERROR;
+
        // Create an environment
        r = pakfire_env_create(&b->env, b->ctx);
        if (r < 0)
index 5250aa024a867c38b590200550725c9ee84bd579..fceb1422d662016e4c9d958669059164bcd207f2 100644 (file)
 struct pakfire_build;
 
 enum pakfire_build_flags {
-       PAKFIRE_BUILD_INTERACTIVE      = (1 << 0),
-       PAKFIRE_BUILD_DISABLE_CCACHE   = (1 << 1),
-       PAKFIRE_BUILD_DISABLE_TESTS    = (1 << 2),
+       PAKFIRE_BUILD_ENABLE_SNAPSHOT  = (1 << 0),
+       PAKFIRE_BUILD_INTERACTIVE      = (1 << 1),
+       PAKFIRE_BUILD_DISABLE_CCACHE   = (1 << 2),
+       PAKFIRE_BUILD_DISABLE_TESTS    = (1 << 3),
 };
 
 typedef int (*pakfire_build_log_callback)
@@ -36,7 +37,7 @@ typedef int (*pakfire_build_log_callback)
                const char* file, int line, const char* function, const char* format, ...);
 
 int pakfire_build_create(struct pakfire_build** build,
-       struct pakfire* pakfire, const char* id, int flags);
+       struct pakfire_ctx* ctx, const char* arch, const char* id, int flags);
 
 struct pakfire_build* pakfire_build_ref(struct pakfire_build* build);
 struct pakfire_build* pakfire_build_unref(struct pakfire_build* build);
index 69e6e87ef66a6f153fb5722c718068ea032dc1ff..f337a359ab5b169339a6cbf93c18c3db37381857 100644 (file)
@@ -420,7 +420,6 @@ static void pakfire_job_log(void* data, int priority, const char* file,
 
 static int pakfire_job_child(struct pakfire_job* job) {
        struct pakfire_ctx* ctx = NULL;
-       struct pakfire* pakfire = NULL;
        struct pakfire_build* build = NULL;
        char job_id[UUID_STR_LEN];
        FILE* conf = NULL;
@@ -465,14 +464,6 @@ static int pakfire_job_child(struct pakfire_job* job) {
                goto ERROR;
        }
 
-       // Create a new Pakfire instance
-       r = pakfire_create(&pakfire, ctx, NULL, job->arch, conf, PAKFIRE_FLAGS_BUILD);
-       if (r) {
-               ERROR(ctx, "Could not initialize Pakfire: %m\n");
-               r = -errno;
-               goto ERROR;
-       }
-
        int build_flags = 0;
 
        // Disable the ccache
@@ -480,7 +471,7 @@ static int pakfire_job_child(struct pakfire_job* job) {
                build_flags |= PAKFIRE_BUILD_DISABLE_CCACHE;
 
        // Create a new build environment
-       r = pakfire_build_create(&build, pakfire, job_id, build_flags);
+       r = pakfire_build_create(&build, ctx, job->arch, job_id, build_flags);
        if (r) {
                ERROR(ctx, "Could not setup the build environment: %m\n");
                r = -errno;
@@ -508,8 +499,6 @@ static int pakfire_job_child(struct pakfire_job* job) {
 ERROR:
        if (build)
                pakfire_build_unref(build);
-       if (pakfire)
-               pakfire_unref(pakfire);
        if (ctx)
                pakfire_ctx_unref(ctx);
        if (conf)