From 7b5d514aef756bf1b0d77e73bc242f841a8142bf Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 18 Mar 2019 10:09:24 +0100 Subject: [PATCH] cli: implement nmc_setting_reset_property() based on nmc_setting_set_property() "reset" is just a special case of "set". We can keep nmc_setting_reset_property() as a convenience function, but it must be implemented based on nmc_setting_set_property(). Also, reset only used nmc_property_set_default_value(), which only works with GObject based properties. It's wrong to assume that all properties are GObject based. By implementing it based via nmc_setting_set_property() we can fix this (later). --- clients/cli/connections.c | 12 +++--- clients/cli/settings.c | 89 +++++++++++++-------------------------- clients/cli/settings.h | 13 ++++-- 3 files changed, 45 insertions(+), 69 deletions(-) diff --git a/clients/cli/connections.c b/clients/cli/connections.c index c9a6d2be4a..7994305f40 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -3988,7 +3988,7 @@ set_property (NMClient *client, * so make a copy if we are going to free it. */ value = value_free = g_strdup (value); - nmc_setting_reset_property (setting, property_name, NULL); + nmc_setting_reset_property (client, setting, property_name, NULL); } if (!nmc_setting_set_property (client, setting, property_name, value, &local)) { g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, @@ -4013,7 +4013,7 @@ set_property (NMClient *client, return FALSE; } } else - nmc_setting_reset_property (setting, property_name, NULL); + nmc_setting_reset_property (client, setting, property_name, NULL); } /* Don't ask for this property in interactive mode. */ @@ -6999,7 +6999,7 @@ property_edit_submenu (NmCli *nmc, g_clear_error (&tmp_err); } } else { - if (!nmc_setting_reset_property (curr_setting, prop_name, &tmp_err)) { + if (!nmc_setting_reset_property (nmc->client, curr_setting, prop_name, &tmp_err)) { g_print (_("Error: failed to remove value of '%s': %s\n"), prop_name, tmp_err->message); g_clear_error (&tmp_err); @@ -7513,8 +7513,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t if (!prop_name) break; - /* Delete property value */ - if (!nmc_setting_reset_property (menu_ctx.curr_setting, prop_name, &tmp_err)) { + if (!nmc_setting_reset_property (nmc->client, menu_ctx.curr_setting, prop_name, &tmp_err)) { g_print (_("Error: failed to remove value of '%s': %s\n"), prop_name, tmp_err->message); g_clear_error (&tmp_err); @@ -7564,8 +7563,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t prop_name = is_property_valid (ss, cmd_arg_p, &tmp_err); if (prop_name) { - /* Delete property value */ - if (!nmc_setting_reset_property (ss, prop_name, &tmp_err)) { + if (!nmc_setting_reset_property (nmc->client, ss, prop_name, &tmp_err)) { g_print (_("Error: failed to remove value of '%s': %s\n"), prop_name, tmp_err->message); g_clear_error (&tmp_err); diff --git a/clients/cli/settings.c b/clients/cli/settings.c index 840eb96201..5dbfab82a2 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -539,40 +539,39 @@ nmc_setting_set_property (NMClient *client, NMSetting *setting, const char *prop g_return_val_if_fail (NM_IS_SETTING (setting), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - if ((property_info = nm_meta_property_info_find_by_setting (setting, prop))) { + if (!(property_info = nm_meta_property_info_find_by_setting (setting, prop))) + goto out_fail_read_only; - if (!value) { - /* No value argument sets default value */ - nmc_property_set_default_value (setting, prop); - return TRUE; - } - - if (property_info->property_type->set_fcn) { - switch (property_info->setting_info->general->meta_type) { - case NM_META_SETTING_TYPE_CONNECTION: - if (nm_streq (property_info->property_name, NM_SETTING_CONNECTION_SECONDARIES)) { - gs_free char *value_coerced = NULL; - - if (!_set_fcn_precheck_connection_secondaries (client, value, &value_coerced, error)) - return FALSE; - - return _set_fcn_call (property_info, - setting, - value_coerced ?: value, - error); - } - break; - default: - break; - } - return _set_fcn_call (property_info, - setting, - value, - error); - } + if (!value) { + /* No value argument sets default value */ + nmc_property_set_default_value (setting, prop); + return TRUE; } - g_set_error_literal (error, 1, 0, _("the property can't be changed")); + if (property_info->property_type->set_fcn) { + gs_free char *value_to_free = NULL; + + switch (property_info->setting_info->general->meta_type) { + case NM_META_SETTING_TYPE_CONNECTION: + if (nm_streq (property_info->property_name, NM_SETTING_CONNECTION_SECONDARIES)) { + if (!_set_fcn_precheck_connection_secondaries (client, value, &value_to_free, error)) + return FALSE; + if (value_to_free) + value = value_to_free; + } + break; + default: + break; + } + + return _set_fcn_call (property_info, + setting, + value, + error); + } + +out_fail_read_only: + nm_utils_error_set (error, NM_UTILS_ERROR_UNKNOWN, _("the property can't be changed")); return FALSE; } @@ -590,34 +589,6 @@ nmc_property_set_default_value (NMSetting *setting, const char *prop) } } -/* - * Generic function for resetting (single value) properties. - * - * The function resets the property value to the default one. It respects - * nmcli restrictions for changing properties. So if 'set_func' is NULL, - * resetting the value is denied. - * - * Returns: TRUE on success; FALSE on failure and sets error - */ -gboolean -nmc_setting_reset_property (NMSetting *setting, const char *prop, GError **error) -{ - const NMMetaPropertyInfo *property_info; - - g_return_val_if_fail (NM_IS_SETTING (setting), FALSE); - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - - if ((property_info = nm_meta_property_info_find_by_setting (setting, prop))) { - if (property_info->property_type->set_fcn) { - nmc_property_set_default_value (setting, prop); - return TRUE; - } - } - - g_set_error_literal (error, 1, 0, _("the property can't be changed")); - return FALSE; -} - gboolean nmc_setting_remove_property_option (NMSetting *setting, const char *prop, diff --git a/clients/cli/settings.h b/clients/cli/settings.h index 8bf2dfc6b4..d3ea8f780f 100644 --- a/clients/cli/settings.h +++ b/clients/cli/settings.h @@ -47,13 +47,20 @@ gboolean nmc_setting_set_property (NMClient *client, const char *prop, const char *val, GError **error); -gboolean nmc_setting_reset_property (NMSetting *setting, - const char *prop, - GError **error); +static inline gboolean +nmc_setting_reset_property (NMClient *client, + NMSetting *setting, + const char *prop, + GError **error) +{ + return nmc_setting_set_property (client, setting, prop, NULL, error); +} + gboolean nmc_setting_remove_property_option (NMSetting *setting, const char *prop, const char *value, GError **error); + void nmc_property_set_default_value (NMSetting *setting, const char *prop); gboolean nmc_property_get_gvalue (NMSetting *setting, const char *prop, GValue *value);