]> git.proxmox.com Git - proxmox-backup.git/blob - src/backup/read_chunk.rs
src/backup/data_blob.rs: avoid Arc<CryptConfig>
[proxmox-backup.git] / src / backup / read_chunk.rs
1 use failure::*;
2 use std::sync::Arc;
3
4 use super::datastore::*;
5 use super::crypt_config::*;
6 use super::data_blob::*;
7
8 /// The ReadChunk trait allows reading backup data chunks (local or remote)
9 pub trait ReadChunk {
10 /// Returns the decoded chunk data
11 fn read_chunk(&mut self, digest:&[u8; 32]) -> Result<Vec<u8>, Error>;
12 }
13
14 pub struct LocalChunkReader {
15 store: Arc<DataStore>,
16 crypt_config: Option<Arc<CryptConfig>>,
17 }
18
19 impl LocalChunkReader {
20
21 pub fn new(store: Arc<DataStore>, crypt_config: Option<Arc<CryptConfig>>) -> Self {
22 Self { store, crypt_config }
23 }
24 }
25
26 impl ReadChunk for LocalChunkReader {
27
28 fn read_chunk(&mut self, digest:&[u8; 32]) -> Result<Vec<u8>, Error> {
29
30 let digest_str = proxmox::tools::digest_to_hex(digest);
31 println!("READ CHUNK {}", digest_str);
32
33 let (path, _) = self.store.chunk_path(digest);
34 let raw_data = proxmox::tools::fs::file_get_contents(&path)?;
35 let chunk = DataBlob::from_raw(raw_data)?;
36 chunk.verify_crc()?;
37
38 let raw_data = chunk.decode(self.crypt_config.as_ref().map(Arc::as_ref))?;
39
40 // fixme: verify digest?
41
42 Ok(raw_data)
43 }
44 }