Skip to content

Commit aa79636

Browse files
vdyegitster
authored andcommitted
dir.[ch]: add 'follow_symlink' arg to 'get_dtype'
Add a 'follow_symlink' boolean option to 'get_type()'. If 'follow_symlink' is enabled, DT_LNK (in addition to DT_UNKNOWN) d_types triggers the stat-based d_type resolution, using 'stat' instead of 'lstat' to get the type of the followed symlink. Note that symlinks are not followed recursively, so a symlink pointing to another symlink will still resolve to DT_LNK. Update callers in 'diagnose.c' to specify 'follow_symlink = 0' to preserve current behavior. Signed-off-by: Victoria Dye <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6dc1004 commit aa79636

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

diagnose.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static int count_files(struct strbuf *path)
8181
return 0;
8282

8383
while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
84-
if (get_dtype(e, path) == DT_REG)
84+
if (get_dtype(e, path, 0) == DT_REG)
8585
count++;
8686

8787
closedir(dir);
@@ -110,7 +110,7 @@ static void loose_objs_stats(struct strbuf *buf, const char *path)
110110
base_path_len = count_path.len;
111111

112112
while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
113-
if (get_dtype(e, &count_path) == DT_DIR &&
113+
if (get_dtype(e, &count_path, 0) == DT_DIR &&
114114
strlen(e->d_name) == 2 &&
115115
!hex_to_bytes(&c, e->d_name, 1)) {
116116
strbuf_setlen(&count_path, base_path_len);
@@ -155,7 +155,7 @@ static int add_directory_to_archiver(struct strvec *archiver_args,
155155

156156
strbuf_add_absolute_path(&abspath, at_root ? "." : path);
157157
strbuf_addch(&abspath, '/');
158-
dtype = get_dtype(e, &abspath);
158+
dtype = get_dtype(e, &abspath, 0);
159159

160160
strbuf_setlen(&buf, len);
161161
strbuf_addstr(&buf, e->d_name);

dir.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,19 +2235,24 @@ 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)
2238+
unsigned char get_dtype(struct dirent *e, struct strbuf *path,
2239+
int follow_symlink)
22392240
{
22402241
struct stat st;
22412242
unsigned char dtype = DTYPE(e);
22422243
size_t base_path_len;
22432244

2244-
if (dtype != DT_UNKNOWN)
2245+
if (dtype != DT_UNKNOWN && !(follow_symlink && dtype == DT_LNK))
22452246
return dtype;
22462247

2247-
/* d_type unknown in dirent, try to fall back on lstat results */
2248+
/*
2249+
* d_type unknown or unfollowed symlink, try to fall back on [l]stat
2250+
* results. If [l]stat fails, explicitly set DT_UNKNOWN.
2251+
*/
22482252
base_path_len = path->len;
22492253
strbuf_addstr(path, e->d_name);
2250-
if (lstat(path->buf, &st))
2254+
if ((follow_symlink && stat(path->buf, &st)) ||
2255+
(!follow_symlink && lstat(path->buf, &st)))
22512256
goto cleanup;
22522257

22532258
/* determine d_type from st_mode */

dir.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,16 @@ struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp);
368368
* stat.st_mode using the path to the dirent's containing directory (path) and
369369
* the name of the dirent itself.
370370
*
371+
* If 'follow_symlink' is 1, this function will attempt to follow DT_LNK types
372+
* using 'stat'. Links are *not* followed recursively, so a symlink pointing
373+
* to another symlink will still resolve to 'DT_LNK'.
374+
*
371375
* Note that 'path' is assumed to have a trailing slash. It is also modified
372376
* in-place during the execution of the function, but is then reverted to its
373377
* original value before returning.
374378
*/
375-
unsigned char get_dtype(struct dirent *e, struct strbuf *path);
379+
unsigned char get_dtype(struct dirent *e, struct strbuf *path,
380+
int follow_symlink);
376381

377382
/*Count the number of slashes for string s*/
378383
int count_slashes(const char *s);

0 commit comments

Comments
 (0)