diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 012517dc3eb..cf4ae5420ac 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -755,13 +755,11 @@ enum DuError { impl UError for DuError {} /// Read a file and return each line in a vector of String -fn file_as_vec(filename: impl AsRef) -> Vec { - let file = File::open(filename).expect("no such file"); +fn file_as_vec(filename: impl AsRef) -> UResult> { + let file = File::open(filename)?; let buf = BufReader::new(file); - - buf.lines() - .map(|l| l.expect("Could not parse line")) - .collect() + let lines = buf.lines().collect::, _>>()?; + Ok(lines) } /// Given the `--exclude-from` and/or `--exclude` arguments, returns the globset lists @@ -770,7 +768,10 @@ fn build_exclude_patterns(matches: &ArgMatches) -> UResult> { let exclude_from_iterator = matches .get_many::(options::EXCLUDE_FROM) .unwrap_or_default() - .flat_map(file_as_vec); + .map(file_as_vec) + .collect::>>>()? + .into_iter() + .flatten(); let excludes_iterator = matches .get_many::(options::EXCLUDE) diff --git a/tests/by-util/test_du.rs b/tests/by-util/test_du.rs index 0adb1662703..fbb1761afe9 100644 --- a/tests/by-util/test_du.rs +++ b/tests/by-util/test_du.rs @@ -1518,6 +1518,23 @@ fn test_du_exclude_invalid_syntax() { .stderr_contains("du: Invalid exclude syntax"); } +#[test] +fn test_du_exclude_from_nonexistent_file() { + new_ucmd!() + .arg("--exclude-from=nonexistent-file") + .fails() + .stderr_contains("du: No such file or directory"); +} + +#[cfg(all(target_os = "linux", not(target_env = "musl")))] +#[test] +fn test_du_exclude_from_read_error() { + new_ucmd!() + .arg("--exclude-from=/proc/self/mem") + .fails() + .stderr_contains("du: Input/output error"); +} + #[cfg(not(windows))] #[test] fn test_du_symlink_fail() {