summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
authorMateusz Grzonka <mateusz.grzonka@intel.com>2023-02-02 12:27:02 +0100
committerJes Sorensen <jes@trained-monkey.org>2023-03-02 16:51:02 +0100
commitee9dcf9549e8cbfeb51123812776cc87016c95b0 (patch)
treeda4b8d39e2a4b197c556bbaaf3ee7fe490275f38 /util.c
parentMdmonitor: Add helper functions (diff)
downloadmdadm-ee9dcf9549e8cbfeb51123812776cc87016c95b0.tar.xz
mdadm-ee9dcf9549e8cbfeb51123812776cc87016c95b0.zip
Add helpers to determine whether directories or files are soft links
Signed-off-by: Mateusz Grzonka <mateusz.grzonka@intel.com> Acked-by: Coly Li <colyli@suse.de> Signed-off-by: Jes Sorensen <jes@trained-monkey.org>
Diffstat (limited to 'util.c')
-rw-r--r--util.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/util.c b/util.c
index 8c7f3fd5..7fc881bf 100644
--- a/util.c
+++ b/util.c
@@ -2401,3 +2401,48 @@ void sleep_for(unsigned int sec, long nsec, bool wake_after_interrupt)
}
} while (!wake_after_interrupt && errno == EINTR);
}
+
+/* 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;
+}