1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use {
crate::{
error::{DebianError, Result},
io::ContentDigest,
repository::{
RepositoryPathVerification, RepositoryPathVerificationState, RepositoryWrite,
RepositoryWriter,
},
},
async_trait::async_trait,
futures::AsyncRead,
std::{borrow::Cow, pin::Pin, sync::Mutex},
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ProxyVerifyBehavior {
Proxy,
AlwaysExistsNoIntegrityCheck,
AlwaysExistsIntegrityVerified,
AlwaysExistsIntegrityMismatch,
AlwaysMissing,
}
pub struct ProxyWriter<W> {
inner: W,
verify_behavior: ProxyVerifyBehavior,
path_writes: Mutex<Vec<String>>,
}
impl<W: RepositoryWriter + Send> ProxyWriter<W> {
pub fn new(writer: W) -> Self {
Self {
inner: writer,
verify_behavior: ProxyVerifyBehavior::Proxy,
path_writes: Mutex::new(vec![]),
}
}
pub fn into_inner(self) -> W {
self.inner
}
pub fn set_verify_behavior(&mut self, behavior: ProxyVerifyBehavior) {
self.verify_behavior = behavior;
}
}
#[async_trait]
impl<W: RepositoryWriter + Send> RepositoryWriter for ProxyWriter<W> {
async fn verify_path<'path>(
&self,
path: &'path str,
expected_content: Option<(u64, ContentDigest)>,
) -> Result<RepositoryPathVerification<'path>> {
match self.verify_behavior {
ProxyVerifyBehavior::Proxy => self.inner.verify_path(path, expected_content).await,
ProxyVerifyBehavior::AlwaysExistsIntegrityVerified => Ok(RepositoryPathVerification {
path,
state: RepositoryPathVerificationState::ExistsIntegrityVerified,
}),
ProxyVerifyBehavior::AlwaysExistsNoIntegrityCheck => Ok(RepositoryPathVerification {
path,
state: RepositoryPathVerificationState::ExistsNoIntegrityCheck,
}),
ProxyVerifyBehavior::AlwaysExistsIntegrityMismatch => Ok(RepositoryPathVerification {
path,
state: RepositoryPathVerificationState::ExistsIntegrityMismatch,
}),
ProxyVerifyBehavior::AlwaysMissing => Ok(RepositoryPathVerification {
path,
state: RepositoryPathVerificationState::Missing,
}),
}
}
async fn write_path<'path, 'reader>(
&self,
path: Cow<'path, str>,
reader: Pin<Box<dyn AsyncRead + Send + 'reader>>,
) -> Result<RepositoryWrite<'path>> {
let res = self.inner.write_path(path.clone(), reader).await?;
self.path_writes
.lock()
.map_err(|_| {
DebianError::RepositoryIoPath(
path.to_string(),
std::io::Error::new(
std::io::ErrorKind::Other,
"error acquiring write paths mutex",
),
)
})?
.push(path.to_string());
Ok(res)
}
}