From de85d893063722725c0e417c72a12c8d6a0a6fb9 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 24 Dec 2023 17:21:49 +0100 Subject: [PATCH] du: prepare tests/du/files0-from.pl --- src/uu/du/src/du.rs | 27 +++++++++++++++++++++++++-- util/build-gnu.sh | 4 ++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 660b54ff33e..d1f10ce54b5 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -603,8 +603,20 @@ fn read_files_from(file_name: &str) -> Result, std::io::Error> { )); } - // Read from a file - Box::new(BufReader::new(File::open(file_name)?)) + // Attempt to open the file and handle the error if it does not exist + match File::open(file_name) { + Ok(file) => Box::new(BufReader::new(file)), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + format!( + "cannot open '{}' for reading: No such file or directory", + file_name + ), + )) + } + Err(e) => return Err(e), + } }; let mut paths = Vec::new(); @@ -634,6 +646,17 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { )?; let files = if let Some(file_from) = matches.get_one::(options::FILES0_FROM) { + if file_from == "-" && matches.get_one::(options::FILE).is_some() { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + format!( + "extra operand {}\nfile operands cannot be combined with --files0-from", + matches.get_one::(options::FILE).unwrap().quote() + ), + ) + .into()); + } + // Read file paths from the specified file, separated by null characters read_files_from(file_from)? } else { diff --git a/util/build-gnu.sh b/util/build-gnu.sh index 0b6f984d9a8..486ed14207f 100755 --- a/util/build-gnu.sh +++ b/util/build-gnu.sh @@ -283,6 +283,10 @@ sed -i -E "s|^([^#]*2_31.*)$|#\1|g" tests/printf/printf-cov.pl sed -i -e "s/du: invalid -t argument/du: invalid --threshold argument/" -e "s/du: option requires an argument/error: a value is required for '--threshold ' but none was supplied/" -e "/Try 'du --help' for more information./d" tests/du/threshold.sh +# Remove the extra output check +sed -i -e "s|Try '\$prog --help' for more information.\\\n||" tests/du/files0-from.pl +sed -i -e "s|when reading file names from stdin, no file name of\"|-: No such file or directory\n\"|" -e "s| '-' allowed\\\n||" tests/du/files0-from.pl + awk 'BEGIN {count=0} /compare exp out2/ && count < 6 {sub(/compare exp out2/, "grep -q \"cannot be used with\" out2"); count++} 1' tests/df/df-output.sh > tests/df/df-output.sh.tmp && mv tests/df/df-output.sh.tmp tests/df/df-output.sh # with ls --dired, in case of error, we have a slightly different error position