| 1175 | } |
| 1176 | |
| 1177 | static void walk_dir_process_entry(cbm_dirent_t *entry, const walk_frame_t *frame, |
| 1178 | const cbm_discover_opts_t *opts, |
| 1179 | const cbm_gitignore_t *global_gi, |
| 1180 | const cbm_gitignore_t *cbmignore, walk_stack_t *ws, |
| 1181 | file_list_t *out) { |
| 1182 | char abs_path[CBM_SZ_4K]; |
| 1183 | char rel_path[CBM_SZ_4K]; |
| 1184 | int absolute_length = snprintf(abs_path, sizeof(abs_path), "%s/%s", frame->dir, entry->name); |
| 1185 | int relative_length; |
| 1186 | if (frame->prefix[0] != '\0') { |
| 1187 | relative_length = snprintf(rel_path, sizeof(rel_path), "%s/%s", frame->prefix, entry->name); |
| 1188 | } else { |
| 1189 | relative_length = snprintf(rel_path, sizeof(rel_path), "%s", entry->name); |
| 1190 | } |
| 1191 | if (absolute_length <= 0 || (size_t)absolute_length >= sizeof(abs_path) || |
| 1192 | relative_length <= 0 || (size_t)relative_length >= sizeof(rel_path)) { |
| 1193 | out->failed = true; |
| 1194 | return; |
| 1195 | } |
| 1196 | |
| 1197 | struct stat st; |
| 1198 | bool is_symlink = false; |
| 1199 | if (safe_stat(abs_path, &st, &is_symlink) != 0) { |
| 1200 | if (out->count_only) { |
| 1201 | out->failed = true; |
| 1202 | } else if (is_symlink) { |
| 1203 | /* Deliberately not indexed (#963): record so callers can |
| 1204 | * surface it, matching the directory-exclusion and file |
| 1205 | * skip_reason paths a few lines below. */ |
| 1206 | file_list_add_ignored(out, rel_path, "symlink"); |
| 1207 | } |
| 1208 | return; |
| 1209 | } |
| 1210 | |
| 1211 | if (S_ISDIR(st.st_mode)) { |
| 1212 | dir_verdict_t verdict = dir_is_cache_tree(abs_path) |
| 1213 | ? DIR_SKIP |
| 1214 | : directory_verdict(entry->name, rel_path, opts, |
| 1215 | frame->ignore_chain, global_gi, cbmignore); |
| 1216 | if (verdict == DIR_KEEP && frame->git_ignored) { |
| 1217 | verdict = DIR_GIT_IGNORED; /* below a git-ignored directory, as in git */ |
| 1218 | } |
| 1219 | if (verdict == DIR_KEEP || |
| 1220 | (verdict == DIR_GIT_IGNORED && tracked_has_dir(out->tracked, rel_path))) { |
| 1221 | walk_push_subdir(ws, abs_path, rel_path, frame, verdict == DIR_GIT_IGNORED, out); |
| 1222 | } else { |
| 1223 | /* Record the excluded subtree root so callers can report it (#411). */ |
| 1224 | file_list_add_excluded(out, rel_path); |
| 1225 | } |
| 1226 | } else if (S_ISREG(st.st_mode)) { |
| 1227 | walk_dir_process_file(abs_path, rel_path, entry->name, opts, frame->ignore_chain, global_gi, |
| 1228 | cbmignore, &frame->sphinx, frame->git_ignored, &st, out); |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | static bool walk_owned_gitignore_append(cbm_gitignore_t ***owned, size_t *count, size_t *capacity, |
| 1233 | cbm_gitignore_t *gitignore) { |
no test coverage detected