use anyhow::Result;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
use std::path::Path;
const QUICK_HASH_BYTES: usize = 64 * 1024;
pub fn quick_hash(path: &Path) -> Result<blake3::Hash> {
let mut f = File::open(path)?;
let mut buf = vec![0u8; QUICK_HASH_BYTES];
let n = f.read(&mut buf)?;
Ok(blake3::hash(&buf[..n]))
}
pub fn full_hash(path: &Path) -> Result<blake3::Hash> {
let mut f = File::open(path)?;
let mut hasher = blake3::Hasher::new();
std::io::copy(&mut f, &mut hasher)?;
Ok(hasher.finalize())
}
pub fn bytes_equal(a: &Path, b: &Path) -> Result<bool> {
const CHUNK: usize = 256 * 1024;
let mut fa = File::open(a)?;
let mut fb = File::open(b)?;
fa.seek(SeekFrom::Start(0))?;
fb.seek(SeekFrom::Start(0))?;
let mut ba = vec![0u8; CHUNK];
let mut bb = vec![0u8; CHUNK];
loop {
let na = fa.read(&mut ba)?;
let nb = fb.read(&mut bb)?;
if na != nb { return Ok(false); }
if na == 0 { return Ok(true); }
if ba[..na] != bb[..nb] { return Ok(false); }
}
}