Skip to content

Commit

Permalink
du: ignore duplicate paths provided by user
Browse files Browse the repository at this point in the history
  • Loading branch information
cakebaker committed Nov 6, 2023
1 parent 1818632 commit 5e13e9b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let excludes = build_exclude_patterns(&matches)?;

let mut grand_total = 0;
let mut processed_paths = Vec::with_capacity(files.len());
'loop_file: for path in files {
// filter duplicate paths
if processed_paths.contains(&path) {
continue 'loop_file;
}

// Skip if we don't want to ignore anything
if !&excludes.is_empty() {
let path_string = path.to_string_lossy();
Expand Down Expand Up @@ -664,6 +670,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
// path_string. We add it to the grand total.
grand_total += size;
}
processed_paths.push(path.clone());
}
} else {
show_error!(
Expand Down
27 changes: 27 additions & 0 deletions tests/by-util/test_du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,33 @@ fn test_du_basics_bad_name() {
.stderr_only("du: bad_name: No such file or directory\n");
}

#[test]
fn test_du_basics_duplicate_bad_name() {
let bad_name = "bad_name";

new_ucmd!()
.arg(bad_name)
.arg(bad_name)
.fails()
.stderr_only(concat!(
"du: bad_name: No such file or directory\n",
"du: bad_name: No such file or directory\n"
));
}

#[test]
fn test_du_basics_duplicate_name() {
let (at, mut ucmd) = at_and_ucmd!();
let filename = "a";

at.touch(filename);

ucmd.arg(filename)
.arg(filename)
.succeeds()
.stdout_is(format!("0\t{filename}\n"));
}

#[test]
fn test_du_soft_link() {
let ts = TestScenario::new(util_name!());
Expand Down

0 comments on commit 5e13e9b

Please sign in to comment.