mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 03:57:44 +00:00
Merge pull request #3066 from jfinkels/dd-skip-beyond-file
dd: show warning if skipping past end of input
This commit is contained in:
commit
090fb07361
2 changed files with 32 additions and 13 deletions
|
@ -42,6 +42,7 @@ use gcd::Gcd;
|
|||
use signal_hook::consts::signal;
|
||||
use uucore::display::Quotable;
|
||||
use uucore::error::{FromIo, UResult, USimpleError};
|
||||
use uucore::show_error;
|
||||
use uucore::InvalidEncodingHandling;
|
||||
|
||||
const ABOUT: &str = "copy, and optionally convert, a file system resource";
|
||||
|
@ -80,9 +81,12 @@ impl Input<io::Stdin> {
|
|||
};
|
||||
|
||||
if let Some(amt) = skip {
|
||||
let mut buf = vec![BUF_INIT_BYTE; amt];
|
||||
i.force_fill(&mut buf, amt)
|
||||
let num_bytes_read = i
|
||||
.force_fill(amt.try_into().unwrap())
|
||||
.map_err_context(|| "failed to read input".to_string())?;
|
||||
if num_bytes_read < amt {
|
||||
show_error!("'standard input': cannot skip to specified offset");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(i)
|
||||
|
@ -264,17 +268,19 @@ impl<R: Read> Input<R> {
|
|||
})
|
||||
}
|
||||
|
||||
/// Force-fills a buffer, ignoring zero-length reads which would otherwise be
|
||||
/// interpreted as EOF.
|
||||
/// Note: This will not return unless the source (eventually) produces
|
||||
/// enough bytes to meet target_len.
|
||||
fn force_fill(&mut self, buf: &mut [u8], target_len: usize) -> std::io::Result<usize> {
|
||||
let mut base_idx = 0;
|
||||
while base_idx < target_len {
|
||||
base_idx += self.read(&mut buf[base_idx..target_len])?;
|
||||
}
|
||||
|
||||
Ok(base_idx)
|
||||
/// Read the specified number of bytes from this reader.
|
||||
///
|
||||
/// On success, this method returns the number of bytes read. If
|
||||
/// this reader has fewer than `n` bytes available, then it reads
|
||||
/// as many as possible. In that case, this method returns a
|
||||
/// number less than `n`.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// If there is a problem reading.
|
||||
fn force_fill(&mut self, n: u64) -> std::io::Result<usize> {
|
||||
let mut buf = vec![];
|
||||
self.take(n).read_to_end(&mut buf)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -657,6 +657,19 @@ fn test_seek_bytes() {
|
|||
.stdout_is("\0\0\0\0\0\0\0\0abcdefghijklm\n");
|
||||
}
|
||||
|
||||
/// Test for skipping beyond the number of bytes in a file.
|
||||
#[test]
|
||||
fn test_skip_beyond_file() {
|
||||
new_ucmd!()
|
||||
.args(&["bs=1", "skip=5", "count=0", "status=noxfer"])
|
||||
.pipe_in("abcd")
|
||||
.succeeds()
|
||||
.no_stdout()
|
||||
.stderr_contains(
|
||||
"'standard input': cannot skip to specified offset\n0+0 records in\n0+0 records out\n",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_seek_do_not_overwrite() {
|
||||
let (at, mut ucmd) = at_and_ucmd!();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue