]> git.ipfire.org Git - pakfire.git/commitdiff
ctx: Read the configuration
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 16 Oct 2023 16:52:57 +0000 (16:52 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 16 Oct 2023 17:05:24 +0000 (17:05 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/_pakfiremodule.c
src/cli/pakfire-builder.c
src/cli/pakfire.c
src/libpakfire/ctx.c
src/libpakfire/include/pakfire/ctx.h
tests/parser/test.c
tests/testsuite.c

index 1d93ed023078ba7cd61f00d617ce76142aa8e97d..74dd0a9f1df299d3b6d0342972618e161674ca00 100644 (file)
@@ -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;
index e29004ea72e91ffd92cc281b2eeedc70c5dc6070..96cc6a3607bd6e67a953294ef864e4d69e23c187 100644 (file)
@@ -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;
 
index 167eb61b5b6fcea71cccac60f88f46506d172d35..327be48fd12c710247676711ae7bb3963c885993 100644 (file)
@@ -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;
 
index 6abe23626d9ef6bf02d71f76c76d96409b1a8a7e..8a3bd53a7dee8cec948ab5cc60c808d2b9dfa755 100644 (file)
@@ -25,6 +25,7 @@
 #include <string.h>
 #include <syslog.h>
 
+#include <pakfire/config.h>
 #include <pakfire/ctx.h>
 #include <pakfire/logging.h>
 #include <pakfire/private.h>
@@ -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) {
index 80edb39eb8dec29fd3f8221c9bff0ebd6b3219a8..05b9de86b71ec51cdb9694b379e161331d1d3331 100644 (file)
 
 struct pakfire_ctx;
 
+#include <pakfire/config.h>
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/progress.h>
 #include <pakfire/transaction.h>
 
-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);
index 0344df64f809b69d9e2840f874b3bdc7133876f9..2d0a961161de50fbd78abef1289f673293b2c161 100644 (file)
@@ -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;
 
index 689f90b6622f44ae9373666f43ae453d1f0d5169..209693831959ede1882fecb4a824ce1cf25a4ee8 100644 (file)
@@ -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;