2019-07-02 06:22:29 +00:00
|
|
|
use failure::*;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use super::datastore::*;
|
|
|
|
use super::crypt_config::*;
|
|
|
|
use super::data_chunk::*;
|
|
|
|
|
|
|
|
/// The ReadChunk trait allows reading backup data chunks (local or remote)
|
|
|
|
pub trait ReadChunk {
|
|
|
|
/// Returns the decoded chunk data
|
2019-07-03 12:25:18 +00:00
|
|
|
fn read_chunk(&mut self, digest:&[u8; 32]) -> Result<Vec<u8>, Error>;
|
2019-07-02 06:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LocalChunkReader {
|
2019-07-02 06:49:16 +00:00
|
|
|
store: Arc<DataStore>,
|
2019-07-02 06:22:29 +00:00
|
|
|
crypt_config: Option<Arc<CryptConfig>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalChunkReader {
|
|
|
|
|
2019-07-02 06:49:16 +00:00
|
|
|
pub fn new(store: Arc<DataStore>, crypt_config: Option<Arc<CryptConfig>>) -> Self {
|
2019-07-02 06:22:29 +00:00
|
|
|
Self { store, crypt_config }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReadChunk for LocalChunkReader {
|
|
|
|
|
2019-07-03 12:25:18 +00:00
|
|
|
fn read_chunk(&mut self, digest:&[u8; 32]) -> Result<Vec<u8>, Error> {
|
2019-07-02 06:22:29 +00:00
|
|
|
|
|
|
|
let digest_str = proxmox::tools::digest_to_hex(digest);
|
|
|
|
println!("READ CHUNK {}", digest_str);
|
|
|
|
|
|
|
|
let (path, _) = self.store.chunk_path(digest);
|
|
|
|
let raw_data = crate::tools::file_get_contents(&path)?;
|
|
|
|
let chunk = DataChunk::from_raw(raw_data, *digest)?;
|
|
|
|
chunk.verify_crc()?;
|
|
|
|
|
|
|
|
let raw_data = match self.crypt_config {
|
|
|
|
Some(ref crypt_config) => chunk.decode(Some(crypt_config))?,
|
|
|
|
None => chunk.decode(None)?,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(raw_data)
|
|
|
|
}
|
|
|
|
}
|