Blame SOURCES/0009-ifcfg-rh-fix-null-next-hop.patch

fc9aca
From a3d2153b90f3f56f1548d01be674a0ab5e82e6b7 Mon Sep 17 00:00:00 2001
fc9aca
From: Beniamino Galvani <bgalvani@redhat.com>
fc9aca
Date: Fri, 19 May 2017 16:18:55 +0200
fc9aca
Subject: [PATCH] ifcfg-rh: omit empty next hop for routes in legacy format
fc9aca
fc9aca
Don't add "via (null)" if the next hop is missing.
fc9aca
fc9aca
https://bugzilla.redhat.com/show_bug.cgi?id=1452648
fc9aca
(cherry picked from commit af8aac9b544cb64df3b77a413dfded23e976d1b0)
fc9aca
(cherry picked from commit cb5ba08f00691b18d272bfb08e4929d00fa246bb)
fc9aca
---
fc9aca
 .../plugins/ifcfg-rh/nms-ifcfg-rh-writer.c         | 77 ++++++++++------------
fc9aca
 1 file changed, 36 insertions(+), 41 deletions(-)
fc9aca
fc9aca
diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c
fc9aca
index d6f33c4..400e9bd 100644
fc9aca
--- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c
fc9aca
+++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c
fc9aca
@@ -1926,12 +1926,8 @@ get_route_attributes_string (NMIPRoute *route, int family)
fc9aca
 static gboolean
fc9aca
 write_route_file_legacy (const char *filename, NMSettingIPConfig *s_ip4, GError **error)
