mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 04:27:45 +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 signal_hook::consts::signal;
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
use uucore::error::{FromIo, UResult, USimpleError};
|
use uucore::error::{FromIo, UResult, USimpleError};
|
||||||
|
use uucore::show_error;
|
||||||
use uucore::InvalidEncodingHandling;
|
use uucore::InvalidEncodingHandling;
|
||||||
|
|
||||||
const ABOUT: &str = "copy, and optionally convert, a file system resource";
|
const ABOUT: &str = "copy, and optionally convert, a file system resource";
|
||||||
|
@ -80,9 +81,12 @@ impl Input<io::Stdin> {
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(amt) = skip {
|
if let Some(amt) = skip {
|
||||||
let mut buf = vec![BUF_INIT_BYTE; amt];
|
let num_bytes_read = i
|
||||||
i.force_fill(&mut buf, amt)
|
.force_fill(amt.try_into().unwrap())
|
||||||
.map_err_context(|| "failed to read input".to_string())?;
|
.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)
|
Ok(i)
|
||||||
|
@ -264,17 +268,19 @@ impl<R: Read> Input<R> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Force-fills a buffer, ignoring zero-length reads which would otherwise be
|
/// Read the specified number of bytes from this reader.
|
||||||
/// interpreted as EOF.
|
///
|
||||||
/// Note: This will not return unless the source (eventually) produces
|
/// On success, this method returns the number of bytes read. If
|
||||||
/// enough bytes to meet target_len.
|
/// this reader has fewer than `n` bytes available, then it reads
|
||||||
fn force_fill(&mut self, buf: &mut [u8], target_len: usize) -> std::io::Result<usize> {
|
/// as many as possible. In that case, this method returns a
|
||||||
let mut base_idx = 0;
|
/// number less than `n`.
|
||||||
while base_idx < target_len {
|
///
|
||||||
base_idx += self.read(&mut buf[base_idx..target_len])?;
|
/// # Errors
|
||||||
}
|
///
|
||||||
|
/// If there is a problem reading.
|
||||||
Ok(base_idx)
|
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");
|
.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]
|
#[test]
|
||||||
fn test_seek_do_not_overwrite() {
|
fn test_seek_do_not_overwrite() {
|
||||||
let (at, mut ucmd) = at_and_ucmd!();
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue