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-08-10 11:25:07 +00:00
|
|
|
use anyhow::{bail, Error};
|
2020-06-12 08:59:34 +00:00
|
|
|
|
2021-07-08 07:17:28 +00:00
|
|
|
use pbs_datastore::crypt_config::{CryptConfig, CryptMode};
|
|
|
|
use pbs_datastore::data_blob::DataBlob;
|
|
|
|
use pbs_datastore::read_chunk::{ReadChunk, AsyncReadChunk};
|
2020-01-02 12:29:10 +00:00
|
|
|
|
2021-07-08 07:17:28 +00:00
|
|
|
use super::datastore::DataStore;
|
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>>,
|
2020-08-10 11:25:07 +00:00
|
|
|
crypt_mode: CryptMode,
|
2019-07-02 06:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalChunkReader {
|
2020-08-10 11:25:07 +00:00
|
|
|
pub fn new(store: Arc<DataStore>, crypt_config: Option<Arc<CryptConfig>>, crypt_mode: CryptMode) -> Self {
|
2020-06-12 08:59:34 +00:00
|
|
|
Self {
|
|
|
|
store,
|
|
|
|
crypt_config,
|
2020-08-10 11:25:07 +00:00
|
|
|
crypt_mode,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ensure_crypt_mode(&self, chunk_mode: CryptMode) -> Result<(), Error> {
|
|
|
|
match self.crypt_mode {
|
|
|
|
CryptMode::Encrypt => {
|
|
|
|
match chunk_mode {
|
|
|
|
CryptMode::Encrypt => Ok(()),
|
|
|
|
CryptMode::SignOnly | CryptMode::None => bail!("Index and chunk CryptMode don't match."),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
CryptMode::SignOnly | CryptMode::None => {
|
|
|
|
match chunk_mode {
|
|
|
|
CryptMode::Encrypt => bail!("Index and chunk CryptMode don't match."),
|
|
|
|
CryptMode::SignOnly | CryptMode::None => Ok(()),
|
|
|
|
}
|
|
|
|
},
|
2020-06-12 08:59:34 +00:00
|
|
|
}
|
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-08-10 11:25:07 +00:00
|
|
|
let chunk = self.store.load_chunk(digest)?;
|
|
|
|
self.ensure_crypt_mode(chunk.crypt_mode()?)?;
|
|
|
|
Ok(chunk)
|
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-08-03 12:10:43 +00:00
|
|
|
let raw_data = chunk.decode(self.crypt_config.as_ref().map(Arc::as_ref), Some(digest))?;
|
2019-07-02 06:22:29 +00:00
|
|
|
|
|
|
|
Ok(raw_data)
|
|
|
|
}
|
|
|
|
}
|
2020-06-12 09:38:21 +00:00
|
|
|
|
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-08-10 11:25:07 +00:00
|
|
|
self.ensure_crypt_mode(chunk.crypt_mode()?)?;
|
|
|
|
|
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?;
|
|
|
|
|
2020-08-03 12:10:43 +00:00
|
|
|
let raw_data = chunk.decode(self.crypt_config.as_ref().map(Arc::as_ref), Some(digest))?;
|
2020-06-18 11:55:26 +00:00
|
|
|
|
|
|
|
// fixme: verify digest?
|
|
|
|
|
|
|
|
Ok(raw_data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|