|
|
4b6aa8 |
From 800aadd6db1ba4429474769b059cc62e75a2c00d Mon Sep 17 00:00:00 2001
|
|
|
4b6aa8 |
From: Matej Habrnal <mhabrnal@redhat.com>
|
|
|
4b6aa8 |
Date: Fri, 12 Feb 2016 14:40:21 +0100
|
|
|
4b6aa8 |
Subject: [PATCH] conf files: be able to make directories optional
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
Related to #316
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
Signed-off-by: Jakub Filak <jfilak@redhat.com>
|
|
|
4b6aa8 |
Signed-off-by: Matej Habrnal <mhabrnal@redhat.com>
|
|
|
4b6aa8 |
---
|
|
|
4b6aa8 |
src/include/internal_libreport.h | 10 ++++++++++
|
|
|
4b6aa8 |
src/lib/configuration_files.c | 19 +++++++++++++++----
|
|
|
4b6aa8 |
tests/configuration_files.at | 35 +++++++++++++++++++++++++++++++++++
|
|
|
4b6aa8 |
3 files changed, 60 insertions(+), 4 deletions(-)
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
diff --git a/src/include/internal_libreport.h b/src/include/internal_libreport.h
|
|
|
4b6aa8 |
index 108f4f1..2046e69 100644
|
|
|
4b6aa8 |
--- a/src/include/internal_libreport.h
|
|
|
4b6aa8 |
+++ b/src/include/internal_libreport.h
|
|
|
4b6aa8 |
@@ -744,6 +744,16 @@ const char *get_user_conf_base_dir(void);
|
|
|
4b6aa8 |
#define load_conf_file_from_dirs libreport_load_conf_file_from_dirs
|
|
|
4b6aa8 |
bool load_conf_file_from_dirs(const char *base_name, const char *const *directories, map_string_t *settings, bool skipKeysWithoutValue);
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
+enum {
|
|
|
4b6aa8 |
+ CONF_DIR_FLAG_NONE = 0,
|
|
|
4b6aa8 |
+ CONF_DIR_FLAG_OPTIONAL = 1,
|
|
|
4b6aa8 |
+};
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+#define load_conf_file_from_dirs_ext libreport_load_conf_file_from_dirs_ext
|
|
|
4b6aa8 |
+bool load_conf_file_from_dirs_ext(const char *base_name, const char *const *directories,
|
|
|
4b6aa8 |
+ const int * dir_flags, map_string_t *settings,
|
|
|
4b6aa8 |
+ bool skipKeysWithoutValue);
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
#define save_conf_file libreport_save_conf_file
|
|
|
4b6aa8 |
bool save_conf_file(const char *path, map_string_t *settings);
|
|
|
4b6aa8 |
#define save_plugin_conf_file libreport_save_plugin_conf_file
|
|
|
4b6aa8 |
diff --git a/src/lib/configuration_files.c b/src/lib/configuration_files.c
|
|
|
4b6aa8 |
index b145b9f..3520d93 100644
|
|
|
4b6aa8 |
--- a/src/lib/configuration_files.c
|
|
|
4b6aa8 |
+++ b/src/lib/configuration_files.c
|
|
|
4b6aa8 |
@@ -329,6 +329,11 @@ const char *get_user_conf_base_dir(void)
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
bool load_conf_file_from_dirs(const char *base_name, const char *const *directories, map_string_t *settings, bool skipKeysWithoutValue)
|
|
|
4b6aa8 |
{
|
|
|
4b6aa8 |
+ return load_conf_file_from_dirs_ext(base_name, directories, NULL, settings, skipKeysWithoutValue);
|
|
|
4b6aa8 |
+}
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+bool load_conf_file_from_dirs_ext(const char *base_name, const char *const *directories, const int *dir_flags, map_string_t *settings, bool skipKeysWithoutValue)
|
|
|
4b6aa8 |
+{
|
|
|
4b6aa8 |
if (NULL == directories || NULL == *directories)
|
|
|
4b6aa8 |
{
|
|
|
4b6aa8 |
log_error("No configuration directory specified");
|
|
|
4b6aa8 |
@@ -336,13 +341,19 @@ bool load_conf_file_from_dirs(const char *base_name, const char *const *director
|
|
|
4b6aa8 |
}
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
bool result = true;
|
|
|
4b6aa8 |
- for (const char *const *dir = directories; *dir != NULL; ++dir)
|
|
|
4b6aa8 |
+ for (size_t i = 0; directories[i] != NULL; ++i)
|
|
|
4b6aa8 |
{
|
|
|
4b6aa8 |
- char *conf_file = concat_path_file(*dir, base_name);
|
|
|
4b6aa8 |
+ char *conf_file = concat_path_file(directories[i], base_name);
|
|
|
4b6aa8 |
if (!load_conf_file(conf_file, settings, skipKeysWithoutValue))
|
|
|
4b6aa8 |
{
|
|
|
4b6aa8 |
- perror_msg("Can't open '%s'", conf_file);
|
|
|
4b6aa8 |
- result = false;
|
|
|
4b6aa8 |
+ if (dir_flags && (dir_flags[i] & CONF_DIR_FLAG_OPTIONAL))
|
|
|
4b6aa8 |
+ log_notice("Can't open '%s'", conf_file);
|
|
|
4b6aa8 |
+ else
|
|
|
4b6aa8 |
+ {
|
|
|
4b6aa8 |
+ perror_msg("Can't open '%s'", conf_file);
|
|
|
4b6aa8 |
+ result = false;
|
|
|
4b6aa8 |
+ }
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
}
|
|
|
4b6aa8 |
free(conf_file);
|
|
|
4b6aa8 |
}
|
|
|
4b6aa8 |
diff --git a/tests/configuration_files.at b/tests/configuration_files.at
|
|
|
4b6aa8 |
index f9275e1..3c75ffb 100644
|
|
|
4b6aa8 |
--- a/tests/configuration_files.at
|
|
|
4b6aa8 |
+++ b/tests/configuration_files.at
|
|
|
4b6aa8 |
@@ -321,6 +321,41 @@ int main(int argc, char **argv)
|
|
|
4b6aa8 |
}
|
|
|
4b6aa8 |
]])
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
+## ---------------------------- ##
|
|
|
4b6aa8 |
+## load_conf_file_from_dirs_ext ##
|
|
|
4b6aa8 |
+## ---------------------------- ##
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+AT_TESTFUN([load_conf_file_from_dirs_ext],
|
|
|
4b6aa8 |
+[[
|
|
|
4b6aa8 |
+#include "internal_libreport.h"
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+#define CONF_NAME "file.conf"
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+int main(void)
|
|
|
4b6aa8 |
+{
|
|
|
4b6aa8 |
+ g_verbose = 3;
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ {
|
|
|
4b6aa8 |
+ const char *const dir_vec[] = {
|
|
|
4b6aa8 |
+ "../../conf/second",
|
|
|
4b6aa8 |
+ "/org/freedesktop/problems/invalid",
|
|
|
4b6aa8 |
+ NULL,
|
|
|
4b6aa8 |
+ };
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ int dir_flags_vec[] = {
|
|
|
4b6aa8 |
+ CONF_DIR_FLAG_NONE,
|
|
|
4b6aa8 |
+ CONF_DIR_FLAG_OPTIONAL,
|
|
|
4b6aa8 |
+ -1,
|
|
|
4b6aa8 |
+ };
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ map_string_t *settings = new_map_string();
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ assert(load_conf_file_from_dirs_ext(CONF_NAME, dir_vec, dir_flags_vec, settings, 0));
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ free_map_string(settings);
|
|
|
4b6aa8 |
+ }
|
|
|
4b6aa8 |
+}
|
|
|
4b6aa8 |
+]])
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
## ---------------##
|
|
|
4b6aa8 |
## save_conf_file ##
|
|
|
4b6aa8 |
--
|
|
|
4b6aa8 |
1.8.3.1
|
|
|
4b6aa8 |
|