Skip to content
Myr

Make broken symlink depth robust under absolute paths

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The depth for a broken symlink was derived by stripping the search root from its path and counting the remaining components. Compare the path and the roots in absolute form before stripping, so the match holds whether or not --absolute-path has already made the roots absolute, and pick the deepest matching root. This avoids an absolute path failing to match a relative root and falling back to an inflated depth. Add a regression test that exercises the broken symlink case together with --absolute-path.

authored by hexbinoct · committed by hexbinoct · parent 350b33a · Browse files · 2 files+65 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+65 added−11 removed

  • tests:+40 added−0 removed
  • other:+25 added−11 removed

2 files+65 added−11 removed

modified:src/walk.rs+25 added-11 removed
656 unmodified lines
657/// Compute the depth of `path` relative to the search root it was found under.658///659/// `ignore::DirEntry::depth()` provides this for normal entries, but broken660/// symlinks are surfaced as errors that carry no depth, so we derive it from the661/// path components instead (see issue #1017). The matching root is the longest662/// search root (by component count) that `path` starts with; if none matches we663/// fall back to the full component count, which keeps the entry visible rather664/// than silently dropping it.661/// path instead (see issue #1017). The walker reports every entry's path662/// prefixed by the search root it was found under, so we strip the matching root663/// and count the remaining components. Both the path and the roots are put into664/// absolute form first, so the comparison is correct regardless of whether665/// `--absolute-path` has already made the roots absolute (otherwise an absolute666/// path would fail to match a relative root and the depth would be wrong). When667/// more than one root is a prefix, the deepest (most specific) one wins. If none668/// matches, which should not happen, we fall back to the full component count,669/// keeping the entry visible rather than silently dropping it.670fn depth_relative_to_roots(path: &Path, roots: &[PathBuf]) -> usize {666 let components = path.components().count();667 let root_components = roots671 // `Path::join` ignores the base when its argument is already absolute, so672 // this leaves absolute inputs untouched and anchors relative ones at the cwd.673 let cwd = std::env::current_dir().ok();674 let absolute = |p: &Path| -> PathBuf {675 match &cwd {676 Some(cwd) => cwd.join(p),677 None => p.to_path_buf(),678 }679 };680681 let absolute_path = absolute(path);682 roots683 .iter()669 .filter(|root| path.starts_with(root))670 .map(|root| root.components().count())671 .max()672 .unwrap_or(0);673 components.saturating_sub(root_components)684 .filter_map(|root| absolute_path.strip_prefix(absolute(root)).ok())685 .map(|relative| relative.components().count())686 .min()687 .unwrap_or_else(|| path.components().count())688}689690fn search_str_for_entry<'a>(691 entry_path: &'a std::path::Path,
87 unmodified lines
modified:tests/tests.rs+40 added
1243 unmodified lines
1244 "",1245 );1246}12471248/// Minimum depth with a broken symlink combined with --absolute-path (#1017)1249///1250/// With --absolute-path the search root is made absolute before walking, so the1251/// broken symlink's depth must still be computed relative to that root rather1252/// than from the absolute path's full component count.1253#[test]1254fn test_min_depth_broken_symlink_absolute_path() {1255 let (mut te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1256 te.create_broken_symlink("one/two/broken_symlink")1257 .expect("Failed to create broken symlink.");12581259 // The broken symlink sits at depth 3 relative to the (absolute) root.1260 te.assert_output(1261 &[1262 "--follow",1263 "--absolute-path",1264 "--type",1265 "symlink",1266 "--min-depth",1267 "3",1268 "broken_symlink",1269 ],1270 &format!("{abs_path}/one/two/broken_symlink"),1271 );12721273 // A --min-depth beyond its actual depth must exclude it.1274 te.assert_output(1275 &[1276 "--follow",1277 "--absolute-path",1278 "--type",1279 "symlink",1280 "--min-depth",1281 "4",1282 "broken_symlink",1283 ],1284 "",1285 );1286}12871288/// Exact depth (--exact-depth)1289#[test]1290fn test_exact_depth() {1291 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
1664 unmodified lines