Skip to content
Open
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
33 changes: 27 additions & 6 deletions src/uu/dd/src/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,37 @@ impl Source {
return Ok(len);
}
}
match io::copy(&mut f.take(n), &mut io::sink()) {
Ok(m) if m < n => {
// Try seek first; fall back to read if not seekable
match n.try_into().ok().map(|n| f.seek(SeekFrom::Current(n))) {
Some(Ok(pos)) => {
if pos > f.metadata().map(|m| m.len()).unwrap_or(u64::MAX) {
show_error!(
"{}",
translate!("dd-error-cannot-skip-offset", "file" => "standard input")
);
}
Ok(pos)
}
Some(Err(e)) if e.raw_os_error() == Some(libc::ESPIPE) => {
Copy link
Contributor

@asder8215 asder8215 Dec 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually prefer using e.kind() over e.raw_os_error(). Is there a reason to use libc::ESPIPE over std::io::NotSeekable?

Edit: Realized that std::io::NotSeekable might not be the equivalent code for this on documentation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, is lib::ESPIPE not equivalently supported through Rust's ErrorKind?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure what the difference is between the two, I can try it out now to see if it still passes all of the tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't a difference between using libc's errors or Rust's ErrorKind; I just thought it looks a bit more idiomatic relying on Rust's ErrorKind over libc's errno values.

But I think this is one of those cases where libc::ESPIPE might fall under Rust's std::io::Other error, which isn't that descriptive.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea this one is not the equivalent, the stable ErrorKind does not have a matching variant for this use case.

match io::copy(&mut f.take(n), &mut io::sink()) {
Ok(m) if m < n => {
show_error!(
"{}",
translate!("dd-error-cannot-skip-offset", "file" => "standard input")
);
Ok(m)
}
other => other,
}
}
_ => {
show_error!(
"{}",
translate!("dd-error-cannot-skip-offset", "file" => "standard input")
translate!("dd-error-cannot-skip-invalid", "file" => "standard input")
);
Ok(m)
set_exit_code(1);
Ok(0)
}
Ok(m) => Ok(m),
Err(e) => Err(e),
}
}
Self::File(f) => f.seek(SeekFrom::Current(n.try_into().unwrap())),
Expand Down
Loading