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.
This commit is contained in:
Thomas Haller 2020-01-10 16:58:23 +01:00
parent da4a3dd24c
commit 9f35d9e49e
2 changed files with 19 additions and 4 deletions

View file

@ -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;
}

View file

@ -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);