summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2020-01-10 16:58:23 +0100
committerThomas Haller <thaller@redhat.com>2020-01-16 12:40:01 +0100
commit9f35d9e49e39505b58b5c90d4239d36999c3c944 (patch)
treee3ef1fe231ef1b41e51be2cd97e44f08c44a4eb6
parentda4a3dd24ca79b9cac83790416f02b5c0ae78b2d (diff)
downloadNetworkManager-9f35d9e49e39505b58b5c90d4239d36999c3c944.tar.gz
ifcfg-rh: split reading file and parsing in utils_has_route_file_new_syntax() function
We will need both variants, so that the caller can read the file only once. Note that also utils_has_route_file_new_syntax_content() will restore the original contents and not modify the input (from point of view of the caller). In practice it will momentarily modify the content.
-rw-r--r--src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c21
-rw-r--r--src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.h2
2 files changed, 19 insertions, 4 deletions
diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c
index 843d2159a2..2bc32e052e 100644
--- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c
+++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c
@@ -261,7 +261,6 @@ gboolean
utils_has_route_file_new_syntax (const char *filename)
{
gs_free char *contents_data = NULL;
- const char *contents;
gsize len;
g_return_val_if_fail (filename != NULL, TRUE);
@@ -269,14 +268,20 @@ utils_has_route_file_new_syntax (const char *filename)
if (!g_file_get_contents (filename, &contents_data, &len, NULL))
return TRUE;
+ return utils_has_route_file_new_syntax_content (contents_data, len);
+}
+
+gboolean
+utils_has_route_file_new_syntax_content (const char *contents,
+ gsize len)
+{
if (len <= 0)
return TRUE;
- contents = contents_data;
-
while (TRUE) {
const char *line = contents;
char *eol;
+ gboolean found = FALSE;
/* matches regex "^[[:space:]]*ADDRESS[0-9]+=" */
@@ -294,10 +299,18 @@ utils_has_route_file_new_syntax (const char *filename)
/* pass */
}
if (line[0] == '=')
- return TRUE;
+ found = TRUE;
}
}
+ if (eol) {
+ /* restore the line ending. We don't want to mangle the content from
+ * POV of the caller. */
+ eol[0] = '\n';
+ }
+
+ if (found)
+ return TRUE;
if (!eol)
return FALSE;
}
diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.h b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.h
index 29c9df7af7..4aed0dff51 100644
--- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.h
+++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.h
@@ -76,6 +76,8 @@ shvarFile *utils_get_keys_ifcfg (const char *parent, gboolean should_create);
shvarFile *utils_get_route_ifcfg (const char *parent, gboolean should_create);
gboolean utils_has_route_file_new_syntax (const char *filename);
+gboolean utils_has_route_file_new_syntax_content (const char *contents,
+ gsize len);
gboolean utils_has_complex_routes (const char *filename, int addr_family);
gboolean utils_is_ifcfg_alias_file (const char *alias, const char *ifcfg);