From: Michael Tremer Date: Sun, 5 Jan 2025 13:48:19 +0000 (+0000) Subject: pakfire: Pass the configuration as a config object X-Git-Tag: 0.9.30~532 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dd84de4edd9099cb667cc1fb26354b4b62bcb95d;p=pakfire.git pakfire: Pass the configuration as a config object Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/pakfire.c b/src/cli/lib/pakfire.c index d8b4a68e5..11f6725b7 100644 --- a/src/cli/lib/pakfire.c +++ b/src/cli/lib/pakfire.c @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -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); diff --git a/src/pakfire/config.c b/src/pakfire/config.c index 0a5e43a17..b3547a70f 100644 --- a/src/pakfire/config.c +++ b/src/pakfire/config.c @@ -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; -} diff --git a/src/pakfire/config.h b/src/pakfire/config.h index 2468bdd00..145c085d5 100644 --- a/src/pakfire/config.h +++ b/src/pakfire/config.h @@ -21,8 +21,6 @@ #ifndef PAKFIRE_CONFIG_H #define PAKFIRE_CONFIG_H -#ifdef PAKFIRE_PRIVATE - #include 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 */ diff --git a/src/pakfire/pakfire.c b/src/pakfire/pakfire.c index 13498ae71..38675df01 100644 --- a/src/pakfire/pakfire.c +++ b/src/pakfire/pakfire.c @@ -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) { diff --git a/src/pakfire/pakfire.h b/src/pakfire/pakfire.h index e69dc8f71..bac39cb18 100644 --- a/src/pakfire/pakfire.h +++ b/src/pakfire/pakfire.h @@ -29,6 +29,7 @@ struct pakfire; +#include #include #include #include @@ -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); diff --git a/src/python/pakfire.c b/src/python/pakfire.c index d347f7957..c8596a044 100644 --- a/src/python/pakfire.c +++ b/src/python/pakfire.c @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -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; } diff --git a/tests/parser/test.c b/tests/parser/test.c index 332eb3da0..f7050076e 100644 --- a/tests/parser/test.c +++ b/tests/parser/test.c @@ -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;