-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.rs
332 lines (314 loc) · 13 KB
/
dump.rs
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// this crate only supports unix for now, and this ensures that the build fails
// quickly, and the user isn't annoyed that it compiles, but doesn't work at runtime.
#![cfg(unix)]
use super::{StoreError as Error, StoreErrorKind as ErrorKind};
use crate::visit_bytes as yvb;
use camino::Utf8PathBuf;
use std::{fmt, fs, path::Path};
/// sort-of emulation of NAR
#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Dump {
Regular { executable: bool, contents: Vec<u8> },
SymLink { target: Utf8PathBuf },
Directory(std::collections::BTreeMap<crate::BaseName, Dump>),
}
impl fmt::Display for Dump {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
crate::StoreHash::hash_complex(self).fmt(f)
}
}
// sort-of NAR serialization impl
impl crate::Serialize for Dump {
fn serialize<H: crate::SerUpdate>(&self, state: &mut H) {
"(".serialize(state);
"type".serialize(state);
match self {
Dump::Regular {
executable,
contents,
} => {
"regular".serialize(state);
if *executable {
"executable".serialize(state);
"".serialize(state);
}
"contents".serialize(state);
contents.serialize(state);
}
Dump::SymLink { target } => {
"symlink".serialize(state);
"target".serialize(state);
target.as_str().serialize(state);
}
Dump::Directory(entries) => {
"directory".serialize(state);
for (k, v) in entries {
for i in ["entry", "(", "name"] {
i.serialize(state);
}
k.serialize(state);
"node".serialize(state);
v.serialize(state);
")".serialize(state);
}
}
}
")".serialize(state);
}
}
impl yvb::Element for Dump {
fn accept<V: yvb::Visitor>(&self, visitor: &mut V) {
match self {
Dump::Regular { contents, .. } => visitor.visit_bytes(&contents[..]),
Dump::SymLink { target } => visitor.visit_bytes(target.as_str().as_bytes()),
Dump::Directory(entries) => {
entries.values().for_each(|val| val.accept(visitor));
}
}
}
fn accept_mut<V: yvb::VisitorMut>(&mut self, visitor: &mut V) {
match self {
Dump::Regular { contents, .. } => visitor.visit_bytes(&mut contents[..]),
Dump::SymLink { target } => {
let mut s = String::from(std::mem::replace(target, Utf8PathBuf::from("")));
s.accept_mut(visitor);
*target = s.into();
}
Dump::Directory(entries) => {
entries.values_mut().for_each(|val| val.accept_mut(visitor));
}
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Flags {
/// if `force` is `true`, then `write_to_path` will take additional
/// measures which will/might overwrite stuff.
///
/// if `force` is `false`, then, if the object to dump already exists,
/// it aborts.
/// FIXME: make this an enum and make it possible to select a third kind
/// of behavoir: validation of an existing tree.
pub force: bool,
pub make_readonly: bool,
}
impl Dump {
pub fn read_from_path(x: &Path) -> Result<Self, Error> {
let mapef = |e: std::io::Error| Error {
real_path: x.to_path_buf(),
kind: e.into(),
};
let meta = fs::symlink_metadata(x).map_err(&mapef)?;
let ty = meta.file_type();
Ok(if ty.is_symlink() {
let mut target: Utf8PathBuf =
fs::read_link(x)
.map_err(&mapef)?
.try_into()
.map_err(|_| Error {
real_path: x.to_path_buf(),
kind: ErrorKind::NonUtf8SymlinkTarget,
})?;
target.shrink_to_fit();
Dump::SymLink { target }
} else if ty.is_file() {
let mut contents = std::fs::read(x).map_err(&mapef)?;
contents.shrink_to_fit();
Dump::Regular {
executable: std::os::unix::fs::PermissionsExt::mode(&meta.permissions()) & 0o111
!= 0,
contents,
}
} else if ty.is_dir() {
Dump::Directory(
std::fs::read_dir(x)
.map_err(&mapef)?
.map(|entry| {
let entry = entry.map_err(&mapef)?;
let name = entry.file_name().into_string().map_err(|_| Error {
real_path: entry.path(),
kind: ErrorKind::NonUtf8Basename,
})?;
let name: crate::BaseName = name.try_into().map_err(|_| Error {
real_path: entry.path(),
kind: ErrorKind::InvalidBasename,
})?;
let val = Dump::read_from_path(&entry.path())?;
Ok((name, val))
})
.collect::<Result<_, Error>>()?,
)
} else {
return Err(Error {
real_path: x.to_path_buf(),
kind: ErrorKind::UnknownFileType(format!("{:?}", ty)),
});
})
}
/// we require that the parent directory already exists,
/// and will override the target path `x` if it already exists.
pub fn write_to_path(&self, x: &Path, flags: Flags) -> Result<(), Error> {
// one second past epoch, necessary for e.g. GNU make to recognize
// the dumped files as "oldest"
// TODO: when https://github.com/alexcrichton/filetime/pull/75 is merged,
// upgrade `filetime` and make this a global `const` binding.
let reftime = filetime::FileTime::from_unix_time(1, 0);
use std::io::ErrorKind as IoErrorKind;
let mapef = |e: std::io::Error| Error {
real_path: x.to_path_buf(),
kind: e.into(),
};
let mut skip_write = false;
if let Ok(y) = fs::symlink_metadata(x) {
if !flags.force {
return Err(Error {
real_path: x.to_path_buf(),
kind: ErrorKind::OverwriteDeclined,
});
} else if !y.is_dir() {
if let Dump::Regular { contents, .. } = self {
if fs::read(x)
.map(|curcts| &curcts == contents)
.unwrap_or(false)
{
skip_write = true;
} else if y.file_type().is_symlink() {
// FIXME: maybe keep existing symlinks if self is also a symlink,
// and the target matches...
fs::remove_file(x).map_err(&mapef)?;
}
} else {
fs::remove_file(x).map_err(&mapef)?;
}
} else if let Dump::Directory(_) = self {
// passthrough
} else {
// `x` is a directory and `self` isn't
fs::remove_dir_all(x).map_err(&mapef)?;
}
}
match self {
Dump::SymLink { target } => {
std::os::unix::fs::symlink(&target, x).map_err(&mapef)?;
}
Dump::Regular {
executable,
contents,
} => {
if !skip_write {
fs::write(x, contents).map_err(&mapef)?;
}
// don't make stuff readonly on windows, it makes overwriting files more complex...
if xattr::SUPPORTED_PLATFORM {
// delete only non-system attributes for now
// see also: https://github.com/NixOS/nix/pull/4765
// e.g. we can't delete attributes like
// - security.selinux
// - system.nfs4_acl
let rem_xattrs = xattr::list(x)
.map_err(&mapef)?
.flat_map(|i| i.into_string().ok())
.filter(|i| !i.starts_with("security.") && !i.starts_with("system."))
.collect::<Vec<_>>();
if !rem_xattrs.is_empty() {
// make the file temporary writable
fs::set_permissions(
x,
std::os::unix::fs::PermissionsExt::from_mode(if *executable {
0o755
} else {
0o644
}),
)
.map_err(&mapef)?;
for i in rem_xattrs {
xattr::remove(x, i).map_err(&mapef)?;
}
}
}
let mut permbits = 0o444;
if *executable {
permbits |= 0o111;
}
if !flags.make_readonly {
permbits |= 0o200;
}
fs::set_permissions(x, std::os::unix::fs::PermissionsExt::from_mode(permbits))
.map_err(&mapef)?;
}
Dump::Directory(contents) => {
if let Err(e) = fs::create_dir(&x) {
if e.kind() == IoErrorKind::AlreadyExists {
let mut already_writable = false;
// the check at the start of the function should have taken
// care of the annoying edge cases.
// x is thus already a directory
for entry in fs::read_dir(x).map_err(&mapef)? {
let entry = entry.map_err(&mapef)?;
if entry
.file_name()
.into_string()
.ok()
.map(|x| contents.contains_key(&x))
!= Some(true)
{
// file does not exist in the entry list...
let real_path = entry.path();
if !flags.force {
return Err(Error {
real_path,
kind: ErrorKind::OverwriteDeclined,
});
} else {
if !already_writable {
fs::set_permissions(
x,
std::os::unix::fs::PermissionsExt::from_mode(0o755),
)
.map_err(&mapef)?;
already_writable = true;
}
if entry.file_type().map_err(&mapef)?.is_dir() {
fs::remove_dir_all(real_path)
} else {
fs::remove_file(real_path)
}
.map_err(&mapef)?;
}
}
}
if flags.force && !already_writable {
fs::set_permissions(
x,
std::os::unix::fs::PermissionsExt::from_mode(0o755),
)
.map_err(&mapef)?;
}
} else {
return Err(mapef(e));
}
}
let mut xs = x.to_path_buf();
for (name, val) in contents {
if name.is_empty() {
return Err(Error {
real_path: x.to_path_buf(),
kind: ErrorKind::InvalidBasename,
});
}
xs.push(name);
// this call also deals with cases where the file already exists
Dump::write_to_path(val, &xs, flags)?;
xs.pop();
}
if flags.make_readonly {
fs::set_permissions(x, std::os::unix::fs::PermissionsExt::from_mode(0o555))
.map_err(&mapef)?;
}
}
}
filetime::set_symlink_file_times(x, reftime, reftime).map_err(&mapef)
}
}