Added a merge option to optionally reload files instead of merging them

This commit is contained in:
Martin Willi
2011-04-15 10:07:13 +02:00
parent ed49e9a303
commit 32973044b0
3 changed files with 32 additions and 13 deletions
+1 -1
View File
@@ -79,7 +79,7 @@ static bool load_configs(char *suite_file, char *test_file)
return FALSE; return FALSE;
} }
conftest->test = settings_create(suite_file); 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)); conftest->suite_dir = strdup(dirname(suite_file));
return TRUE; return TRUE;
} }
+21 -6
View File
@@ -151,6 +151,17 @@ static void section_destroy(section_t *this)
free(this); free(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 * callback to find a section by 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. * All files (even included ones) have to be loaded successfully.
*/ */
static bool load_files_internal(private_settings_t *this, section_t *parent, static bool load_files_internal(private_settings_t *this, section_t *parent,
char *pattern) char *pattern, bool merge)
{ {
char *text; char *text;
linked_list_t *contents = linked_list_create(); 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); this->lock->write_lock(this->lock);
if (!merge)
{
section_purge(parent);
}
/* extend parent section */ /* extend parent section */
section_extend(parent, section); section_extend(parent, section);
/* move contents of loaded files to main store */ /* 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, 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, 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; section_t *section;
va_list args; va_list args;
@@ -1150,7 +1165,7 @@ METHOD(settings_t, load_files_section, bool,
{ {
return FALSE; return FALSE;
} }
return load_files_internal(this, section, pattern); return load_files_internal(this, section, pattern, merge);
} }
METHOD(settings_t, destroy, void, METHOD(settings_t, destroy, void,
@@ -1197,7 +1212,7 @@ settings_t *settings_create(char *file)
file = STRONGSWAN_CONF; file = STRONGSWAN_CONF;
} }
load_files(this, file); load_files(this, file, FALSE);
return &this->public; return &this->public;
} }
+10 -6
View File
@@ -261,22 +261,25 @@ struct settings_t {
/** /**
* Load settings from the files matching the given pattern. * Load settings from the files matching the given pattern.
* *
* Existing sections are extended, existing values replaced, by those found * If merge is TRUE, existing sections are extended, existing values
* in the loaded files. * 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 * @note If any of the files matching the pattern fails to load, no settings
* are added at all. So, it's all or nothing. * are added at all. So, it's all or nothing.
* *
* @param pattern file pattern * @param pattern file pattern
* @param merge TRUE to merge config with existing values
* @return TRUE, if settings were loaded successfully * @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. * Load settings from the files matching the given pattern.
* *
* Existing sections are extended, existing values replaced, by those found * If merge is TRUE, existing sections are extended, existing values
* in the loaded files. * 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 * All settings are loaded relative to the given section. The section is
* created, if it does not yet exist. * created, if it does not yet exist.
@@ -285,11 +288,12 @@ struct settings_t {
* are added at all. So, it's all or nothing. * are added at all. So, it's all or nothing.
* *
* @param pattern file pattern * @param pattern file pattern
* @param merge TRUE to merge config with existing values
* @param section section name of parent section, printf style * @param section section name of parent section, printf style
* @param ... argument list for section * @param ... argument list for section
* @return TRUE, if settings were loaded successfully * @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, ...); char *section, ...);
/** /**