use std::fs::File;
use std::io::{BufReader, Read};
use std::path::Path;
use crate::error::{Result, io_context};
use crate::i18n::tr_path;
pub type Blake3Digest = [u8; 32];
pub fn blake3_file(path: &Path) -> Result<Blake3Digest> {
let file = io_context(
tr_path("io.open_file_for_hash", path.display()),
File::open(path),
)?;
blake3_reader(path, file)
}
pub fn blake3_reader(path: &Path, reader: impl Read) -> Result<Blake3Digest> {
let mut reader = BufReader::with_capacity(1024 * 1024, reader);
let mut hasher = blake3::Hasher::new();
let mut buffer = [0_u8; 1024 * 1024];
loop {
let read = io_context(
tr_path("io.read_file_for_hash", path.display()),
reader.read(&mut buffer),
)?;
if read == 0 {
break;
}
hasher.update(&buffer[..read]);
}
Ok(*hasher.finalize().as_bytes())
}
pub fn same_blake3(left: &Path, right: &Path) -> Result<bool> {
Ok(blake3_file(left)? == blake3_file(right)?)
}