diff options
author | Thomas Haller <thaller@redhat.com> | 2015-06-08 13:30:30 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2015-07-02 15:50:03 +0200 |
commit | 71323122c6b755cfc50ce93c07d6758b45c19cba (patch) | |
tree | 5cfb77568cb8dacf55a40aa6ddf224073e836b77 /libnm-core | |
parent | 4a8a0b09180f7f3e9bc63f9cf45e6875dc402b50 (diff) | |
download | NetworkManager-71323122c6b755cfc50ce93c07d6758b45c19cba.tar.gz |
libnm: add keyfile utility functions
Diffstat (limited to 'libnm-core')
-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 |
3 files changed, 86 insertions, 35 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); } } |