Skip to content

Commit 6dc1004

Browse files
vdyegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5305474 commit 6dc1004

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

diagnose.c

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -71,42 +71,6 @@ static int dir_file_stats(struct object_directory *object_dir, void *data)
7171
return 0;
7272
}
7373

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-
11074
static int count_files(struct strbuf *path)
11175
{
11276
DIR *dir = opendir(path->buf);

dir.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,6 +2235,34 @@ static int get_index_dtype(struct index_state *istate,
22352235
return DT_UNKNOWN;
22362236
}
22372237

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+
22382266
static int resolve_dtype(int dtype, struct index_state *istate,
22392267
const char *path, int len)
22402268
{

dir.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,17 @@ struct dir_struct {
363363

364364
struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp);
365365

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+
366377
/*Count the number of slashes for string s*/
367378
int count_slashes(const char *s);
368379

0 commit comments

Comments
 (0)