From: Michael Tremer Date: Sun, 15 Oct 2023 13:54:28 +0000 (+0000) Subject: ctx: Require the new context when creating a Pakfire instance X-Git-Tag: 0.9.30~1503 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f2a8f4f2eac8283871abdd75b575335388ddb30f;p=pakfire.git ctx: Require the new context when creating a Pakfire instance Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/_pakfiremodule.c b/src/_pakfire/_pakfiremodule.c index b16b65a8a..a93133516 100644 --- a/src/_pakfire/_pakfiremodule.c +++ b/src/_pakfire/_pakfiremodule.c @@ -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: diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index 1e7ea60d5..f9a1e9cf4 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -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 diff --git a/src/cli/lib/pakfire.c b/src/cli/lib/pakfire.c index 4d570cebd..3e43788bd 100644 --- a/src/cli/lib/pakfire.c +++ b/src/cli/lib/pakfire.c @@ -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); diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index 235479743..dbf12b9f9 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -29,6 +29,7 @@ struct pakfire; +#include #include #include #include @@ -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); diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 2cbc2c750..b4d68963f 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -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; diff --git a/tests/parser/test.c b/tests/parser/test.c index 36b61679f..982da6381 100644 --- a/tests/parser/test.c +++ b/tests/parser/test.c @@ -26,6 +26,7 @@ #include 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; } diff --git a/tests/testsuite.c b/tests/testsuite.c index 398099575..f9bd40557 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -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);