From 6dc10043338bbb29ffd7f8fc431f37b0fed08ae6 Mon Sep 17 00:00:00 2001 From: Victoria Dye Date: Mon, 9 Oct 2023 21:58:54 +0000 Subject: 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 Signed-off-by: Junio C Hamano --- dir.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'dir.c') diff --git a/dir.c b/dir.c index 8486e4d56f..5e01af3a25 100644 --- 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) { -- cgit v1.2.3