summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2014-12-16 10:53:58 +0100
committerThomas Haller <thaller@redhat.com>2015-02-03 13:01:53 +0100
commit3c7f71e44aadb65a8a661b7d57f08e653851fef5 (patch)
treed3c42772ba62042b8249cb4121e7294d6e6f0391
parentb814c3122acbf9105effd54976cb675672264930 (diff)
downloadNetworkManager-3c7f71e44aadb65a8a661b7d57f08e653851fef5.tar.gz
config: refactor read_config() to make it independent from NMConfig
-rw-r--r--src/nm-config.c53
1 files changed, 30 insertions, 23 deletions
diff --git a/src/nm-config.c b/src/nm-config.c
index 5ecfc0bbfc..c6eaa6559d 100644
--- a/src/nm-config.c
+++ b/src/nm-config.c
@@ -437,14 +437,15 @@ nm_config_cmd_line_options_add_to_entries (NMConfigCmdLineOptions *cli,
/************************************************************************/
static gboolean
-read_config (NMConfig *config, const char *path, GError **error)
+read_config (GKeyFile *keyfile, const char *path, GError **error)
{
- NMConfigPrivate *priv = NM_CONFIG_GET_PRIVATE (config);
GKeyFile *kf;
char **groups, **keys;
gsize ngroups, nkeys;
int g, k;
+ g_return_val_if_fail (keyfile, FALSE);
+
if (g_file_test (path, G_FILE_TEST_EXISTS) == FALSE) {
g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_NOT_FOUND, "file %s not found", path);
return FALSE;
@@ -471,22 +472,22 @@ read_config (NMConfig *config, const char *path, GError **error)
if (keys[k][len - 1] == '+') {
char *base_key = g_strndup (keys[k], len - 1);
- const char *old_val = g_key_file_get_value (priv->keyfile, groups[g], base_key, NULL);
+ const char *old_val = g_key_file_get_value (keyfile, groups[g], base_key, NULL);
const char *new_val = g_key_file_get_value (kf, groups[g], keys[k], NULL);
if (old_val && *old_val) {
char *combined = g_strconcat (old_val, ",", new_val, NULL);
- g_key_file_set_value (priv->keyfile, groups[g], base_key, combined);
+ g_key_file_set_value (keyfile, groups[g], base_key, combined);
g_free (combined);
} else
- g_key_file_set_value (priv->keyfile, groups[g], base_key, new_val);
+ g_key_file_set_value (keyfile, groups[g], base_key, new_val);
g_free (base_key);
continue;
}
- g_key_file_set_value (priv->keyfile, groups[g], keys[k],
+ g_key_file_set_value (keyfile, groups[g], keys[k],
v = g_key_file_get_value (kf, groups[g], keys[k], NULL));
g_free (v);
}
@@ -499,16 +500,22 @@ read_config (NMConfig *config, const char *path, GError **error)
}
static gboolean
-find_base_config (NMConfig *config, GError **error)
+read_base_config (GKeyFile *keyfile,
+ const char *cli_config_path,
+ char **out_config_path,
+ GError **error)
{
- NMConfigPrivate *priv = NM_CONFIG_GET_PRIVATE (config);
GError *my_error = NULL;
+ g_return_val_if_fail (keyfile, FALSE);
+ g_return_val_if_fail (out_config_path && !*out_config_path, FALSE);
+ g_return_val_if_fail (!error || !*error, FALSE);
+
/* Try a user-specified config file first */
- if (priv->cli.config_path) {
+ if (cli_config_path) {
/* Bad user-specific config file path is a hard error */
- if (read_config (config, priv->cli.config_path, error)) {
- priv->nm_conf_path = g_strdup (priv->cli.config_path);
+ if (read_config (keyfile, cli_config_path, error)) {
+ *out_config_path = g_strdup (cli_config_path);
return TRUE;
} else
return FALSE;
@@ -522,8 +529,8 @@ find_base_config (NMConfig *config, GError **error)
*/
/* Try deprecated nm-system-settings.conf first */
- if (read_config (config, NM_OLD_SYSTEM_CONF_FILE, &my_error)) {
- priv->nm_conf_path = g_strdup (NM_OLD_SYSTEM_CONF_FILE);
+ if (read_config (keyfile, NM_OLD_SYSTEM_CONF_FILE, &my_error)) {
+ *out_config_path = g_strdup (NM_OLD_SYSTEM_CONF_FILE);
return TRUE;
}
@@ -535,8 +542,8 @@ find_base_config (NMConfig *config, GError **error)
g_clear_error (&my_error);
/* Try the standard config file location next */
- if (read_config (config, NM_DEFAULT_SYSTEM_CONF_FILE, &my_error)) {
- priv->nm_conf_path = g_strdup (NM_DEFAULT_SYSTEM_CONF_FILE);
+ if (read_config (keyfile, NM_DEFAULT_SYSTEM_CONF_FILE, &my_error)) {
+ *out_config_path = g_strdup (NM_DEFAULT_SYSTEM_CONF_FILE);
return TRUE;
}
@@ -552,7 +559,7 @@ find_base_config (NMConfig *config, GError **error)
/* If for some reason no config file exists, use the default
* config file path.
*/
- priv->nm_conf_path = g_strdup (NM_DEFAULT_SYSTEM_CONF_FILE);
+ *out_config_path = g_strdup (NM_DEFAULT_SYSTEM_CONF_FILE);
nm_log_info (LOGD_CORE, "No config file found or given; using %s\n",
NM_DEFAULT_SYSTEM_CONF_FILE);
return TRUE;
@@ -671,18 +678,18 @@ nm_config_new (const NMConfigCmdLineOptions *cli, GError **error)
else
_nm_config_cmd_line_options_copy (cli, &priv->cli);
- /* First read the base config file */
- if (!find_base_config (self, error)) {
- g_object_unref (self);
- return NULL;
- }
-
/* Now read the overrides in the config dir */
if (priv->cli.config_dir)
priv->config_dir = g_strdup (priv->cli.config_dir);
else
priv->config_dir = g_strdup (NM_DEFAULT_SYSTEM_CONF_DIR);
+ /* First read the base config file */
+ if (!read_base_config (priv->keyfile, priv->cli.config_path, &priv->nm_conf_path, error)) {
+ g_object_unref (self);
+ return NULL;
+ }
+
confs = g_ptr_array_new_with_free_func (g_free);
config_description = g_string_new (priv->nm_conf_path);
dir = g_file_new_for_path (priv->config_dir);
@@ -707,7 +714,7 @@ nm_config_new (const NMConfigCmdLineOptions *cli, GError **error)
g_ptr_array_sort (confs, sort_asciibetically);
priv->config_description = g_string_free (config_description, FALSE);
for (i = 0; i < confs->len; i++) {
- if (!read_config (self, confs->pdata[i], error)) {
+ if (!read_config (priv->keyfile, confs->pdata[i], error)) {
g_object_unref (self);
self = NULL;
break;