From 9ac6ec868aaa4412a01ae4b70bf3b2c6d67276e2 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Fri, 22 Feb 2019 10:35:40 +0100 Subject: [PATCH] backup/chunk_store: split insert_chunk The protocol handler will receive chunk data plus a hash pre-calculated by the client. It will verify the hash before sending it up to the datastore in order to respond to the client with an error on a mismatch, so there's no need to recalculate the hash another time. Signed-off-by: Wolfgang Bumiller --- src/backup/chunk_store.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/backup/chunk_store.rs b/src/backup/chunk_store.rs index 9a95e8ca..b5c32903 100644 --- a/src/backup/chunk_store.rs +++ b/src/backup/chunk_store.rs @@ -298,24 +298,30 @@ impl ChunkStore { pub fn insert_chunk(&self, chunk: &[u8]) -> Result<(bool, [u8; 32], u64), Error> { // fixme: use Sha512/256 when available - let mut hasher = sha::Sha256::new(); - hasher.update(chunk); + let digest = sha::sha256(chunk); + let (new, csize) = self.insert_chunk_noverify(&digest, chunk)?; + Ok((new, digest, csize)) + } - let digest = hasher.finish(); + pub fn insert_chunk_noverify( + &self, + digest: &[u8; 32], + chunk: &[u8], + ) -> Result<(bool, u64), Error> { //println!("DIGEST {}", tools::digest_to_hex(&digest)); let mut chunk_path = self.chunk_dir.clone(); - let prefix = digest_to_prefix(&digest); + let prefix = digest_to_prefix(digest); chunk_path.push(&prefix); - let digest_str = tools::digest_to_hex(&digest); + let digest_str = tools::digest_to_hex(digest); chunk_path.push(&digest_str); let lock = self.mutex.lock(); if let Ok(metadata) = std::fs::metadata(&chunk_path) { if metadata.is_file() { - return Ok((true, digest, metadata.len())); + return Ok((true, metadata.len())); } else { bail!("Got unexpected file type on store '{}' for chunk {}", self.name, digest_str); } @@ -351,7 +357,7 @@ impl ChunkStore { drop(lock); - Ok((false, digest, compressed_size)) + Ok((false, compressed_size)) } pub fn relative_path(&self, path: &Path) -> PathBuf {