Skip to content

Commit

Permalink
Fix VersionedChecksummedBlobWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
cswinter committed May 5, 2024
1 parent 547d84f commit a2f6838
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/disk_store/file_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl BlobWriter for VersionedChecksummedBlobWriter {
path: &Path,
data: &[u8],
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let mut wrapped_data = Vec::<u8>::with_capacity(8 + 8 + 256 + data.len());
let mut wrapped_data = Vec::<u8>::with_capacity(8 + 8 + 32 + data.len());
// Version number
wrapped_data.extend(0u64.to_be_bytes().iter());
// Data length
Expand All @@ -40,13 +40,15 @@ impl BlobWriter for VersionedChecksummedBlobWriter {
hasher.update(data);
let checksum = hasher.finalize();
wrapped_data.extend(checksum.iter());
// Data
wrapped_data.extend(data);

self.writer.store(path, &wrapped_data)
}

fn load(&self, path: &Path) -> Result<Vec<u8>, Box<dyn Error + Send + Sync + 'static>> {
let data = self.writer.load(path)?;
if data.len() < 8 + 8 + 256 {
if data.len() < 8 + 8 + 32 {
return Err(format!("Invalid data length for {:?}: {}", path, data.len()).into());
}
let version = u64::from_be_bytes([
Expand All @@ -58,14 +60,14 @@ impl BlobWriter for VersionedChecksummedBlobWriter {
let data_len = usize::from_be_bytes([
data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15],
]);
if data.len() != 8 + 8 + 256 + data_len {
return Err(format!("Invalid data length for {:?}: {}", path, data.len()).into());
if data.len() != 8 + 8 + 32 + data_len {
return Err(format!("Invalid data length for {:?}: {}, expected {}", path, data.len(), data_len).into());
}
let checksum = &data[16..16 + 256];
let checksum = &data[16..16 + 32];
let actual_checksum = {
use sha2::{Digest, Sha256};
let mut hasher = Sha256::new();
hasher.update(&data[16 + 256..]);
hasher.update(&data[16 + 32..]);
hasher.finalize()
};
if checksum != actual_checksum.as_slice() {
Expand All @@ -74,12 +76,12 @@ impl BlobWriter for VersionedChecksummedBlobWriter {
checksum_hex.push_str(&format!("{:02x}", byte));
}
return Err(format!(
"Checksum mismatch for {:?}: expected {}, actual {:0512x}",
"Checksum mismatch for {:?}: expected {}, actual {:064x}",
path, checksum_hex, actual_checksum
)
.into());
}
Ok(data[16 + 256..].to_vec())
Ok(data[16 + 32..].to_vec())
}

fn delete(&self, path: &Path) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
Expand Down

0 comments on commit a2f6838

Please sign in to comment.