ifcfg-rh: refactor common parts of write_wired_setting()/write_wired_for_virtual()

This commit is contained in:
Thomas Haller 2021-05-10 22:44:41 +02:00
parent a21714b821
commit 166c458411
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -1077,16 +1077,112 @@ write_hostname_setting(NMConnection *connection, shvarFile *ifcfg)
svSetValueInt64_cond(ifcfg, "HOSTNAME_ONLY_FROM_DEFAULT", t != NM_TERNARY_DEFAULT, t);
}
static gboolean
write_wired_setting(NMConnection *connection, shvarFile *ifcfg, GError **error)
static void
write_wired_setting_impl(NMSettingWired *s_wired, shvarFile *ifcfg, gboolean is_virtual)
{
NMSettingWired * s_wired;
const char *const *s390_subchannels;
guint32 mtu;
guint32 num_opts;
guint32 i;
const char *const *macaddr_blacklist;
svSetValue(ifcfg,
"HWADDR",
nm_setting_wired_get_mac_address(s_wired) ?: (is_virtual ? "" : NULL));
svSetValueStr(ifcfg, "MACADDR", nm_setting_wired_get_cloned_mac_address(s_wired));
svSetValueStr(ifcfg,
"GENERATE_MAC_ADDRESS_MASK",
nm_setting_wired_get_generate_mac_address_mask(s_wired));
if (!is_virtual) {
macaddr_blacklist = nm_setting_wired_get_mac_address_blacklist(s_wired);
if (macaddr_blacklist[0]) {
gs_free char *blacklist_str = NULL;
blacklist_str = g_strjoinv(" ", (char **) macaddr_blacklist);
svSetValueStr(ifcfg, "HWADDR_BLACKLIST", blacklist_str);
}
}
mtu = nm_setting_wired_get_mtu(s_wired);
svSetValueInt64_cond(ifcfg, "MTU", mtu != 0, mtu);
if (!is_virtual) {
s390_subchannels = nm_setting_wired_get_s390_subchannels(s_wired);
{
gs_free char *tmp = NULL;
gsize len = NM_PTRARRAY_LEN(s390_subchannels);
if (len == 2) {
tmp = g_strdup_printf("%s,%s", s390_subchannels[0], s390_subchannels[1]);
} else if (len == 3) {
tmp = g_strdup_printf("%s,%s,%s",
s390_subchannels[0],
s390_subchannels[1],
s390_subchannels[2]);
}
svSetValueStr(ifcfg, "SUBCHANNELS", tmp);
}
svSetValueStr(ifcfg, "NETTYPE", nm_setting_wired_get_s390_nettype(s_wired));
svSetValueStr(ifcfg,
"PORTNAME",
nm_setting_wired_get_s390_option_by_key(s_wired, "portname"));
svSetValueStr(ifcfg,
"CTCPROT",
nm_setting_wired_get_s390_option_by_key(s_wired, "ctcprot"));
num_opts = nm_setting_wired_get_num_s390_options(s_wired);
if (s390_subchannels && num_opts) {
nm_auto_free_gstring GString *tmp = NULL;
for (i = 0; i < num_opts; i++) {
const char *s390_key, *s390_val;
nm_setting_wired_get_s390_option(s_wired, i, &s390_key, &s390_val);
/* portname is handled separately */
if (NM_IN_STRSET(s390_key, "portname", "ctcprot"))
continue;
if (strchr(s390_key, '=')) {
/* this key cannot be expressed. But after all, it's not valid anyway
* and the connection shouldn't even verify. */
continue;
}
if (!tmp)
tmp = g_string_sized_new(30);
else
g_string_append_c(tmp, ' ');
nm_utils_escaped_tokens_escape_gstr(s390_key, NM_ASCII_SPACES, tmp);
g_string_append_c(tmp, '=');
nm_utils_escaped_tokens_escape_gstr(s390_val, NM_ASCII_SPACES, tmp);
}
if (tmp)
svSetValueStr(ifcfg, "OPTIONS", tmp->str);
}
}
if (!is_virtual)
svSetValueStr(ifcfg, "TYPE", TYPE_ETHERNET);
svSetValueTernary(ifcfg,
"ACCEPT_ALL_MAC_ADDRESSES",
nm_setting_wired_get_accept_all_mac_addresses(s_wired));
}
static gboolean
write_wired_setting(NMConnection *connection, shvarFile *ifcfg, GError **error)
{
NMSettingWired *s_wired;
s_wired = nm_connection_get_setting_wired(connection);
if (!s_wired) {
g_set_error(error,
@ -1097,86 +1193,7 @@ write_wired_setting(NMConnection *connection, shvarFile *ifcfg, GError **error)
return FALSE;
}
svSetValueStr(ifcfg, "HWADDR", nm_setting_wired_get_mac_address(s_wired));
svSetValueStr(ifcfg, "MACADDR", nm_setting_wired_get_cloned_mac_address(s_wired));
svSetValueStr(ifcfg,
"GENERATE_MAC_ADDRESS_MASK",
nm_setting_wired_get_generate_mac_address_mask(s_wired));
macaddr_blacklist = nm_setting_wired_get_mac_address_blacklist(s_wired);
if (macaddr_blacklist[0]) {
gs_free char *blacklist_str = NULL;
blacklist_str = g_strjoinv(" ", (char **) macaddr_blacklist);
svSetValueStr(ifcfg, "HWADDR_BLACKLIST", blacklist_str);
}
mtu = nm_setting_wired_get_mtu(s_wired);
svSetValueInt64_cond(ifcfg, "MTU", mtu != 0, mtu);
s390_subchannels = nm_setting_wired_get_s390_subchannels(s_wired);
{
gs_free char *tmp = NULL;
gsize len = NM_PTRARRAY_LEN(s390_subchannels);
if (len == 2) {
tmp = g_strdup_printf("%s,%s", s390_subchannels[0], s390_subchannels[1]);
} else if (len == 3) {
tmp = g_strdup_printf("%s,%s,%s",
s390_subchannels[0],
s390_subchannels[1],
s390_subchannels[2]);
}
svSetValueStr(ifcfg, "SUBCHANNELS", tmp);
}
svSetValueStr(ifcfg, "NETTYPE", nm_setting_wired_get_s390_nettype(s_wired));
svSetValueStr(ifcfg, "PORTNAME", nm_setting_wired_get_s390_option_by_key(s_wired, "portname"));
svSetValueStr(ifcfg, "CTCPROT", nm_setting_wired_get_s390_option_by_key(s_wired, "ctcprot"));
num_opts = nm_setting_wired_get_num_s390_options(s_wired);
if (s390_subchannels && num_opts) {
nm_auto_free_gstring GString *tmp = NULL;
for (i = 0; i < num_opts; i++) {
const char *s390_key, *s390_val;
nm_setting_wired_get_s390_option(s_wired, i, &s390_key, &s390_val);
/* portname is handled separately */
if (NM_IN_STRSET(s390_key, "portname", "ctcprot"))
continue;
if (strchr(s390_key, '=')) {
/* this key cannot be expressed. But after all, it's not valid anyway
* and the connection shouldn't even verify. */
continue;
}
if (!tmp)
tmp = g_string_sized_new(30);
else
g_string_append_c(tmp, ' ');
nm_utils_escaped_tokens_escape_gstr(s390_key, NM_ASCII_SPACES, tmp);
g_string_append_c(tmp, '=');
nm_utils_escaped_tokens_escape_gstr(s390_val, NM_ASCII_SPACES, tmp);
}
if (tmp)
svSetValueStr(ifcfg, "OPTIONS", tmp->str);
}
svSetValueStr(ifcfg, "TYPE", TYPE_ETHERNET);
svSetValueTernary(ifcfg,
"ACCEPT_ALL_MAC_ADDRESSES",
nm_setting_wired_get_accept_all_mac_addresses(s_wired));
write_wired_setting_impl(s_wired, ifcfg, FALSE);
return TRUE;
}
@ -1184,30 +1201,12 @@ static gboolean
write_wired_for_virtual(NMConnection *connection, shvarFile *ifcfg)
{
NMSettingWired *s_wired;
const char * device_mac;
const char * cloned_mac;
guint32 mtu;
s_wired = nm_connection_get_setting_wired(connection);
if (!s_wired)
return FALSE;
device_mac = nm_setting_wired_get_mac_address(s_wired);
svSetValue(ifcfg, "HWADDR", device_mac ?: "");
cloned_mac = nm_setting_wired_get_cloned_mac_address(s_wired);
svSetValueStr(ifcfg, "MACADDR", cloned_mac);
svSetValueStr(ifcfg,
"GENERATE_MAC_ADDRESS_MASK",
nm_setting_wired_get_generate_mac_address_mask(s_wired));
svSetValueTernary(ifcfg,
"ACCEPT_ALL_MAC_ADDRESSES",
nm_setting_wired_get_accept_all_mac_addresses(s_wired));
mtu = nm_setting_wired_get_mtu(s_wired);
svSetValueInt64_cond(ifcfg, "MTU", mtu != 0, mtu);
write_wired_setting_impl(s_wired, ifcfg, TRUE);
return TRUE;
}