From 7b36a6a890a83ddf585644ffada28f37b00e5c82 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 28 Oct 2017 17:46:26 +0200 Subject: [PATCH] libnm: add nm_connection_get_settings() There is no API to get all settings. You can only ask for settings explicitly, but that requires you to probe for them and know which ones may exist. The alternative API might be nm_connection_for_each_setting_value(), but that only iterates over settings' properties. If a setting has no properties, it is ignored. --- libnm-core/nm-connection.c | 88 +++++++++++++++++++++++++------------- libnm-core/nm-connection.h | 4 ++ libnm/libnm.ver | 1 + 3 files changed, 64 insertions(+), 29 deletions(-) diff --git a/libnm-core/nm-connection.c b/libnm-core/nm-connection.c index 32fa9fd78a..720ba8468d 100644 --- a/libnm-core/nm-connection.c +++ b/libnm-core/nm-connection.c @@ -1797,6 +1797,60 @@ _for_each_sort (NMSetting **p_a, NMSetting **p_b, void *unused) return strcmp (nm_setting_get_name (a), nm_setting_get_name (b)); } +/** + * nm_connection_get_settings: + * @connection: the #NMConnection instance + * @out_length: (allow-none): (out): the length of the returned array + * + * Retrieves the settings in @connection. + * + * The returned array is %NULL-terminated. + * + * Returns: (array length=out_length) (transfer container): a + * %NULL-terminated array containing every setting of + * @connection. + * If the connection has no settings, %NULL is returned. + * + * Since: 1.10 + */ +NMSetting ** +nm_connection_get_settings (NMConnection *connection, + guint *out_length) +{ + NMConnectionPrivate *priv; + NMSetting **arr; + GHashTableIter iter; + NMSetting *setting; + guint i, size; + + g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL); + + priv = NM_CONNECTION_GET_PRIVATE (connection); + + size = g_hash_table_size (priv->settings); + + if (!size) { + NM_SET_OUT (out_length, 0); + return NULL; + } + + arr = g_new (NMSetting *, size + 1); + + g_hash_table_iter_init (&iter, priv->settings); + for (i = 0; g_hash_table_iter_next (&iter, NULL, (gpointer *) &setting); i++) + arr[i] = setting; + nm_assert (i == size); + arr[size] = NULL; + + /* 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); + + NM_SET_OUT (out_length, size); + return arr; +} + /** * nm_connection_for_each_setting_value: * @connection: the #NMConnection @@ -1811,39 +1865,15 @@ nm_connection_for_each_setting_value (NMConnection *connection, NMSettingValueIterFn func, gpointer user_data) { - NMConnectionPrivate *priv; - gs_free NMSetting **arr_free = NULL; - NMSetting *arr_temp[20], **arr; - GHashTableIter iter; - gpointer value; - guint i, size; + gs_free NMSetting **settings = NULL; + guint i, length = 0; g_return_if_fail (NM_IS_CONNECTION (connection)); g_return_if_fail (func); - priv = NM_CONNECTION_GET_PRIVATE (connection); - - 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); + settings = nm_connection_get_settings (connection, &length); + for (i = 0; i < length; i++) + nm_setting_enumerate_values (settings[i], func, user_data); } /** diff --git a/libnm-core/nm-connection.h b/libnm-core/nm-connection.h index 9419c6254e..c6b23a00bc 100644 --- a/libnm-core/nm-connection.h +++ b/libnm-core/nm-connection.h @@ -179,6 +179,10 @@ void nm_connection_for_each_setting_value (NMConnection *connection, NMSettingValueIterFn func, gpointer user_data); +NM_AVAILABLE_IN_1_10 +NMSetting ** nm_connection_get_settings (NMConnection *connection, + guint *out_length); + void nm_connection_dump (NMConnection *connection); /* Helpers */ diff --git a/libnm/libnm.ver b/libnm/libnm.ver index 72690ff4f2..1b245b277b 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -1185,6 +1185,7 @@ global: nm_client_connectivity_check_get_available; nm_client_connectivity_check_get_enabled; nm_client_connectivity_check_set_enabled; + nm_connection_get_settings; nm_device_dummy_get_hw_address; nm_device_ovs_bridge_get_type; nm_device_ovs_interface_get_type;