summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2020-05-14 09:16:33 +0200
committerThomas Haller <thaller@redhat.com>2020-05-22 15:58:09 +0200
commit49db9d8d787a69ea4afb78761effbb00d3dc0c85 (patch)
tree3da5b862694ea906b71d01af993cc6c170ebc5f1
parent1a56a2105cf1b3ca18a2e889bb1edcf4539bacf7 (diff)
downloadNetworkManager-49db9d8d787a69ea4afb78761effbb00d3dc0c85.tar.gz
libnm: add nm_setting_option_clear_by_name()
More general purpose API for generic options of settings. The predicate function is also nicely usable via bindings. One question is about the form of the predicate. In this case, it is convenient to pass nm_ethtool_optname_is_coalesce(). On the other hand, it's not very flexible as it does not accept a user data argument. Use NMUtilsPredicateStr here, which is not flexible but convenient for where it's used.
-rw-r--r--libnm-core/nm-core-internal.h5
-rw-r--r--libnm-core/nm-setting-ethtool.c15
-rw-r--r--libnm-core/nm-setting.c47
-rw-r--r--libnm-core/nm-setting.h5
-rw-r--r--libnm/libnm.ver1
5 files changed, 44 insertions, 29 deletions
diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h
index 9fe658c1c2..6d7661b916 100644
--- a/libnm-core/nm-core-internal.h
+++ b/libnm-core/nm-core-internal.h
@@ -310,8 +310,6 @@ gboolean _nm_setting_get_property (NMSetting *setting, const char *name, GValue
/*****************************************************************************/
-typedef gboolean (*NMSettingOptionFilterFcn)(const char *name);
-
GHashTable *_nm_setting_option_hash (NMSetting *setting,
gboolean create_if_necessary);
@@ -325,9 +323,6 @@ guint _nm_setting_option_get_all (NMSetting *setting,
gboolean _nm_setting_option_clear (NMSetting *setting,
const char *optname);
-gboolean _nm_setting_option_clear_all (NMSetting *setting,
- NMSettingOptionFilterFcn filter);
-
/*****************************************************************************/
guint nm_setting_ethtool_init_features (NMSettingEthtool *setting,
diff --git a/libnm-core/nm-setting-ethtool.c b/libnm-core/nm-setting-ethtool.c
index 1be74cc9cb..ac0310da82 100644
--- a/libnm-core/nm-setting-ethtool.c
+++ b/libnm-core/nm-setting-ethtool.c
@@ -226,9 +226,8 @@ nm_setting_ethtool_clear_features (NMSettingEthtool *setting)
{
g_return_if_fail (NM_IS_SETTING_ETHTOOL (setting));
- if (_nm_setting_option_clear_all (NM_SETTING (setting),
- &nm_ethtool_optname_is_feature))
- _notify_attributes (setting);
+ nm_setting_option_clear_by_name (NM_SETTING (setting),
+ nm_ethtool_optname_is_feature);
}
guint
@@ -369,9 +368,8 @@ nm_setting_ethtool_clear_coalesce_all (NMSettingEthtool *setting)
{
g_return_if_fail (NM_IS_SETTING_ETHTOOL (setting));
- if (_nm_setting_option_clear_all (NM_SETTING (setting),
- &nm_ethtool_optname_is_coalesce))
- _notify_attributes (setting);
+ nm_setting_option_clear_by_name (NM_SETTING (setting),
+ nm_ethtool_optname_is_coalesce);
}
/**
@@ -468,9 +466,8 @@ nm_setting_ethtool_clear_ring_all (NMSettingEthtool *setting)
{
g_return_if_fail (NM_IS_SETTING_ETHTOOL (setting));
- if (_nm_setting_option_clear_all (NM_SETTING (setting),
- &nm_ethtool_optname_is_ring))
- _notify_attributes (setting);
+ nm_setting_option_clear_by_name (NM_SETTING (setting),
+ nm_ethtool_optname_is_ring);
}
/*****************************************************************************/
diff --git a/libnm-core/nm-setting.c b/libnm-core/nm-setting.c
index a99a46b0ea..dfb2bed958 100644
--- a/libnm-core/nm-setting.c
+++ b/libnm-core/nm-setting.c
@@ -2525,30 +2525,47 @@ _nm_setting_option_clear (NMSetting *setting,
return g_hash_table_remove (ht, optname);
}
-gboolean
-_nm_setting_option_clear_all (NMSetting *setting,
- NMSettingOptionFilterFcn filter)
+/**
+ * nm_setting_option_clear_by_name:
+ * @setting: the #NMSetting
+ * @predicate: (allow-none) (scope call): the predicate for which names
+ * should be clear.
+ * If the predicate returns %TRUE for an option name, the option
+ * gets removed. If %NULL, all options will be removed.
+ *
+ * Since: 1.26
+ */
+void
+nm_setting_option_clear_by_name (NMSetting *setting,
+ NMUtilsPredicateStr predicate)
{
- GHashTable *ht;
- const char *name;
+ GHashTable *hash;
GHashTableIter iter;
+ const char *name;
gboolean changed = FALSE;
- nm_assert (NM_IS_SETTING (setting));
+ g_return_if_fail (NM_IS_SETTING (setting));
- ht = _nm_setting_option_hash (setting, FALSE);
- if (!ht)
- return FALSE;
+ hash = _nm_setting_option_hash (NM_SETTING (setting), FALSE);
+ if (!hash)
+ return;
- g_hash_table_iter_init (&iter, ht);
- while (g_hash_table_iter_next (&iter, (gpointer *) &name, NULL)) {
- if (!filter || filter (name)) {
- g_hash_table_iter_remove (&iter);
- changed = TRUE;
+ if (!predicate) {
+ changed = (g_hash_table_size (hash) > 0);
+ if (changed)
+ g_hash_table_remove_all (hash);
+ } else {
+ g_hash_table_iter_init (&iter, hash);
+ while (g_hash_table_iter_next (&iter, (gpointer *) &name, NULL)) {
+ if (predicate (name)) {
+ g_hash_table_iter_remove (&iter);
+ changed = TRUE;
+ }
}
}
- return changed;
+ if (changed)
+ _nm_setting_option_notify (setting, TRUE);
}
/*****************************************************************************/
diff --git a/libnm-core/nm-setting.h b/libnm-core/nm-setting.h
index a3a732218e..01cd1503e8 100644
--- a/libnm-core/nm-setting.h
+++ b/libnm-core/nm-setting.h
@@ -366,6 +366,11 @@ NM_AVAILABLE_IN_1_26
const char *const*nm_setting_option_get_all_names (NMSetting *setting,
guint *out_len);
+
+NM_AVAILABLE_IN_1_26
+void nm_setting_option_clear_by_name (NMSetting *setting,
+ NMUtilsPredicateStr predicate);
+
/*****************************************************************************/
const GVariantType *nm_setting_get_dbus_property_type (NMSetting *setting,
diff --git a/libnm/libnm.ver b/libnm/libnm.ver
index 5a3c5eeeac..030a68c885 100644
--- a/libnm/libnm.ver
+++ b/libnm/libnm.ver
@@ -1732,6 +1732,7 @@ global:
nm_setting_match_remove_driver_by_value;
nm_setting_match_remove_kernel_command_line;
nm_setting_match_remove_kernel_command_line_by_value;
+ nm_setting_option_clear_by_name;
nm_setting_option_get;
nm_setting_option_get_all_names;
nm_setting_option_get_boolean;