summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2015-06-08 13:30:30 +0200
committerThomas Haller <thaller@redhat.com>2015-06-16 15:38:46 +0200
commit21c65607701c5ebd1ae7eb271c7309517b856933 (patch)
treefb229ee0df8f1d0fdaacc0bf2b4298e62de56825
parentade02c9d3e93ccae4ec8326d6a2a1208dcbb7273 (diff)
downloadNetworkManager-21c65607701c5ebd1ae7eb271c7309517b856933.tar.gz
libnm: add keyfile utility functions
-rw-r--r--libnm-core/nm-keyfile-internal.h5
-rw-r--r--libnm-core/nm-keyfile-utils.c78
-rw-r--r--libnm-core/tests/test-keyfile.c37
-rw-r--r--src/nm-config-data.c31
4 files changed, 87 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..1dff9597be 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,81 @@ 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[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) {
+ 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 f093292564..68f4125337 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 {
@@ -290,33 +291,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) {
@@ -338,8 +312,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