Added a merge option to optionally reload files instead of merging them
authorMartin Willi <martin@revosec.ch>
Tue, 12 Apr 2011 09:13:08 +0000 (11:13 +0200)
committerMartin Willi <martin@revosec.ch>
Fri, 15 Apr 2011 08:07:13 +0000 (10:07 +0200)
src/conftest/conftest.c
src/libstrongswan/settings.c
src/libstrongswan/settings.h

index fea8881..48bf968 100644 (file)
@@ -79,7 +79,7 @@ static bool load_configs(char *suite_file, char *test_file)
                return FALSE;
        }
        conftest->test = settings_create(suite_file);
-       conftest->test->load_files(conftest->test, test_file);
+       conftest->test->load_files(conftest->test, test_file, TRUE);
        conftest->suite_dir = strdup(dirname(suite_file));
        return TRUE;
 }
index 82a24e9..c26a74a 100644 (file)
@@ -152,6 +152,17 @@ static void section_destroy(section_t *this)
 }
 
 /**
+ * Purge contents of a section
+ */
+static void section_purge(section_t *this)
+{
+       this->kv->destroy_function(this->kv, (void*)kv_destroy);
+       this->kv = linked_list_create();
+       this->sections->destroy_function(this->sections, (void*)section_destroy);
+       this->sections = linked_list_create();
+}
+
+/**
  * callback to find a section by name
  */
 static bool section_find(section_t *this, char *name)
@@ -1102,7 +1113,7 @@ static void section_extend(section_t *base, section_t *extension)
  * All files (even included ones) have to be loaded successfully.
  */
 static bool load_files_internal(private_settings_t *this, section_t *parent,
-                                                               char *pattern)
+                                                               char *pattern, bool merge)
 {
        char *text;
        linked_list_t *contents = linked_list_create();
@@ -1116,6 +1127,10 @@ static bool load_files_internal(private_settings_t *this, section_t *parent,
        }
 
        this->lock->write_lock(this->lock);
+       if (!merge)
+       {
+               section_purge(parent);
+       }
        /* extend parent section */
        section_extend(parent, section);
        /* move contents of loaded files to main store */
@@ -1131,13 +1146,13 @@ static bool load_files_internal(private_settings_t *this, section_t *parent,
 }
 
 METHOD(settings_t, load_files, bool,
-          private_settings_t *this, char *pattern)
+          private_settings_t *this, char *pattern, bool merge)
 {
-       return load_files_internal(this, this->top, pattern);
+       return load_files_internal(this, this->top, pattern, merge);
 }
 
 METHOD(settings_t, load_files_section, bool,
-          private_settings_t *this, char *pattern, char *key, ...)
+          private_settings_t *this, char *pattern, bool merge, char *key, ...)
 {
        section_t *section;
        va_list args;
@@ -1150,7 +1165,7 @@ METHOD(settings_t, load_files_section, bool,
        {
                return FALSE;
        }
-       return load_files_internal(this, section, pattern);
+       return load_files_internal(this, section, pattern, merge);
 }
 
 METHOD(settings_t, destroy, void,
@@ -1197,7 +1212,7 @@ settings_t *settings_create(char *file)
                file = STRONGSWAN_CONF;
        }
 
-       load_files(this, file);
+       load_files(this, file, FALSE);
 
        return &this->public;
 }
index bc3df37..9ccd023 100644 (file)
@@ -261,22 +261,25 @@ struct settings_t {
        /**
         * Load settings from the files matching the given pattern.
         *
-        * Existing sections are extended, existing values replaced, by those found
-        * in the loaded files.
+        * If merge is TRUE, existing sections are extended, existing values
+        * replaced, by those found in the loaded files. If it is FALSE, existing
+        * sections are purged before reading the new config.
         *
         * @note If any of the files matching the pattern fails to load, no settings
         * are added at all. So, it's all or nothing.
         *
         * @param pattern       file pattern
+        * @param merge         TRUE to merge config with existing values
         * @return                      TRUE, if settings were loaded successfully
         */
-       bool (*load_files)(settings_t *this, char *pattern);
+       bool (*load_files)(settings_t *this, char *pattern, bool merge);
 
        /**
         * Load settings from the files matching the given pattern.
         *
-        * Existing sections are extended, existing values replaced, by those found
-        * in the loaded files.
+        * If merge is TRUE, existing sections are extended, existing values
+        * replaced, by those found in the loaded files. If it is FALSE, existing
+        * sections are purged before reading the new config.
         *
         * All settings are loaded relative to the given section. The section is
         * created, if it does not yet exist.
@@ -285,11 +288,12 @@ struct settings_t {
         * are added at all. So, it's all or nothing.
         *
         * @param pattern       file pattern
+        * @param merge         TRUE to merge config with existing values
         * @param section       section name of parent section, printf style
         * @param ...           argument list for section
         * @return                      TRUE, if settings were loaded successfully
         */
-       bool (*load_files_section)(settings_t *this, char *pattern,
+       bool (*load_files_section)(settings_t *this, char *pattern, bool merge,
                                                           char *section, ...);
 
        /**