use gix_error::{ErrorExt, Exn, Message, message};
pub fn from_std_io(source: std::io::Error) -> gix_error::Exn {
source.raise_erased()
}
pub fn from_hasher(source: Exn<Message>) -> Exn {
source.raise(message("Failed to hash data")).erased()
}
pub(super) mod _impl {
use crate::{
Hasher, hasher,
io::{from_hasher, from_std_io},
};
use gix_error::ExnResult;
pub fn bytes_of_file(
path: &std::path::Path,
num_bytes_from_start: u64,
kind: crate::Kind,
progress: &mut dyn gix_features::progress::Progress,
should_interrupt: &std::sync::atomic::AtomicBool,
) -> ExnResult<crate::ObjectId> {
bytes(
&mut std::fs::File::open(path).map_err(from_std_io)?,
num_bytes_from_start,
kind,
progress,
should_interrupt,
)
}
pub fn bytes(
read: &mut dyn std::io::Read,
num_bytes_from_start: u64,
kind: crate::Kind,
progress: &mut dyn gix_features::progress::Progress,
should_interrupt: &std::sync::atomic::AtomicBool,
) -> ExnResult<crate::ObjectId> {
bytes_with_hasher(read, num_bytes_from_start, hasher(kind), progress, should_interrupt)
}
pub fn bytes_with_hasher(
read: &mut dyn std::io::Read,
num_bytes_from_start: u64,
mut hasher: Hasher,
progress: &mut dyn gix_features::progress::Progress,
should_interrupt: &std::sync::atomic::AtomicBool,
) -> ExnResult<crate::ObjectId> {
let start = std::time::Instant::now();
progress.init(
Some(num_bytes_from_start as gix_features::progress::prodash::progress::Step),
gix_features::progress::bytes(),
);
const BUF_SIZE: usize = u16::MAX as usize;
let mut buf = [0u8; BUF_SIZE];
let mut bytes_left = num_bytes_from_start;
while bytes_left > 0 {
let out = &mut buf[..BUF_SIZE.min(bytes_left as usize)];
read.read_exact(out).map_err(from_std_io)?;
bytes_left -= out.len() as u64;
progress.inc_by(out.len());
hasher.update(out);
if should_interrupt.load(std::sync::atomic::Ordering::SeqCst) {
return Err(from_std_io(std::io::Error::new(
std::io::ErrorKind::Interrupted,
"Interrupted",
)));
}
}
let id = hasher.try_finalize().map_err(from_hasher)?;
progress.show_throughput(start);
Ok(id)
}
pub struct Write<T> {
pub hash: Hasher,
pub inner: T,
}
impl<T> std::io::Write for Write<T>
where
T: std::io::Write,
{
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let written = self.inner.write(buf)?;
self.hash.update(&buf[..written]);
Ok(written)
}
fn flush(&mut self) -> std::io::Result<()> {
self.inner.flush()
}
}
impl<T> Write<T>
where
T: std::io::Write,
{
pub fn new(inner: T, object_hash: crate::Kind) -> Self {
match object_hash {
#[cfg(feature = "sha1")]
crate::Kind::Sha1 => Write {
inner,
hash: crate::hasher(object_hash),
},
#[cfg(feature = "sha256")]
crate::Kind::Sha256 => Write {
inner,
hash: crate::hasher(object_hash),
},
}
}
}
}
pub use _impl::Write;