diff options
author | Thomas Haller <thaller@redhat.com> | 2020-01-10 16:58:23 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2020-01-10 17:47:17 +0100 |
commit | 48c3a2a88a48c53b96f577eaafed8c17e8ecbba8 (patch) | |
tree | aa7c8c4859121adecc8156c521e811dcedd0b2fc | |
parent | 1fdd075e567e104c8aceac3a99fffc19bd6193a7 (diff) | |
download | NetworkManager-48c3a2a88a48c53b96f577eaafed8c17e8ecbba8.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.c | 21 | ||||
-rw-r--r-- | src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.h | 2 |
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 62a326ff99..862dd91851 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 734a6a63c2..a78fdeda14 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); |