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 <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed Oct 9, 2023 at 21:58 UTC aa79636fe70247a19648d73e52a7774536100567
3 files changed +18 -8
diagnose.c
+3 -3
@@ -81,7 +81,7 @@ static int count_files(struct strbuf *path)
81 return 0;
82
83 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)
85 count++;
86
87 closedir(dir);
@@ -110,7 +110,7 @@ static void loose_objs_stats(struct strbuf *buf, const char *path)
110 base_path_len = count_path.len;
111
112 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 &&
114 strlen(e->d_name) == 2 &&
115 !hex_to_bytes(&c, e->d_name, 1)) {
116 strbuf_setlen(&count_path, base_path_len);
@@ -155,7 +155,7 @@ static int add_directory_to_archiver(struct strvec *archiver_args,
155
156 strbuf_add_absolute_path(&abspath, at_root ? "." : path);
157 strbuf_addch(&abspath, '/');
158 - dtype = get_dtype(e, &abspath);
158 + dtype = get_dtype(e, &abspath, 0);
159
160 strbuf_setlen(&buf, len);
161 strbuf_addstr(&buf, e->d_name);
dir.c
+9 -4
@@ -2235,19 +2235,24 @@ 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)
2238 +unsigned char get_dtype(struct dirent *e, struct strbuf *path,
2239 + int follow_symlink)
2240 {
2241 struct stat st;
2242 unsigned char dtype = DTYPE(e);
2243 size_t base_path_len;
2244
2244 - if (dtype != DT_UNKNOWN)
2245 + if (dtype != DT_UNKNOWN && !(follow_symlink && dtype == DT_LNK))
2246 return dtype;
2247
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 + */
2252 base_path_len = path->len;
2253 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)))
2256 goto cleanup;
2257
2258 /* determine d_type from st_mode */
dir.h
+6 -1
@@ -368,11 +368,16 @@ struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp);
368 * stat.st_mode using the path to the dirent's containing directory (path) and
369 * the name of the dirent itself.
370 *
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 + *
375 * Note that 'path' is assumed to have a trailing slash. It is also modified
376 * in-place during the execution of the function, but is then reverted to its
377 * original value before returning.
378 */
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);
381
382 /*Count the number of slashes for string s*/
383 int count_slashes(const char *s);