config: add NMConfigGetValueFlags argument to nm_config_data_get_value()
In some cases we want the returned value to be stripped. In some cases, we want to read the raw value instead of the string parsed by GKeyFile. Add an flags argument to nm_config_data_get_value(). It is up to the caller to determine the exact meaning (and whether to strip). By adding the flags argument, the caller can get the desired behavior easier without having to workaround it afterwards. But more importantly, it becomes apparent that there are different ways to retrieve the value and the caller should decide on the details.
This commit is contained in:
@@ -118,19 +118,17 @@ nm_config_data_has_group (const NMConfigData *self, const char *group)
|
||||
}
|
||||
|
||||
char *
|
||||
nm_config_data_get_value (const NMConfigData *self, const char *group, const char *key)
|
||||
nm_config_data_get_value (const NMConfigData *self, const char *group, const char *key, NMConfigGetValueFlags flags)
|
||||
{
|
||||
g_return_val_if_fail (NM_IS_CONFIG_DATA (self), NULL);
|
||||
g_return_val_if_fail (group && *group, NULL);
|
||||
g_return_val_if_fail (key && *key, NULL);
|
||||
|
||||
/* nm_config_data_get_value() translates to g_key_file_get_string(), because we want
|
||||
* to use the string representation, not the (raw) GKeyFile value. */
|
||||
return g_key_file_get_string (NM_CONFIG_DATA_GET_PRIVATE (self)->keyfile, group, key, NULL);
|
||||
return nm_config_keyfile_get_value (NM_CONFIG_DATA_GET_PRIVATE (self)->keyfile, group, key, flags);
|
||||
}
|
||||
|
||||
gboolean
|
||||
nm_config_data_has_value (const NMConfigData *self, const char *group, const char *key)
|
||||
nm_config_data_has_value (const NMConfigData *self, const char *group, const char *key, NMConfigGetValueFlags flags)
|
||||
{
|
||||
gs_free char *value = NULL;
|
||||
|
||||
@@ -138,9 +136,7 @@ nm_config_data_has_value (const NMConfigData *self, const char *group, const cha
|
||||
g_return_val_if_fail (group && *group, FALSE);
|
||||
g_return_val_if_fail (key && *key, FALSE);
|
||||
|
||||
/* nm_config_data_get_value() translates to g_key_file_get_string(), because we want
|
||||
* to use the string representation, not the (raw) GKeyFile value. */
|
||||
value = g_key_file_get_string (NM_CONFIG_DATA_GET_PRIVATE (self)->keyfile, group, key, NULL);
|
||||
value = nm_config_keyfile_get_value (NM_CONFIG_DATA_GET_PRIVATE (self)->keyfile, group, key, flags);
|
||||
return !!value;
|
||||
}
|
||||
|
||||
|
@@ -45,6 +45,23 @@ G_BEGIN_DECLS
|
||||
#define NM_CONFIG_DATA_NO_AUTO_DEFAULT "no-auto-default"
|
||||
#define NM_CONFIG_DATA_DNS_MODE "dns"
|
||||
|
||||
typedef enum { /*<flags >*/
|
||||
NM_CONFIG_GET_VALUE_NONE = 0,
|
||||
|
||||
/* use g_key_file_get_value() instead of g_key_file_get_string(). */
|
||||
NM_CONFIG_GET_VALUE_RAW = (1LL << 0),
|
||||
|
||||
/* strip whitespaces */
|
||||
NM_CONFIG_GET_VALUE_STRIP = (1LL << 1),
|
||||
|
||||
/* if the returned string would be the empty word, return NULL. */
|
||||
NM_CONFIG_GET_VALUE_NO_EMPTY = (1LL << 2),
|
||||
|
||||
/* special flag to read device spec. You want to use this before passing the
|
||||
* value to nm_match_spec_split(). */
|
||||
NM_CONFIG_GET_VALUE_TYPE_SPEC = NM_CONFIG_GET_VALUE_RAW,
|
||||
} NMConfigGetValueFlags;
|
||||
|
||||
typedef enum { /*< flags >*/
|
||||
NM_CONFIG_CHANGE_NONE = 0,
|
||||
|
||||
@@ -87,8 +104,8 @@ const char *nm_config_data_get_config_main_file (const NMConfigData *config_data
|
||||
const char *nm_config_data_get_config_description (const NMConfigData *config_data);
|
||||
|
||||
gboolean nm_config_data_has_group (const NMConfigData *self, const char *group);
|
||||
gboolean nm_config_data_has_value (const NMConfigData *self, const char *group, const char *key);
|
||||
char *nm_config_data_get_value (const NMConfigData *config_data, const char *group, const char *key);
|
||||
gboolean nm_config_data_has_value (const NMConfigData *self, const char *group, const char *key, NMConfigGetValueFlags flags);
|
||||
char *nm_config_data_get_value (const NMConfigData *config_data, const char *group, const char *key, NMConfigGetValueFlags flags);
|
||||
gint nm_config_data_get_value_boolean (const NMConfigData *self, const char *group, const char *key, gint default_value);
|
||||
|
||||
const char *nm_config_data_get_connectivity_uri (const NMConfigData *config_data);
|
||||
|
@@ -156,6 +156,34 @@ nm_config_keyfile_get_boolean (GKeyFile *keyfile,
|
||||
return nm_config_parse_boolean (str, default_value);
|
||||
}
|
||||
|
||||
char *
|
||||
nm_config_keyfile_get_value (GKeyFile *keyfile,
|
||||
const char *section,
|
||||
const char *key,
|
||||
NMConfigGetValueFlags flags)
|
||||
{
|
||||
char *value;
|
||||
|
||||
if (NM_FLAGS_HAS (flags, NM_CONFIG_GET_VALUE_RAW))
|
||||
value = g_key_file_get_value (keyfile, section, key, NULL);
|
||||
else
|
||||
value = g_key_file_get_string (keyfile, section, key, NULL);
|
||||
|
||||
if (!value)
|
||||
return NULL;
|
||||
|
||||
if (NM_FLAGS_HAS (flags, NM_CONFIG_GET_VALUE_STRIP))
|
||||
g_strstrip (value);
|
||||
|
||||
if ( NM_FLAGS_HAS (flags, NM_CONFIG_GET_VALUE_NO_EMPTY)
|
||||
&& !*value) {
|
||||
g_free (value);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
void
|
||||
nm_config_keyfile_set_string_list (GKeyFile *keyfile,
|
||||
const char *group,
|
||||
|
@@ -116,6 +116,10 @@ gint nm_config_keyfile_get_boolean (GKeyFile *keyfile,
|
||||
const char *section,
|
||||
const char *key,
|
||||
gint default_value);
|
||||
char *nm_config_keyfile_get_value (GKeyFile *keyfile,
|
||||
const char *section,
|
||||
const char *key,
|
||||
NMConfigGetValueFlags flags);
|
||||
void nm_config_keyfile_set_string_list (GKeyFile *keyfile,
|
||||
const char *group,
|
||||
const char *key,
|
||||
|
@@ -323,8 +323,8 @@ config_changed_cb (NMConfig *config,
|
||||
{
|
||||
gs_free char *old_value = NULL, *new_value = NULL;
|
||||
|
||||
old_value = nm_config_data_get_value (old_data, NM_CONFIG_KEYFILE_GROUP_KEYFILE, "unmanaged-devices");
|
||||
new_value = nm_config_data_get_value (config_data, NM_CONFIG_KEYFILE_GROUP_KEYFILE, "unmanaged-devices");
|
||||
old_value = nm_config_data_get_value (old_data, NM_CONFIG_KEYFILE_GROUP_KEYFILE, "unmanaged-devices", NM_CONFIG_GET_VALUE_TYPE_SPEC);
|
||||
new_value = nm_config_data_get_value (config_data, NM_CONFIG_KEYFILE_GROUP_KEYFILE, "unmanaged-devices", NM_CONFIG_GET_VALUE_TYPE_SPEC);
|
||||
|
||||
if (g_strcmp0 (old_value, new_value) != 0)
|
||||
g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_UNMANAGED_SPECS_CHANGED);
|
||||
@@ -526,7 +526,7 @@ get_unmanaged_specs (NMSystemConfigInterface *config)
|
||||
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (config);
|
||||
gs_free char *value = NULL;
|
||||
|
||||
value = nm_config_data_get_value (nm_config_get_data (priv->config), NM_CONFIG_KEYFILE_GROUP_KEYFILE, "unmanaged-devices");
|
||||
value = nm_config_data_get_value (nm_config_get_data (priv->config), NM_CONFIG_KEYFILE_GROUP_KEYFILE, "unmanaged-devices", NM_CONFIG_GET_VALUE_TYPE_SPEC);
|
||||
return nm_match_spec_split (value);
|
||||
}
|
||||
|
||||
@@ -639,19 +639,16 @@ nm_settings_keyfile_plugin_new (void)
|
||||
{
|
||||
static SCPluginKeyfile *singleton = NULL;
|
||||
SCPluginKeyfilePrivate *priv;
|
||||
char *value;
|
||||
|
||||
if (!singleton) {
|
||||
singleton = SC_PLUGIN_KEYFILE (g_object_new (SC_TYPE_PLUGIN_KEYFILE, NULL));
|
||||
priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (singleton);
|
||||
|
||||
priv->config = g_object_ref (nm_config_get ());
|
||||
value = nm_config_data_get_value (nm_config_get_data (priv->config),
|
||||
NM_CONFIG_KEYFILE_GROUP_KEYFILE, "hostname");
|
||||
if (value) {
|
||||
if (nm_config_data_has_value (nm_config_get_data_orig (priv->config),
|
||||
NM_CONFIG_KEYFILE_GROUP_KEYFILE, "hostname",
|
||||
NM_CONFIG_GET_VALUE_RAW))
|
||||
nm_log_warn (LOGD_SETTINGS, "keyfile: 'hostname' option is deprecated and has no effect");
|
||||
g_free (value);
|
||||
}
|
||||
} else
|
||||
g_object_ref (singleton);
|
||||
|
||||
|
@@ -110,21 +110,21 @@ test_config_simple (void)
|
||||
g_assert_cmpstr (plugins[1], ==, "bar");
|
||||
g_assert_cmpstr (plugins[2], ==, "baz");
|
||||
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "extra-section", "extra-key");
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "extra-section", "extra-key", NM_CONFIG_GET_VALUE_NONE);
|
||||
g_assert_cmpstr (value, ==, "some value");
|
||||
g_free (value);
|
||||
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "extra-section", "no-key");
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "extra-section", "no-key", NM_CONFIG_GET_VALUE_NONE);
|
||||
g_assert (!value);
|
||||
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "no-section", "no-key");
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "no-section", "no-key", NM_CONFIG_GET_VALUE_NONE);
|
||||
g_assert (!value);
|
||||
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "connection", "ipv6.ip6_privacy");
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "connection", "ipv6.ip6_privacy", NM_CONFIG_GET_VALUE_NONE);
|
||||
g_assert_cmpstr (value, ==, "0");
|
||||
g_free (value);
|
||||
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "connection.dev51", "ipv4.route-metric");
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "connection.dev51", "ipv4.route-metric", NM_CONFIG_GET_VALUE_NONE);
|
||||
g_assert_cmpstr (value, ==, "51");
|
||||
g_free (value);
|
||||
|
||||
@@ -301,21 +301,21 @@ test_config_confdir (void)
|
||||
g_assert_cmpstr (plugins[3], ==, "one");
|
||||
g_assert_cmpstr (plugins[4], ==, "two");
|
||||
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "main", "extra");
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "main", "extra", NM_CONFIG_GET_VALUE_NONE);
|
||||
g_assert_cmpstr (value, ==, "hello");
|
||||
g_free (value);
|
||||
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "main", "new");
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "main", "new", NM_CONFIG_GET_VALUE_NONE);
|
||||
g_assert_cmpstr (value, ==, "something"); /* not ",something" */
|
||||
g_free (value);
|
||||
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "order", "a");
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "order", "a", NM_CONFIG_GET_VALUE_NONE);
|
||||
g_assert_cmpstr (value, ==, "90");
|
||||
g_free (value);
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "order", "b");
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "order", "b", NM_CONFIG_GET_VALUE_NONE);
|
||||
g_assert_cmpstr (value, ==, "10");
|
||||
g_free (value);
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "order", "c");
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "order", "c", NM_CONFIG_GET_VALUE_NONE);
|
||||
g_assert_cmpstr (value, ==, "0");
|
||||
g_free (value);
|
||||
|
||||
@@ -336,23 +336,23 @@ test_config_confdir (void)
|
||||
ASSERT_GET_CONN_DEFAULT (config, "ord.key09", "C-2.1.09");
|
||||
ASSERT_GET_CONN_DEFAULT (config, "ord.ovw01", "C-0.1.ovw01");
|
||||
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "append", "val1");
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "append", "val1", NM_CONFIG_GET_VALUE_NONE);
|
||||
g_assert_cmpstr (value, ==, "a,c");
|
||||
g_free (value);
|
||||
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "append", "val2");
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "append", "val2", NM_CONFIG_GET_VALUE_NONE);
|
||||
g_assert_cmpstr (value, ==, "VAL2");
|
||||
g_free (value);
|
||||
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "append", "val3");
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "append", "val3", NM_CONFIG_GET_VALUE_NONE);
|
||||
g_assert_cmpstr (value, ==, NULL);
|
||||
g_free (value);
|
||||
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "append", "val4");
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "append", "val4", NM_CONFIG_GET_VALUE_NONE);
|
||||
g_assert_cmpstr (value, ==, "vb,vb");
|
||||
g_free (value);
|
||||
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "append", "val5");
|
||||
value = nm_config_data_get_value (nm_config_get_data_orig (config), "append", "val5", NM_CONFIG_GET_VALUE_NONE);
|
||||
g_assert_cmpstr (value, ==, "VAL5");
|
||||
g_free (value);
|
||||
|
||||
|
Reference in New Issue
Block a user