|
13 | 13 |
|
14 | 14 | #include "foundation/constants.h" |
15 | 15 | #include "foundation/compat_fs.h" |
| 16 | +#include "foundation/workspace.h" |
16 | 17 | #include "foundation/platform.h" |
17 | 18 | #ifdef _WIN32 |
18 | 19 | #include "foundation/win_utf8.h" |
@@ -562,6 +563,30 @@ static bool is_safety_core_dir(const char *name) { |
562 | 563 | } |
563 | 564 |
|
564 | 565 | /* Check if a directory entry should be skipped (hardcoded dirs + gitignore). */ |
| 566 | +/* The cache directory holds every indexed project's graph database. When a custom |
| 567 | + * CBM_CACHE_DIR sits inside a repository — which happens in tests and is legal in |
| 568 | + * production — walking into it would pull other projects' databases into this |
| 569 | + * project's file list. Prune it by absolute path. |
| 570 | + * |
| 571 | + * This is the narrow remedy for a concern that was briefly implemented as |
| 572 | + * refusing any root containing the cache: refusing a whole root was too blunt, |
| 573 | + * and not walking the cache is what the concern actually asks for. */ |
| 574 | +static bool dir_is_cache_tree(const char *abs_path) { |
| 575 | + const char *cache = cbm_workspace_cache_dir(); |
| 576 | + if (!cache || !cache[0] || !abs_path || !abs_path[0]) { |
| 577 | + return false; |
| 578 | + } |
| 579 | + size_t n = strlen(cache); |
| 580 | + while (n > 1 && (cache[n - 1] == '/' || cache[n - 1] == '\\')) { |
| 581 | + n--; |
| 582 | + } |
| 583 | + if (strncmp(abs_path, cache, n) != 0) { |
| 584 | + return false; |
| 585 | + } |
| 586 | + /* Boundary-aware so "<cache>x" is not treated as living under "<cache>". */ |
| 587 | + return abs_path[n] == '\0' || abs_path[n] == '/' || abs_path[n] == '\\'; |
| 588 | +} |
| 589 | + |
565 | 590 | static bool should_skip_directory(const char *entry_name, const char *rel_path, |
566 | 591 | const cbm_discover_opts_t *opts, const cbm_gitignore_t *gitignore, |
567 | 592 | const cbm_gitignore_t *global_gi, |
@@ -853,7 +878,8 @@ static void walk_dir_process_entry(cbm_dirent_t *entry, const walk_frame_t *fram |
853 | 878 | } |
854 | 879 |
|
855 | 880 | if (S_ISDIR(st.st_mode)) { |
856 | | - if (!should_skip_directory(entry->name, rel_path, opts, gitignore, global_gi, cbmignore, |
| 881 | + if (!dir_is_cache_tree(abs_path) && |
| 882 | + !should_skip_directory(entry->name, rel_path, opts, gitignore, global_gi, cbmignore, |
857 | 883 | frame->local_gi, frame->local_gi_prefix)) { |
858 | 884 | walk_push_subdir(ws, abs_path, rel_path, frame, out); |
859 | 885 | } else { |
|
0 commit comments