[PATCH 5/9] Add helpers to determine whether directories or files are soft links
From: Mateusz Grzonka <hidden>
Date: 2022-09-07 13:14:50
Subsystem:
the rest · Maintainer:
Linus Torvalds
Signed-off-by: Mateusz Grzonka <redacted> --- mdadm.h | 3 +++ util.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+)
diff --git a/mdadm.h b/mdadm.h
index 09915a00..9183ee70 100644
--- a/mdadm.h
+++ b/mdadm.h@@ -1707,6 +1707,9 @@ extern int cluster_get_dlmlock(void); extern int cluster_release_dlmlock(void); extern void set_dlm_hooks(void); +extern bool is_directory(const char *path); +extern bool is_file(const char *path); + #define _ROUND_UP(val, base) (((val) + (base) - 1) & ~(base - 1)) #define ROUND_UP(val, base) _ROUND_UP(val, (typeof(val))(base)) #define ROUND_UP_PTR(ptr, base) ((typeof(ptr)) \
diff --git a/util.c b/util.c
index cc94f96e..97926f19 100644
--- a/util.c
+++ b/util.c@@ -2375,3 +2375,49 @@ out: close(fd_zero); return ret; } + +/** + * is_directory() - Checks if directory provided by path is indeed a regular directory. + * @path: directory path to be checked + * + * Doesn't accept symlinks. + * + * Return: true if is a directory, false if not + */ +bool is_directory(const char *path) +{ + struct stat st; + + if (lstat(path, &st) != 0) { + pr_err("%s: %s\n", strerror(errno), path); + return false; + } + + if (!S_ISDIR(st.st_mode)) + return false; + + return true; +} + +/** + * is_file() - Checks if file provided by path is indeed a regular file. + * @path: file path to be checked + * + * Doesn't accept symlinks. + * + * Return: true if is a file, false if not + */ +bool is_file(const char *path) +{ + struct stat st; + + if (lstat(path, &st) != 0) { + pr_err("%s: %s\n", strerror(errno), path); + return false; + } + + if (!S_ISREG(st.st_mode)) + return false; + + return true; +}
--
2.26.2