fc9aca
 {
fc9aca
-	const char *dest, *next_hop;
fc9aca
-	char **route_items;
fc9aca
-	gs_free char *route_contents = NULL;
fc9aca
+	nm_auto_free_gstring GString *contents = NULL;
fc9aca
 	NMIPRoute *route;
fc9aca
-	guint32 prefix;
fc9aca
-	gint64 metric;
fc9aca
 	guint32 i, num;
fc9aca
 
fc9aca
 	g_return_val_if_fail (filename != NULL, FALSE);
fc9aca
@@ -1945,36 +1941,34 @@ write_route_file_legacy (const char *filename, NMSettingIPConfig *s_ip4, GError
fc9aca
 		return TRUE;
fc9aca
 	}
fc9aca
 
fc9aca
-	route_items = g_malloc0 (sizeof (char *) * (num + 1));
fc9aca
+	contents = g_string_new ("");
fc9aca
+
fc9aca
 	for (i = 0; i < num; i++) {
fc9aca
+		const char *next_hop;
fc9aca
 		gs_free char *options = NULL;
fc9aca
+		gint64 metric;
fc9aca
 
fc9aca
 		route = nm_setting_ip_config_get_route (s_ip4, i);
fc9aca
-
fc9aca
-		dest = nm_ip_route_get_dest (route);
fc9aca
-		prefix = nm_ip_route_get_prefix (route);
fc9aca
 		next_hop = nm_ip_route_get_next_hop (route);
fc9aca
 		metric = nm_ip_route_get_metric (route);
fc9aca
-
fc9aca
 		options = get_route_attributes_string (route, AF_INET);
fc9aca
 
fc9aca
-		if (metric == -1) {
fc9aca
-			route_items[i] = g_strdup_printf ("%s/%u via %s%s%s\n",
fc9aca
-			                                  dest, prefix, next_hop,
fc9aca
-			                                  options ? " " : "",
fc9aca
-			                                  options ?: "");
fc9aca
-		} else {
fc9aca
-			route_items[i] = g_strdup_printf ("%s/%u via %s metric %u%s%s\n",
fc9aca
-			                                  dest, prefix, next_hop, (guint32) metric,
fc9aca
-			                                  options ? " " : "",
fc9aca
-			                                  options ?: "");
fc9aca
+		g_string_append_printf (contents, "%s/%u",
fc9aca
+		                        nm_ip_route_get_dest (route),
fc9aca
+		                        nm_ip_route_get_prefix (route));
fc9aca
+		if (next_hop)
fc9aca
+			g_string_append_printf (contents, " via %s", next_hop);
fc9aca
+		if (metric >= 0)
fc9aca
+			g_string_append_printf (contents, " metric %u", (guint) metric);
fc9aca
+		if (options) {
fc9aca
+			g_string_append_c (contents, ' ');
fc9aca
+			g_string_append (contents, options);
fc9aca
 		}
fc9aca
+
fc9aca
+		g_string_append_c (contents, '\n');
fc9aca
 	}
fc9aca
-	route_items[num] = NULL;
fc9aca
-	route_contents = g_strjoinv (NULL, route_items);
fc9aca
-	g_strfreev (route_items);
fc9aca
 
fc9aca
-	if (!g_file_set_contents (filename, route_contents, -1, NULL)) {
fc9aca
+	if (!g_file_set_contents (filename, contents->str, contents->len, NULL)) {
fc9aca
 		g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
fc9aca
 		             "Writing route file '%s' failed", filename);
fc9aca
 		return FALSE;
fc9aca
@@ -2492,32 +2486,33 @@ write_route6_file (const char *filename, NMSettingIPConfig *s_ip6, GError **erro
fc9aca
 	}
fc9aca
 
fc9aca
 	contents = g_string_new ("");
fc9aca
+
fc9aca
 	for (i = 0; i < num; i++) {
fc9aca
 		gs_free char *options = NULL;
fc9aca
+		const char *next_hop;
fc9aca
+		gint64 metric;
fc9aca
 
fc9aca
 		route = nm_setting_ip_config_get_route (s_ip6, i);
fc9aca
+		next_hop = nm_ip_route_get_next_hop (route);
fc9aca
+		metric = nm_ip_route_get_metric (route);
fc9aca
 		options = get_route_attributes_string (route, AF_INET6);
fc9aca
 
fc9aca
-		if (nm_ip_route_get_metric (route) == -1) {
fc9aca
-			g_string_append_printf (contents, "%s/%u via %s%s%s",
fc9aca
-			                                  nm_ip_route_get_dest (route),
fc9aca
-			                                  nm_ip_route_get_prefix (route),
fc9aca
-			                                  nm_ip_route_get_next_hop (route),
fc9aca
-			                                  options ? " " : "",
fc9aca
-			                                  options ?: "");
fc9aca
-		} else {
fc9aca
-			g_string_append_printf (contents, "%s/%u via %s metric %u%s%s",
fc9aca
-			                                  nm_ip_route_get_dest (route),
fc9aca
-			                                  nm_ip_route_get_prefix (route),
fc9aca
-			                                  nm_ip_route_get_next_hop (route),
fc9aca
-			                                  (unsigned) nm_ip_route_get_metric (route),
fc9aca
-			                                  options ? " " : "",
fc9aca
-			                                  options ?: "");
fc9aca
+		g_string_append_printf (contents, "%s/%u",
fc9aca
+		                        nm_ip_route_get_dest (route),
fc9aca
+		                        nm_ip_route_get_prefix (route));
fc9aca
+		if (next_hop)
fc9aca
+			g_string_append_printf (contents, " via %s", next_hop);
fc9aca
+		if (metric >= 0)
fc9aca
+			g_string_append_printf (contents, " metric %u", (guint) metric);
fc9aca
+		if (options) {
fc9aca
+			g_string_append_c (contents, ' ');
fc9aca
+			g_string_append (contents, options);
fc9aca
 		}
fc9aca
-		g_string_append (contents, "\n");
fc9aca
+
fc9aca
+		g_string_append_c (contents, '\n');
fc9aca
 	}
fc9aca
 
fc9aca
-	if (!g_file_set_contents (filename, contents->str, -1, NULL)) {
fc9aca
+	if (!g_file_set_contents (filename, contents->str, contents->len, NULL)) {
fc9aca
 		g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
fc9aca
 		             "Writing route6 file '%s' failed", filename);
fc9aca
 		return FALSE;
fc9aca
-- 
fc9aca
2.9.3
fc9aca