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 crate::api2
::types
::Userid
;
10 use crate::server
::formatter
::*;
11 use crate::server
::WorkerTask
;
15 /// `RpcEnvironmet` implementation for backup reader service
17 pub struct ReaderEnvironment
{
18 env_type
: RpcEnvironmentType
,
19 result_attributes
: Value
,
22 pub formatter
: &'
static OutputFormatter
,
23 pub worker
: Arc
<WorkerTask
>,
24 pub datastore
: Arc
<DataStore
>,
25 pub backup_dir
: BackupDir
,
26 allowed_chunks
: Arc
<RwLock
<HashSet
<[u8;32]>>>,
29 impl ReaderEnvironment
{
31 env_type
: RpcEnvironmentType
,
33 worker
: Arc
<WorkerTask
>,
34 datastore
: Arc
<DataStore
>,
35 backup_dir
: BackupDir
,
40 result_attributes
: json
!({}
),
46 formatter
: &JSON_FORMATTER
,
48 allowed_chunks
: Arc
::new(RwLock
::new(HashSet
::new())),
52 pub fn log
<S
: AsRef
<str>>(&self, msg
: S
) {
56 pub fn debug
<S
: AsRef
<str>>(&self, msg
: S
) {
57 if self.debug { self.worker.log(msg); }
61 pub fn register_chunk(&self, digest
: [u8;32]) {
62 let mut allowed_chunks
= self.allowed_chunks
.write().unwrap();
63 allowed_chunks
.insert(digest
);
66 pub fn check_chunk_access(&self, digest
: [u8;32]) -> bool
{
67 self.allowed_chunks
.read().unwrap().contains(&digest
)
71 impl RpcEnvironment
for ReaderEnvironment
{
73 fn result_attrib_mut(&mut self) -> &mut Value
{
74 &mut self.result_attributes
77 fn result_attrib(&self) -> &Value
{
78 &self.result_attributes
81 fn env_type(&self) -> RpcEnvironmentType
{
85 fn set_user(&mut self, _user
: Option
<String
>) {
86 panic
!("unable to change user");
89 fn get_user(&self) -> Option
<String
> {
90 Some(self.user
.to_string())
94 impl AsRef
<ReaderEnvironment
> for dyn RpcEnvironment
{
95 fn as_ref(&self) -> &ReaderEnvironment
{
96 self.as_any().downcast_ref
::<ReaderEnvironment
>().unwrap()
100 impl AsRef
<ReaderEnvironment
> for Box
<dyn RpcEnvironment
> {
101 fn as_ref(&self) -> &ReaderEnvironment
{
102 self.as_any().downcast_ref
::<ReaderEnvironment
>().unwrap()