]> git.proxmox.com Git - proxmox-backup.git/blob - src/api2/reader/environment.rs
more api type cleanups: avoid re-exports
[proxmox-backup.git] / src / api2 / reader / environment.rs
1 use std::sync::{Arc,RwLock};
2 use std::collections::HashSet;
3
4 use serde_json::{json, Value};
5
6 use proxmox::api::{RpcEnvironment, RpcEnvironmentType};
7
8 use pbs_datastore::backup_info::BackupDir;
9 use pbs_api_types::Authid;
10
11 use crate::backup::DataStore;
12 use crate::server::formatter::*;
13 use crate::server::WorkerTask;
14
15 //use proxmox::tools;
16
17 /// `RpcEnvironmet` implementation for backup reader service
18 #[derive(Clone)]
19 pub struct ReaderEnvironment {
20 env_type: RpcEnvironmentType,
21 result_attributes: Value,
22 auth_id: Authid,
23 pub debug: bool,
24 pub formatter: &'static OutputFormatter,
25 pub worker: Arc<WorkerTask>,
26 pub datastore: Arc<DataStore>,
27 pub backup_dir: BackupDir,
28 allowed_chunks: Arc<RwLock<HashSet<[u8;32]>>>,
29 }
30
31 impl ReaderEnvironment {
32 pub fn new(
33 env_type: RpcEnvironmentType,
34 auth_id: Authid,
35 worker: Arc<WorkerTask>,
36 datastore: Arc<DataStore>,
37 backup_dir: BackupDir,
38 ) -> Self {
39
40
41 Self {
42 result_attributes: json!({}),
43 env_type,
44 auth_id,
45 worker,
46 datastore,
47 debug: false,
48 formatter: &JSON_FORMATTER,
49 backup_dir,
50 allowed_chunks: Arc::new(RwLock::new(HashSet::new())),
51 }
52 }
53
54 pub fn log<S: AsRef<str>>(&self, msg: S) {
55 self.worker.log(msg);
56 }
57
58 pub fn debug<S: AsRef<str>>(&self, msg: S) {
59 if self.debug { self.worker.log(msg); }
60 }
61
62
63 pub fn register_chunk(&self, digest: [u8;32]) {
64 let mut allowed_chunks = self.allowed_chunks.write().unwrap();
65 allowed_chunks.insert(digest);
66 }
67
68 pub fn check_chunk_access(&self, digest: [u8;32]) -> bool {
69 self.allowed_chunks.read().unwrap().contains(&digest)
70 }
71 }
72
73 impl RpcEnvironment for ReaderEnvironment {
74
75 fn result_attrib_mut(&mut self) -> &mut Value {
76 &mut self.result_attributes
77 }
78
79 fn result_attrib(&self) -> &Value {
80 &self.result_attributes
81 }
82
83 fn env_type(&self) -> RpcEnvironmentType {
84 self.env_type
85 }
86
87 fn set_auth_id(&mut self, _auth_id: Option<String>) {
88 panic!("unable to change auth_id");
89 }
90
91 fn get_auth_id(&self) -> Option<String> {
92 Some(self.auth_id.to_string())
93 }
94 }
95
96 impl AsRef<ReaderEnvironment> for dyn RpcEnvironment {
97 fn as_ref(&self) -> &ReaderEnvironment {
98 self.as_any().downcast_ref::<ReaderEnvironment>().unwrap()
99 }
100 }
101
102 impl AsRef<ReaderEnvironment> for Box<dyn RpcEnvironment> {
103 fn as_ref(&self) -> &ReaderEnvironment {
104 self.as_any().downcast_ref::<ReaderEnvironment>().unwrap()
105 }
106 }