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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use std::path::Path;
use std::{path::PathBuf, sync::Arc};
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
#[derive(Clone, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("File or directory {0} not found")]
NotFound(PathBuf),
#[error("Incorrect permissions: {0} is {}; must be {}",
format_access_bits(* .1, '='), format_access_bits(* .2, '-'))]
BadPermission(PathBuf, u32, u32),
#[error("Bad owner (UID {1}) on file or directory {0}")]
BadOwner(PathBuf, u32),
#[error("Wrong type of file at {0}")]
BadType(PathBuf),
#[error("Unable to access {0}")]
CouldNotInspect(PathBuf, #[source] Arc<IoError>),
#[error("Multiple errors found")]
Multiple(Vec<Box<Error>>),
#[error("Too many steps taken or planned: Possible symlink loop?")]
StepsExceeded,
#[error("Problem finding current directory")]
CurrentDirectory(#[source] Arc<IoError>),
#[error("Problem creating directory")]
CreatingDir(#[source] Arc<IoError>),
#[error("Invalid directory content")]
Content(#[source] Box<Error>),
#[cfg(feature = "walkdir")]
#[error("Unable to list directory")]
Listing(#[source] Arc<walkdir::Error>),
#[error("Path was not valid for use with CheckedDir.")]
InvalidSubdirectory,
#[error("IO error on {0}")]
Io(PathBuf, #[source] Arc<IoError>),
#[error("Could not name temporary file for {0}")]
NoTempFile(PathBuf),
#[error("Missing field: {0}")]
MissingField(#[from] derive_builder::UninitializedFieldError),
#[error("No such group: {0}")]
NoSuchGroup(String),
#[error("No such user: {0}")]
NoSuchUser(String),
}
impl Error {
pub(crate) fn inspecting(err: IoError, fname: impl Into<PathBuf>) -> Self {
match err.kind() {
IoErrorKind::NotFound => Error::NotFound(fname.into()),
_ => Error::CouldNotInspect(fname.into(), Arc::new(err)),
}
}
pub(crate) fn io(err: IoError, fname: impl Into<PathBuf>) -> Self {
match err.kind() {
IoErrorKind::NotFound => Error::NotFound(fname.into()),
_ => Error::Io(fname.into(), Arc::new(err)),
}
}
pub fn path(&self) -> Option<&Path> {
Some(
match self {
Error::NotFound(pb) => pb,
Error::BadPermission(pb, ..) => pb,
Error::BadOwner(pb, _) => pb,
Error::BadType(pb) => pb,
Error::CouldNotInspect(pb, _) => pb,
Error::Io(pb, _) => pb,
Error::NoTempFile(pb) => pb,
Error::Multiple(_) => return None,
Error::StepsExceeded => return None,
Error::CurrentDirectory(_) => return None,
Error::CreatingDir(_) => return None,
Error::InvalidSubdirectory => return None,
Error::Content(e) => return e.path(),
Error::Listing(e) => return e.path(),
Error::MissingField(_) => return None,
Error::NoSuchGroup(_) => return None,
Error::NoSuchUser(_) => return None,
}
.as_path(),
)
}
pub fn is_bad_permission(&self) -> bool {
match self {
Error::BadPermission(..) | Error::BadOwner(_, _) | Error::BadType(_) => true,
Error::NotFound(_)
| Error::CouldNotInspect(_, _)
| Error::StepsExceeded
| Error::CurrentDirectory(_)
| Error::CreatingDir(_)
| Error::Listing(_)
| Error::InvalidSubdirectory
| Error::Io(_, _)
| Error::NoTempFile(_)
| Error::MissingField(_)
| Error::NoSuchGroup(_)
| Error::NoSuchUser(_) => false,
Error::Multiple(errs) => errs.iter().any(|e| e.is_bad_permission()),
Error::Content(err) => err.is_bad_permission(),
}
}
pub fn errors<'a>(&'a self) -> impl Iterator<Item = &Error> + 'a {
let result: Box<dyn Iterator<Item = &Error> + 'a> = match self {
Error::Multiple(v) => Box::new(v.iter().map(|e| e.as_ref())),
_ => Box::new(vec![self].into_iter()),
};
result
}
}
impl std::iter::FromIterator<Error> for Option<Error> {
fn from_iter<T: IntoIterator<Item = Error>>(iter: T) -> Self {
let mut iter = iter.into_iter();
let first_err = iter.next()?;
if let Some(second_err) = iter.next() {
let mut errors = Vec::with_capacity(iter.size_hint().0 + 2);
errors.push(Box::new(first_err));
errors.push(Box::new(second_err));
errors.extend(iter.map(Box::new));
Some(Error::Multiple(errors))
} else {
Some(first_err)
}
}
}
fn format_access_bits(bits: u32, c: char) -> String {
let mut s = String::new();
for (shift, prefix) in [(6, 'u'), (3, 'g'), (0, 'o')] {
let b = (bits >> shift) & 7;
if b != 0 {
if !s.is_empty() {
s.push(',');
}
s.push(prefix);
s.push(c);
for (bit, ch) in [(4, 'r'), (2, 'w'), (1, 'x')] {
if b & bit != 0 {
s.push(ch);
}
}
}
}
s
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn bits() {
assert_eq!(format_access_bits(0o777, '='), "u=rwx,g=rwx,o=rwx");
assert_eq!(format_access_bits(0o022, '='), "g=w,o=w");
assert_eq!(format_access_bits(0o022, '-'), "g-w,o-w");
assert_eq!(format_access_bits(0o020, '-'), "g-w");
assert_eq!(format_access_bits(0, ' '), "");
}
#[test]
fn bad_perms() {
assert_eq!(
Error::BadPermission(PathBuf::from("/path"), 0o777, 0o022).to_string(),
"Incorrect permissions: /path is u=rwx,g=rwx,o=rwx; must be g-w,o-w"
);
}
}