diff mbox series

[5/9] Add helpers to determine whether directories or files are soft links

Message ID 20220907125657.12192-6-mateusz.grzonka@intel.com (mailing list archive)
State Superseded, archived
Delegated to: Coly Li
Headers show
Series Mdmonitor refactor and udev event handling improvements | expand

Commit Message

Mateusz Grzonka Sept. 7, 2022, 12:56 p.m. UTC
Signed-off-by: Mateusz Grzonka <mateusz.grzonka@intel.com>
---
 mdadm.h |  3 +++
 util.c  | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

Comments

Coly Li Oct. 28, 2022, 3:46 p.m. UTC | #1
On 9/7/22 8:56 PM, Mateusz Grzonka wrote:
> Signed-off-by: Mateusz Grzonka<mateusz.grzonka@intel.com>

Except for the comment style, the rested part is good to me.


Acked-by: Coly Li <colyli@suse.de>

Thanks.


Coly Li



> ---
>   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;
> +}
diff mbox series

Patch

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