mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2025-12-31 20:40:18 +01:00
ifcfg-rh: refactor is_numbered_tag() macro and make it a function
Previously, IS_NUMBERED_TAG() could only be called with a C literal. Add is_numbered_tag() which can be called with any C string. Also, IS_NUMBERED_TAG_PARSE() and IS_NUMBERED_TAG() didn't do exactly the same. I think they should. The only difference was if the number was larger than 2^63-1. Now IS_NUMBERED_TAG() starts ignoring such keys, which is fine.
This commit is contained in:
parent
32033d9086
commit
3fa86a463c
3 changed files with 69 additions and 46 deletions
|
|
@ -586,3 +586,37 @@ nms_ifcfg_rh_utils_get_ethtool_by_name (const char *name)
|
|||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
gboolean
|
||||
nms_ifcfg_rh_utils_is_numbered_tag_impl (const char *key,
|
||||
const char *tag,
|
||||
gsize tag_len,
|
||||
gint64 *out_idx)
|
||||
{
|
||||
gint64 idx;
|
||||
|
||||
nm_assert (key);
|
||||
nm_assert (tag);
|
||||
nm_assert (tag_len == strlen (tag));
|
||||
nm_assert (tag_len > 0);
|
||||
|
||||
if (strncmp (key, tag, tag_len) != 0)
|
||||
return FALSE;
|
||||
|
||||
key += tag_len;
|
||||
|
||||
if (key[0] == '\0')
|
||||
return FALSE;
|
||||
|
||||
if (!NM_STRCHAR_ALL (key, ch, g_ascii_isdigit (ch)))
|
||||
return FALSE;
|
||||
|
||||
idx = _nm_utils_ascii_str_to_int64 (key, 10, 0, G_MAXINT64, -1);
|
||||
if (idx == -1)
|
||||
return FALSE;
|
||||
|
||||
NM_SET_OUT (out_idx, idx);
|
||||
return TRUE;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -70,6 +70,24 @@ _nms_ifcfg_rh_utils_numbered_tag (char *buf, gsize buf_len, const char *tag_name
|
|||
_nms_ifcfg_rh_utils_numbered_tag (buf, sizeof (buf), ""tag_name"", (which)); \
|
||||
})
|
||||
|
||||
gboolean nms_ifcfg_rh_utils_is_numbered_tag_impl (const char *key,
|
||||
const char *tag,
|
||||
gsize tag_len,
|
||||
gint64 *out_idx);
|
||||
|
||||
static inline gboolean
|
||||
nms_ifcfg_rh_utils_is_numbered_tag (const char *key,
|
||||
const char *tag,
|
||||
gint64 *out_idx)
|
||||
{
|
||||
nm_assert (tag);
|
||||
|
||||
return nms_ifcfg_rh_utils_is_numbered_tag_impl (key, tag, strlen (tag), out_idx);
|
||||
}
|
||||
|
||||
#define NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG(key, tag, out_idx) \
|
||||
nms_ifcfg_rh_utils_is_numbered_tag_impl (key, tag, NM_STRLEN (tag), out_idx)
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
extern const char *const _nm_ethtool_ifcfg_names[_NM_ETHTOOL_ID_FEATURE_NUM];
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@
|
|||
#include "nm-glib-aux/nm-enum-utils.h"
|
||||
#include "nm-glib-aux/nm-io-utils.h"
|
||||
#include "c-list/src/c-list.h"
|
||||
#include "nms-ifcfg-rh-utils.h"
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
|
|
@ -863,36 +864,6 @@ 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 *_key2 = (key); \
|
||||
\
|
||||
( (strncmp (_key2, tab_name, NM_STRLEN (tab_name)) == 0) \
|
||||
&& _is_all_digits (&_key2[NM_STRLEN (tab_name)])); \
|
||||
})
|
||||
|
||||
#define IS_NUMBERED_TAG_PARSE(key, tab_name, out_idx) \
|
||||
({ \
|
||||
const char *_key = (key); \
|
||||
gint64 _idx; \
|
||||
gboolean _good = FALSE; \
|
||||
gint64 *_out_idx = (out_idx); \
|
||||
\
|
||||
if ( IS_NUMBERED_TAG (_key, ""tab_name"") \
|
||||
&& (_idx = _nm_utils_ascii_str_to_int64 (&_key[NM_STRLEN (tab_name)], 10, 0, G_MAXINT64, -1)) != -1) { \
|
||||
NM_SET_OUT (_out_idx, _idx); \
|
||||
_good = TRUE; \
|
||||
} \
|
||||
_good; \
|
||||
})
|
||||
|
||||
static gboolean
|
||||
_svKeyMatchesType (const char *key, SvKeyType match_key_type)
|
||||
{
|
||||
|
|
@ -900,18 +871,18 @@ _svKeyMatchesType (const char *key, SvKeyType match_key_type)
|
|||
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"))
|
||||
if ( NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG (key, "ADDRESS", NULL)
|
||||
|| NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG (key, "NETMASK", NULL)
|
||||
|| NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG (key, "GATEWAY", NULL)
|
||||
|| NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG (key, "METRIC", NULL)
|
||||
|| NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG (key, "OPTIONS", NULL))
|
||||
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"))
|
||||
if ( NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG (key, "IPADDR", NULL)
|
||||
|| NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG (key, "PREFIX", NULL)
|
||||
|| NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG (key, "NETMASK", NULL)
|
||||
|| NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG (key, "GATEWAY", NULL))
|
||||
return TRUE;
|
||||
}
|
||||
if (NM_FLAGS_HAS (match_key_type, SV_KEY_TYPE_USER)) {
|
||||
|
|
@ -919,20 +890,20 @@ _svKeyMatchesType (const char *key, SvKeyType match_key_type)
|
|||
return TRUE;
|
||||
}
|
||||
if (NM_FLAGS_HAS (match_key_type, SV_KEY_TYPE_TC)) {
|
||||
if ( IS_NUMBERED_TAG (key, "QDISC")
|
||||
|| IS_NUMBERED_TAG (key, "FILTER"))
|
||||
if ( NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG (key, "QDISC", NULL)
|
||||
|| NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG (key, "FILTER", NULL))
|
||||
return TRUE;
|
||||
}
|
||||
if (NM_FLAGS_HAS (match_key_type, SV_KEY_TYPE_SRIOV_VF)) {
|
||||
if (IS_NUMBERED_TAG (key, "SRIOV_VF"))
|
||||
if (NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG (key, "SRIOV_VF", NULL))
|
||||
return TRUE;
|
||||
}
|
||||
if (NM_FLAGS_HAS (match_key_type, SV_KEY_TYPE_ROUTING_RULE4)) {
|
||||
if (IS_NUMBERED_TAG_PARSE (key, "ROUTING_RULE_", NULL))
|
||||
if (NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG (key, "ROUTING_RULE_", NULL))
|
||||
return TRUE;
|
||||
}
|
||||
if (NM_FLAGS_HAS (match_key_type, SV_KEY_TYPE_ROUTING_RULE6)) {
|
||||
if (IS_NUMBERED_TAG_PARSE (key, "ROUTING_RULE6_", NULL))
|
||||
if (NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG (key, "ROUTING_RULE6_", NULL))
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
|
@ -944,9 +915,9 @@ svNumberedParseKey (const char *key)
|
|||
{
|
||||
gint64 idx;
|
||||
|
||||
if (IS_NUMBERED_TAG_PARSE (key, "ROUTING_RULE_", &idx))
|
||||
if (NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG (key, "ROUTING_RULE_", &idx))
|
||||
return idx;
|
||||
if (IS_NUMBERED_TAG_PARSE (key, "ROUTING_RULE6_", &idx))
|
||||
if (NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG (key, "ROUTING_RULE6_", &idx))
|
||||
return idx;
|
||||
return -1;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue