| 277 | } |
| 278 | |
| 279 | cbm_ws_verdict_t cbm_workspace_classify_root(const char *canonical_path, const char *home_dir, |
| 280 | const char *cache_dir) { |
| 281 | if (!canonical_path || !canonical_path[0] || ws_volume_prefix_len(canonical_path) == 0) { |
| 282 | /* A relative or empty path is not a usable root; refuse it the same way |
| 283 | * as a volume root rather than letting it fall through as allowed. */ |
| 284 | return CBM_WS_DENY_ABSOLUTE; |
| 285 | } |
| 286 | |
| 287 | bool windows_style = ws_is_windows_style(canonical_path); |
| 288 | int depth = cbm_workspace_path_depth(canonical_path); |
| 289 | |
| 290 | /* A volume, drive or share root, whatever the platform. */ |
| 291 | if (depth == 0) { |
| 292 | return CBM_WS_DENY_ABSOLUTE; |
| 293 | } |
| 294 | |
| 295 | /* Order matters below, and not for cosmetic reasons. |
| 296 | * |
| 297 | * The home directory normally CONTAINS the cache directory, so testing the |
| 298 | * cache first would report every $HOME as "holds the cache" — and, worse, |
| 299 | * would make it absolutely denied when the design says a person may override |
| 300 | * it. Home is therefore classified first. |
| 301 | * |
| 302 | * Depth comes before the cache test for the same kind of reason: "/Users" is |
| 303 | * both too broad and an ancestor of the cache, and "too broad, name a |
| 304 | * project directory below it" is the reason that actually helps the reader. */ |
| 305 | if (home_dir && home_dir[0] && ws_paths_equal(canonical_path, home_dir)) { |
| 306 | return CBM_WS_DENY_SENSITIVE; |
| 307 | } |
| 308 | |
| 309 | /* Below a drive or a UNC share the first component is already user space |
| 310 | * ("D:/repos", "//srv/share/proj"), so one component is enough there. Below a |
| 311 | * POSIX root it is a system tree, so require two. */ |
| 312 | int min_depth = |
| 313 | (windows_style || ws_is_unc(canonical_path)) ? WS_MIN_DEPTH_WINDOWS : WS_MIN_DEPTH_POSIX; |
| 314 | if (depth < min_depth) { |
| 315 | return CBM_WS_DENY_TOO_SHALLOW; |
| 316 | } |
| 317 | |
| 318 | /* No rule here for "this root contains the cache directory". |
| 319 | * |
| 320 | * An earlier draft refused such roots outright, on the theory that indexing |
| 321 | * them would absorb every other project's graph database. Two things make |
| 322 | * that wrong. The indexer only parses recognised source files, and a graph |
| 323 | * database is binary SQLite it would never extract; and refusing an entire |
| 324 | * root is the wrong remedy even where the concern holds — the right one is to |
| 325 | * not walk the cache. Excluding the cache from discovery is tracked |
| 326 | * separately; classifying the root is not the place for it. |
| 327 | * |
| 328 | * cache_dir stays in the signature because the exclusion work will need it |
| 329 | * and because callers already have it to hand. */ |
| 330 | (void)cache_dir; |
| 331 | |
| 332 | if (ws_any_component_matches(canonical_path, WS_CREDENTIAL_NAMES, |
| 333 | sizeof(WS_CREDENTIAL_NAMES) / sizeof(WS_CREDENTIAL_NAMES[0]), |
| 334 | windows_style)) { |
| 335 | return CBM_WS_DENY_SENSITIVE; |
| 336 | } |
no test coverage detected