util_path: Use S_IS*() macros to test type of file

Testing whether a file is a directory or a regular file by masking the
struct stat field st_mode with S_IFDIR or S_IFREG is wrong. Depending
on the values of the macros S_IF* sockets and symbolic links might
erroneously be considered as regular files and block special files as
directories.

The file type encoded in the struct stat field st_mode is actually an
enumeration. To test whether a file is a directory or a regular file
either extract the file type from st_mode using the mask S_IFMT and
compare it against S_IFDIR or S_IFREG or simply use the macros S_ISDIR()
and S_ISREG().

Fixes: b627b8d8e1 ("Initial s390-tools-2.0.0 import")
Signed-off-by: Jens Remus <jremus@linux.ibm.com>
Reviewed-by: Jan Hoeppner <hoeppner@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Jens Remus
2019-06-06 17:03:12 +02:00
committed by Jan Höppner
parent 61baa23e48
commit 1e248abd53

View File

@@ -160,7 +160,7 @@ bool util_path_is_reg_file(const char *fmt, ...)
rc = false;
goto free_str;
}
rc = (sb.st_mode & S_IFREG) ? true : false;
rc = S_ISREG(sb.st_mode) ? true : false;
free_str:
free(path);
return rc;
@@ -189,7 +189,7 @@ bool util_path_is_dir(const char *fmt, ...)
rc = false;
goto free_str;
}
rc = (sb.st_mode & S_IFDIR) ? true : false;
rc = S_ISDIR(sb.st_mode) ? true : false;
free_str:
free(path);
return rc;