1 use std
::sync
::{Arc,RwLock}
;
2 use std
::collections
::HashSet
;
4 use serde_json
::{json, Value}
;
6 use proxmox
::api
::{RpcEnvironment, RpcEnvironmentType}
;
8 use pbs_datastore
::backup_info
::BackupDir
;
9 use pbs_api_types
::Authid
;
10 use proxmox_rest_server
::formatter
::*;
12 use crate::backup
::DataStore
;
13 use proxmox_rest_server
::WorkerTask
;
17 /// `RpcEnvironmet` implementation for backup reader service
19 pub struct ReaderEnvironment
{
20 env_type
: RpcEnvironmentType
,
21 result_attributes
: Value
,
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]>>>,
31 impl ReaderEnvironment
{
33 env_type
: RpcEnvironmentType
,
35 worker
: Arc
<WorkerTask
>,
36 datastore
: Arc
<DataStore
>,
37 backup_dir
: BackupDir
,
42 result_attributes
: json
!({}
),
48 formatter
: &JSON_FORMATTER
,
50 allowed_chunks
: Arc
::new(RwLock
::new(HashSet
::new())),
54 pub fn log
<S
: AsRef
<str>>(&self, msg
: S
) {
55 self.worker
.log_message(msg
);
58 pub fn debug
<S
: AsRef
<str>>(&self, msg
: S
) {
59 if self.debug { self.worker.log_message(msg); }
63 pub fn register_chunk(&self, digest
: [u8;32]) {
64 let mut allowed_chunks
= self.allowed_chunks
.write().unwrap();
65 allowed_chunks
.insert(digest
);
68 pub fn check_chunk_access(&self, digest
: [u8;32]) -> bool
{
69 self.allowed_chunks
.read().unwrap().contains(&digest
)
73 impl RpcEnvironment
for ReaderEnvironment
{
75 fn result_attrib_mut(&mut self) -> &mut Value
{
76 &mut self.result_attributes
79 fn result_attrib(&self) -> &Value
{
80 &self.result_attributes
83 fn env_type(&self) -> RpcEnvironmentType
{
87 fn set_auth_id(&mut self, _auth_id
: Option
<String
>) {
88 panic
!("unable to change auth_id");
91 fn get_auth_id(&self) -> Option
<String
> {
92 Some(self.auth_id
.to_string())
96 impl AsRef
<ReaderEnvironment
> for dyn RpcEnvironment
{
97 fn as_ref(&self) -> &ReaderEnvironment
{
98 self.as_any().downcast_ref
::<ReaderEnvironment
>().unwrap()
102 impl AsRef
<ReaderEnvironment
> for Box
<dyn RpcEnvironment
> {
103 fn as_ref(&self) -> &ReaderEnvironment
{
104 self.as_any().downcast_ref
::<ReaderEnvironment
>().unwrap()