From: Michael Tremer Date: Mon, 16 Oct 2023 16:52:57 +0000 (+0000) Subject: ctx: Read the configuration X-Git-Tag: 0.9.30~1483 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=55c00b977e80d283a5dc59eb71eab540596120d1;p=pakfire.git ctx: Read the configuration Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/_pakfiremodule.c b/src/_pakfire/_pakfiremodule.c index 1d93ed023..74dd0a9f1 100644 --- a/src/_pakfire/_pakfiremodule.c +++ b/src/_pakfire/_pakfiremodule.c @@ -149,7 +149,7 @@ static int initialize_context(void) { int r; // Create a new context - r = pakfire_ctx_create(&pakfire_ctx); + r = pakfire_ctx_create(&pakfire_ctx, NULL); if (r) { PyErr_SetFromErrno(PyExc_OSError); goto ERROR; diff --git a/src/cli/pakfire-builder.c b/src/cli/pakfire-builder.c index e29004ea7..96cc6a360 100644 --- a/src/cli/pakfire-builder.c +++ b/src/cli/pakfire-builder.c @@ -138,7 +138,7 @@ int main(int argc, char* argv[]) { int r; // Setup the context - r = pakfire_ctx_create(&ctx); + r = pakfire_ctx_create(&ctx, NULL); if (r) goto ERROR; diff --git a/src/cli/pakfire.c b/src/cli/pakfire.c index 167eb61b5..327be48fd 100644 --- a/src/cli/pakfire.c +++ b/src/cli/pakfire.c @@ -141,7 +141,7 @@ int main(int argc, char* argv[]) { int r; // Setup the context - r = pakfire_ctx_create(&ctx); + r = pakfire_ctx_create(&ctx, NULL); if (r) goto ERROR; diff --git a/src/libpakfire/ctx.c b/src/libpakfire/ctx.c index 6abe23626..8a3bd53a7 100644 --- a/src/libpakfire/ctx.c +++ b/src/libpakfire/ctx.c @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -33,6 +34,9 @@ struct pakfire_ctx { // Reference counter int nrefs; + // Config + struct pakfire_config* config; + // Logging struct pakfire_ctx_log { int level; @@ -109,11 +113,35 @@ static int pakfire_ctx_default_confirm_callback(struct pakfire_ctx* ctx, return 0; } +static int pakfire_ctx_load_config(struct pakfire_ctx* ctx, const char* path) { + FILE* f = NULL; + int r; + + // Load some default configuration if not path was provided + if (!path) + path = "/etc/pakfire/pakfire.conf"; + + // Open the configuration file + f = fopen(path, "r"); + if (!f) + return -errno; + + // Read the configuration file + r = pakfire_config_read(ctx->config, f); + + // Cleanup + fclose(f); + + return r; +} + static void pakfire_ctx_free(struct pakfire_ctx* ctx) { + if (ctx->config) + pakfire_config_unref(ctx->config); free(ctx); } -PAKFIRE_EXPORT int pakfire_ctx_create(struct pakfire_ctx** ctx) { +PAKFIRE_EXPORT int pakfire_ctx_create(struct pakfire_ctx** ctx, const char* path) { struct pakfire_ctx* c = NULL; int r; @@ -125,6 +153,16 @@ PAKFIRE_EXPORT int pakfire_ctx_create(struct pakfire_ctx** ctx) { // Initialize the reference counter c->nrefs = 1; + // Initialize the configuration + r = pakfire_config_create(&c->config); + if (r) + goto ERROR; + + // Load configuration + r = pakfire_ctx_load_config(c, path); + if (r) + goto ERROR; + // Setup logging r = pakfire_ctx_setup_logging(c); if (r) @@ -159,6 +197,15 @@ PAKFIRE_EXPORT struct pakfire_ctx* pakfire_ctx_unref(struct pakfire_ctx* ctx) { return NULL; } +// Config + +struct pakfire_config* pakfire_ctx_get_config(struct pakfire_ctx* ctx) { + if (!ctx->config) + return NULL; + + return pakfire_config_ref(ctx->config); +} + // Logging PAKFIRE_EXPORT int pakfire_ctx_get_log_level(struct pakfire_ctx* ctx) { diff --git a/src/libpakfire/include/pakfire/ctx.h b/src/libpakfire/include/pakfire/ctx.h index 80edb39eb..05b9de86b 100644 --- a/src/libpakfire/include/pakfire/ctx.h +++ b/src/libpakfire/include/pakfire/ctx.h @@ -23,16 +23,21 @@ struct pakfire_ctx; +#include #include #include #include #include -int pakfire_ctx_create(struct pakfire_ctx** ctx); +int pakfire_ctx_create(struct pakfire_ctx** ctx, const char* path); struct pakfire_ctx* pakfire_ctx_ref(struct pakfire_ctx* ctx); struct pakfire_ctx* pakfire_ctx_unref(struct pakfire_ctx* ctx); +// Config + +struct pakfire_config* pakfire_ctx_get_config(struct pakfire_ctx* ctx); + // Logging int pakfire_ctx_get_log_level(struct pakfire_ctx* ctx); diff --git a/tests/parser/test.c b/tests/parser/test.c index 0344df64f..2d0a96116 100644 --- a/tests/parser/test.c +++ b/tests/parser/test.c @@ -44,7 +44,7 @@ int main(int argc, const char* argv[]) { } // Create a new context - r = pakfire_ctx_create(&ctx); + r = pakfire_ctx_create(&ctx, NULL); if (r) goto ERROR; diff --git a/tests/testsuite.c b/tests/testsuite.c index 689f90b66..209693831 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -51,7 +51,7 @@ 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); + r = pakfire_ctx_create(&ctx, NULL); if (r) { LOG("Could not create context: %m\n"); goto ERROR;