]> git.ipfire.org Git - pakfire.git/commitdiff
pakfire: Pass the configuration as a config object
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Jan 2025 13:48:19 +0000 (13:48 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Jan 2025 13:48:19 +0000 (13:48 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/pakfire.c
src/pakfire/config.c
src/pakfire/config.h
src/pakfire/pakfire.c
src/pakfire/pakfire.h
src/python/pakfire.c
tests/parser/test.c

index d8b4a68e5849e4053cdd14e321e903805be4d746..11f6725b7bcc7d25ca171b659d4c7de73cc37c3b 100644 (file)
@@ -21,6 +21,7 @@
 #include <errno.h>
 #include <limits.h>
 
+#include <pakfire/config.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/repo.h>
 
@@ -71,10 +72,16 @@ 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_config* c = NULL;
        struct pakfire* p = NULL;
        FILE* f = NULL;
        int r;
 
+       // Create a new config object
+       r = pakfire_config_create(&c);
+       if (r < 0)
+               goto ERROR;
+
        // Open the distro configuration
        if (config->distro) {
                f = open_distro_config(config->distro);
@@ -92,8 +99,13 @@ int cli_setup_pakfire(struct pakfire** pakfire, struct cli_config* config) {
                }
        }
 
+       // Read the configuration file
+       r = pakfire_config_read(c, f);
+       if (r < 0)
+               goto ERROR;
+
        // Initialize Pakfire
-       r = pakfire_create(&p, config->ctx, config->root, config->arch, f, config->flags);
+       r = pakfire_create(&p, config->ctx, c, config->root, config->arch, config->flags);
        if (r < 0) {
                fprintf(stderr, "Could not initialize Pakfire: %s\n", strerror(-r));
                goto ERROR;
@@ -111,6 +123,8 @@ int cli_setup_pakfire(struct pakfire** pakfire, struct cli_config* config) {
        *pakfire = p;
 
 ERROR:
+       if (c)
+               pakfire_config_unref(c);
        if (f)
                fclose(f);
 
index 0a5e43a17f54395058165711615a8468cf88e105..b3547a70f3e3fab65e111625e455c827d1db46de 100644 (file)
@@ -632,37 +632,3 @@ ERROR:
 int pakfire_config_dump(struct pakfire_config* config, FILE* f) {
        return pakfire_config_walk_sections(config, pakfire_config_dump_section, f);
 }
-
-FILE* pakfire_config_fdump(struct pakfire_config* config) {
-       FILE* f = NULL;
-       int r;
-
-       char* buffer = NULL;
-       size_t length = 0;
-
-       // Open a new memory stream
-       f = open_memstream(&buffer, &length);
-       if (!f)
-               goto ERROR;
-
-       // Dump the configuration
-       r = pakfire_config_dump(config, f);
-       if (r < 0)
-               goto ERROR;
-
-       // Close the memory stream
-       fclose(f);
-
-       // Re-open as a new file handle
-       f = fmemopen(buffer, length, "r");
-       if (!f)
-               goto ERROR;
-
-       return f;
-
-ERROR:
-       if (buffer)
-               free(buffer);
-
-       return NULL;
-}
index 2468bdd00bb83a3d839076e8ed8e0cc0dae7a4eb..145c085d5d767707bf4ba7a54319ff5b38fc20cf 100644 (file)
@@ -21,8 +21,6 @@
 #ifndef PAKFIRE_CONFIG_H
 #define PAKFIRE_CONFIG_H
 
-#ifdef PAKFIRE_PRIVATE
-
 #include <stdio.h>
 
 struct pakfire_config;
@@ -60,8 +58,5 @@ int pakfire_config_read(struct pakfire_config* config, FILE* f);
 int pakfire_config_parse(struct pakfire_config* config, const char* s, ssize_t length);
 
 int pakfire_config_dump(struct pakfire_config* config, FILE* f);
-FILE* pakfire_config_fdump(struct pakfire_config* config);
-
-#endif
 
 #endif /* PAKFIRE_CONFIG_H */
index 13498ae713b4d617f996904435b39962a23ace33..38675df01c274815671a7a0d6073eb2d512fdc1b 100644 (file)
@@ -799,7 +799,7 @@ ERROR:
 }
 
 int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx* ctx,
-               const char* path, const char* arch, FILE* conf, int flags) {
+               struct pakfire_config* config, const char* path, const char* arch, int flags) {
        struct pakfire* p = NULL;
        int r;
 
@@ -838,6 +838,15 @@ int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx* ctx,
        // Store the flags
        p->flags = flags;
 
+       // Store a reference to the configuration
+       if (config) {
+               p->config = pakfire_config_ref(config);
+       } else {
+               r = pakfire_config_create(&p->config);
+               if (r < 0)
+                       goto ERROR;
+       }
+
        // Store the nominal architecture
        r = pakfire_string_set(p->arches.nominal, arch);
        if (r < 0)
@@ -862,20 +871,6 @@ int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx* ctx,
        if (path)
                p->internal_flags |= PAKFIRE_HAS_PATH;
 
-       // Initialise configuration
-       r = pakfire_config_create(&p->config);
-       if (r < 0)
-               goto ERROR;
-
-       // Read the configuration file
-       if (conf) {
-               r = pakfire_config_read(p->config, conf);
-               if (r < 0) {
-                       ERROR(p->ctx, "Could not read configuration: %s\n", strerror(-r));
-                       goto ERROR;
-               }
-       }
-
        // Import distro configuration
        r = pakfire_config_import_distro(p);
        if (r < 0) {
@@ -1024,31 +1019,17 @@ ERROR:
 
 int pakfire_clone(struct pakfire** clone, struct pakfire* pakfire, const char* path) {
        struct pakfire* p = NULL;
-       FILE* f = NULL;
        int r;
 
-       // Dump the configuration
-       f = pakfire_config_fdump(pakfire->config);
-       if (!f) {
-               r = -errno;
-               goto ERROR;
-       }
-
        // Create a new Pakfire instance
-       r = pakfire_create(&p, pakfire->ctx, path, pakfire->arches.nominal, f, 0);
+       r = pakfire_create(&p, pakfire->ctx, pakfire->config, path, pakfire->arches.nominal, 0);
        if (r < 0)
-               goto ERROR;
+               return r;
 
        // Return the pointer
-       *clone = pakfire_ref(p);
+       *clone = p;
 
-ERROR:
-       if (p)
-               pakfire_unref(p);
-       if (f)
-               fclose(f);
-
-       return r;
+       return 0;
 }
 
 struct pakfire* pakfire_ref(struct pakfire* pakfire) {
index e69dc8f715972a7e26e5892d67dfe7d59a4119ec..bac39cb18e4ef44313a1d163d2c6973886063ce5 100644 (file)
@@ -29,6 +29,7 @@
 
 struct pakfire;
 
+#include <pakfire/config.h>
 #include <pakfire/ctx.h>
 #include <pakfire/filelist.h>
 #include <pakfire/key.h>
@@ -49,7 +50,7 @@ typedef void (*pakfire_status_callback)(struct pakfire* pakfire, void* data,
        int progress, const char* status);
 
 int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx* ctx,
-       const char* path, const char* arch, FILE* conf, int flags);
+       struct pakfire_config* config, const char* path, const char* arch, int flags);
 
 struct pakfire* pakfire_ref(struct pakfire* pakfire);
 struct pakfire* pakfire_unref(struct pakfire* pakfire);
index d347f79575b5bd7d1e710e05d8318f4b93ecd9ae..c8596a044b04d897f99e6d98e5009b2e8c10896f 100644 (file)
@@ -25,6 +25,7 @@
 
 #include <pakfire/archive.h>
 #include <pakfire/build.h>
+#include <pakfire/config.h>
 #include <pakfire/constants.h>
 #include <pakfire/ctx.h>
 #include <pakfire/dist.h>
@@ -58,6 +59,7 @@ static PyObject* Pakfire_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
 }
 
 static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) {
+       struct pakfire_config* c = NULL;
        const char* kwlist[] = { "ctx", "path", "arch", "conf", NULL };
        CtxObject* ctx = NULL;
        const char* path = NULL;
@@ -65,17 +67,31 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) {
        PyObject* conf = Py_None;
        int r = 1;
 
-       FILE* fconf = NULL;
+       FILE* f = NULL;
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|zzO", (char**)kwlist,
                        &CtxType, &ctx, &path, &arch, &conf))
                goto ERROR;
 
