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.
This commit is contained in:
Thomas Haller
2019-04-23 16:19:11 +02:00
parent d4d1e5f00d
commit e55a45faa2
2 changed files with 31 additions and 67 deletions

View File

@@ -938,34 +938,6 @@ nmc_convert_string_hash_to_string (const GValue *src_value, GValue *dest_value)
g_value_take_string (dest_value, g_string_free (string, FALSE));
}
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)
{
@@ -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

View File

@@ -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;
}
@@ -1652,23 +1680,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)
{
@@ -2257,21 +2268,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)
{
@@ -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,
};