From b30fd5da992fb6a183db386373b6f23290a2d6a9 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 1 Feb 2021 10:10:13 +0100 Subject: Make source verification completely async Signed-off-by: Matthias Beyer --- src/package/source.rs | 34 ++++++++++++++++++++++++++-------- src/source/mod.rs | 19 +++++++++---------- 2 files changed, 35 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/package/source.rs b/src/package/source.rs index d60444a..773b0a5 100644 --- a/src/package/source.rs +++ b/src/package/source.rs @@ -8,8 +8,6 @@ // SPDX-License-Identifier: EPL-2.0 // -use std::io::Read; - use anyhow::anyhow; use anyhow::Result; use getset::Getters; @@ -52,9 +50,9 @@ pub struct SourceHash { } impl SourceHash { - pub fn matches_hash_of(&self, reader: R) -> Result<()> { + pub async fn matches_hash_of(&self, reader: R) -> Result<()> { trace!("Hashing buffer with: {:?}", self.hashtype); - let h = self.hashtype.hash_from_reader(reader)?; + let h = self.hashtype.hash_from_reader(reader).await?; trace!("Hashing buffer with: {} finished", self.hashtype); if h == self.value { @@ -92,7 +90,9 @@ pub enum HashType { } impl HashType { - fn hash_from_reader(&self, mut reader: R) -> Result { + async fn hash_from_reader(&self, mut reader: R) -> Result { + use tokio::io::AsyncReadExt; + let mut buffer = [0; 1024]; match self { @@ -100,10 +100,16 @@ impl HashType { trace!("SHA1 hashing buffer"); let mut m = sha1::Sha1::new(); loop { - let count = reader.read(&mut buffer)?; + trace!("Reading"); + let count = reader.read(&mut buffer).await?; + trace!("Read {} bytes", count); + if count == 0 { + trace!("ready"); break; } + + trace!("Updating buffer"); m.update(&buffer[..count]); } Ok(HashValue(m.digest().to_string())) @@ -112,10 +118,16 @@ impl HashType { trace!("SHA256 hashing buffer"); let mut m = sha2::Sha256::new(); loop { - let count = reader.read(&mut buffer)?; + trace!("Reading"); + let count = reader.read(&mut buffer).await?; + trace!("Read {} bytes", count); + if count == 0 { + trace!("ready"); break; } + + trace!("Updating buffer"); m.update(&buffer[..count]); } Ok(HashValue(String::from_utf8(m.finalize()[..].to_vec())?)) @@ -124,10 +136,16 @@ impl HashType { trace!("SHA512 hashing buffer"); let mut m = sha2::Sha512::new(); loop { - let count = reader.read(&mut buffer)?; + trace!("Reading"); + let count = reader.read(&mut buffer).await?; + trace!("Read {} bytes", count); + if count == 0 { + trace!("ready"); break; } + + trace!("Updating buffer"); m.update(&buffer[..count]); } Ok(HashValue(String::from_utf8(m.finalize()[..].to_vec())?)) diff --git a/src/source/mod.rs b/src/source/mod.rs index 1c97a87..bebd68a 100644 --- a/src/source/mod.rs +++ b/src/source/mod.rs @@ -101,20 +101,19 @@ impl SourceEntry { let p = self.source_file_path(); trace!("Verifying : {}", p.display()); - let path = p.clone(); - let reader = tokio::task::spawn_blocking(move || { - std::fs::OpenOptions::new() - .create(false) - .create_new(false) - .read(true) - .open(path) - .map(std::io::BufReader::new) - }) - .await??; + let reader = tokio::fs::OpenOptions::new() + .create(false) + .create_new(false) + .read(true) + .open(&p) + .await + .map(tokio::io::BufReader::new)?; + trace!("Reader constructed for path: {}", p.display()); self.package_source .hash() .matches_hash_of(reader) + .await } pub async fn create(&self) -> Result { -- cgit v1.2.3