diff options
author | Thomas Haller <thaller@redhat.com> | 2019-04-23 16:19:11 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2019-04-25 08:20:03 +0200 |
commit | e55a45faa25eaa282d8dc96e9d1ffa5d049977d3 (patch) | |
tree | d5983a8a851257d41f068055624734e0d872f366 | |
parent | d4d1e5f00d0bdf03f55e06d157c793ecdb472b2c (diff) | |
download | NetworkManager-e55a45faa25eaa282d8dc96e9d1ffa5d049977d3.tar.gz |
cli: drop GValue transformation of GBytes to string and implement bytes getter via _get_fcn_gobject_impl()
The g_value_register_transform_func() for handling GBytes was not actually used.
All properties of type G_TYPE_BYTES have their explit handler how to convert bytes
to string. That is good, because the implementation there was very bad (it did not
honor pretty/parsable get-type).
Also, merge _get_fcn_gobject_bytes() into _get_fcn_gobject_impl(). We
already have a generic handler that handles properties solely based on
the GObject type: _get_fcn_gobject_impl(). Just let it also handle
bytes. It's better to have fewer handlers, if they don't need special
context.
-rw-r--r-- | clients/cli/nmcli.c | 32 | ||||
-rw-r--r-- | clients/common/nm-meta-setting-desc.c | 66 |
2 files changed, 31 insertions, 67 deletions
diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c index 2f401e134f..9076e13d7c 100644 --- a/clients/cli/nmcli.c +++ b/clients/cli/nmcli.c @@ -939,34 +939,6 @@ nmc_convert_string_hash_to_string (const GValue *src_value, GValue *dest_value) } static void -nmc_convert_bytes_to_string (const GValue *src_value, GValue *dest_value) -{ - GBytes *bytes; - const guint8 *array; - gsize length; - GString *printable; - guint i = 0; - - bytes = g_value_get_boxed (src_value); - - printable = g_string_new ("["); - - if (bytes) { - array = g_bytes_get_data (bytes, &length); - while (i < MIN (length, 35)) { - if (i > 0) - g_string_append_c (printable, ' '); - g_string_append_printf (printable, "0x%02X", array[i++]); - } - if (i < length) - g_string_append (printable, " ... "); - } - g_string_append_c (printable, ']'); - - g_value_take_string (dest_value, g_string_free (printable, FALSE)); -} - -static void nmc_value_transforms_register (void) { /* This depends on the fact that all of the hash-table-valued properties @@ -975,10 +947,6 @@ nmc_value_transforms_register (void) g_value_register_transform_func (G_TYPE_HASH_TABLE, G_TYPE_STRING, nmc_convert_string_hash_to_string); - - g_value_register_transform_func (G_TYPE_BYTES, - G_TYPE_STRING, - nmc_convert_bytes_to_string); } void diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 5bb9a3e8e7..5136d98a2b 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -77,6 +77,25 @@ _gtype_property_get_gtype (GType gtype, const char *property_name) /*****************************************************************************/ +static char * +bytes_to_string (GBytes *bytes) +{ + const guint8 *data; + gsize len; + + if (!bytes) + return NULL; + + data = g_bytes_get_data (bytes, &len); + return nm_utils_bin2hexstr_full (data, + len, + '\0', + TRUE, + NULL); +} + +/*****************************************************************************/ + static int _int64_cmp_desc (gconstpointer a, gconstpointer b, @@ -836,13 +855,14 @@ _get_fcn_gobject_impl (const NMMetaPropertyInfo *property_info, gtype_prop = _gobject_property_get_gtype (G_OBJECT (setting), property_info->property_name); glib_handles_str_transform = !NM_IN_SET (gtype_prop, G_TYPE_BOOLEAN, - G_TYPE_STRV); + G_TYPE_STRV, + G_TYPE_BYTES); if (glib_handles_str_transform) { /* We rely on the type convertion of the gobject property to string. * * Note that we register some transformations via nmc_value_transforms_register() - * to make that working for G_TYPE_HASH_TABLE, and G_TYPE_BYTES. + * to make that working for G_TYPE_HASH_TABLE. * * FIXME: that is particularly ugly because it's non-obvious which code relies * on nmc_value_transforms_register(). Also, nmc_value_transforms_register() is @@ -894,6 +914,14 @@ _get_fcn_gobject_impl (const NMMetaPropertyInfo *property_info, return ""; } + if (gtype_prop == G_TYPE_BYTES) { + char *str; + + str = bytes_to_string (g_value_get_boxed (&val)); + NM_SET_OUT (out_is_default, !str || !str[0]); + RETURN_STR_TO_FREE (str); + } + nm_assert_not_reached (); return NULL; } @@ -1653,23 +1681,6 @@ wep_key_type_to_string (NMWepKeyType type) } static char * -bytes_to_string (GBytes *bytes) -{ - const guint8 *data; - gsize len; - - if (!bytes) - return NULL; - - data = g_bytes_get_data (bytes, &len); - return nm_utils_bin2hexstr_full (data, - len, - '\0', - TRUE, - NULL); -} - -static char * vlan_flags_to_string (guint32 flags, NMMetaAccessorGetType get_type) { GString *flag_str; @@ -2258,21 +2269,6 @@ _set_fcn_cert_8021x (ARGS_SET_FCN) } static gconstpointer -_get_fcn_gobject_bytes (ARGS_GET_FCN) -{ - gs_unref_bytes GBytes *bytes = NULL; - char *str; - - RETURN_UNSUPPORTED_GET_TYPE (); - - g_object_get (setting, property_info->property_name, &bytes, NULL); - - str = bytes_to_string (bytes); - NM_SET_OUT (out_is_default, !str || !str[0]); - RETURN_STR_TO_FREE (str); -} - -static gconstpointer _get_fcn_bond_options (ARGS_GET_FCN) { NMSettingBond *s_bond = NM_SETTING_BOND (setting); @@ -4370,7 +4366,7 @@ static const NMMetaPropertyType _pt_gobject_mtu = { }; static const NMMetaPropertyType _pt_gobject_bytes = { - .get_fcn = _get_fcn_gobject_bytes, + .get_fcn = _get_fcn_gobject, .set_fcn = _set_fcn_gobject_bytes, }; |