From 72eef224fdac23b184b94fba8a875627222bde77 Mon Sep 17 00:00:00 2001 From: Stefan Schubert Date: Thu, 5 Oct 2023 17:14:29 +0200 Subject: [PATCH] fixed testcase --- lib/libeconf.c | 26 +++++++++---------- lib/libeconf.map | 7 ++++- tests/meson.build | 4 +-- .../run/foo}/getconfdir.conf | 0 .../foo}/getconfdir.conf.d/1-override.conf | 0 .../foo}/getconfdir.conf.d/5-override.conf | 0 .../run/foo}/getconfdir.conf.d/9-broken | 0 .../usr/etc/foo}/getconfdir.conf | 0 tests/{tst-getconfdirs9.c => tst-uapi-1.c} | 23 +++++++++------- 9 files changed, 35 insertions(+), 25 deletions(-) rename tests/{tst-getconfdirs9-data/run => tst-uapi-1-data/run/foo}/getconfdir.conf (100%) rename tests/{tst-getconfdirs9-data/run => tst-uapi-1-data/run/foo}/getconfdir.conf.d/1-override.conf (100%) rename tests/{tst-getconfdirs9-data/run => tst-uapi-1-data/run/foo}/getconfdir.conf.d/5-override.conf (100%) rename tests/{tst-getconfdirs9-data/run => tst-uapi-1-data/run/foo}/getconfdir.conf.d/9-broken (100%) rename tests/{tst-getconfdirs9-data/usr/etc => tst-uapi-1-data/usr/etc/foo}/getconfdir.conf (100%) rename tests/{tst-getconfdirs9.c => tst-uapi-1.c} (69%) diff --git a/lib/libeconf.c b/lib/libeconf.c index 36a233a..abe5ecc 100644 --- a/lib/libeconf.c +++ b/lib/libeconf.c @@ -282,20 +282,22 @@ econf_err econf_readConfigWithCallback(econf_file **key_file, bool (*callback)(const char *filename, const void *data), const void *callback_data) { - char *run_dir = DEFAULT_RUN_SUBDIR; - char *etc_dir = DEFAULT_ETC_SUBDIR; + char usr_dir[PATH_MAX]; + char run_dir[PATH_MAX]; + char etc_dir[PATH_MAX]; + if (project != NULL) { - if (asprintf(&run_dir, "%s/%s", DEFAULT_RUN_SUBDIR, project) < 0) { - return ECONF_NOMEM; - } - if (asprintf(&etc_dir, "%s/%s", DEFAULT_ETC_SUBDIR, project) < 0) { - free(run_dir); - return ECONF_NOMEM; - } - } + snprintf(usr_dir, sizeof(usr_dir),"%s/%s", usr_subdir, project); + snprintf(run_dir, sizeof(run_dir), "%s/%s", DEFAULT_RUN_SUBDIR, project); + snprintf(etc_dir, sizeof(etc_dir), "%s/%s", DEFAULT_ETC_SUBDIR, project); + } else { + snprintf(usr_dir, sizeof(usr_dir),"%s", usr_subdir); + snprintf(run_dir, sizeof(run_dir), "%s", DEFAULT_RUN_SUBDIR); + snprintf(etc_dir, sizeof(etc_dir), "%s", DEFAULT_ETC_SUBDIR); + } econf_err ret = readConfigWithCallback(key_file, - usr_subdir, + usr_dir, run_dir, etc_dir, config_name, @@ -306,8 +308,6 @@ econf_err econf_readConfigWithCallback(econf_file **key_file, conf_count, callback, callback_data); - free(run_dir); - free(etc_dir); return ret; } diff --git a/lib/libeconf.map b/lib/libeconf.map index dbabdda..1914c62 100644 --- a/lib/libeconf.map +++ b/lib/libeconf.map @@ -72,4 +72,9 @@ LIBECONF_0.5 { econf_readDirsWithCallback; econf_readDirsHistoryWithCallback; econf_set_conf_dirs; -} LIBECONF_0.4; \ No newline at end of file +} LIBECONF_0.4; +LIBECONF_0.6 { + global: + econf_readConfigWithCallback; + econf_readConfig; +} LIBECONF_0.5; \ No newline at end of file diff --git a/tests/meson.build b/tests/meson.build index 205c9d1..db481f7 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -70,8 +70,8 @@ tst_getconfdirs7_exe = executable('tst-getconfdirs7', 'tst-getconfdirs7.c', c_ar test('tst-getconfdirs7', tst_getconfdirs7_exe) tst_getconfdirs8_exe = executable('tst-getconfdirs8', 'tst-getconfdirs8.c', c_args: ['-DSUFFIX="conf"', test_args], dependencies : libeconf_dep) test('tst-getconfdirs8', tst_getconfdirs8_exe) -tst_getconfdirs9_exe = executable('tst-getconfdirs9', 'tst-getconfdirs9.c', c_args: test_args, dependencies : libeconf_dep) -test('tst-getconfdirs9', tst_getconfdirs9_exe) +tst_uapi_1_exe = executable('tst-uapi-1', sources: ['tst-uapi-1.c', '../lib/helpers.c', '../lib/readconfig.c', '../lib/mergefiles.c'], c_args: test_args, dependencies : libeconf_dep) +test('tst-uapi-1', tst_uapi_1_exe) tst_parse_error_exe = executable('tst-parse-error', 'tst-parse-error.c', c_args: test_args, dependencies : libeconf_dep) test('tst-parse-error', tst_parse_error_exe) diff --git a/tests/tst-getconfdirs9-data/run/getconfdir.conf b/tests/tst-uapi-1-data/run/foo/getconfdir.conf similarity index 100% rename from tests/tst-getconfdirs9-data/run/getconfdir.conf rename to tests/tst-uapi-1-data/run/foo/getconfdir.conf diff --git a/tests/tst-getconfdirs9-data/run/getconfdir.conf.d/1-override.conf b/tests/tst-uapi-1-data/run/foo/getconfdir.conf.d/1-override.conf similarity index 100% rename from tests/tst-getconfdirs9-data/run/getconfdir.conf.d/1-override.conf rename to tests/tst-uapi-1-data/run/foo/getconfdir.conf.d/1-override.conf diff --git a/tests/tst-getconfdirs9-data/run/getconfdir.conf.d/5-override.conf b/tests/tst-uapi-1-data/run/foo/getconfdir.conf.d/5-override.conf similarity index 100% rename from tests/tst-getconfdirs9-data/run/getconfdir.conf.d/5-override.conf rename to tests/tst-uapi-1-data/run/foo/getconfdir.conf.d/5-override.conf diff --git a/tests/tst-getconfdirs9-data/run/getconfdir.conf.d/9-broken b/tests/tst-uapi-1-data/run/foo/getconfdir.conf.d/9-broken similarity index 100% rename from tests/tst-getconfdirs9-data/run/getconfdir.conf.d/9-broken rename to tests/tst-uapi-1-data/run/foo/getconfdir.conf.d/9-broken diff --git a/tests/tst-getconfdirs9-data/usr/etc/getconfdir.conf b/tests/tst-uapi-1-data/usr/etc/foo/getconfdir.conf similarity index 100% rename from tests/tst-getconfdirs9-data/usr/etc/getconfdir.conf rename to tests/tst-uapi-1-data/usr/etc/foo/getconfdir.conf diff --git a/tests/tst-getconfdirs9.c b/tests/tst-uapi-1.c similarity index 69% rename from tests/tst-getconfdirs9.c rename to tests/tst-uapi-1.c index 86bbde7..e5f1930 100644 --- a/tests/tst-getconfdirs9.c +++ b/tests/tst-uapi-1.c @@ -6,14 +6,17 @@ #include #include "libeconf.h" +#include "../lib/helpers.h" +#include "../lib/mergefiles.h" +#include "../lib/readconfig.h" /* Test case: Test the systemd like behavior: - /usr/etc/getconfdir.conf exists - /run/getconfdir.conf exists - /etc/getconfidr.conf.d/.conf exists + /usr/foo/etc/getconfdir.conf exists + /run/foo/getconfdir.conf exists + /etc/foo/getconfidr.conf.d/.conf exists - libeconf should ignore /usr/etc/getconfdir.conf, as this contains + libeconf should ignore /usr/foo/etc/getconfdir.conf, as this contains */ static int @@ -53,13 +56,15 @@ main(void) int retval = 0; econf_err error; - error = econf_readDirs (&key_file, - TESTSDIR"tst-getconfdirs1-data/usr/etc", - TESTSDIR"tst-getconfdirs1-data/etc", - "getconfdir", "conf", "=", "#"); + error = readConfigWithCallback (&key_file, + TESTSDIR"tst-uapi-1-data/usr/etc/foo", + TESTSDIR"tst-uapi-1-data/run/foo", + TESTSDIR"tst-uapi-1-data/etc/foo", + "getconfdir", "conf", "=", "#", + NULL, 0, NULL, NULL); if (error) { - fprintf (stderr, "ERROR: econf_readDirs: %s\n", + fprintf (stderr, "ERROR: econf_readConfig: %s\n", econf_errString(error)); return 1; }