]> git.proxmox.com Git - proxmox-backup.git/blob - src/client/remote_chunk_reader.rs
src/backup/data_blob.rs: avoid Arc<CryptConfig>
[proxmox-backup.git] / src / client / remote_chunk_reader.rs
1 use std::collections::HashMap;
2 use std::sync::Arc;
3
4 use failure::*;
5
6 use super::BackupReader;
7 use crate::backup::{ReadChunk, DataBlob, CryptConfig};
8
9 /// Read chunks from remote host using ``BackupReader``
10 pub struct RemoteChunkReader {
11 client: Arc<BackupReader>,
12 crypt_config: Option<Arc<CryptConfig>>,
13 cache_hint: HashMap<[u8; 32], usize>,
14 cache: HashMap<[u8; 32], Vec<u8>>,
15 }
16
17 impl RemoteChunkReader {
18
19 /// Create a new instance.
20 ///
21 /// Chunks listed in ``cache_hint`` are cached and kept in RAM.
22 pub fn new(
23 client: Arc<BackupReader>,
24 crypt_config: Option<Arc<CryptConfig>>,
25 cache_hint: HashMap<[u8; 32], usize>,
26 ) -> Self {
27
28 Self { client, crypt_config, cache_hint, cache: HashMap::new() }
29 }
30 }
31
32 impl ReadChunk for RemoteChunkReader {
33
34 fn read_chunk(&mut self, digest:&[u8; 32]) -> Result<Vec<u8>, Error> {
35
36 let mut chunk_data = Vec::with_capacity(4*1024*1024);
37
38 if let Some(raw_data) = self.cache.get(digest) {
39 return Ok(raw_data.to_vec());
40 }
41
42 let use_cache = self.cache_hint.contains_key(digest);
43
44 futures::executor::block_on(self.client.download_chunk(&digest, &mut chunk_data))?;
45
46 let chunk = DataBlob::from_raw(chunk_data)?;
47 chunk.verify_crc()?;
48
49 let raw_data = chunk.decode(self.crypt_config.as_ref().map(Arc::as_ref))?;
50
51 // fixme: verify chunk digest
52
53 if use_cache {
54 self.cache.insert(*digest, raw_data.to_vec());
55 }
56
57 Ok(raw_data)
58 }
59 }