Skip to content

Commit

Permalink
Merge pull request #7122 from jfinkels/dd-error-iflag-directory
Browse files Browse the repository at this point in the history
dd: error if iflag=directory and input is stdin
  • Loading branch information
sylvestre authored Jan 13, 2025
2 parents acd79d3 + d9b3b3e commit 40511f6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/uu/dd/src/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use nix::{
};
use uucore::display::Quotable;
#[cfg(unix)]
use uucore::error::set_exit_code;
use uucore::error::{set_exit_code, USimpleError};
use uucore::error::{FromIo, UResult};
#[cfg(target_os = "linux")]
use uucore::show_if_err;
Expand Down Expand Up @@ -338,11 +338,11 @@ impl<'a> Input<'a> {
let mut src = Source::stdin_as_file();
#[cfg(unix)]
if let Source::StdinFile(f) = &src {
// GNU compatibility:
// this will check whether stdin points to a folder or not
if f.metadata()?.is_file() && settings.iflags.directory {
show_error!("standard input: not a directory");
return Err(1.into());
if settings.iflags.directory && !f.metadata()?.is_dir() {
return Err(USimpleError::new(
1,
"setting flags for 'standard input': Not a directory",
));
}
};
if settings.skip > 0 {
Expand Down
21 changes: 20 additions & 1 deletion tests/by-util/test_dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,26 @@ fn test_iflag_directory_fails_when_file_is_passed_via_std_in() {
.args(&["iflag=directory", "count=0"])
.set_stdin(std::process::Stdio::from(File::open(filename).unwrap()))
.fails()
.stderr_contains("standard input: not a directory");
.stderr_only("dd: setting flags for 'standard input': Not a directory\n");
}

#[test]
#[cfg(any(target_os = "linux", target_os = "android"))]
fn test_iflag_directory_passes_when_dir_is_redirected() {
new_ucmd!()
.args(&["iflag=directory", "count=0"])
.set_stdin(std::process::Stdio::from(File::open(".").unwrap()))
.succeeds();
}

#[test]
#[cfg(any(target_os = "linux", target_os = "android"))]
fn test_iflag_directory_fails_when_file_is_piped_via_std_in() {
new_ucmd!()
.arg("iflag=directory")
.pipe_in("")
.fails()
.stderr_only("dd: setting flags for 'standard input': Not a directory\n");
}

#[test]
Expand Down

0 comments on commit 40511f6

Please sign in to comment.