file_exists currently only does an lstat to see if anything is at the
path. For some purposes it would be better to ensure that a path
is a certain type of file. Add function directory_exists that checks if
a path exists and is a directory.

Signed-off-by: Devin Lehmacher <lehma...@gmail.com>
---
 dir.c | 7 +++++++
 dir.h | 1 +
 2 files changed, 8 insertions(+)

diff --git a/dir.c b/dir.c
index aeeb5ce10..f6c23ecd2 100644
--- a/dir.c
+++ b/dir.c
@@ -2055,6 +2055,13 @@ int file_exists(const char *f)
        return lstat(f, &sb) == 0;
 }
 
+int directory_exists(const char *path)
+{
+       struct stat sb;
+       int ret = lstat(path, &sb);
+       return ret == 0 && S_ISDIR(sb.st_mode);
+}
+
 static int cmp_icase(char a, char b)
 {
        if (a == b)
diff --git a/dir.h b/dir.h
index bf23a470a..9f8278795 100644
--- a/dir.h
+++ b/dir.h
@@ -247,6 +247,7 @@ extern void add_exclude(const char *string, const char 
*base,
 extern void clear_exclude_list(struct exclude_list *el);
 extern void clear_directory(struct dir_struct *dir);
 extern int file_exists(const char *);
+extern int directory_exists(const char *);
 
 extern int is_inside_dir(const char *dir);
 extern int dir_inside_of(const char *subdir, const char *dir);
-- 
2.11.0

Reply via email to