From 3d8776108c6cf2a2831d58e45e0f1b19a5e0634f Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 17 Mar 2016 10:34:44 +0100 Subject: [PATCH] libnm-core: add _nm_simple_connection_new_from_dbus() function Contary to nm_simple_connection_new_from_dbus(), this internal function allows to specify parse-flags. --- libnm-core/nm-connection.c | 19 +++++++++++++-- libnm-core/nm-core-internal.h | 5 ++++ libnm-core/nm-simple-connection.c | 34 ++++++++++++++++++++++----- src/settings/nm-settings-connection.c | 4 +++- src/settings/nm-settings.c | 4 +++- 5 files changed, 56 insertions(+), 10 deletions(-) diff --git a/libnm-core/nm-connection.c b/libnm-core/nm-connection.c index f0b2387ba..c03e62656 100644 --- a/libnm-core/nm-connection.c +++ b/libnm-core/nm-connection.c @@ -245,6 +245,9 @@ validate_permissions_type (GVariant *variant, GError **error) * * Returns: %TRUE if connection was updated, %FALSE if @new_settings could not * be deserialized (in which case @connection will be unchanged). + * Only exception is the NM_SETTING_PARSE_FLAGS_NORMALIZE flag: if normalization + * fails, the input @connection is already modified and the original settings + * are lost. **/ gboolean _nm_connection_replace_settings (NMConnection *connection, @@ -257,7 +260,7 @@ _nm_connection_replace_settings (NMConnection *connection, const char *setting_name; GVariant *setting_dict; GSList *settings = NULL, *s; - gboolean changed; + gboolean changed, success; g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE); g_return_val_if_fail (g_variant_is_of_type (new_settings, NM_VARIANT_TYPE_CONNECTION), FALSE); @@ -339,9 +342,21 @@ _nm_connection_replace_settings (NMConnection *connection, g_slist_free (settings); + /* If verification/normalization fails, the original connection + * is already lost. From an API point of view, it would be nicer + * not to touch the input argument if we fail at the end. + * However, that would require creating a temporary connection + * to validate it first. As none of the caller cares about the + * state of the @connection when normalization fails, just do it + * this way. */ + if (NM_FLAGS_HAS (parse_flags, NM_SETTING_PARSE_FLAGS_NORMALIZE)) + success = nm_connection_normalize (connection, NULL, NULL, error); + else + success = TRUE; + if (changed) g_signal_emit (connection, signals[CHANGED], 0); - return TRUE; + return success; } /** diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h index 60c3c5529..9512ee561 100644 --- a/libnm-core/nm-core-internal.h +++ b/libnm-core/nm-core-internal.h @@ -104,6 +104,7 @@ typedef enum { /*< skip >*/ NM_SETTING_PARSE_FLAGS_NONE = 0, NM_SETTING_PARSE_FLAGS_STRICT = 1LL << 0, NM_SETTING_PARSE_FLAGS_BEST_EFFORT = 1LL << 1, + NM_SETTING_PARSE_FLAGS_NORMALIZE = 1LL << 2, _NM_SETTING_PARSE_FLAGS_LAST, NM_SETTING_PARSE_FLAGS_ALL = ((_NM_SETTING_PARSE_FLAGS_LAST - 1) << 1) - 1, @@ -114,6 +115,10 @@ gboolean _nm_connection_replace_settings (NMConnection *connection, NMSettingParseFlags parse_flags, GError **error); +NMConnection *_nm_simple_connection_new_from_dbus (GVariant *dict, + NMSettingParseFlags parse_flags, + GError **error); + guint32 _nm_setting_get_setting_priority (NMSetting *setting); gboolean _nm_setting_get_property (NMSetting *setting, const char *name, GValue *value); diff --git a/libnm-core/nm-simple-connection.c b/libnm-core/nm-simple-connection.c index 1036c8ecc..11700666f 100644 --- a/libnm-core/nm-simple-connection.c +++ b/libnm-core/nm-simple-connection.c @@ -51,7 +51,7 @@ nm_simple_connection_new (void) } /** - * nm_simple_connection_new_from_dbus: + * _nm_simple_connection_new_from_dbus: * @dict: a #GVariant of type %NM_VARIANT_TYPE_CONNECTION describing the connection * @error: on unsuccessful return, an error * @@ -60,24 +60,46 @@ nm_simple_connection_new (void) * hash table. * * Returns: (transfer full): the new #NMSimpleConnection object, populated with - * settings created from the values in the hash table, or %NULL if the - * connection failed to validate + * settings created from the values in the hash table, or %NULL if there was + * an error. **/ NMConnection * -nm_simple_connection_new_from_dbus (GVariant *dict, GError **error) +_nm_simple_connection_new_from_dbus (GVariant *dict, NMSettingParseFlags parse_flags, GError **error) { NMConnection *connection; g_return_val_if_fail (dict != NULL, NULL); g_return_val_if_fail (g_variant_is_of_type (dict, NM_VARIANT_TYPE_CONNECTION), NULL); + g_return_val_if_fail (!NM_FLAGS_ANY (parse_flags, ~NM_SETTING_PARSE_FLAGS_ALL), NULL); + g_return_val_if_fail (!NM_FLAGS_ALL (parse_flags, NM_SETTING_PARSE_FLAGS_STRICT | NM_SETTING_PARSE_FLAGS_BEST_EFFORT), NULL); connection = nm_simple_connection_new (); - if ( !nm_connection_replace_settings (connection, dict, error) - || !nm_connection_normalize (connection, NULL, NULL, error)) + if (!_nm_connection_replace_settings (connection, dict, parse_flags, error)) g_clear_object (&connection); return connection; } +/** + * nm_simple_connection_new_from_dbus: + * @dict: a #GVariant of type %NM_VARIANT_TYPE_CONNECTION describing the connection + * @error: on unsuccessful return, an error + * + * Creates a new #NMSimpleConnection from a hash table describing the + * connection and normalize the connection. See nm_connection_to_dbus() for a + * description of the expected hash table. + * + * Returns: (transfer full): the new #NMSimpleConnection object, populated with + * settings created from the values in the hash table, or %NULL if the + * connection failed to normalize. + **/ +NMConnection * +nm_simple_connection_new_from_dbus (GVariant *dict, GError **error) +{ + return _nm_simple_connection_new_from_dbus (dict, + NM_SETTING_PARSE_FLAGS_NORMALIZE, + error); +} + /** * nm_simple_connection_new_clone: * @connection: the #NMConnection to clone diff --git a/src/settings/nm-settings-connection.c b/src/settings/nm-settings-connection.c index 10aa77ab4..bce3dff10 100644 --- a/src/settings/nm-settings-connection.c +++ b/src/settings/nm-settings-connection.c @@ -1735,7 +1735,9 @@ settings_connection_update_helper (NMSettingsConnection *self, /* Check if the settings are valid first */ if (new_settings) { - tmp = nm_simple_connection_new_from_dbus (new_settings, &error); + tmp = _nm_simple_connection_new_from_dbus (new_settings, + NM_SETTING_PARSE_FLAGS_NORMALIZE, + &error); if (!tmp) goto error; } diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c index f09018548..fc2daa063 100644 --- a/src/settings/nm-settings.c +++ b/src/settings/nm-settings.c @@ -1407,7 +1407,9 @@ impl_settings_add_connection_helper (NMSettings *self, NMConnection *connection; GError *error = NULL; - connection = nm_simple_connection_new_from_dbus (settings, &error); + connection = _nm_simple_connection_new_from_dbus (settings, + NM_SETTING_PARSE_FLAGS_NORMALIZE, + &error); if (connection) { if (!nm_connection_verify_secrets (connection, &error)) -- 2.17.1