libnm: sort settings for nm_connection_for_each_setting_value()

nm_connection_for_each_setting_value() is used by keyfile writer to iterate
over the settings and write the keyfile entires. The order there is important
as g_key_file_to_data() prints the groups in the order they were created.

To have a stable order and to have the [connection] entry first, sort the
settings.
This commit is contained in:
Thomas Haller
2015-03-13 13:42:35 +01:00
parent 81119c69d8
commit e50fbe466b

View File

@@ -30,6 +30,7 @@
#include "nm-utils.h" #include "nm-utils.h"
#include "nm-setting-private.h" #include "nm-setting-private.h"
#include "nm-core-internal.h" #include "nm-core-internal.h"
#include "gsystem-local-alloc.h"
/** /**
* SECTION:nm-connection * SECTION:nm-connection
@@ -1306,6 +1307,19 @@ nm_connection_is_type (NMConnection *connection, const char *type)
return (g_strcmp0 (type2, type) == 0); return (g_strcmp0 (type2, type) == 0);
} }
static int
_for_each_sort (NMSetting **p_a, NMSetting **p_b, void *unused)
{
NMSetting *a = *p_a;
NMSetting *b = *p_b;
int c;
c = _nm_setting_compare_priority (a, b);
if (c != 0)
return c;
return strcmp (nm_setting_get_name (a), nm_setting_get_name (b));
}
/** /**
* nm_connection_for_each_setting_value: * nm_connection_for_each_setting_value:
* @connection: the #NMConnection * @connection: the #NMConnection
@@ -1320,15 +1334,39 @@ nm_connection_for_each_setting_value (NMConnection *connection,
NMSettingValueIterFn func, NMSettingValueIterFn func,
gpointer user_data) gpointer user_data)
{ {
NMConnectionPrivate *priv;
gs_free NMSetting **arr_free = NULL;
NMSetting *arr_temp[20], **arr;
GHashTableIter iter; GHashTableIter iter;
gpointer value; gpointer value;
guint i, size;
g_return_if_fail (NM_IS_CONNECTION (connection)); g_return_if_fail (NM_IS_CONNECTION (connection));
g_return_if_fail (func != NULL); g_return_if_fail (func != NULL);
g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings); priv = NM_CONNECTION_GET_PRIVATE (connection);
while (g_hash_table_iter_next (&iter, NULL, &value))
nm_setting_enumerate_values (NM_SETTING (value), func, user_data); size = g_hash_table_size (priv->settings);
if (!size)
return;
if (size > G_N_ELEMENTS (arr_temp))
arr = arr_free = g_new (NMSetting *, size);
else
arr = arr_temp;
g_hash_table_iter_init (&iter, priv->settings);
for (i = 0; g_hash_table_iter_next (&iter, NULL, &value); i++)
arr[i] = NM_SETTING (value);
g_assert (i == size);
/* sort the settings. This has an effect on the order in which keyfile
* prints them. */
if (size > 1)
g_qsort_with_data (arr, size, sizeof (NMSetting *), (GCompareDataFunc) _for_each_sort, NULL);
for (i = 0; i < size; i++)
nm_setting_enumerate_values (arr[i], func, user_data);
} }
/** /**