]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
using libeconf for reading blkid.conf
authorStefan Schubert <schubi@suse.de>
Fri, 1 Dec 2023 17:33:12 +0000 (18:33 +0100)
committerStefan Schubert <schubi@suse.de>
Wed, 6 Dec 2023 09:49:01 +0000 (10:49 +0100)
libblkid/meson.build
libblkid/src/Makemodule.am
libblkid/src/config.c

index 0cb134ff9f3b804db14a1954f80e4e7f5f8c57de..5a28bdfcb84e1fc6befc145dc9fafaa17e570270 100644 (file)
@@ -140,7 +140,7 @@ lib_blkid = both_libraries(
   version : libblkid_version,
   link_args : ['-Wl,--version-script=@0@'.format(libblkid_sym_path)],
   link_with : lib_common,
-  dependencies : build_libblkid ? [] : disabler(),
+  dependencies : build_libblkid ? [lib_econf] : disabler(),
   install : build_libblkid)
 blkid_dep = declare_dependency(link_with: lib_blkid, include_directories: '.')
 
index bc90eb8fde96a31e1e7c3efa81994b82b87a5418..3fae3f039ec15ead63ee403a430df72376bd77ef 100644 (file)
@@ -118,6 +118,9 @@ libblkid_la_SOURCES += \
 endif
 
 libblkid_la_LIBADD = libcommon.la
+if HAVE_ECONF
+libblkid_la_LIBADD += -leconf
+endif
 
 EXTRA_libblkid_la_DEPENDENCIES = \
        libblkid/src/libblkid.sym
index d3f5eea3ddf1257586b77128d44e55a5164fe16b..7b8b04f921136ed1eb9677ea16e562c0bb524527 100644 (file)
@@ -22,6 +22,9 @@
 #endif
 #include <stdint.h>
 #include <stdarg.h>
+#if defined (HAVE_LIBECONF)
+#include <libeconf.h>
+#endif
 
 #include "blkidP.h"
 #include "env.h"
@@ -55,6 +58,7 @@ err:
        return -1;
 }
 
+#ifndef HAVE_LIBECONF
 static int parse_next(FILE *fd, struct blkid_config *conf)
 {
        char buf[BUFSIZ];
@@ -111,25 +115,29 @@ static int parse_next(FILE *fd, struct blkid_config *conf)
        }
        return 0;
 }
+#endif /* !HAVE_LIBECONF */
 
 /* return real config data or built-in default */
 struct blkid_config *blkid_read_config(const char *filename)
 {
        struct blkid_config *conf;
-       FILE *f;
-
-       if (!filename)
-               filename = safe_getenv("BLKID_CONF");
-       if (!filename)
-               filename = BLKID_CONFIG_FILE;
 
        conf = calloc(1, sizeof(*conf));
        if (!conf)
                return NULL;
        conf->uevent = -1;
 
-       DBG(CONFIG, ul_debug("reading config file: %s.", filename));
 
+       if (!filename)
+               filename = safe_getenv("BLKID_CONF");
+
+#ifndef HAVE_LIBECONF
+
+       FILE *f;
+       if (!filename)
+               filename = BLKID_CONFIG_FILE;
+
+       DBG(CONFIG, ul_debug("reading config file: %s.", filename));
        f = fopen(filename, "r" UL_CLOEXECSTR);
        if (!f) {
                DBG(CONFIG, ul_debug("%s: does not exist, using built-in default", filename));
@@ -141,6 +149,81 @@ struct blkid_config *blkid_read_config(const char *filename)
                        goto err;
                }
        }
+
+#else /* !HAVE_LIBECONF */
+
+       static econf_file *file = NULL;
+       econf_err error;
+
+       if (filename) {
+               DBG(CONFIG, ul_debug("reading config file: %s.", filename));
+               error = econf_readFile(&file, filename, "= \t", "#");
+       } else {
+               error = econf_readDirs(&file,
+#if USE_VENDORDIR
+                                      _PATH_VENDORDIR,
+#else
+                                      NULL,
+#endif
+                                      "/etc", "blkid", "conf", "= \t", "#");
+       }
+
+       if (error) {
+               if (error == ECONF_NOFILE) {
+                       if (filename)
+                               DBG(CONFIG,
+                                   ul_debug("%s: does not exist, using built-in default", filename));
+                       else
+                               DBG(CONFIG,
+                                   ul_debug("No configuration file blkid.conf found, using built-in default "));
+                       goto dflt;
+               } else {
+                       if (filename)
+                               DBG(CONFIG, ul_debug("%s: parse error:%s", filename, econf_errString(error)));
+                       else
+                               DBG(CONFIG, ul_debug("parse error:%s", econf_errString(error)));
+
+                       goto err;
+               }
+       }
+
+       bool uevent = false;
+       if ((error = econf_getBoolValue(file, NULL, "SEND_UEVENT", &uevent))) {
+               if (error != ECONF_NOKEY) {
+                       DBG(CONFIG, ul_debug("couldn't fetch SEND_UEVENT corrently: %s", econf_errString(error)));
+                       goto err;
+               } else {
+                       DBG(CONFIG, ul_debug("key SEND_UEVENT not found, using built-in default "));
+               }
+       } else {
+               conf->uevent = uevent ? TRUE : FALSE;
+       }
+
+       if ((error = econf_getStringValue(file, NULL, "CACHE_FILE", &(conf->cachefile)))) {
+               conf->cachefile = NULL;
+               if (error != ECONF_NOKEY) {
+                       DBG(CONFIG, ul_debug("couldn't fetch CACHE_FILE correctly: %s", econf_errString(error)));
+                       goto err;
+               } else {
+                       DBG(CONFIG, ul_debug("key CACHE_FILE not found, using built-in default "));
+               }
+       }
+
+       char *line = NULL;
+       if ((error = econf_getStringValue(file, NULL, "EVALUATE", &line))) {
+               conf->nevals = 0;
+               if (error != ECONF_NOKEY) {
+                       DBG(CONFIG, ul_debug("couldn't fetch EVALUATE correctly: %s", econf_errString(error)));
+                       goto err;
+               } else {
+                       DBG(CONFIG, ul_debug("key CACHE_FILE not found, using built-in default "));
+               }
+       } else {
+               if (*line && parse_evaluate(conf, line) == -1)
+                       goto err;
+       }
+
+#endif /* HAVE_LIBECONF */
 dflt:
        if (!conf->nevals) {
                conf->eval[0] = BLKID_EVAL_UDEV;
@@ -151,13 +234,23 @@ dflt:
                conf->cachefile = strdup(BLKID_CACHE_FILE);
        if (conf->uevent == -1)
                conf->uevent = TRUE;
+#ifndef HAVE_LIBECONF
        if (f)
                fclose(f);
+#else
+       econf_free (file);
+       free (line);
+#endif
        return conf;
 err:
        free(conf->cachefile);
        free(conf);
+#ifndef HAVE_LIBECONF
        fclose(f);
+#else
+       econf_free (file);
+       free (line);
+#endif
        return NULL;
 }