diff options
author | Beniamino Galvani <bgalvani@redhat.com> | 2018-06-04 15:11:57 +0200 |
---|---|---|
committer | Beniamino Galvani <bgalvani@redhat.com> | 2018-07-11 16:16:22 +0200 |
commit | 347e0d8b5a0f172c80fc230b930117001956459e (patch) | |
tree | 66eebd4f124466833b2c0a80ce9eaa3082a758e5 | |
parent | d7382fbe93c60dfe133e14f6289eb82c1303f76b (diff) | |
download | NetworkManager-347e0d8b5a0f172c80fc230b930117001956459e.tar.gz |
ifcfg-rh: add @match_key_type argument to svGetKeys()
Add a @match_key_type to svGetKeys() to filter the keys to be returned.
-rw-r--r-- | src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c | 5 | ||||
-rw-r--r-- | src/settings/plugins/ifcfg-rh/shvar.c | 107 | ||||
-rw-r--r-- | src/settings/plugins/ifcfg-rh/shvar.h | 19 |
3 files changed, 68 insertions, 63 deletions
diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c index 57394791a6..882932b11a 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c @@ -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) diff --git a/src/settings/plugins/ifcfg-rh/shvar.c b/src/settings/plugins/ifcfg-rh/shvar.c index c739b19dec..e8d028094b 100644 --- a/src/settings/plugins/ifcfg-rh/shvar.c +++ b/src/settings/plugins/ifcfg-rh/shvar.c @@ -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; + } } } diff --git a/src/settings/plugins/ifcfg-rh/shvar.h b/src/settings/plugins/ifcfg-rh/shvar.h index 838c79cc16..bef5f47743 100644 --- a/src/settings/plugins/ifcfg-rh/shvar.h +++ b/src/settings/plugins/ifcfg-rh/shvar.h @@ -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 |