diff options
author | Thomas Haller <thaller@redhat.com> | 2015-06-11 21:12:00 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2015-07-02 15:50:03 +0200 |
commit | 7fbfaf567df0562e6b9c39954087dc897c5e05b6 (patch) | |
tree | b1baf2614cd4fb74ab7eba18b698eee2c8e6bd2d | |
parent | e1b0195c67d16f6fd44770828e912d190f2cd888 (diff) | |
download | NetworkManager-7fbfaf567df0562e6b9c39954087dc897c5e05b6.tar.gz |
libnm: consider ordering for _nm_keyfile_equals()
GKeyFile considers the order of the files, so add a possibility
to check whether to keyfiles are equal -- also with respect to
the order of the elements.
-rw-r--r-- | libnm-core/nm-keyfile-internal.h | 2 | ||||
-rw-r--r-- | libnm-core/nm-keyfile-utils.c | 62 | ||||
-rw-r--r-- | libnm-core/tests/test-keyfile.c | 4 | ||||
-rw-r--r-- | src/nm-config-data.c | 2 |
4 files changed, 63 insertions, 7 deletions
diff --git a/libnm-core/nm-keyfile-internal.h b/libnm-core/nm-keyfile-internal.h index bce5e8726a..f4bb079637 100644 --- a/libnm-core/nm-keyfile-internal.h +++ b/libnm-core/nm-keyfile-internal.h @@ -164,7 +164,7 @@ void nm_keyfile_plugin_kf_set_string (GKeyFile *kf, const char *group, const cha 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_equals (GKeyFile *kf_a, GKeyFile *kf_b, gboolean consider_order); gboolean _nm_keyfile_has_values (GKeyFile *keyfile); diff --git a/libnm-core/nm-keyfile-utils.c b/libnm-core/nm-keyfile-utils.c index 1ddc1b59e6..c05f4cd26f 100644 --- a/libnm-core/nm-keyfile-utils.c +++ b/libnm-core/nm-keyfile-utils.c @@ -267,11 +267,67 @@ _nm_keyfile_a_contains_all_in_b (GKeyFile *kf_a, GKeyFile *kf_b) return TRUE; } + +static gboolean +_nm_keyfile_equals_ordered (GKeyFile *kf_a, GKeyFile *kf_b) +{ + gs_strfreev char **groups = NULL; + gs_strfreev char **groups_b = 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); + groups_b = g_key_file_get_groups (kf_b, NULL); + if (!groups && !groups_b) + return TRUE; + if (!groups || !groups_b) + return FALSE; + for (i = 0; groups[i] && groups_b[i] && !strcmp (groups[i], groups_b[i]); i++) + ; + if (groups[i] || groups_b[i]) + return FALSE; + + for (i = 0; groups[i]; i++) { + gs_strfreev char **keys = NULL; + gs_strfreev char **keys_b = NULL; + + keys = g_key_file_get_keys (kf_a, groups[i], NULL, NULL); + keys_b = g_key_file_get_keys (kf_b, groups[i], NULL, NULL); + + if ((!keys) != (!keys_b)) + return FALSE; + if (!keys) + continue; + + for (j = 0; keys[j] && keys_b[j] && !strcmp (keys[j], keys_b[j]); j++) + ; + if (keys[j] || keys_b[j]) + return FALSE; + + 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) +_nm_keyfile_equals (GKeyFile *kf_a, GKeyFile *kf_b, gboolean consider_order) { - return _nm_keyfile_a_contains_all_in_b (kf_a, kf_b) - && _nm_keyfile_a_contains_all_in_b (kf_b, kf_a); + if (!consider_order) { + return _nm_keyfile_a_contains_all_in_b (kf_a, kf_b) + && _nm_keyfile_a_contains_all_in_b (kf_b, kf_a); + } else { + return _nm_keyfile_equals_ordered (kf_a, kf_b); + } } gboolean diff --git a/libnm-core/tests/test-keyfile.c b/libnm-core/tests/test-keyfile.c index c2e0ad6e93..99f88ac543 100644 --- a/libnm-core/tests/test-keyfile.c +++ b/libnm-core/tests/test-keyfile.c @@ -152,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); - g_assert (_nm_keyfile_equals (c0_k1, c0_k1_c2_k3)); + g_assert (_nm_keyfile_equals (c0_k1, c0_k1_c2_k3, TRUE)); } if (k0) { NMSetting8021x *s1, *s2; @@ -214,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. */ - g_assert (_nm_keyfile_equals (c0_k1, k0_c1_k2)); + g_assert (_nm_keyfile_equals (c0_k1, k0_c1_k2, TRUE)); 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 c8376132f7..d5e8314656 100644 --- a/src/nm-config-data.c +++ b/src/nm-config-data.c @@ -317,7 +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 (!_nm_keyfile_equals (priv_old->keyfile, priv_new->keyfile)) + if (!_nm_keyfile_equals (priv_old->keyfile, priv_new->keyfile, TRUE)) 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 |