libnm: unify property-to-dbus handling of NMSetting

Merge the function pointer get_func() into to_dbus_fcn().

Previously, get_func() as handled separately from to_dbus_fnc()
(formerly synth_func()). The notion was that synth-func would syntetize
properties that are D-Bus only. But that distinction does not seem
very helpful to me.

Instaed, we want to convert a property to D-Bus. Period. The
implementation should be handled uniformly. Hence, now that is
all done by property_to_dbus().

Note that property_to_dbus() is also called as default implementation
for compare-property. At least, for properties that are backed by a
GObject property.
This commit is contained in:
Thomas Haller 2019-04-24 17:41:32 +02:00
parent bb7829cb37
commit 0d1b8ee92a
11 changed files with 136 additions and 135 deletions

View file

@ -662,8 +662,6 @@ GVariant *nm_ip_routing_rule_to_dbus (const NMIPRoutingRule *self);
typedef struct _NMSettInfoSetting NMSettInfoSetting;
typedef struct _NMSettInfoProperty NMSettInfoProperty;
typedef GVariant *(*NMSettingPropertyGetFunc) (NMSetting *setting,
const char *property);
typedef GVariant *(*NMSettInfoPropToDBusFcn) (const NMSettInfoSetting *sett_info,
guint property_idx,
NMConnection *connection,
@ -690,9 +688,6 @@ struct _NMSettInfoProperty {
const GVariantType *dbus_type;
/* TODO: merge @get_func with @to_dbus_fcn. */
NMSettingPropertyGetFunc get_func;
NMSettInfoPropToDBusFcn to_dbus_fcn;
NMSettInfoPropFromDBusFcn from_dbus_fcn;
NMSettInfoPropMissingFromDBusFcn missing_from_dbus_fcn;

View file

@ -253,19 +253,18 @@ ip4_dns_from_dbus (GVariant *dbus_value,
}
static GVariant *
ip4_addresses_get (NMSetting *setting,
const char *property)
ip4_addresses_get (const NMSettInfoSetting *sett_info,
guint property_idx,
NMConnection *connection,
NMSetting *setting,
NMConnectionSerializationFlags flags)
{
GPtrArray *addrs;
gs_unref_ptrarray GPtrArray *addrs = NULL;
const char *gateway;
GVariant *ret;
g_object_get (setting, property, &addrs, NULL);
g_object_get (setting, NM_SETTING_IP_CONFIG_ADDRESSES, &addrs, NULL);
gateway = nm_setting_ip_config_get_gateway (NM_SETTING_IP_CONFIG (setting));
ret = nm_utils_ip4_addresses_to_variant (addrs, gateway);
g_ptr_array_unref (addrs);
return ret;
return nm_utils_ip4_addresses_to_variant (addrs, gateway);
}
static gboolean
@ -388,17 +387,16 @@ ip4_address_data_set (NMSetting *setting,
}
static GVariant *
ip4_routes_get (NMSetting *setting,
const char *property)
ip4_routes_get (const NMSettInfoSetting *sett_info,
guint property_idx,
NMConnection *connection,
NMSetting *setting,
NMConnectionSerializationFlags flags)
{
GPtrArray *routes;
GVariant *ret;
gs_unref_ptrarray GPtrArray *routes = NULL;
g_object_get (setting, property, &routes, NULL);
ret = nm_utils_ip4_routes_to_variant (routes);
g_ptr_array_unref (routes);
return ret;
g_object_get (setting, NM_SETTING_IP_CONFIG_ROUTES, &routes, NULL);
return nm_utils_ip4_routes_to_variant (routes);
}
static gboolean

View file

@ -313,19 +313,18 @@ ip6_dns_from_dbus (GVariant *dbus_value,
}
static GVariant *
ip6_addresses_get (NMSetting *setting,
const char *property)
ip6_addresses_get (const NMSettInfoSetting *sett_info,
guint property_idx,
NMConnection *connection,
NMSetting *setting,
NMConnectionSerializationFlags flags)
{
GPtrArray *addrs;
gs_unref_ptrarray GPtrArray *addrs = NULL;
const char *gateway;
GVariant *ret;
g_object_get (setting, property, &addrs, NULL);
g_object_get (setting, NM_SETTING_IP_CONFIG_ADDRESSES, &addrs, NULL);
gateway = nm_setting_ip_config_get_gateway (NM_SETTING_IP_CONFIG (setting));
ret = nm_utils_ip6_addresses_to_variant (addrs, gateway);
g_ptr_array_unref (addrs);
return ret;
return nm_utils_ip6_addresses_to_variant (addrs, gateway);
}
static gboolean
@ -394,17 +393,16 @@ ip6_address_data_set (NMSetting *setting,
}
static GVariant *
ip6_routes_get (NMSetting *setting,
const char *property)
ip6_routes_get (const NMSettInfoSetting *sett_info,
guint property_idx,
NMConnection *connection,
NMSetting *setting,
NMConnectionSerializationFlags flags)
{
GPtrArray *routes;
GVariant *ret;
gs_unref_ptrarray GPtrArray *routes = NULL;
g_object_get (setting, property, &routes, NULL);
ret = nm_utils_ip6_routes_to_variant (routes);
g_ptr_array_unref (routes);
return ret;
g_object_get (setting, NM_SETTING_IP_CONFIG_ROUTES, &routes, NULL);
return nm_utils_ip6_routes_to_variant (routes);
}
static gboolean

View file

@ -181,7 +181,7 @@ void _properties_override_add_dbus_only (GArray *properties_override,
void _properties_override_add_override (GArray *properties_override,
GParamSpec *param_spec,
const GVariantType *dbus_type,
NMSettingPropertyGetFunc get_func,
NMSettInfoPropToDBusFcn to_dbus_fcn,
NMSettInfoPropFromDBusFcn from_dbus_fcn,
NMSettInfoPropMissingFromDBusFcn missing_from_dbus_fcn);

View file

@ -905,7 +905,11 @@ _nm_setting_sriov_sort_vfs (NMSettingSriov *setting)
/*****************************************************************************/
static GVariant *
vfs_to_dbus (NMSetting *setting, const char *property)
vfs_to_dbus (const NMSettInfoSetting *sett_info,
guint property_idx,
NMConnection *connection,
NMSetting *setting,
NMConnectionSerializationFlags flags)
{
gs_unref_ptrarray GPtrArray *vfs = NULL;
GVariantBuilder builder;

View file

@ -1483,17 +1483,16 @@ next:
}
static GVariant *
tc_qdiscs_get (NMSetting *setting,
const char *property)
tc_qdiscs_get (const NMSettInfoSetting *sett_info,
guint property_idx,
NMConnection *connection,
NMSetting *setting,
NMConnectionSerializationFlags flags)
{
GPtrArray *qdiscs;
GVariant *ret;
gs_unref_ptrarray GPtrArray *qdiscs = NULL;
g_object_get (setting, NM_SETTING_TC_CONFIG_QDISCS, &qdiscs, NULL);
ret = _qdiscs_to_variant (qdiscs);
g_ptr_array_unref (qdiscs);
return ret;
return _qdiscs_to_variant (qdiscs);
}
static gboolean
@ -1669,17 +1668,16 @@ next:
}
static GVariant *
tc_tfilters_get (NMSetting *setting,
const char *property)
tc_tfilters_get (const NMSettInfoSetting *sett_info,
guint property_idx,
NMConnection *connection,
NMSetting *setting,
NMConnectionSerializationFlags flags)
{
GPtrArray *tfilters;
GVariant *ret;
gs_unref_ptrarray GPtrArray *tfilters = NULL;
g_object_get (setting, NM_SETTING_TC_CONFIG_TFILTERS, &tfilters, NULL);
ret = _tfilters_to_variant (tfilters);
g_ptr_array_unref (tfilters);
return ret;
return _tfilters_to_variant (tfilters);
}
static gboolean
@ -1690,12 +1688,10 @@ tc_tfilters_set (NMSetting *setting,
NMSettingParseFlags parse_flags,
GError **error)
{
GPtrArray *tfilters;
gs_unref_ptrarray GPtrArray *tfilters = NULL;
tfilters = _tfilters_from_variant (value);
g_object_set (setting, NM_SETTING_TC_CONFIG_TFILTERS, tfilters, NULL);
g_ptr_array_unref (tfilters);
return TRUE;
}

View file

@ -679,7 +679,11 @@ verify (NMSetting *setting, NMConnection *connection, GError **error)
}
static GVariant *
_override_flags_get (NMSetting *setting, const char *property)
_override_flags_get (const NMSettInfoSetting *sett_info,
guint property_idx,
NMConnection *connection,
NMSetting *setting,
NMConnectionSerializationFlags flags)
{
return g_variant_new_uint32 (nm_setting_vlan_get_flags ((NMSettingVlan *) setting));
}
@ -921,7 +925,7 @@ nm_setting_vlan_class_init (NMSettingVlanClass *klass)
_properties_override_add_override (properties_override,
obj_properties[PROP_FLAGS],
NULL,
G_VARIANT_TYPE_UINT32,
_override_flags_get,
NULL,
_override_flags_not_set);

View file

@ -928,7 +928,11 @@ compare_property (const NMSettInfoSetting *sett_info,
}
static GVariant *
_override_autoneg_get (NMSetting *setting, const char *property)
_override_autoneg_get (const NMSettInfoSetting *sett_info,
guint property_idx,
NMConnection *connection,
NMSetting *setting,
NMConnectionSerializationFlags flags)
{
return g_variant_new_boolean (nm_setting_wired_get_auto_negotiate ((NMSettingWired *) setting));
}

View file

@ -294,17 +294,17 @@ _properties_override_add_dbus_only (GArray *properties_override,
* @properties_override: an array collecting the overrides
* @param_spec: the name of the property to override
* @dbus_type: the type of the property (in its D-Bus representation)
* @get_func: (allow-none): function to call to get the value of the property
* @to_dbus_fcn: (allow-none): function to call to get the value of the property
* @from_dbus_fcn: (allow-none): function to call to set the value of the property
* @missing_from_dbus_fcn: (allow-none): function to call to indicate the property was not set
*
* Overrides the D-Bus representation of the #GObject property that shares the
* same name as @param_spec.
*
* When serializing a setting to D-Bus, if @get_func is non-%NULL, then it will
* When serializing a setting to D-Bus, if @to_dbus_fcn is non-%NULL, then it will
* be called to get the property's value. If it returns a #GVariant, the
* property will be added to the hash, and if it returns %NULL, the property
* will be omitted. (If @get_func is %NULL, the property will be read normally
* will be omitted. (If @to_dbus_fcn is %NULL, the property will be read normally
* with g_object_get_property(), and added to the hash if it is not the default
* value.)
*
@ -322,7 +322,7 @@ void
_properties_override_add_override (GArray *properties_override,
GParamSpec *param_spec,
const GVariantType *dbus_type,
NMSettingPropertyGetFunc get_func,
NMSettInfoPropToDBusFcn to_dbus_fcn,
NMSettInfoPropFromDBusFcn from_dbus_fcn,
NMSettInfoPropMissingFromDBusFcn missing_from_dbus_fcn)
{
@ -331,7 +331,7 @@ _properties_override_add_override (GArray *properties_override,
_properties_override_add (properties_override,
.param_spec = param_spec,
.dbus_type = dbus_type,
.get_func = get_func,
.to_dbus_fcn = to_dbus_fcn,
.from_dbus_fcn = from_dbus_fcn,
.missing_from_dbus_fcn = missing_from_dbus_fcn);
}
@ -683,20 +683,44 @@ _nm_setting_use_legacy_property (NMSetting *setting,
/*****************************************************************************/
static GVariant *
get_property_for_dbus (NMSetting *setting,
const NMSettInfoProperty *property,
gboolean ignore_default)
property_to_dbus (const NMSettInfoSetting *sett_info,
guint property_idx,
NMConnection *connection,
NMSetting *setting,
NMConnectionSerializationFlags flags,
gboolean ignore_flags,
gboolean ignore_default)
{
const NMSettInfoProperty *property = &sett_info->property_infos[property_idx];
GVariant *variant;
/* to_dbus_fcn() is currently not allowed for GObject backed properties. No strong
* reason except that get_property_for_dbus() can only consider "real" properties. */
nm_assert (!property->to_dbus_fcn);
nm_assert (property->dbus_type);
if (property->get_func) {
variant = property->get_func (setting, property->name);
if (!property->param_spec) {
if (!property->to_dbus_fcn)
return NULL;
} else if (!ignore_flags) {
if (!NM_FLAGS_HAS (property->param_spec->flags, G_PARAM_WRITABLE))
return NULL;
if (NM_FLAGS_ANY (property->param_spec->flags, NM_SETTING_PARAM_GENDATA_BACKED))
return NULL;
if ( NM_FLAGS_HAS (property->param_spec->flags, NM_SETTING_PARAM_LEGACY)
&& !_nm_utils_is_manager_process)
return NULL;
if ( NM_FLAGS_HAS (flags, NM_CONNECTION_SERIALIZE_NO_SECRETS)
&& NM_FLAGS_HAS (property->param_spec->flags, NM_SETTING_PARAM_SECRET))
return NULL;
if ( NM_FLAGS_HAS (flags, NM_CONNECTION_SERIALIZE_ONLY_SECRETS)
&& !NM_FLAGS_HAS (property->param_spec->flags, NM_SETTING_PARAM_SECRET))
return NULL;
}
if (property->to_dbus_fcn) {
variant = property->to_dbus_fcn (sett_info, property_idx, connection, setting, flags);
nm_g_variant_take_ref (variant);
} else {
nm_auto_unset_gvalue GValue prop_value = { 0, };
@ -778,7 +802,6 @@ _nm_setting_to_dbus (NMSetting *setting, NMConnection *connection, NMConnectionS
{
NMSettingPrivate *priv;
GVariantBuilder builder;
GVariant *dbus_value;
const NMSettInfoSetting *sett_info;
guint n_properties, i;
const char *const*gendata_keys;
@ -799,52 +822,14 @@ _nm_setting_to_dbus (NMSetting *setting, NMConnection *connection, NMConnectionS
sett_info = _nm_setting_class_get_sett_info (NM_SETTING_GET_CLASS (setting));
for (i = 0; i < sett_info->property_infos_len; i++) {
const NMSettInfoProperty *property = &sett_info->property_infos[i];
GParamSpec *prop_spec = property->param_spec;
nm_assert (property->dbus_type);
if (!prop_spec) {
if (!property->to_dbus_fcn)
continue;
} else {
/* For the moment, properties backed by a GObject property don't
* define a synth function. There is no problem supporting that,
* however, for now just disallow it. */
nm_assert (!property->to_dbus_fcn);
if (!NM_FLAGS_HAS (prop_spec->flags, G_PARAM_WRITABLE))
continue;
if (NM_FLAGS_ANY (prop_spec->flags, NM_SETTING_PARAM_GENDATA_BACKED))
continue;
if ( NM_FLAGS_HAS (prop_spec->flags, NM_SETTING_PARAM_LEGACY)
&& !_nm_utils_is_manager_process)
continue;
if ( NM_FLAGS_HAS (flags, NM_CONNECTION_SERIALIZE_NO_SECRETS)
&& NM_FLAGS_HAS (prop_spec->flags, NM_SETTING_PARAM_SECRET))
continue;
if ( NM_FLAGS_HAS (flags, NM_CONNECTION_SERIALIZE_ONLY_SECRETS)
&& !NM_FLAGS_HAS (prop_spec->flags, NM_SETTING_PARAM_SECRET))
continue;
}
if (property->to_dbus_fcn) {
dbus_value = property->to_dbus_fcn (sett_info, i, connection, setting, flags);
nm_g_variant_take_ref (dbus_value);
} else
dbus_value = get_property_for_dbus (setting, property, TRUE);
gs_unref_variant GVariant *dbus_value = NULL;
dbus_value = property_to_dbus (sett_info, i, connection, setting, flags, FALSE, TRUE);
if (dbus_value) {
nm_assert (!g_variant_is_floating (dbus_value));
nm_assert (g_variant_is_of_type (dbus_value, property->dbus_type));
g_variant_builder_add (&builder, "{sv}", property->name, dbus_value);
g_variant_unref (dbus_value);
g_variant_builder_add (&builder,
"{sv}",
sett_info->property_infos[i].name,
dbus_value);
}
}
@ -1433,9 +1418,8 @@ compare_property (const NMSettInfoSetting *sett_info,
gs_unref_variant GVariant *value1 = NULL;
gs_unref_variant GVariant *value2 = NULL;
value1 = get_property_for_dbus (set_a, property_info, TRUE);
value2 = get_property_for_dbus (set_b, property_info, TRUE);
value1 = property_to_dbus (sett_info, property_idx, con_a, set_a, NM_CONNECTION_SERIALIZE_ALL, TRUE, TRUE);
value2 = property_to_dbus (sett_info, property_idx, con_b, set_b, NM_CONNECTION_SERIALIZE_ALL, TRUE, TRUE);
if (nm_property_compare (value1, value2) != 0)
return NM_TERNARY_FALSE;
}

View file

@ -55,8 +55,11 @@ gboolean _nm_utils_wps_method_validate (NMSettingWirelessSecurityWpsMethod wps_m
/* D-Bus transform funcs */
GVariant *_nm_utils_hwaddr_cloned_get (NMSetting *setting,
const char *property);
GVariant *_nm_utils_hwaddr_cloned_get (const NMSettInfoSetting *sett_info,
guint property_idx,
NMConnection *connection,
NMSetting *setting,
NMConnectionSerializationFlags flags);
gboolean _nm_utils_hwaddr_cloned_set (NMSetting *setting,
GVariant *connection_dict,
const char *property,
@ -103,7 +106,12 @@ void _nm_utils_format_variant_attributes_full (GString *str,
char key_value_separator);
gboolean _nm_sriov_vf_parse_vlans (NMSriovVF *vf, const char *str, GError **error);
GVariant * _nm_utils_bridge_vlans_to_dbus (NMSetting *setting, const char *property);
GVariant * _nm_utils_bridge_vlans_to_dbus (const NMSettInfoSetting *sett_info,
guint property_idx,
NMConnection *connection,
NMSetting *setting,
NMConnectionSerializationFlags flags);
gboolean _nm_utils_bridge_vlans_from_dbus (NMSetting *setting,
GVariant *connection_dict,
const char *property,

View file

@ -4280,12 +4280,15 @@ _nm_utils_hwaddr_to_dbus_impl (const char *str)
}
GVariant *
_nm_utils_hwaddr_cloned_get (NMSetting *setting,
const char *property)
_nm_utils_hwaddr_cloned_get (const NMSettInfoSetting *sett_info,
guint property_idx,
NMConnection *connection,
NMSetting *setting,
NMConnectionSerializationFlags flags)
{
gs_free char *addr = NULL;
nm_assert (nm_streq0 (property, "cloned-mac-address"));
nm_assert (nm_streq (sett_info->property_infos[property_idx].name, "cloned-mac-address"));
g_object_get (setting, "cloned-mac-address", &addr, NULL);
return _nm_utils_hwaddr_to_dbus_impl (addr);
@ -6790,13 +6793,20 @@ nm_utils_base64secret_normalize (const char *base64_key,
}
GVariant *
_nm_utils_bridge_vlans_to_dbus (NMSetting *setting, const char *property)
_nm_utils_bridge_vlans_to_dbus (const NMSettInfoSetting *sett_info,
guint property_idx,
NMConnection *connection,
NMSetting *setting,
NMConnectionSerializationFlags flags)
{
gs_unref_ptrarray GPtrArray *vlans = NULL;
GVariantBuilder builder;
guint i;
const char *property_name = sett_info->property_infos[property_idx].name;
g_object_get (setting, property, &vlans, NULL);
nm_assert (property_name);
g_object_get (setting, property_name, &vlans, NULL);
g_variant_builder_init (&builder, G_VARIANT_TYPE ("aa{sv}"));
if (vlans) {