]> git.proxmox.com Git - proxmox-backup.git/commitdiff
src/backup/fixed_index.rs: new helper to compute checksum and file size
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 3 Sep 2019 11:11:45 +0000 (13:11 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 3 Sep 2019 11:11:45 +0000 (13:11 +0200)
src/backup/fixed_index.rs

index 61e0a058981c3ad034d7f76a94a9ecdd782e0277..4c1d18e37b6a98035c55773852722f80ef5ec33c 100644 (file)
@@ -1,5 +1,6 @@
 use failure::*;
 use std::io::{Seek, SeekFrom};
+use std::convert::TryInto;
 
 use crate::tools;
 use super::IndexFile;
@@ -166,6 +167,15 @@ impl FixedIndexReader {
         Ok((start, end, unsafe { digest.assume_init() }))
     }
 
+    #[inline]
+    fn chunk_digest(&self, pos: usize) -> &[u8; 32] {
+        if pos >= self.index_length {
+            panic!("chunk index out of range");
+        }
+        let slice = unsafe { std::slice::from_raw_parts(self.index.add(pos*32), 32) };
+        slice.try_into().unwrap()
+    }
+
     #[inline]
     fn chunk_end(&self, pos: usize) -> u64 {
         if pos >= self.index_length {
@@ -180,6 +190,22 @@ impl FixedIndexReader {
         }
     }
 
+    /// Compute checksum and data size
+    pub fn compute_csum(&self) -> ([u8; 32], u64) {
+
+        let mut csum = openssl::sha::Sha256::new();
+        let mut chunk_end = 0;
+        for pos in 0..self.index_length {
+            chunk_end = ((pos+1) * self.chunk_size) as u64;
+            let digest = self.chunk_digest(pos);
+            csum.update(&chunk_end.to_le_bytes());
+            csum.update(digest);
+        }
+        let csum = csum.finish();
+
+        (csum, chunk_end)
+    }
+
     pub fn print_info(&self) {
         println!("Size: {}", self.size);
         println!("ChunkSize: {}", self.chunk_size);