src/backup/fixed_index.rs: new helper to compute checksum and file size
This commit is contained in:
		@ -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);
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user