]> git.ipfire.org Git - thirdparty/git.git/commitdiff
dir.[ch]: expose 'get_dtype'
authorVictoria Dye <vdye@github.com>
Mon, 9 Oct 2023 21:58:54 +0000 (21:58 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 9 Oct 2023 22:53:13 +0000 (15:53 -0700)
Move 'get_dtype()' from 'diagnose.c' to 'dir.c' and add its declaration to
'dir.h' so that it is accessible to callers in other files. The function and
its documentation are moved verbatim except for a small addition to the
description clarifying what the 'path' arg represents.

Signed-off-by: Victoria Dye <vdye@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diagnose.c
dir.c
dir.h

index 8430064000bcba96506e4b9c99c9efe60ba072f4..fc4d344bd63babb782b7fef91cffb60579109001 100644 (file)
@@ -71,42 +71,6 @@ static int dir_file_stats(struct object_directory *object_dir, void *data)
        return 0;
 }
 
-/*
- * Get the d_type of a dirent. If the d_type is unknown, derive it from
- * stat.st_mode.
- *
- * Note that 'path' is assumed to have a trailing slash. It is also modified
- * in-place during the execution of the function, but is then reverted to its
- * original value before returning.
- */
-static unsigned char get_dtype(struct dirent *e, struct strbuf *path)
-{
-       struct stat st;
-       unsigned char dtype = DTYPE(e);
-       size_t base_path_len;
-
-       if (dtype != DT_UNKNOWN)
-               return dtype;
-
-       /* d_type unknown in dirent, try to fall back on lstat results */
-       base_path_len = path->len;
-       strbuf_addstr(path, e->d_name);
-       if (lstat(path->buf, &st))
-               goto cleanup;
-
-       /* determine d_type from st_mode */
-       if (S_ISREG(st.st_mode))
-               dtype = DT_REG;
-       else if (S_ISDIR(st.st_mode))
-               dtype = DT_DIR;
-       else if (S_ISLNK(st.st_mode))
-               dtype = DT_LNK;
-
-cleanup:
-       strbuf_setlen(path, base_path_len);
-       return dtype;
-}
-
 static int count_files(struct strbuf *path)
 {
        DIR *dir = opendir(path->buf);
diff --git a/dir.c b/dir.c
index 8486e4d56ff50cafd14aa3ef00ee89c069d87269..5e01af3a25ee2cca5b0abd59b55ea7a23a7a0814 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -2235,6 +2235,34 @@ static int get_index_dtype(struct index_state *istate,
        return DT_UNKNOWN;
 }
 
+unsigned char get_dtype(struct dirent *e, struct strbuf *path)
+{
+       struct stat st;
+       unsigned char dtype = DTYPE(e);
+       size_t base_path_len;
+
+       if (dtype != DT_UNKNOWN)
+               return dtype;
+
+       /* d_type unknown in dirent, try to fall back on lstat results */
+       base_path_len = path->len;
+       strbuf_addstr(path, e->d_name);
+       if (lstat(path->buf, &st))
+               goto cleanup;
+
+       /* determine d_type from st_mode */
+       if (S_ISREG(st.st_mode))
+               dtype = DT_REG;
+       else if (S_ISDIR(st.st_mode))
+               dtype = DT_DIR;
+       else if (S_ISLNK(st.st_mode))
+               dtype = DT_LNK;
+
+cleanup:
+       strbuf_setlen(path, base_path_len);
+       return dtype;
+}
+
 static int resolve_dtype(int dtype, struct index_state *istate,
                         const char *path, int len)
 {
diff --git a/dir.h b/dir.h
index ad06682fd54b3e7ae47d6f8a8d80048da0324f01..28c630ce8065986fb8104feb5428b7afe2b6196d 100644 (file)
--- a/dir.h
+++ b/dir.h
@@ -363,6 +363,17 @@ struct dir_struct {
 
 struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp);
 
+/*
+ * Get the d_type of a dirent. If the d_type is unknown, derive it from
+ * stat.st_mode using the path to the dirent's containing directory (path) and
+ * the name of the dirent itself.
+ *
+ * Note that 'path' is assumed to have a trailing slash. It is also modified
+ * in-place during the execution of the function, but is then reverted to its
+ * original value before returning.
+ */
+unsigned char get_dtype(struct dirent *e, struct strbuf *path);
+
 /*Count the number of slashes for string s*/
 int count_slashes(const char *s);