Skip to content
Merged
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
15 changes: 8 additions & 7 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path>) -> Vec<String> {
let file = File::open(filename).expect("no such file");
fn file_as_vec(filename: impl AsRef<Path>) -> UResult<Vec<String>> {
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::<Result<Vec<String>, _>>()?;
Ok(lines)
}

/// Given the `--exclude-from` and/or `--exclude` arguments, returns the globset lists
Expand All @@ -770,7 +768,10 @@ fn build_exclude_patterns(matches: &ArgMatches) -> UResult<Vec<Pattern>> {
let exclude_from_iterator = matches
.get_many::<String>(options::EXCLUDE_FROM)
.unwrap_or_default()
.flat_map(file_as_vec);
.map(file_as_vec)
.collect::<UResult<Vec<Vec<String>>>>()?
.into_iter()
.flatten();

let excludes_iterator = matches
.get_many::<String>(options::EXCLUDE)
Expand Down
17 changes: 17 additions & 0 deletions tests/by-util/test_du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading