dir.[ch]: expose 'get_dtype'

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>

Victoria Dye committed Oct 9, 2023 at 21:58 UTC 6dc10043338bbb29ffd7f8fc431f37b0fed08ae6
3 files changed +39 -36
diagnose.c
-36
@@ -71,42 +71,6 @@ static int dir_file_stats(struct object_directory *object_dir, void *data)
71 return 0;
72 }
73
74 -/*
75 - * Get the d_type of a dirent. If the d_type is unknown, derive it from
76 - * stat.st_mode.
77 - *
78 - * Note that 'path' is assumed to have a trailing slash. It is also modified
79 - * in-place during the execution of the function, but is then reverted to its
80 - * original value before returning.
81 - */
82 -static unsigned char get_dtype(struct dirent *e, struct strbuf *path)
83 -{
84 - struct stat st;
85 - unsigned char dtype = DTYPE(e);
86 - size_t base_path_len;
87 -
88 - if (dtype != DT_UNKNOWN)
89 - return dtype;
90 -
91 - /* d_type unknown in dirent, try to fall back on lstat results */
92 - base_path_len = path->len;
93 - strbuf_addstr(path, e->d_name);
94 - if (lstat(path->buf, &st))
95 - goto cleanup;
96 -
97 - /* determine d_type from st_mode */
98 - if (S_ISREG(st.st_mode))
99 - dtype = DT_REG;
100 - else if (S_ISDIR(st.st_mode))
101 - dtype = DT_DIR;
102 - else if (S_ISLNK(st.st_mode))
103 - dtype = DT_LNK;
104 -
105 -cleanup:
106 - strbuf_setlen(path, base_path_len);
107 - return dtype;
108 -}
109 -
74 static int count_files(struct strbuf *path)
75 {
76 DIR *dir = opendir(path->buf);
dir.c
+28
@@ -2235,6 +2235,34 @@ static int get_index_dtype(struct index_state *istate,
2235 return DT_UNKNOWN;
2236 }
2237
2238 +unsigned char get_dtype(struct dirent *e, struct strbuf *path)
2239 +{
2240 + struct stat st;
2241 + unsigned char dtype = DTYPE(e);
2242 + size_t base_path_len;
2243 +
2244 + if (dtype != DT_UNKNOWN)
2245 + return dtype;
2246 +
2247 + /* d_type unknown in dirent, try to fall back on lstat results */
2248 + base_path_len = path->len;
2249 + strbuf_addstr(path, e->d_name);
2250 + if (lstat(path->buf, &st))
2251 + goto cleanup;
2252 +
2253 + /* determine d_type from st_mode */
2254 + if (S_ISREG(st.st_mode))
2255 + dtype = DT_REG;
2256 + else if (S_ISDIR(st.st_mode))
2257 + dtype = DT_DIR;
2258 + else if (S_ISLNK(st.st_mode))
2259 + dtype = DT_LNK;
2260 +
2261 +cleanup:
2262 + strbuf_setlen(path, base_path_len);
2263 + return dtype;
2264 +}
2265 +
2266 static int resolve_dtype(int dtype, struct index_state *istate,
2267 const char *path, int len)
2268 {
dir.h
+11
@@ -363,6 +363,17 @@ struct dir_struct {
363
364 struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp);
365
366 +/*
367 + * Get the d_type of a dirent. If the d_type is unknown, derive it from
368 + * stat.st_mode using the path to the dirent's containing directory (path) and
369 + * the name of the dirent itself.
370 + *
371 + * Note that 'path' is assumed to have a trailing slash. It is also modified
372 + * in-place during the execution of the function, but is then reverted to its
373 + * original value before returning.
374 + */
375 +unsigned char get_dtype(struct dirent *e, struct strbuf *path);
376 +
377 /*Count the number of slashes for string s*/
378 int count_slashes(const char *s);
379