Blame SOURCES/0164-conf-files-be-able-to-make-directories-optional.patch

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