diff options
author | Thomas Haller <thaller@redhat.com> | 2015-06-08 13:30:30 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2015-07-02 14:21:40 +0200 |
commit | 10f3ef8cc7a128c3242d2aa635cddf47761cd4cb (patch) | |
tree | e72707aa2eec70361c8c1c971ad3489d3118e924 | |
parent | b3d1be2bfd1771242855f3becdf2f23add376098 (diff) | |
download | NetworkManager-10f3ef8cc7a128c3242d2aa635cddf47761cd4cb.tar.gz |
libnm: add keyfile utility functions
-rw-r--r-- | libnm-core/nm-keyfile-internal.h | 5 | ||||
-rw-r--r-- | libnm-core/nm-keyfile-utils.c | 79 | ||||
-rw-r--r-- | libnm-core/tests/test-keyfile.c | 37 | ||||
-rw-r--r-- | src/nm-config-data.c | 31 |
4 files changed, 88 insertions, 64 deletions
diff --git a/libnm-core/nm-keyfile-internal.h b/libnm-core/nm-keyfile-internal.h index 90af562cdf..bce5e8726a 100644 --- a/libnm-core/nm-keyfile-internal.h +++ b/libnm-core/nm-keyfile-internal.h @@ -162,5 +162,10 @@ GKeyFile *nm_keyfile_write (NMConnection *connection, char *nm_keyfile_plugin_kf_get_string (GKeyFile *kf, const char *group, const char *key, GError **error); void nm_keyfile_plugin_kf_set_string (GKeyFile *kf, const char *group, const char *key, const char *value); +void _nm_keyfile_copy (GKeyFile *dst, GKeyFile *src); +gboolean _nm_keyfile_a_contains_all_in_b (GKeyFile *kf_a, GKeyFile *kf_b); +gboolean _nm_keyfile_equals (GKeyFile *kf_a, GKeyFile *kf_b); +gboolean _nm_keyfile_has_values (GKeyFile *keyfile); + #endif /* __NM_KEYFILE_INTERNAL_H__ */ diff --git a/libnm-core/nm-keyfile-utils.c b/libnm-core/nm-keyfile-utils.c index 61b30ab9c8..1ddc1b59e6 100644 --- a/libnm-core/nm-keyfile-utils.c +++ b/libnm-core/nm-keyfile-utils.c @@ -24,6 +24,7 @@ #include <stdlib.h> #include <string.h> +#include "gsystem-local-alloc.h" #include "nm-keyfile-utils.h" #include "nm-keyfile-internal.h" #include "nm-setting-wired.h" @@ -204,4 +205,82 @@ nm_keyfile_plugin_kf_has_key (GKeyFile *kf, return has; } +/************************************************************************/ +void +_nm_keyfile_copy (GKeyFile *dst, GKeyFile *src) +{ + gs_strfreev char **groups = NULL; + guint g, k; + + groups = g_key_file_get_groups (src, NULL); + for (g = 0; groups && groups[g]; g++) { + const char *group = groups[g]; + gs_strfreev char **keys = NULL; + + keys = g_key_file_get_keys (src, group, NULL, NULL); + if (!keys) + continue; + + for (k = 0; keys[k]; k++) { + const char *key = keys[k]; + gs_free char *value = NULL; + + value = g_key_file_get_value (src, group, key, NULL); + if (value) + g_key_file_set_value (dst, group, key, value); + else + g_key_file_remove_key (dst, group, key, NULL); + } + } +} + +/************************************************************************/ + +gboolean +_nm_keyfile_a_contains_all_in_b (GKeyFile *kf_a, GKeyFile *kf_b) +{ + gs_strfreev char **groups = NULL; + guint i, j; + + if (kf_a == kf_b) + return TRUE; + if (!kf_a || !kf_b) + return FALSE; + + groups = g_key_file_get_groups (kf_a, NULL); + for (i = 0; groups && groups[i]; i++) { + gs_strfreev char **keys = NULL; + + keys = g_key_file_get_keys (kf_a, groups[i], NULL, NULL); + if (!keys) + continue; + + for (j = 0; keys[j]; j++) { + gs_free char *key_a = g_key_file_get_value (kf_a, groups[i], keys[j], NULL); + gs_free char *key_b = g_key_file_get_value (kf_b, groups[i], keys[j], NULL); + + if (g_strcmp0 (key_a, key_b) != 0) + return FALSE; + } + } + return TRUE; +} + +gboolean +_nm_keyfile_equals (GKeyFile *kf_a, GKeyFile *kf_b) +{ + return _nm_keyfile_a_contains_all_in_b (kf_a, kf_b) + && _nm_keyfile_a_contains_all_in_b (kf_b, kf_a); +} + +gboolean +_nm_keyfile_has_values (GKeyFile *keyfile) +{ + gs_strfreev char **groups; + + g_return_val_if_fail (keyfile, FALSE); + + groups = g_key_file_get_groups (keyfile, NULL); + return groups && groups[0]; +} diff --git a/libnm-core/tests/test-keyfile.c b/libnm-core/tests/test-keyfile.c index 28d659f985..f9eb574b0c 100644 --- a/libnm-core/tests/test-keyfile.c +++ b/libnm-core/tests/test-keyfile.c @@ -81,39 +81,6 @@ _keyfile_load_from_data (const char *str) return keyfile; } -static gboolean -_keyfile_a_contains_all_in_b (GKeyFile *kf_a, GKeyFile *kf_b) -{ - gs_strfreev char **groups = NULL; - guint i, j; - - if (kf_a == kf_b) - return TRUE; - - groups = g_key_file_get_groups (kf_a, NULL); - for (i = 0; groups && groups[i]; i++) { - gs_strfreev char **keys = NULL; - - keys = g_key_file_get_keys (kf_a, groups[i], NULL, NULL); - if (keys) { - for (j = 0; keys[j]; j++) { - gs_free char *key_a = g_key_file_get_value (kf_a, groups[i], keys[j], NULL); - gs_free char *key_b = g_key_file_get_value (kf_b, groups[i], keys[j], NULL); - - if (g_strcmp0 (key_a, key_b) != 0) - return FALSE; - } - } - } - return TRUE; -} - -static gboolean -_keyfile_equals (GKeyFile *kf_a, GKeyFile *kf_b) -{ - return _keyfile_a_contains_all_in_b (kf_a, kf_b) && _keyfile_a_contains_all_in_b (kf_b, kf_a); -} - static GKeyFile * _nm_keyfile_write (NMConnection *connection, NMKeyfileWriteHandler handler, @@ -185,7 +152,7 @@ _keyfile_convert (NMConnection **con, c0_k1_c2 = _nm_keyfile_read (c0_k1, keyfile_name, base_dir, read_handler, read_data, FALSE); c0_k1_c2_k3 = _nm_keyfile_write (c0_k1_c2, write_handler, write_data); - _keyfile_equals (c0_k1, c0_k1_c2_k3); + _nm_keyfile_equals (c0_k1, c0_k1_c2_k3); } if (k0) { NMSetting8021x *s1, *s2; @@ -247,7 +214,7 @@ _keyfile_convert (NMConnection **con, else { /* finally, if both a keyfile and a connection are given, assert that they are equal * after a round of conversion. */ - _keyfile_equals (c0_k1, k0_c1_k2); + _nm_keyfile_equals (c0_k1, k0_c1_k2); nmtst_assert_connection_equals (k0_c1, FALSE, c0_k1_c2, FALSE); } } diff --git a/src/nm-config-data.c b/src/nm-config-data.c index 9e6ac78733..c8376132f7 100644 --- a/src/nm-config-data.c +++ b/src/nm-config-data.c @@ -27,6 +27,7 @@ #include "gsystem-local-alloc.h" #include "nm-device.h" #include "nm-core-internal.h" +#include "nm-keyfile-internal.h" #include "nm-macros-internal.h" typedef struct { @@ -295,33 +296,6 @@ _get_connection_infos (GKeyFile *keyfile) /************************************************************************/ static gboolean -_keyfile_a_contains_all_in_b (GKeyFile *kf_a, GKeyFile *kf_b) -{ - gs_strfreev char **groups = NULL; - guint i, j; - - if (kf_a == kf_b) - return TRUE; - - groups = g_key_file_get_groups (kf_a, NULL); - for (i = 0; groups && groups[i]; i++) { - gs_strfreev char **keys = NULL; - - keys = g_key_file_get_keys (kf_a, groups[i], NULL, NULL); - if (keys) { - for (j = 0; keys[j]; j++) { - gs_free char *key_a = g_key_file_get_value (kf_a, groups[i], keys[j], NULL); - gs_free char *key_b = g_key_file_get_value (kf_b, groups[i], keys[j], NULL); - - if (g_strcmp0 (key_a, key_b) != 0) - return FALSE; - } - } - } - return TRUE; -} - -static gboolean _slist_str_equals (GSList *a, GSList *b) { while (a && b && g_strcmp0 (a->data, b->data) == 0) { @@ -343,8 +317,7 @@ nm_config_data_diff (NMConfigData *old_data, NMConfigData *new_data) priv_old = NM_CONFIG_DATA_GET_PRIVATE (old_data); priv_new = NM_CONFIG_DATA_GET_PRIVATE (new_data); - if ( !_keyfile_a_contains_all_in_b (priv_old->keyfile, priv_new->keyfile) - || !_keyfile_a_contains_all_in_b (priv_new->keyfile, priv_old->keyfile)) + if (!_nm_keyfile_equals (priv_old->keyfile, priv_new->keyfile)) changes |= NM_CONFIG_CHANGE_VALUES; if ( g_strcmp0 (nm_config_data_get_config_main_file (old_data), nm_config_data_get_config_main_file (new_data)) != 0 |