feat: progress bar.

This commit is contained in:
2025-12-18 23:52:50 +08:00
parent 13083ed92e
commit 09f2452bc4
4 changed files with 386 additions and 16 deletions
+21
View File
@@ -33,6 +33,14 @@ pub struct FileMeta {
impl FileMeta {
pub fn from_path(path: &Path) -> Result<Self> {
Self::from_path_with_callback(path, |_| {}, || {})
}
pub fn from_path_with_callback<F1, F2>(path: &Path, mut on_bytes_read: F1, mut on_iop: F2) -> Result<Self>
where
F1: FnMut(u64),
F2: FnMut(),
{
let info =
fs::metadata(path).with_context(|| format!("无法读取文件信息: {}", path.display()))?;
if !info.is_file() {
@@ -78,6 +86,9 @@ impl FileMeta {
head115.feed(chunk);
head_baidu.feed(chunk);
on_bytes_read(read_len as u64);
on_iop(); // 每次 read 调用算一次 IOPS
}
let head_115 = calc_head_115(head115.as_slice());
@@ -114,6 +125,14 @@ impl FileMeta {
}
pub fn calc_xxh128(path: &Path) -> Result<String> {
calc_xxh128_with_callback(path, |_| {}, || {})
}
pub fn calc_xxh128_with_callback<F1, F2>(path: &Path, mut on_bytes_read: F1, mut on_iop: F2) -> Result<String>
where
F1: FnMut(u64),
F2: FnMut(),
{
let mut file = File::open(path).with_context(|| format!("无法打开文件: {}", path.display()))?;
let mut buffer = vec![0u8; DEFAULT_BUFFER_SIZE];
let mut hasher = Xxh3::new();
@@ -124,6 +143,8 @@ pub fn calc_xxh128(path: &Path) -> Result<String> {
break;
}
hasher.update(&buffer[..read_len]);
on_bytes_read(read_len as u64);
on_iop(); // 每次 read 调用算一次 IOPS
}
Ok(hex_upper(hasher.digest128().to_be_bytes()))