ifcfg-rh: cleanup utils_detect_ifcfg_path()

- avoid cloing the basename. Determining the basename can be done conveniently
  with strrchr().
- use cleanup macro for temporary variable.
- while in practice it should not happen, check that the colon in the name
  of alias file names is not followed by another '/'.
This commit is contained in:
Thomas Haller 2019-06-21 12:35:52 +02:00
parent a4642c78f7
commit e5b21344c5

View file

@ -316,36 +316,41 @@ utils_is_ifcfg_alias_file (const char *alias, const char *ifcfg)
char *
utils_detect_ifcfg_path (const char *path, gboolean only_ifcfg)
{
gs_free char *base = NULL;
char *ptr, *ifcfg = NULL;
const char *base;
g_return_val_if_fail (path != NULL, NULL);
if (utils_should_ignore_file (path, only_ifcfg))
return NULL;
base = g_path_get_basename (path);
base = strrchr (path, '/');
if (!base)
base = path;
else
base += 1;
if (strncmp (base, IFCFG_TAG, NM_STRLEN (IFCFG_TAG)) == 0) {
if (NM_STR_HAS_PREFIX (base, IFCFG_TAG)) {
if (base[NM_STRLEN (IFCFG_TAG)] == '\0')
return NULL;
if (utils_is_ifcfg_alias_file (base, NULL)) {
gs_free char *ifcfg = NULL;
char *ptr;
ifcfg = g_strdup (path);
ptr = strrchr (ifcfg, ':');
if (ptr && ptr > ifcfg) {
if ( ptr
&& ptr > ifcfg
&& !strchr (ptr, '/')) {
*ptr = '\0';
if (g_file_test (ifcfg, G_FILE_TEST_EXISTS)) {
/* the file has a colon, so it is probably an alias.
* To be ~more~ certain that this is an alias file,
* check whether a corresponding base file exists. */
if (only_ifcfg) {
g_free (ifcfg);
if (only_ifcfg)
return NULL;
}
return ifcfg;
return g_steal_pointer (&ifcfg);
}
}
g_free (ifcfg);
}
return g_strdup (path);
}