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
use cap_std::fs::{Dir, File, Metadata};
use cap_tempfile::cap_std;
use std::ffi::OsStr;
use std::io::Result;
use std::io::{self, Write};
use std::ops::Deref;
use std::path::Path;
pub trait CapStdExtDirExt {
fn open_optional(&self, path: impl AsRef<Path>) -> Result<Option<File>>;
fn open_dir_optional(&self, path: impl AsRef<Path>) -> Result<Option<Dir>>;
fn ensure_dir_with(
&self,
p: impl AsRef<Path>,
builder: &cap_std::fs::DirBuilder,
) -> Result<bool>;
fn metadata_optional(&self, path: impl AsRef<Path>) -> Result<Option<Metadata>>;
fn symlink_metadata_optional(&self, path: impl AsRef<Path>) -> Result<Option<Metadata>>;
fn remove_file_optional(&self, path: impl AsRef<Path>) -> Result<bool>;
fn atomic_replace_with<F, T, E>(
&self,
destname: impl AsRef<Path>,
f: F,
) -> std::result::Result<T, E>
where
F: FnOnce(&mut std::io::BufWriter<cap_tempfile::TempFile>) -> std::result::Result<T, E>,
E: From<std::io::Error>;
fn atomic_write(&self, destname: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()>;
fn atomic_write_with_perms(
&self,
destname: impl AsRef<Path>,
contents: impl AsRef<[u8]>,
perms: cap_std::fs::Permissions,
) -> Result<()>;
}
fn map_optional<R>(r: Result<R>) -> Result<Option<R>> {
match r {
Ok(v) => Ok(Some(v)),
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound {
Ok(None)
} else {
Err(e)
}
}
}
}
enum DirOwnedOrBorrowed<'d> {
Owned(Dir),
Borrowed(&'d Dir),
}
impl<'d> Deref for DirOwnedOrBorrowed<'d> {
type Target = Dir;
fn deref(&self) -> &Self::Target {
match self {
Self::Owned(d) => d,
Self::Borrowed(d) => d,
}
}
}
fn subdir_of<'d, 'p>(d: &'d Dir, p: &'p Path) -> io::Result<(DirOwnedOrBorrowed<'d>, &'p OsStr)> {
let name = p
.file_name()
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidInput, "Not a file name"))?;
let r = if let Some(subdir) = p
.parent()
.filter(|v| !v.as_os_str().is_empty())
.map(|p| d.open_dir(p))
{
DirOwnedOrBorrowed::Owned(subdir?)
} else {
DirOwnedOrBorrowed::Borrowed(d)
};
Ok((r, name))
}
impl CapStdExtDirExt for Dir {
fn open_optional(&self, path: impl AsRef<Path>) -> Result<Option<File>> {
map_optional(self.open(path.as_ref()))
}
fn open_dir_optional(&self, path: impl AsRef<Path>) -> Result<Option<Dir>> {
map_optional(self.open_dir(path.as_ref()))
}
fn ensure_dir_with(
&self,
p: impl AsRef<Path>,
builder: &cap_std::fs::DirBuilder,
) -> Result<bool> {
let p = p.as_ref();
match self.create_dir_with(p, builder) {
Ok(()) => Ok(true),
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
if !self.symlink_metadata(p)?.is_dir() {
return Err(io::Error::new(io::ErrorKind::Other, "Found non-directory"));
}
Ok(false)
}
Err(e) => Err(e),
}
}
fn metadata_optional(&self, path: impl AsRef<Path>) -> Result<Option<Metadata>> {
map_optional(self.metadata(path.as_ref()))
}
fn symlink_metadata_optional(&self, path: impl AsRef<Path>) -> Result<Option<Metadata>> {
map_optional(self.symlink_metadata(path.as_ref()))
}
fn remove_file_optional(&self, path: impl AsRef<Path>) -> Result<bool> {
match self.remove_file(path.as_ref()) {
Ok(()) => Ok(true),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(false),
Err(e) => Err(e),
}
}
fn atomic_replace_with<F, T, E>(
&self,
destname: impl AsRef<Path>,
f: F,
) -> std::result::Result<T, E>
where
F: FnOnce(&mut std::io::BufWriter<cap_tempfile::TempFile>) -> std::result::Result<T, E>,
E: From<std::io::Error>,
{
let destname = destname.as_ref();
let (d, name) = subdir_of(self, destname)?;
let t = cap_tempfile::TempFile::new(&d)?;
let mut bufw = std::io::BufWriter::new(t);
let r = f(&mut bufw)?;
bufw.into_inner()
.map_err(From::from)
.and_then(|t| t.replace(name))?;
Ok(r)
}
fn atomic_write(&self, destname: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()> {
self.atomic_replace_with(destname, |f| f.write_all(contents.as_ref()))
}
fn atomic_write_with_perms(
&self,
destname: impl AsRef<Path>,
contents: impl AsRef<[u8]>,
perms: cap_std::fs::Permissions,
) -> Result<()> {
self.atomic_replace_with(destname, |f| -> io::Result<_> {
#[cfg(unix)]
{
use std::os::unix::prelude::PermissionsExt;
let perms = cap_std::fs::Permissions::from_mode(0o600);
f.get_mut().as_file_mut().set_permissions(perms)?;
}
f.write_all(contents.as_ref())?;
f.flush()?;
f.get_mut().as_file_mut().set_permissions(perms)?;
Ok(())
})
}
}