From d3f98b0d3af7d57db870e4c70d72dd16b2e8803a Mon Sep 17 00:00:00 2001 From: Chad Austin Date: Wed, 18 Dec 2024 23:17:44 -0800 Subject: [PATCH] unify compute_blake3 and compute_extra_hashes --- src/hash/mod.rs | 60 ++++++------------------------------------------- tests/hash.rs | 4 ++-- 2 files changed, 9 insertions(+), 55 deletions(-) diff --git a/src/hash/mod.rs b/src/hash/mod.rs index eb26512..bd1b73c 100644 --- a/src/hash/mod.rs +++ b/src/hash/mod.rs @@ -317,62 +317,16 @@ pub async fn compute_content_hashes( } pub async fn compute_blake3(path: PathBuf) -> anyhow::Result { - // This assumes that computing blake3 is much faster than IO and - // will not contend with other workers. - iopool::run_in_io_pool(move || { - let mut hasher = blake3::Hasher::new(); - let mut file = std::fs::File::open(path)?; - let mut buffer = [0u8; READ_SIZE]; - loop { - let n = file.read(&mut buffer)?; - if n == 0 { - break; - } - hasher.update(&buffer[..n]); - } - Ok(hasher.finalize().into()) - }) - .await + Ok(compute_content_hashes(path, ContentHashType::BLAKE3.into()) + .await? + .blake3 + .expect("blake3 requested but not returned")) } pub async fn compute_extra_hashes(path: PathBuf) -> anyhow::Result { - // TODO: To save IO, this could be folded into compute_blake3 on - // the initial computation. - - iopool::run_in_io_pool(move || { - let mut extra_hashes = ExtraHashes::default(); - - let mut hash_md5 = md5::Md5::default(); - let mut hash_sha1 = sha1::Sha1::default(); - let mut hash_sha256 = sha2::Sha256::default(); - let mut hashers: [(&mut dyn DynDigest, &mut [u8]); 3] = [ - (&mut hash_md5, extra_hashes.md5.get_or_insert_default()), - (&mut hash_sha1, extra_hashes.sha1.get_or_insert_default()), - ( - &mut hash_sha256, - extra_hashes.sha256.get_or_insert_default(), - ), - ]; - let mut file = std::fs::File::open(path)?; - let mut buffer = [0u8; READ_SIZE]; - loop { - let n = file.read(&mut buffer)?; - if n == 0 { - break; - } - let buffer = &buffer[..n]; - for (h, _) in &mut hashers { - h.update(buffer); - } - } - - for (h, dest) in &mut hashers { - h.finalize_into_reset(dest).expect("invalid buffer size"); - } - - Ok(extra_hashes) - }) - .await + let hashes = + compute_content_hashes(path, ContentHashSet::all() - ContentHashType::BLAKE3).await?; + Ok(hashes.extra_hashes) } #[cfg(test)] diff --git a/tests/hash.rs b/tests/hash.rs index f470e39..6f2520c 100644 --- a/tests/hash.rs +++ b/tests/hash.rs @@ -38,7 +38,7 @@ async fn saturn_v() -> anyhow::Result<()> { Ok(()) } -#[tokio::test] +#[tokio::test(flavor = "multi_thread")] async fn blake3() -> anyhow::Result<()> { let b3 = compute_blake3("tests//images/Moonlight.heic".into()).await?; assert_eq!( @@ -48,7 +48,7 @@ async fn blake3() -> anyhow::Result<()> { Ok(()) } -#[tokio::test] +#[tokio::test(flavor = "multi_thread")] async fn extra_hashes() -> anyhow::Result<()> { let extra_hashes = compute_extra_hashes("tests/images/Moonlight.heic".into()).await?; assert_eq!(