Commit 347e0d8b authored by Beniamino Galvani's avatar Beniamino Galvani

ifcfg-rh: add @match_key_type argument to svGetKeys()

Add a @match_key_type to svGetKeys() to filter the keys to be returned.
parent d7382fbe
......@@ -1178,7 +1178,7 @@ make_user_setting (shvarFile *ifcfg)
const char *key;
nm_auto_free_gstring GString *str = NULL;
keys = svGetKeys (ifcfg);
keys = svGetKeys (ifcfg, SV_KEY_TYPE_USER);
if (!keys)
return NULL;
......@@ -1187,9 +1187,6 @@ make_user_setting (shvarFile *ifcfg)
const char *value;
gs_free char *value_to_free = NULL;
if (!g_str_has_prefix (key, "NM_USER_"))
continue;
value = svGetValue (ifcfg, key, &value_to_free);
if (!value)
......
......@@ -870,8 +870,57 @@ svCreateFile (const char *name)
/*****************************************************************************/
static gboolean
_is_all_digits (const char *str)
{
return str[0]
&& NM_STRCHAR_ALL (str, ch, g_ascii_isdigit (ch));
}
#define IS_NUMBERED_TAG(key, tab_name) \
({ \
const char *_key = (key); \
\
( (strncmp (_key, tab_name, NM_STRLEN (tab_name)) == 0) \
&& _is_all_digits (&_key[NM_STRLEN (tab_name)])); \
})
static gboolean
_svKeyMatchesType (const char *key, SvKeyType match_key_type)
{
if (NM_FLAGS_HAS (match_key_type, SV_KEY_TYPE_ANY))
return TRUE;
if (NM_FLAGS_HAS (match_key_type, SV_KEY_TYPE_ROUTE_SVFORMAT)) {
if ( IS_NUMBERED_TAG (key, "ADDRESS")
|| IS_NUMBERED_TAG (key, "NETMASK")
|| IS_NUMBERED_TAG (key, "GATEWAY")
|| IS_NUMBERED_TAG (key, "METRIC")
|| IS_NUMBERED_TAG (key, "OPTIONS"))
return TRUE;
}
if (NM_FLAGS_HAS (match_key_type, SV_KEY_TYPE_IP4_ADDRESS)) {
if ( IS_NUMBERED_TAG (key, "IPADDR")
|| IS_NUMBERED_TAG (key, "PREFIX")
|| IS_NUMBERED_TAG (key, "NETMASK")
|| IS_NUMBERED_TAG (key, "GATEWAY"))
return TRUE;
}
if (NM_FLAGS_HAS (match_key_type, SV_KEY_TYPE_USER)) {
if (g_str_has_prefix (key, "NM_USER_"))
return TRUE;
}
if (NM_FLAGS_HAS (match_key_type, SV_KEY_TYPE_TC)) {
if ( IS_NUMBERED_TAG (key, "QDISC")
|| IS_NUMBERED_TAG (key, "FILTER"))
return TRUE;
}
return FALSE;
}
GHashTable *
svGetKeys (shvarFile *s)
svGetKeys (shvarFile *s, SvKeyType match_key_type)
{
GHashTable *keys = NULL;
CList *current;
......@@ -881,7 +930,9 @@ svGetKeys (shvarFile *s)
c_list_for_each (current, &s->lst_head) {
line = c_list_entry (current, shvarLine, lst);
if (line->key && line->line) {
if ( line->key
&& line->line
&& _svKeyMatchesType (line->key, match_key_type)) {
/* we don't clone the keys. The keys are only valid
* until @s gets modified. */
if (!keys)
......@@ -1120,21 +1171,6 @@ svGetValueEnum (shvarFile *s, const char *key,
/*****************************************************************************/
static gboolean
_is_all_digits (const char *str)
{
return str[0]
&& NM_STRCHAR_ALL (str, ch, g_ascii_isdigit (ch));
}
#define IS_NUMBERED_TAG(key, tab_name) \
({ \
const char *_key = (key); \
\
( (strncmp (_key, tab_name, NM_STRLEN (tab_name)) == 0) \
&& _is_all_digits (&_key[NM_STRLEN (tab_name)])); \
})
gboolean
svUnsetAll (shvarFile *s, SvKeyType match_key_type)
{
......@@ -1150,38 +1186,11 @@ svUnsetAll (shvarFile *s, SvKeyType match_key_type)
if (!line->key)
continue;
if (NM_FLAGS_HAS (match_key_type, SV_KEY_TYPE_ANY))
goto do_clear;
if (NM_FLAGS_HAS (match_key_type, SV_KEY_TYPE_ROUTE_SVFORMAT)) {
if ( IS_NUMBERED_TAG (line->key, "ADDRESS")
|| IS_NUMBERED_TAG (line->key, "NETMASK")
|| IS_NUMBERED_TAG (line->key, "GATEWAY")
|| IS_NUMBERED_TAG (line->key, "METRIC")
|| IS_NUMBERED_TAG (line->key, "OPTIONS"))
goto do_clear;
}
if (NM_FLAGS_HAS (match_key_type, SV_KEY_TYPE_IP4_ADDRESS)) {
if ( IS_NUMBERED_TAG (line->key, "IPADDR")
|| IS_NUMBERED_TAG (line->key, "PREFIX")
|| IS_NUMBERED_TAG (line->key, "NETMASK")
|| IS_NUMBERED_TAG (line->key, "GATEWAY"))
goto do_clear;
}
if (NM_FLAGS_HAS (match_key_type, SV_KEY_TYPE_USER)) {
if (g_str_has_prefix (line->key, "NM_USER_"))
goto do_clear;
}
if (NM_FLAGS_HAS (match_key_type, SV_KEY_TYPE_TC)) {
if ( IS_NUMBERED_TAG (line->key, "QDISC")
|| IS_NUMBERED_TAG (line->key, "FILTER"))
goto do_clear;
}
continue;
do_clear:
if (nm_clear_g_free (&line->line)) {
ASSERT_shvarLine (line);
changed = TRUE;
if (_svKeyMatchesType (line->key, match_key_type)) {
if (nm_clear_g_free (&line->line)) {
ASSERT_shvarLine (line);
changed = TRUE;
}
}
}
......
......@@ -33,6 +33,14 @@
typedef struct _shvarFile shvarFile;
typedef enum {
SV_KEY_TYPE_ANY = (1LL << 0),
SV_KEY_TYPE_ROUTE_SVFORMAT = (1LL << 1),
SV_KEY_TYPE_IP4_ADDRESS = (1LL << 2),
SV_KEY_TYPE_TC = (1LL << 3),
SV_KEY_TYPE_USER = (1LL << 4),
} SvKeyType;
const char *svFileGetName (const shvarFile *s);
void _nmtst_svFileSetName (shvarFile *s, const char *fileName);
......@@ -58,7 +66,7 @@ char *svGetValueStr_cp (shvarFile *s, const char *key);
int svParseBoolean (const char *value, int def);
GHashTable *svGetKeys (shvarFile *s);
GHashTable *svGetKeys (shvarFile *s, SvKeyType match_key_type);
/* return TRUE if <key> resolves to any truth value (e.g. "yes", "y", "true")
* return FALSE if <key> resolves to any non-truth value (e.g. "no", "n", "false")
......@@ -85,15 +93,6 @@ gboolean svSetValueInt64_cond (shvarFile *s, const char *key, gboolean do_set, g
gboolean svSetValueEnum (shvarFile *s, const char *key, GType gtype, int value);
gboolean svUnsetValue (shvarFile *s, const char *key);
typedef enum {
SV_KEY_TYPE_ANY = (1LL << 0),
SV_KEY_TYPE_ROUTE_SVFORMAT = (1LL << 1),
SV_KEY_TYPE_IP4_ADDRESS = (1LL << 2),
SV_KEY_TYPE_TC = (1LL << 3),
SV_KEY_TYPE_USER = (1LL << 4),
} SvKeyType;
gboolean svUnsetAll (shvarFile *s, SvKeyType match_key_type);
/* Write the current contents iff modified. Returns FALSE on error
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment