src/backup/index.rs: add compute_csum

And use it for fixed and dynamic index. Please note that this
changes checksums for fixed indexes, so restore older backups
will fails now (not backward compatible).
This commit is contained in:
Dietmar Maurer 2020-06-26 09:00:34 +02:00
parent 19b5c3c43e
commit 1f82f9b7b5
3 changed files with 15 additions and 42 deletions

View File

@ -141,24 +141,6 @@ impl DynamicIndexReader {
&self.index[pos].digest &self.index[pos].digest
} }
/// Compute checksum and data size
pub fn compute_csum(&self) -> ([u8; 32], u64) {
let mut csum = openssl::sha::Sha256::new();
for entry in &self.index {
csum.update(&entry.end_le.to_ne_bytes());
csum.update(&entry.digest);
}
let csum = csum.finish();
(
csum,
self.index
.last()
.map(|entry| entry.end())
.unwrap_or(0)
)
}
// TODO: can we use std::slice::binary_search with Mmap now? // TODO: can we use std::slice::binary_search with Mmap now?
fn binary_search( fn binary_search(
&self, &self,

View File

@ -1,5 +1,4 @@
use anyhow::{bail, format_err, Error}; use anyhow::{bail, format_err, Error};
use std::convert::TryInto;
use std::io::{Seek, SeekFrom}; use std::io::{Seek, SeekFrom};
use super::chunk_stat::*; use super::chunk_stat::*;
@ -147,15 +146,6 @@ impl FixedIndexReader {
Ok(()) Ok(())
} }
#[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] #[inline]
fn chunk_end(&self, pos: usize) -> u64 { fn chunk_end(&self, pos: usize) -> u64 {
if pos >= self.index_length { if pos >= self.index_length {
@ -170,20 +160,6 @@ 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 = self.chunk_end(pos);
let digest = self.chunk_digest(pos);
csum.update(digest);
}
let csum = csum.finish();
(csum, chunk_end)
}
pub fn print_info(&self) { pub fn print_info(&self) {
println!("Size: {}", self.size); println!("Size: {}", self.size);
println!("ChunkSize: {}", self.chunk_size); println!("ChunkSize: {}", self.chunk_size);

View File

@ -22,6 +22,21 @@ pub trait IndexFile {
fn index_bytes(&self) -> u64; fn index_bytes(&self) -> u64;
fn chunk_info(&self, pos: usize) -> Option<ChunkReadInfo>; fn chunk_info(&self, pos: usize) -> Option<ChunkReadInfo>;
/// Compute index checksum and size
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_count() {
let info = self.chunk_info(pos).unwrap();
chunk_end = info.range.end;
csum.update(&chunk_end.to_le_bytes());
csum.update(&info.digest);
}
let csum = csum.finish();
(csum, chunk_end)
}
/// Returns most often used chunks /// Returns most often used chunks
fn find_most_used_chunks(&self, max: usize) -> HashMap<[u8; 32], usize> { fn find_most_used_chunks(&self, max: usize) -> HashMap<[u8; 32], usize> {
let mut map = HashMap::new(); let mut map = HashMap::new();