]> git.proxmox.com Git - proxmox-backup.git/blob - src/backup/read_chunk.rs
9662ea8ea904b78525f02054442f3851c67dd26b
[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 encoded chunk data
11 fn read_raw_chunk(&mut self, digest:&[u8; 32]) -> Result<DataBlob, Error>;
12
13 /// Returns the decoded chunk data
14 fn read_chunk(&mut self, digest:&[u8; 32]) -> Result<Vec<u8>, Error>;
15 }
16
17 pub struct LocalChunkReader {
18 store: Arc<DataStore>,
19 crypt_config: Option<Arc<CryptConfig>>,
20 }
21
22 impl LocalChunkReader {
23
24 pub fn new(store: Arc<DataStore>, crypt_config: Option<Arc<CryptConfig>>) -> Self {
25 Self { store, crypt_config }
26 }
27 }
28
29 impl ReadChunk for LocalChunkReader {
30
31 fn read_raw_chunk(&mut self, digest:&[u8; 32]) -> Result<DataBlob, Error> {
32
33 let digest_str = proxmox::tools::digest_to_hex(digest);
34 println!("READ CHUNK {}", digest_str);
35
36 let (path, _) = self.store.chunk_path(digest);
37 let raw_data = proxmox::tools::fs::file_get_contents(&path)?;
38 let chunk = DataBlob::from_raw(raw_data)?;
39 chunk.verify_crc()?;
40
41 Ok(chunk)
42 }
43
44 fn read_chunk(&mut self, digest:&[u8; 32]) -> Result<Vec<u8>, Error> {
45 let chunk = self.read_raw_chunk(digest)?;
46
47 let raw_data = chunk.decode(self.crypt_config.as_ref().map(Arc::as_ref))?;
48
49 // fixme: verify digest?
50
51 Ok(raw_data)
52 }
53 }