mirror of
https://github.com/sharkdp/hexyl.git
synced 2026-10-30 09:05:54 +00:00
BREAKING CHANGE: feat(lib): skip forward when relative forward seeks are specified on non-seekable streams
This is technically a breaking change, since the data and behavior of `Input` is exposed publicly. However, it seems unlikely to me that clients actually depended on a failure in the case that's being changed.
This commit is contained in:
1 parent
1459616d27
commit
c5c8d1190f
1 file changed
+28
-11
+28
-11
@@ -1,5 +1,6 @@
|
|||||||
|
use std::convert::TryFrom;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::{self, Read, Seek, SeekFrom};
|
use std::io::{self, copy, sink, Read, Seek, SeekFrom};
|
||||||
|
|
||||||
pub enum Input<'a> {
|
pub enum Input<'a> {
|
||||||
File(fs::File),
|
File(fs::File),
|
||||||
@@ -17,23 +18,39 @@ impl<'a> Read for Input<'a> {
|
|||||||
|
|
||||||
impl<'a> Seek for Input<'a> {
|
impl<'a> Seek for Input<'a> {
|
||||||
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
|
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
|
||||||
|
fn try_skip<R>(reader: R, pos: SeekFrom, err_desc: &'static str) -> io::Result<u64>
|
||||||
|
where
|
||||||
|
R: Read,
|
||||||
|
{
|
||||||
|
let cant_seek_abs_err = || Err(io::Error::new(io::ErrorKind::Other, err_desc));
|
||||||
|
|
||||||
|
let offset = match pos {
|
||||||
|
SeekFrom::Current(o) => u64::try_from(o).or_else(|_e| cant_seek_abs_err())?,
|
||||||
|
SeekFrom::Start(_) | SeekFrom::End(_) => cant_seek_abs_err()?,
|
||||||
|
};
|
||||||
|
|
||||||
|
copy(&mut reader.take(offset), &mut sink())
|
||||||
|
}
|
||||||
|
|
||||||
match *self {
|
match *self {
|
||||||
Input::File(ref mut file) => {
|
Input::File(ref mut file) => {
|
||||||
let seek_res = file.seek(pos);
|
let seek_res = file.seek(pos);
|
||||||
if let Err(Some(libc::ESPIPE)) = seek_res.as_ref().map_err(|err| err.raw_os_error())
|
if let Err(Some(libc::ESPIPE)) = seek_res.as_ref().map_err(|err| err.raw_os_error())
|
||||||
{
|
{
|
||||||
return Err(io::Error::new(
|
try_skip(
|
||||||
io::ErrorKind::Other,
|
file,
|
||||||
"Using '--seek' is not supported when using a pipe",
|
pos,
|
||||||
|
"Pipes only support seeking forward with a relative offset",
|
||||||
)
|
)
|
||||||
.into());
|
} else {
|
||||||
};
|
seek_res
|
||||||
seek_res
|
}
|
||||||
}
|
}
|
||||||
Input::Stdin(_) => Err(io::Error::new(
|
Input::Stdin(ref mut stdin) => try_skip(
|
||||||
io::ErrorKind::Other,
|
stdin,
|
||||||
"Using '--seek' is not supported when reading from STDIN",
|
pos,
|
||||||
)),
|
"STDIN only supports seeking forward with a relative offset",
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user