Skip to content
Merged
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
79 changes: 40 additions & 39 deletions src/uu/dd/src/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,17 @@ impl Source {
}
#[cfg(unix)]
Self::StdinFile(f) => {
if let Ok(Some(len)) = try_get_len_of_block_device(f) {
if len < n {
// GNU compatibility:
// this case prints the stats but sets the exit code to 1
show_error!(
"{}",
translate!("dd-error-cannot-skip-invalid", "file" => "standard input")
);
set_exit_code(1);
return Ok(len);
}
if let Ok(Some(len)) = try_get_len_of_block_device(f)
&& len < n
{
// GNU compatibility:
// this case prints the stats but sets the exit code to 1
show_error!(
"{}",
translate!("dd-error-cannot-skip-invalid", "file" => "standard input")
);
set_exit_code(1);
return Ok(len);
}
// Get file length before seeking to avoid race condition
let file_len = f.metadata().as_ref().map_or(u64::MAX, Metadata::len);
Expand Down Expand Up @@ -381,14 +381,16 @@ impl<'a> Input<'a> {
#[cfg(unix)]
let mut src = Source::stdin_as_file();
#[cfg(unix)]
if let Source::StdinFile(f) = &src {
if settings.iflags.directory && !f.metadata()?.is_dir() {
return Err(USimpleError::new(
1,
translate!("dd-error-not-directory", "file" => "standard input"),
));
}
if let Source::StdinFile(f) = &src
&& settings.iflags.directory
&& !f.metadata()?.is_dir()
{
return Err(USimpleError::new(
1,
translate!("dd-error-not-directory", "file" => "standard input"),
));
}

if settings.skip > 0 {
src.skip(settings.skip, settings.ibs)?;
}
Expand Down Expand Up @@ -655,17 +657,17 @@ impl Dest {
Self::Stdout(stdout) => io::copy(&mut io::repeat(0).take(n), stdout),
Self::File(f, _) => {
#[cfg(unix)]
if let Ok(Some(len)) = try_get_len_of_block_device(f) {
if len < n {
// GNU compatibility:
// this case prints the stats but sets the exit code to 1
show_error!(
"{}",
translate!("dd-error-cannot-seek-invalid", "output" => "standard output")
);
set_exit_code(1);
return Ok(len);
}
if let Ok(Some(len)) = try_get_len_of_block_device(f)
&& len < n
{
// GNU compatibility:
// this case prints the stats but sets the exit code to 1
show_error!(
"{}",
translate!("dd-error-cannot-seek-invalid", "output" => "standard output")
);
set_exit_code(1);
return Ok(len);
}
f.seek(SeekFrom::Current(n.try_into().unwrap()))
}
Expand Down Expand Up @@ -1174,10 +1176,14 @@ fn dd_copy(mut i: Input, o: Output) -> io::Result<()> {
let alarm = Alarm::with_interval(Duration::from_secs(1));

#[cfg(target_os = "linux")]
if let Err(e) = install_sigusr1_handler() {
if i.settings.status != Some(StatusLevel::None) {
eprintln!("{}\n\t{e}", translate!("dd-warning-signal-handler"));
}
if let Err(e) = install_sigusr1_handler()
&& i.settings.status != Some(StatusLevel::None)
{
let _ = writeln!(
io::stderr(),
"{}\n\t{e}",
translate!("dd-warning-signal-handler")
);
}

// Index in the input file where we are reading bytes and in
Expand Down Expand Up @@ -1498,12 +1504,7 @@ fn try_get_len_of_block_device(file: &mut File) -> io::Result<Option<u64>> {
/// Decide whether the named file is a named pipe, also known as a FIFO.
#[cfg(unix)]
fn is_fifo(filename: &str) -> bool {
if let Ok(metadata) = std::fs::metadata(filename) {
if metadata.file_type().is_fifo() {
return true;
}
}
false
std::fs::metadata(filename).is_ok_and(|m| m.file_type().is_fifo())
}

#[uucore::main]
Expand Down
Loading