Skip to content

Commit

Permalink
unify compute_blake3 and compute_extra_hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
chadaustin committed Dec 19, 2024
1 parent 6f334ba commit d3f98b0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 55 deletions.
60 changes: 7 additions & 53 deletions src/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,62 +317,16 @@ pub async fn compute_content_hashes(
}

pub async fn compute_blake3(path: PathBuf) -> anyhow::Result<Hash32> {
// 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<ExtraHashes> {
// 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)]
Expand Down
4 changes: 2 additions & 2 deletions tests/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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!(
Expand Down

0 comments on commit d3f98b0

Please sign in to comment.