src/backup/read_chunk.rs: add read_raw_chunk()

Allow to read chunk data without decoding.
This commit is contained in:
Dietmar Maurer 2020-01-02 13:29:10 +01:00
parent 2585a8a4e2
commit 3758b398cd
2 changed files with 26 additions and 10 deletions

View File

@ -7,6 +7,9 @@ use super::data_blob::*;
/// The ReadChunk trait allows reading backup data chunks (local or remote)
pub trait ReadChunk {
/// Returns the encoded chunk data
fn read_raw_chunk(&mut self, digest:&[u8; 32]) -> Result<DataBlob, Error>;
/// Returns the decoded chunk data
fn read_chunk(&mut self, digest:&[u8; 32]) -> Result<Vec<u8>, Error>;
}
@ -25,7 +28,7 @@ impl LocalChunkReader {
impl ReadChunk for LocalChunkReader {
fn read_chunk(&mut self, digest:&[u8; 32]) -> Result<Vec<u8>, Error> {
fn read_raw_chunk(&mut self, digest:&[u8; 32]) -> Result<DataBlob, Error> {
let digest_str = proxmox::tools::digest_to_hex(digest);
println!("READ CHUNK {}", digest_str);
@ -35,6 +38,12 @@ impl ReadChunk for LocalChunkReader {
let chunk = DataBlob::from_raw(raw_data)?;
chunk.verify_crc()?;
Ok(chunk)
}
fn read_chunk(&mut self, digest:&[u8; 32]) -> Result<Vec<u8>, Error> {
let chunk = self.read_raw_chunk(digest)?;
let raw_data = chunk.decode(self.crypt_config.as_ref().map(Arc::as_ref))?;
// fixme: verify digest?

View File

@ -31,29 +31,36 @@ impl RemoteChunkReader {
impl ReadChunk for RemoteChunkReader {
fn read_chunk(&mut self, digest:&[u8; 32]) -> Result<Vec<u8>, Error> {
fn read_raw_chunk(&mut self, digest:&[u8; 32]) -> Result<DataBlob, Error> {
let mut chunk_data = Vec::with_capacity(4*1024*1024);
if let Some(raw_data) = self.cache.get(digest) {
return Ok(raw_data.to_vec());
}
let use_cache = self.cache_hint.contains_key(digest);
futures::executor::block_on(self.client.download_chunk(&digest, &mut chunk_data))?;
let chunk = DataBlob::from_raw(chunk_data)?;
chunk.verify_crc()?;
Ok(chunk)
}
fn read_chunk(&mut self, digest:&[u8; 32]) -> Result<Vec<u8>, Error> {
if let Some(raw_data) = self.cache.get(digest) {
return Ok(raw_data.to_vec());
}
let chunk = self.read_raw_chunk(digest)?;
let raw_data = chunk.decode(self.crypt_config.as_ref().map(Arc::as_ref))?;
// fixme: verify chunk digest
// fixme: verify digest?
let use_cache = self.cache_hint.contains_key(digest);
if use_cache {
self.cache.insert(*digest, raw_data.to_vec());
}
Ok(raw_data)
}
}