Skip to content

Commit

Permalink
dd: Error message of invalid args is matched with GNU (#3831)
Browse files Browse the repository at this point in the history
  • Loading branch information
p-fuchs authored Aug 17, 2022
1 parent 9fad6fd commit 3acbd1c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/uu/dd/src/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl Input<File> {
}

opts.open(fname)
.map_err_context(|| "failed to open input file".to_string())?
.map_err_context(|| format!("failed to open {}", fname.quote()))?
};

// The --skip and --iseek flags are additive. On a file, they seek.
Expand Down
14 changes: 12 additions & 2 deletions src/uu/dd/src/parseargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub enum ParseError {
IbsOutOfRange,
ObsOutOfRange,
CbsOutOfRange,
InvalidNumber(String),
}

impl ParseError {
Expand All @@ -56,6 +57,7 @@ impl ParseError {
Self::IbsOutOfRange => Self::IbsOutOfRange,
Self::ObsOutOfRange => Self::ObsOutOfRange,
Self::CbsOutOfRange => Self::CbsOutOfRange,
Self::InvalidNumber(_) => Self::InvalidNumber(s),
}
}
}
Expand All @@ -79,7 +81,12 @@ impl std::fmt::Display for ParseError {
write!(f, "Only one ov conv=excl or conv=nocreat may be specified")
}
Self::FlagNoMatch(arg) => {
write!(f, "Unrecognized iflag=FLAG or oflag=FLAG -> {}", arg)
// Additional message about 'dd --help' is displayed only in this situation.
write!(
f,
"invalid input flag: ‘{}’\nTry 'dd --help' for more information.",
arg
)
}
Self::ConvFlagNoMatch(arg) => {
write!(f, "Unrecognized conv=CONV -> {}", arg)
Expand Down Expand Up @@ -115,6 +122,9 @@ impl std::fmt::Display for ParseError {
Self::Unimplemented(arg) => {
write!(f, "feature not implemented on this system -> {}", arg)
}
Self::InvalidNumber(arg) => {
write!(f, "invalid number: ‘{}’", arg)
}
}
}
}
Expand Down Expand Up @@ -389,7 +399,7 @@ fn parse_bytes_no_x(s: &str) -> Result<u64, ParseError> {
(None, None, None) => match uucore::parse_size::parse_size(s) {
Ok(n) => (n, 1),
Err(ParseSizeError::InvalidSuffix(s)) | Err(ParseSizeError::ParseFailure(s)) => {
return Err(ParseError::MultiplierStringParseFailure(s))
return Err(ParseError::InvalidNumber(s))
}
Err(ParseSizeError::SizeTooBig(s)) => {
return Err(ParseError::MultiplierStringOverflow(s))
Expand Down
59 changes: 59 additions & 0 deletions tests/by-util/test_dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1187,3 +1187,62 @@ fn test_final_stats_si_iec() {
let s = result.stderr_str();
assert!(s.starts_with("2+0 records in\n2+0 records out\n1024 bytes (1 KB, 1024 B) copied,"));
}

#[test]
fn test_invalid_number_arg_gnu_compatibility() {
let commands = vec!["bs", "cbs", "count", "ibs", "obs", "seek", "skip"];

for command in commands {
new_ucmd!()
.args(&[format!("{}=", command)])
.fails()
.stderr_is("dd: invalid number: ‘’");

new_ucmd!()
.args(&[format!("{}=29d", command)])
.fails()
.stderr_is("dd: invalid number: ‘29d’");
}
}

#[test]
fn test_invalid_flag_arg_gnu_compatibility() {
let commands = vec!["iflag", "oflag"];

for command in commands {
new_ucmd!()
.args(&[format!("{}=", command)])
.fails()
.stderr_is("dd: invalid input flag: ‘’\nTry 'dd --help' for more information.");

new_ucmd!()
.args(&[format!("{}=29d", command)])
.fails()
.stderr_is("dd: invalid input flag: ‘29d’\nTry 'dd --help' for more information.");
}
}

#[test]
fn test_invalid_file_arg_gnu_compatibility() {
new_ucmd!()
.args(&["if="])
.fails()
.stderr_is("dd: failed to open '': No such file or directory");

new_ucmd!()
.args(&["if=81as9bn8as9g302az8ns9.pdf.zip.pl.com"])
.fails()
.stderr_is(
"dd: failed to open '81as9bn8as9g302az8ns9.pdf.zip.pl.com': No such file or directory",
);

new_ucmd!()
.args(&["of="])
.fails()
.stderr_is("dd: failed to open '': No such file or directory");

new_ucmd!()
.args(&["of=81as9bn8as9g302az8ns9.pdf.zip.pl.com"])
.pipe_in("")
.succeeds();
}

0 comments on commit 3acbd1c

Please sign in to comment.