2019-07-02 06:22:29 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-06-12 08:59:34 +00:00
|
|
|
use anyhow::Error;
|
|
|
|
|
|
|
|
use super::crypt_config::CryptConfig;
|
|
|
|
use super::data_blob::DataBlob;
|
|
|
|
use super::datastore::DataStore;
|
2019-07-02 06:22:29 +00:00
|
|
|
|
|
|
|
/// The ReadChunk trait allows reading backup data chunks (local or remote)
|
|
|
|
pub trait ReadChunk {
|
2020-01-02 12:29:10 +00:00
|
|
|
/// Returns the encoded chunk data
|
2020-06-12 08:59:34 +00:00
|
|
|
fn read_raw_chunk(&mut self, digest: &[u8; 32]) -> Result<DataBlob, Error>;
|
2020-01-02 12:29:10 +00:00
|
|
|
|
2019-07-02 06:22:29 +00:00
|
|
|
/// Returns the decoded chunk data
|
2020-06-12 08:59:34 +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 {
|
2020-06-12 08:59:34 +00:00
|
|
|
Self {
|
|
|
|
store,
|
|
|
|
crypt_config,
|
|
|
|
}
|
2019-07-02 06:22:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReadChunk for LocalChunkReader {
|
2020-06-12 08:59:34 +00:00
|
|
|
fn read_raw_chunk(&mut self, digest: &[u8; 32]) -> Result<DataBlob, 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);
|
2019-08-03 11:05:38 +00:00
|
|
|
let raw_data = proxmox::tools::fs::file_get_contents(&path)?;
|
2019-10-06 08:31:06 +00:00
|
|
|
let chunk = DataBlob::from_raw(raw_data)?;
|
2019-07-02 06:22:29 +00:00
|
|
|
chunk.verify_crc()?;
|
|
|
|
|
2020-01-02 12:29:10 +00:00
|
|
|
Ok(chunk)
|
|
|
|
}
|
|
|
|
|
2020-06-12 08:59:34 +00:00
|
|
|
fn read_chunk(&mut self, digest: &[u8; 32]) -> Result<Vec<u8>, Error> {
|
2020-01-02 12:29:10 +00:00
|
|
|
let chunk = self.read_raw_chunk(digest)?;
|
|
|
|
|
2020-06-12 08:59:34 +00:00
|
|
|
let raw_data = chunk.decode(self.crypt_config.as_ref().map(Arc::as_ref))?;
|
2019-10-06 08:31:06 +00:00
|
|
|
|
|
|
|
// fixme: verify digest?
|
2019-07-02 06:22:29 +00:00
|
|
|
|
|
|
|
Ok(raw_data)
|
|
|
|
}
|
|
|
|
}
|