Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 75 additions & 26 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -849,6 +848,52 @@ fn file_as_vec(filename: impl AsRef<Path>) -> UResult<Vec<String>> {
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<Vec<Pattern>> {
Expand Down Expand Up @@ -1171,6 +1216,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
} else {
vec![PathBuf::from(".")]
};
let files: Vec<PathBuf> = files
.into_iter()
.map(collapse_trailing_separators)
.collect();

let time = matches.contains_id(options::TIME).then(|| {
matches
Expand Down Expand Up @@ -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!(
Expand Down
71 changes: 71 additions & 0 deletions tests/by-util/test_du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!());
Expand Down
Loading