]> git.ipfire.org Git - pakfire.git/commitdiff
pakfire: Read configuration file on initialization
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 16 Apr 2021 14:32:59 +0000 (14:32 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 16 Apr 2021 14:32:59 +0000 (14:32 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
configure.ac
src/_pakfire/pakfire.c
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/pakfire.c
tests/pakfire.conf [new file with mode: 0644]
tests/testsuite.c

index e82b4abb1bb24492af6c396fcf4c92fd04e50402..848861e85853789689dafa601350599e831ffd93 100644 (file)
@@ -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 \
        \
index db24f1cfece1e424c1c4370ea6e45c3e4be4b51e..6f32e90ca4172d12dd320077070f026aed2ba680 100644 (file)
@@ -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}"],
index 88ef89f59e06160ee66b535250025543968c1469..98e774a6013d000b155e438651f65e7a796fc9df 100644 (file)
@@ -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
index bda5297b653c6d28d9a8169c864a5469810c31f6..32ffbfa46eb1c25db108196a73a46d548071d2c7 100644 (file)
@@ -29,7 +29,7 @@
 #include <pakfire/parser.h>
 #include <pakfire/types.h>
 
-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);
index 211746e50209448b27b4616c095e3a687d592632..dbedffc0fde70bf47b2fc8ebdf28bfa66f67d315 100644 (file)
@@ -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 (file)
index 0000000..16c5415
--- /dev/null
@@ -0,0 +1 @@
+# This is the configuration file for the test environment
index a615b3ed7943bfd332f3bd8e1dba8a82911d41f9..76d0e1531372798a11c91fc91ffcf3af8b02150c 100644 (file)
@@ -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);