From c593980b2dd352ccd1d38ce275efafcb31355a23 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 26 Jul 2021 22:12:05 +0200 Subject: [PATCH] libnm: add helper function for init_from_dbus() to set property There is one caller of property_type->from_dbus_fcn(). But we will call it from multiple places, so move the code to a helper function. --- src/libnm-core-impl/nm-setting.c | 116 ++++++++++++++++++------------- 1 file changed, 69 insertions(+), 47 deletions(-) diff --git a/src/libnm-core-impl/nm-setting.c b/src/libnm-core-impl/nm-setting.c index 326d180866..b7c7bc2650 100644 --- a/src/libnm-core-impl/nm-setting.c +++ b/src/libnm-core-impl/nm-setting.c @@ -1560,6 +1560,67 @@ _nm_setting_new_from_dbus(GType setting_type, return g_steal_pointer(&setting); } +static gboolean +_property_set_from_dbus(const NMSettInfoSetting * sett_info, + const NMSettInfoProperty *property_info, + NMSetting * setting, + GVariant * connection_dict, + GVariant * value, + NMSettingParseFlags parse_flags, + GError ** error) +{ + gs_free_error GError *local = NULL; + + if (!property_info->property_type->from_dbus_fcn) { + nm_assert(!property_info->param_spec); + return TRUE; + } + + if (property_info->property_type->from_dbus_is_full) { + /* These hooks perform their own type checking, and can coerce/ignore + * a value regardless of the D-Bus type. */ + } else if (!g_variant_type_equal(g_variant_get_type(value), + property_info->property_type->dbus_type)) { + /* for backward behavior, fail unless best-effort is chosen. */ + if (NM_FLAGS_HAS(parse_flags, NM_SETTING_PARSE_FLAGS_BEST_EFFORT)) + return TRUE; + g_set_error(error, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + _("can't set property of type '%s' from value of type '%s'"), + property_info->property_type->dbus_type + ? g_variant_type_peek_string(property_info->property_type->dbus_type) + : property_info->param_spec ? g_type_name(property_info->param_spec->value_type) + : "(unknown)", + g_variant_get_type_string(value)); + g_prefix_error(error, "%s.%s: ", nm_setting_get_name(setting), property_info->name); + return FALSE; + } + + if (!property_info->property_type->from_dbus_fcn(sett_info, + property_info, + setting, + connection_dict, + value, + parse_flags, + &local)) { + if (property_info->property_type->from_dbus_is_full) { + /* the error we received from from_dbus_fcn() should be propagated, even + * in non-strict mode. */ + } else if (!NM_FLAGS_HAS(parse_flags, NM_SETTING_PARSE_FLAGS_STRICT)) + return TRUE; + g_set_error(error, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + _("failed to set property: %s"), + local->message); + g_prefix_error(error, "%s.%s: ", nm_setting_get_name(setting), property_info->name); + return FALSE; + } + + return TRUE; +} + static gboolean init_from_dbus(NMSetting * setting, GHashTable * keys, @@ -1644,53 +1705,14 @@ init_from_dbus(NMSetting * setting, if (keys) g_hash_table_remove(keys, property_info->name); - if (property_info->property_type->from_dbus_fcn) { - if (property_info->property_type->from_dbus_is_full) { - /* These hooks perform their own type checking, and can coerce/ignore - * a value regardless of the D-Bus type. */ - } else if (!g_variant_type_equal(g_variant_get_type(value), - property_info->property_type->dbus_type)) { - /* for backward behavior, fail unless best-effort is chosen. */ - if (NM_FLAGS_HAS(parse_flags, NM_SETTING_PARSE_FLAGS_BEST_EFFORT)) - continue; - g_set_error( - error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("can't set property of type '%s' from value of type '%s'"), - property_info->property_type->dbus_type - ? g_variant_type_peek_string(property_info->property_type->dbus_type) - : property_info->param_spec ? g_type_name(property_info->param_spec->value_type) - : "(unknown)", - g_variant_get_type_string(value)); - g_prefix_error(error, "%s.%s: ", nm_setting_get_name(setting), property_info->name); - return FALSE; - } - - if (!property_info->property_type->from_dbus_fcn(sett_info, - property_info, - setting, - connection_dict, - value, - parse_flags, - &local)) { - if (property_info->property_type->from_dbus_is_full) { - /* the error we received from from_dbus_fcn() should be propagated, even - * in non-strict mode. */ - } else if (!NM_FLAGS_HAS(parse_flags, NM_SETTING_PARSE_FLAGS_STRICT)) - continue; - g_set_error(error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("failed to set property: %s"), - local->message); - g_prefix_error(error, "%s.%s: ", nm_setting_get_name(setting), property_info->name); - return FALSE; - } - continue; - } - - nm_assert(!property_info->param_spec); + if (!_property_set_from_dbus(sett_info, + property_info, + setting, + connection_dict, + value, + parse_flags, + error)) + return FALSE; } return TRUE;