| 2333 | } |
| 2334 | |
| 2335 | unsigned char get_dtype(struct dirent *e, struct strbuf *path, |
| 2336 | int follow_symlink) |
| 2337 | { |
| 2338 | struct stat st; |
| 2339 | unsigned char dtype = DTYPE(e); |
| 2340 | size_t base_path_len; |
| 2341 | |
| 2342 | if (dtype != DT_UNKNOWN && !(follow_symlink && dtype == DT_LNK)) |
| 2343 | return dtype; |
| 2344 | |
| 2345 | /* |
| 2346 | * d_type unknown or unfollowed symlink, try to fall back on [l]stat |
| 2347 | * results. If [l]stat fails, explicitly set DT_UNKNOWN. |
| 2348 | */ |
| 2349 | base_path_len = path->len; |
| 2350 | strbuf_addstr(path, e->d_name); |
| 2351 | if ((follow_symlink && stat(path->buf, &st)) || |
| 2352 | (!follow_symlink && lstat(path->buf, &st))) |
| 2353 | goto cleanup; |
| 2354 | |
| 2355 | /* determine d_type from st_mode */ |
| 2356 | if (S_ISREG(st.st_mode)) |
| 2357 | dtype = DT_REG; |
| 2358 | else if (S_ISDIR(st.st_mode)) |
| 2359 | dtype = DT_DIR; |
| 2360 | else if (S_ISLNK(st.st_mode)) |
| 2361 | dtype = DT_LNK; |
| 2362 | |
| 2363 | cleanup: |
| 2364 | strbuf_setlen(path, base_path_len); |
| 2365 | return dtype; |
| 2366 | } |
| 2367 | |
| 2368 | static int resolve_dtype(int dtype, struct index_state *istate, |
| 2369 | const char *path, int len) |
no test coverage detected