From ae1ff688bf6adc3eced43efa6b42ca5183453eb3 Mon Sep 17 00:00:00 2001 From: John Costa Date: Wed, 18 Nov 2026 18:44:54 +0000 Subject: [PATCH 1/4] du: match --exclude against trailing path components Exclude patterns were only tried against the full path and the base name, so --exclude=target/debug missed ./target/debug when the operand was . or ./target. Also try every part of the path that starts after a separator, for operands and traversed entries alike, matching GNU du. Fixes #16691 --- src/uu/du/src/du.rs | 24 ++++++++++++++---------- tests/by-util/test_du.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 42d9d3a9b86..c6980eab3db 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -562,9 +562,7 @@ 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 is_excluded(pattern, &this_stat.path) { if options.verbose { println!( "{}", @@ -722,12 +720,7 @@ 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 is_excluded(pattern, &this_stat.path) { // if the directory is ignored, leave early if options.verbose { println!( @@ -849,6 +842,17 @@ 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: &Path) -> bool { + let path = path.to_string_lossy(); + pattern.matches(&path) + || path + .char_indices() + .any(|(i, c)| std::path::is_separator(c) && pattern.matches(&path[i + 1..])) +} + /// Given the `--exclude-from` and/or `--exclude` arguments, returns the globset lists /// to ignore the files fn build_exclude_patterns(matches: &ArgMatches) -> UResult> { @@ -1253,7 +1257,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) { // 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..95f5448f108 100644 --- a/tests/by-util/test_du.rs +++ b/tests/by-util/test_du.rs @@ -1853,6 +1853,35 @@ 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:?}"); + } +} + #[test] fn test_du_exclude_invalid_syntax() { let ts = TestScenario::new(util_name!()); From badc7f24dafcd8414f68bfe4fb4cac4978bab300 Mon Sep 17 00:00:00 2001 From: John Costa Date: Wed, 18 Nov 2026 19:18:33 +0000 Subject: [PATCH 2/4] du: collapse trailing slashes on operands An operand like a// was kept as is, so entries showed as a//x, unlike GNU's a/x, and the exclude suffix walk could match /x against them. Collapse trailing separators to one before traversal, keeping / and // as GNU does. --- src/uu/du/src/du.rs | 24 ++++++++++++++++++++++++ tests/by-util/test_du.rs | 25 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index c6980eab3db..f7ec55d7026 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -853,6 +853,26 @@ fn is_excluded(pattern: &Pattern, path: &Path) -> bool { .any(|(i, c)| std::path::is_separator(c) && pattern.matches(&path[i + 1..])) } +/// 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; + } + uucore::os_str_from_bytes(&bytes[..end]).map_or_else(|_| path.clone(), PathBuf::from) +} + /// Given the `--exclude-from` and/or `--exclude` arguments, returns the globset lists /// to ignore the files fn build_exclude_patterns(matches: &ArgMatches) -> UResult> { @@ -1175,6 +1195,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 diff --git a/tests/by-util/test_du.rs b/tests/by-util/test_du.rs index 95f5448f108..164f9d8b03a 100644 --- a/tests/by-util/test_du.rs +++ b/tests/by-util/test_du.rs @@ -1882,6 +1882,31 @@ fn test_du_exclude_trailing_components() { } } +// 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!()); From 63f925cc9628657447ba77108ec6013a6533bb8e Mon Sep 17 00:00:00 2001 From: John Costa Date: Wed, 18 Nov 2026 16:18:03 -0800 Subject: [PATCH 3/4] du: skip the per-component exclude scan when it cannot match A pattern starting with * already matched the whole path, and a pattern without wildcards or separators can only match the last component. Convert the path once per entry instead of once per pattern, and drop the unreachable fallback in collapse_trailing_separators. --- src/uu/du/src/du.rs | 37 +++++++++++++++++++++++++++---------- tests/by-util/test_du.rs | 17 +++++++++++++++++ 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index f7ec55d7026..cca6c0300b5 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -561,8 +561,9 @@ fn safe_du( }; // Check excludes + let path_string = this_stat.path.to_string_lossy(); for pattern in &options.excludes { - if is_excluded(pattern, &this_stat.path) { + if is_excluded(pattern, &path_string) { if options.verbose { println!( "{}", @@ -719,8 +720,9 @@ fn du_regular( } // We have an exclude list + let path_string = this_stat.path.to_string_lossy(); for pattern in &options.excludes { - if is_excluded(pattern, &this_stat.path) { + if is_excluded(pattern, &path_string) { // if the directory is ignored, leave early if options.verbose { println!( @@ -845,12 +847,26 @@ fn file_as_vec(filename: impl AsRef) -> UResult> { /// 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: &Path) -> bool { - let path = path.to_string_lossy(); - pattern.matches(&path) - || path - .char_indices() - .any(|(i, c)| std::path::is_separator(c) && pattern.matches(&path[i + 1..])) +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//` @@ -870,7 +886,8 @@ fn collapse_trailing_separators(path: PathBuf) -> PathBuf { if end == bytes.len() { return path; } - uucore::os_str_from_bytes(&bytes[..end]).map_or_else(|_| path.clone(), PathBuf::from) + // 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 @@ -1281,7 +1298,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 is_excluded(pattern, &path) { + 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 164f9d8b03a..307ad404524 100644 --- a/tests/by-util/test_du.rs +++ b/tests/by-util/test_du.rs @@ -1882,6 +1882,23 @@ fn test_du_exclude_trailing_components() { } } +// 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))] From d22c67738df8b6386e54a2fb54bd1f6acd6dce47 Mon Sep 17 00:00:00 2001 From: John Costa Date: Thu, 19 Nov 2026 12:38:48 -0800 Subject: [PATCH 4/4] du: skip the exclude path conversion when no --exclude is given --- src/uu/du/src/du.rs | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index cca6c0300b5..1e637a8f149 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -561,16 +561,18 @@ fn safe_du( }; // Check excludes - 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()) - ); + 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; } } @@ -720,18 +722,20 @@ fn du_regular( } // We have an exclude list - 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()) - ); + 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; } }