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