+       // Create a new configuration object
+       r = pakfire_config_create(&c);
+       if (r < 0) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               goto ERROR;
+       }
+
        // Map the configuration
        if (conf != Py_None) {
-               fconf = PyObject_AsFileHandle(conf, "r");
-               if (!fconf)
+               f = PyObject_AsFileHandle(conf, "r");
+               if (!f)
                        goto ERROR;
+
+               // Read the configuration
+               r = pakfire_config_read(c, f);
+               if (r < 0) {
+                       PyErr_SetFromErrno(PyExc_OSError);
+                       goto ERROR;
+               }
        }
 
        int flags = 0;
@@ -86,7 +102,7 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) {
        self->ctx = pakfire_ctx_ref(ctx->ctx);
 
        // Create a new Pakfire instance
-       r = pakfire_create(&self->pakfire, self->ctx, path, arch, fconf, flags);
+       r = pakfire_create(&self->pakfire, self->ctx, c, path, arch, flags);
 
        Py_END_ALLOW_THREADS
 
@@ -109,8 +125,10 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) {
     }
 
 ERROR:
-       if (fconf)
-               fclose(fconf);
+       if (c)
+               pakfire_config_unref(c);
+       if (f)
+               fclose(f);
 
        return -r;
 }
index 332eb3da057193ac9d7dbe88d37d8c540462271e..f7050076e0821e35e5ef439692d7ea233a97484e 100644 (file)
@@ -55,7 +55,7 @@ int main(int argc, const char* argv[]) {
        pakfire_ctx_set_log_callback(ctx, pakfire_log_stderr, NULL);
 
        // Create a pakfire instance
-       r = pakfire_create(&pakfire, ctx, root, NULL, NULL, 0);
+       r = pakfire_create(&pakfire, ctx, NULL, root, NULL, 0);
        if (r) {
                fprintf(stderr, "Could not create Pakfire: %m\n");
                goto ERROR;