diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 42d9d3a9b86..1e637a8f149 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -561,17 +561,18 @@ fn safe_du( }; // Check excludes - for pattern in &options.excludes { - if pattern.matches(&this_stat.path.to_string_lossy()) - || pattern.matches(&entry_name.to_string_lossy()) - { - if options.verbose { - println!( - "{}", - translate!("du-verbose-ignored", "path" => this_stat.path.quote()) - ); + if !options.excludes.is_empty() { + let path_string = this_stat.path.to_string_lossy(); + for pattern in &options.excludes { + if is_excluded(pattern, &path_string) { + if options.verbose { + println!( + "{}", + translate!("du-verbose-ignored", "path" => this_stat.path.quote()) + ); + } + continue 'file_loop; } - continue 'file_loop; } } @@ -721,22 +722,20 @@ fn du_regular( } // We have an exclude list - for pattern in &options.excludes { - // Look at all patterns with both short and long paths - // if we have 'du foo' but search to exclude 'foo/bar' - // we need the full path - if pattern.matches(&this_stat.path.to_string_lossy()) - || pattern.matches(&entry.file_name().into_string().unwrap()) - { - // if the directory is ignored, leave early - if options.verbose { - println!( - "{}", - translate!("du-verbose-ignored", "path" => this_stat.path.quote()) - ); + if !options.excludes.is_empty() { + let path_string = this_stat.path.to_string_lossy(); + for pattern in &options.excludes { + if is_excluded(pattern, &path_string) { + // if the directory is ignored, leave early + if options.verbose { + println!( + "{}", + translate!("du-verbose-ignored", "path" => this_stat.path.quote()) + ); + } + // Go to the next file + continue 'file_loop; } - // Go to the next file - continue 'file_loop; } } @@ -849,6 +848,52 @@ fn file_as_vec(filename: impl AsRef) -> UResult> { Ok(lines) } +/// Whether `pattern` matches `path` or a trailing part of it that starts +/// after a separator, so `target/debug` excludes `./target/debug` and +/// `x/target/debug` but `get/debug` does not. +fn is_excluded(pattern: &Pattern, path: &str) -> bool { + if pattern.matches(path) { + return true; + } + let glob = pattern.as_str(); + // `*` also matches separators, so a leading `*` that matches a trailing + // part matches the whole path too, which was checked above. + if glob.starts_with('*') { + return false; + } + let mut starts = path + .match_indices(std::path::is_separator) + .map(|(i, sep)| i + sep.len()); + // Without wildcards or separators only the last component can match. + if !glob.contains(|c| std::path::is_separator(c) || matches!(c, '*' | '?' | '[')) { + return starts + .next_back() + .is_some_and(|i| pattern.matches(&path[i..])); + } + starts.any(|i| pattern.matches(&path[i..])) +} + +/// Collapse trailing separators to one like GNU du, so the operand `a//` +/// is shown as `a/` and its entries as `a/b`. `//` is kept as GNU does. +fn collapse_trailing_separators(path: PathBuf) -> PathBuf { + let Ok(bytes) = uucore::os_str_as_bytes(path.as_os_str()) else { + return path; + }; + let is_sep = |i: usize| std::path::is_separator(bytes[i] as char); + if bytes.len() == 2 && is_sep(0) && is_sep(1) { + return path; + } + let mut end = bytes.len(); + while end > 1 && is_sep(end - 1) && is_sep(end - 2) { + end -= 1; + } + if end == bytes.len() { + return path; + } + // A prefix of bytes that converted fine above cannot fail to convert back. + PathBuf::from(uucore::os_str_from_bytes(&bytes[..end]).unwrap()) +} + /// Given the `--exclude-from` and/or `--exclude` arguments, returns the globset lists /// to ignore the files fn build_exclude_patterns(matches: &ArgMatches) -> UResult> { @@ -1171,6 +1216,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } else { vec![PathBuf::from(".")] }; + let files: Vec = files + .into_iter() + .map(collapse_trailing_separators) + .collect(); let time = matches.contains_id(options::TIME).then(|| { matches @@ -1253,7 +1302,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { if !&traversal_options.excludes.is_empty() { let path_string = path.to_string_lossy(); for pattern in &traversal_options.excludes { - if pattern.matches(&path_string) { + if is_excluded(pattern, &path_string) { // if the directory is ignored, leave early if traversal_options.verbose { println!( diff --git a/tests/by-util/test_du.rs b/tests/by-util/test_du.rs index 8c0005ca28a..307ad404524 100644 --- a/tests/by-util/test_du.rs +++ b/tests/by-util/test_du.rs @@ -1853,6 +1853,77 @@ fn test_du_exclude_several_components() { assert!(!result.stdout_str().contains("a/b")); } +// A pattern also matches the trailing components of a path, whatever +// the operand puts in front of them, but never part of a component. +#[cfg(not(windows))] +#[test] +fn test_du_exclude_trailing_components() { + let ts = TestScenario::new(util_name!()); + let at = &ts.fixtures; + + at.mkdir_all("a/b/c"); + at.mkdir_all("a/u"); + at.mkdir_all("xa/b"); + + for (operand, excluded, kept) in [ + (".", "./a/b", "./xa/b"), + ("a", "a/b", "a/u"), + ("./a", "./a/b", "./a/u"), + ] { + let result = ts.ucmd().arg("--exclude=a/b").arg(operand).succeeds(); + let paths: Vec<_> = result + .stdout_str() + .lines() + .filter_map(|l| l.split_once('\t').map(|(_, p)| p)) + .collect(); + assert!(!paths.contains(&excluded), "{operand}: {paths:?}"); + assert!(!paths.contains(&format!("{excluded}/c").as_str())); + assert!(paths.contains(&kept), "{operand}: {paths:?}"); + } +} + +// Like GNU, `?` and `*` match a separator, so a pattern without `/` can +// still match several trailing components. +#[cfg(not(windows))] +#[test] +fn test_du_exclude_wildcard_spans_components() { + let ts = TestScenario::new(util_name!()); + ts.fixtures.mkdir_all("x/a/b"); + ts.fixtures.mkdir_all("x/c"); + + ts.ucmd() + .args(&["--exclude=a?b", "x"]) + .succeeds() + .stdout_does_not_contain("x/a/b") + .stdout_contains("x/a\n") + .stdout_contains("x/c"); +} + +// Trailing slashes on an operand collapse to one, so entries are `a/x` +// and not `a//x`, and excludes see the same paths. +#[cfg(not(windows))] +#[test] +fn test_du_operand_trailing_slashes() { + let ts = TestScenario::new(util_name!()); + ts.fixtures.mkdir_all("a/target"); + + for (args, expected) in [ + (&["a/"][..], &["a/target", "a/"][..]), + (&["a//"], &["a/target", "a/"]), + (&["a///"], &["a/target", "a/"]), + (&["--exclude=/target", "a//"], &["a/target", "a/"]), + (&["--exclude=*/a/target", "./a//"], &["./a/"]), + ] { + let result = ts.ucmd().args(args).succeeds(); + let paths: Vec<_> = result + .stdout_str() + .lines() + .filter_map(|l| l.split_once('\t').map(|(_, p)| p)) + .collect(); + assert_eq!(paths, expected, "{args:?}"); + } +} + #[test] fn test_du_exclude_invalid_syntax() { let ts = TestScenario::new(util_name!());