From: Michael Tremer Date: Fri, 16 Apr 2021 14:32:59 +0000 (+0000) Subject: pakfire: Read configuration file on initialization X-Git-Tag: 0.9.28~1285^2~348 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=54bf8fbaef2f8356ad77384a5028a1865f5c938c;p=pakfire.git pakfire: Read configuration file on initialization Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index e82b4abb1..848861e85 100644 --- a/Makefile.am +++ b/Makefile.am @@ -758,6 +758,8 @@ TESTS = \ # Some test data to run tests EXTRA_DIST += \ + tests/pakfire.conf \ + \ tests/data/726D8B0B0889B04E.key \ tests/data/beep-1.3-2.ip3.x86_64.pfm \ \ diff --git a/configure.ac b/configure.ac index db24f1cfe..6f32e90ca 100644 --- a/configure.ac +++ b/configure.ac @@ -180,6 +180,8 @@ AC_ARG_WITH([systemdsystemunitdir], AC_SUBST([systemdsystemunitdir], [$with_systemdsystemunitdir]) AM_CONDITIONAL(HAVE_SYSTEMD, [test -n "$with_systemdsystemunitdir"]) +AC_DEFINE_UNQUOTED([PAKFIRE_CONFIG_PATH], ["${sysconfdir}/${PACKAGE_NAME}"], + [The path where Pakfire stores configuration files]) AC_DEFINE_UNQUOTED([PAKFIRE_CACHE_PATH], ["/var/cache/${PACKAGE_NAME}"], [The path where Pakfire stores temporary data]) AC_DEFINE_UNQUOTED([PAKFIRE_PRIVATE_DIR], ["/var/lib/${PACKAGE_NAME}"], diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index 88ef89f59..98e774a60 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -49,16 +49,18 @@ static PyObject* Pakfire_new(PyTypeObject* type, PyObject* args, PyObject* kwds) } static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) { - char* kwlist[] = { "path", "arch", "offline", NULL }; + char* kwlist[] = { "path", "arch", "offline", "conf", NULL }; const char* path = NULL; const char* arch = NULL; + const char* conf = NULL; int offline = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzp", kwlist, &path, &arch, &offline)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzpz", kwlist, + &path, &arch, &offline, &conf)) return -1; // Create a new Pakfire instance - int r = pakfire_create(&self->pakfire, path, arch); + int r = pakfire_create(&self->pakfire, path, arch, conf); if (r) { switch (errno) { // Invalid architecture or path diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index bda5297b6..32ffbfa46 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -29,7 +29,7 @@ #include #include -int pakfire_create(Pakfire* pakfire, const char* path, const char* arch); +int pakfire_create(Pakfire* pakfire, const char* path, const char* arch, const char* conf); Pakfire pakfire_ref(Pakfire pakfire); Pakfire pakfire_unref(Pakfire pakfire); diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 211746e50..dbedffc0f 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -396,6 +396,43 @@ static int pakfire_safety_checks(Pakfire pakfire) { return 0; } +static int pakfire_read_config(Pakfire pakfire, const char* path) { + char* default_path = NULL; + + // Use default path if none set + if (!path) { + default_path = pakfire_make_path(pakfire, PAKFIRE_CONFIG_PATH "/general.conf"); + if (!default_path) + return 1; + + path = default_path; + } + + DEBUG(pakfire, "Reading configuration from %s\n", path); + + FILE* f = fopen(path, "r"); + if (!f) { + // Silently ignore when there is no default configuration file + if (default_path && errno == ENOENT) + return 0; + + return 1; + } + + // Read configuration from file + int r = pakfire_config_read(pakfire->config, f); + if (r) + goto ERROR; + +ERROR: + fclose(f); + + if (default_path) + free(default_path); + + return r; +} + static int pakfire_read_os_release(Pakfire pakfire) { char* line = NULL; size_t l = 0; @@ -470,7 +507,8 @@ ERROR: return r; } -PAKFIRE_EXPORT int pakfire_create(Pakfire* pakfire, const char* path, const char* arch) { +PAKFIRE_EXPORT int pakfire_create( + Pakfire* pakfire, const char* path, const char* arch, const char* conf) { char tempdir[PATH_MAX] = PAKFIRE_PRIVATE_DIR "/tmp/XXXXXX"; int r = 1; @@ -546,6 +584,11 @@ PAKFIRE_EXPORT int pakfire_create(Pakfire* pakfire, const char* path, const char if (r) goto ERROR; + // Read configuration file + r = pakfire_read_config(p, conf); + if (r) + goto ERROR; + // Set cache path snprintf(p->cache_path, sizeof(p->cache_path) - 1, "%s/%s/%s", PAKFIRE_CACHE_PATH, p->distro.id, p->distro.version_id); diff --git a/tests/pakfire.conf b/tests/pakfire.conf new file mode 100644 index 000000000..16c5415f5 --- /dev/null +++ b/tests/pakfire.conf @@ -0,0 +1 @@ +# This is the configuration file for the test environment diff --git a/tests/testsuite.c b/tests/testsuite.c index a615b3ed7..76d0e1531 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -41,7 +41,7 @@ static int test_run(int i, struct test* t) { LOG("running %s (%s)\n", t->name, root); // Create a pakfire instance - r = pakfire_create(&t->pakfire, root, NULL); + r = pakfire_create(&t->pakfire, root, NULL, TEST_SRC_PATH "/pakfire.conf"); if (r) { LOG("ERROR: Could not initialize Pakfire: %s\n", strerror(errno)); exit(1);