2020-06-12 09:38:21 +00:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
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-07-03 05:36:23 +00:00
|
|
|
fn read_raw_chunk(&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-07-03 05:36:23 +00:00
|
|
|
fn read_chunk(&self, digest: &[u8; 32]) -> Result<Vec<u8>, Error>;
|
2019-07-02 06:22:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 10:09:50 +00:00
|
|
|
#[derive(Clone)]
|
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-07-03 05:36:23 +00:00
|
|
|
fn read_raw_chunk(&self, digest: &[u8; 32]) -> Result<DataBlob, Error> {
|
2020-07-28 08:23:16 +00:00
|
|
|
self.store.load_chunk(digest)
|
2020-01-02 12:29:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 05:36:23 +00:00
|
|
|
fn read_chunk(&self, digest: &[u8; 32]) -> Result<Vec<u8>, Error> {
|
2020-06-18 11:55:26 +00:00
|
|
|
let chunk = ReadChunk::read_raw_chunk(self, digest)?;
|
2020-01-02 12:29:10 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2020-06-12 09:38:21 +00:00
|
|
|
|
2020-06-16 07:50:29 +00:00
|
|
|
pub trait AsyncReadChunk: Send {
|
2020-06-12 09:38:21 +00:00
|
|
|
/// Returns the encoded chunk data
|
|
|
|
fn read_raw_chunk<'a>(
|
2020-07-03 05:36:23 +00:00
|
|
|
&'a self,
|
2020-06-12 09:38:21 +00:00
|
|
|
digest: &'a [u8; 32],
|
|
|
|
) -> Pin<Box<dyn Future<Output = Result<DataBlob, Error>> + Send + 'a>>;
|
|
|
|
|
|
|
|
/// Returns the decoded chunk data
|
|
|
|
fn read_chunk<'a>(
|
2020-07-03 05:36:23 +00:00
|
|
|
&'a self,
|
2020-06-12 09:38:21 +00:00
|
|
|
digest: &'a [u8; 32],
|
|
|
|
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, Error>> + Send + 'a>>;
|
|
|
|
}
|
2020-06-18 11:55:26 +00:00
|
|
|
|
|
|
|
impl AsyncReadChunk for LocalChunkReader {
|
|
|
|
fn read_raw_chunk<'a>(
|
2020-07-03 05:36:23 +00:00
|
|
|
&'a self,
|
2020-06-18 11:55:26 +00:00
|
|
|
digest: &'a [u8; 32],
|
|
|
|
) -> Pin<Box<dyn Future<Output = Result<DataBlob, Error>> + Send + 'a>> {
|
|
|
|
Box::pin(async move{
|
|
|
|
let (path, _) = self.store.chunk_path(digest);
|
|
|
|
|
|
|
|
let raw_data = tokio::fs::read(&path).await?;
|
|
|
|
|
2020-07-28 08:23:16 +00:00
|
|
|
let chunk = DataBlob::load_from_reader(&mut &raw_data[..])?;
|
|
|
|
|
2020-06-18 11:55:26 +00:00
|
|
|
Ok(chunk)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_chunk<'a>(
|
2020-07-03 05:36:23 +00:00
|
|
|
&'a self,
|
2020-06-18 11:55:26 +00:00
|
|
|
digest: &'a [u8; 32],
|
|
|
|
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, Error>> + Send + 'a>> {
|
|
|
|
Box::pin(async move {
|
|
|
|
let chunk = AsyncReadChunk::read_raw_chunk(self, digest).await?;
|
|
|
|
|
|
|
|
let raw_data = chunk.decode(self.crypt_config.as_ref().map(Arc::as_ref))?;
|
|
|
|
|
|
|
|
// fixme: verify digest?
|
|
|
|
|
|
|
|
Ok(raw_data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|