]> git.ipfire.org Git - pakfire.git/commitdiff
ctx: Require the new context when creating a Pakfire instance
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 15 Oct 2023 13:54:28 +0000 (13:54 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 15 Oct 2023 13:54:28 +0000 (13:54 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/_pakfiremodule.c
src/_pakfire/pakfire.c
src/cli/lib/pakfire.c
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/pakfire.c
tests/parser/test.c
tests/testsuite.c

index b16b65a8a552dc572484b73ef779dc4abfb7f3bc..a9313351684074281b88ef945ff609013a88a20e 100644 (file)
@@ -46,6 +46,25 @@ PyObject* PyExc_DependencyError;
 PyObject* PyExc_CheckError;
 PyObject* PyExc_CheckFileVerificationError;
 
+struct pakfire_ctx* pakfire_ctx = NULL;
+
+static int initialize_context(void) {
+       int r;
+
+       // Create a new context
+       r = pakfire_ctx_create(&pakfire_ctx);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               goto ERROR;
+       }
+
+ERROR:
+       if (pakfire_ctx)
+               pakfire_ctx_unref(pakfire_ctx);
+
+       return r;
+}
+
 static PyObject* _pakfire_native_arch(void) {
        const char* arch = pakfire_arch_native();
        if (!arch)
@@ -127,6 +146,8 @@ static struct PyModuleDef moduledef = {
 };
 
 PyMODINIT_FUNC PyInit__pakfire(void) {
+       int r;
+
        /* Initialize locale */
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE_TARNAME, "/usr/share/locale");
@@ -242,6 +263,11 @@ PyMODINIT_FUNC PyInit__pakfire(void) {
        if (PyModule_AddIntMacro(module, PAKFIRE_KEY_ALGO_ED25519) < 0)
                goto ERROR;
 
+       // Initialize the context
+       r = initialize_context();
+       if (r)
+               goto ERROR;
+
        return module;
 
 ERROR:
index 1e7ea60d5d908c304c9be44d2121ae7c86722a77..f9a1e9cf490f7081dcecb22f397226dbfc76d43b 100644 (file)
@@ -25,6 +25,7 @@
 #include <pakfire/archive.h>
 #include <pakfire/build.h>
 #include <pakfire/constants.h>
+#include <pakfire/ctx.h>
 #include <pakfire/dist.h>
 #include <pakfire/jail.h>
 #include <pakfire/logging.h>
@@ -44,6 +45,8 @@
 #include "repo.h"
 #include "util.h"
 
+extern struct pakfire_ctx* pakfire_ctx;
+
 static PyObject* Pakfire_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
        PakfireObject* self = (PakfireObject *)type->tp_alloc(type, 0);
        if (self) {
@@ -234,7 +237,7 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) {
        Py_BEGIN_ALLOW_THREADS
 
        // Create a new Pakfire instance
-       r = pakfire_create(&self->pakfire, path, arch, fconf, flags,
+       r = pakfire_create(&self->pakfire, pakfire_ctx, path, arch, fconf, flags,
                Pakfire_log_callback, self->callbacks.log);
 
        Py_END_ALLOW_THREADS
index 4d570cebd13e8cfc587c70d898931567075079ce..3e43788bd9dbf7511b57ce22340d3e43f63dd6d5 100644 (file)
@@ -72,6 +72,7 @@ static void cli_set_repo_enabled(struct pakfire* pakfire, const char* name, int
 }
 
 int cli_setup_pakfire(struct pakfire** pakfire, struct cli_config* config) {
+       struct pakfire_ctx* ctx = NULL;
        struct pakfire* p = NULL;
        FILE* f = NULL;
        int r;
@@ -93,8 +94,13 @@ int cli_setup_pakfire(struct pakfire** pakfire, struct cli_config* config) {
                }
        }
 
+       // Create a new context
+       r = pakfire_ctx_create(&ctx);
+       if (r)
+               goto ERROR;
+
        // Initialize Pakfire
-       r = pakfire_create(&p, config->root, config->arch, f, config->flags, NULL, NULL);
+       r = pakfire_create(&p, ctx, config->root, config->arch, f, config->flags, NULL, NULL);
        if (r)
                goto ERROR;
 
@@ -128,6 +134,8 @@ int cli_setup_pakfire(struct pakfire** pakfire, struct cli_config* config) {
        *pakfire = p;
 
 ERROR:
+       if (ctx)
+               pakfire_ctx_unref(ctx);
        if (f)
                fclose(f);
 
index 235479743d89a4cecbeb1a64896935266c9bc009..dbf12b9f925a96ac442a4d257930f1f3ae27154e 100644 (file)
@@ -29,6 +29,7 @@
 
 struct pakfire;
 
+#include <pakfire/ctx.h>
 #include <pakfire/filelist.h>
 #include <pakfire/key.h>
 #include <pakfire/packagelist.h>
@@ -56,8 +57,9 @@ void pakfire_set_confirm_callback(struct pakfire* pakfire,
 typedef void (*pakfire_status_callback)(struct pakfire* pakfire, void* data,
        int progress, const char* status);
 
-int pakfire_create(struct pakfire** pakfire, const char* path, const char* arch,
-       FILE* conf, int flags, pakfire_log_callback log_callback, void* log_data);
+int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx* ctx,
+       const char* path, const char* arch, FILE* conf, int flags,
+       pakfire_log_callback log_callback, void* log_data);
 
 struct pakfire* pakfire_ref(struct pakfire* pakfire);
 struct pakfire* pakfire_unref(struct pakfire* pakfire);
index 2cbc2c75004a64499e2b2cbd5291a59aabf68179..b4d68963f0e2e968ca6d6590b30481868fa8507d 100644 (file)
@@ -46,6 +46,7 @@
 #include <pakfire/build.h>
 #include <pakfire/config.h>
 #include <pakfire/constants.h>
+#include <pakfire/ctx.h>
 #include <pakfire/db.h>
 #include <pakfire/dependencies.h>
 #include <pakfire/dist.h>
@@ -66,6 +67,7 @@
 #define LOCK_PATH PAKFIRE_PRIVATE_DIR "/.lock"
 
 struct pakfire {
+       struct pakfire_ctx* ctx;
        int nrefs;
 
        char path[PATH_MAX];
@@ -467,6 +469,9 @@ static void pakfire_free(struct pakfire* pakfire) {
        if (pakfire->config)
                pakfire_config_unref(pakfire->config);
 
+       if (pakfire->ctx)
+               pakfire_ctx_unref(pakfire->ctx);
+
        free(pakfire);
 }
 
@@ -893,8 +898,9 @@ ERROR:
        return r;
 }
 
-PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path,
-               const char* arch, FILE* conf, int flags, pakfire_log_callback log_callback, void* log_data) {
+PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx* ctx,
+               const char* path, const char* arch, FILE* conf, int flags,
+               pakfire_log_callback log_callback, void* log_data) {
        char tempdir[PATH_MAX] = PAKFIRE_TMP_DIR "/pakfire-root-XXXXXX";
        char private_dir[PATH_MAX];
        const char* env = NULL;
@@ -911,6 +917,9 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path,
        if (!p)
                return -errno;
 
+       // Reference the context
+       p->ctx = pakfire_ctx_ref(ctx);
+
        p->nrefs = 1;
        p->flags = flags;
 
index 36b61679f427d1890b0777965dbd99d3e6310167..982da638103db18914799b1b1959fdef3410d297 100644 (file)
@@ -26,6 +26,7 @@
 #include <pakfire/util.h>
 
 int main(int argc, const char* argv[]) {
+       struct pakfire_ctx* ctx = NULL;
        struct pakfire* pakfire = NULL;
        struct pakfire_parser* parser = NULL;
        struct pakfire_parser_error* error = NULL;
@@ -42,8 +43,13 @@ int main(int argc, const char* argv[]) {
                goto ERROR;
        }
 
+       // Create a new context
+       r = pakfire_ctx_create(&ctx);
+       if (r)
+               goto ERROR;
+
        // Create a pakfire instance
-       r = pakfire_create(&pakfire, root, NULL, NULL,
+       r = pakfire_create(&pakfire, ctx, root, NULL, NULL,
                PAKFIRE_FLAGS_DEBUG, pakfire_log_stderr, NULL);
        if (r) {
                fprintf(stderr, "Could not create Pakfire: %m\n");
@@ -98,6 +104,8 @@ ERROR:
                pakfire_parser_unref(parser);
        if (pakfire)
                pakfire_unref(pakfire);
+       if (ctx)
+               pakfire_ctx_unref(ctx);
 
        return r;
 }
index 3980995754334e6caed031b70a23f316a240180c..f9bd4055710665ce51a776dafa1e9c8374254bfb 100644 (file)
@@ -34,6 +34,7 @@
 struct testsuite ts;
 
 static int test_run(int i, struct test* t) {
+       struct pakfire_ctx* ctx = NULL;
        struct pakfire* p = NULL;
        FILE* c = NULL;
        const int flags = PAKFIRE_FLAGS_DEBUG;
@@ -50,6 +51,13 @@ static int test_run(int i, struct test* t) {
 
        LOG("running %s (%s)\n", t->name, root);
 
+       // Create a new context
+       r = pakfire_ctx_create(&ctx);
+       if (r) {
+               LOG("Could not create context: %m\n");
+               goto ERROR;
+       }
+
        // Open the configuration file
        c = fopen(TEST_SRC_PATH "/pakfire.conf", "r");
        if (!c) {
@@ -59,7 +67,7 @@ static int test_run(int i, struct test* t) {
        }
 
        // Create a pakfire instance
-       r = pakfire_create(&t->pakfire, root, NULL, c, flags, pakfire_log_stderr, NULL);
+       r = pakfire_create(&t->pakfire, ctx, root, NULL, c, flags, pakfire_log_stderr, NULL);
        if (r) {
                LOG("ERROR: Could not initialize pakfire: %m\n");
                goto ERROR;
@@ -98,6 +106,9 @@ ERROR:
                t->pakfire = NULL;
        }
 
+       if (ctx)
+               pakfire_ctx_unref(ctx);
+
        // Close the configuration file
        if (c)
                fclose(c);