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
//! What a replacement would take away (#256).
//!
//! Updating a skill replaces its directory wholesale, so anything the user — or
//! the skill itself — wrote inside it disappears without warning. The reporter
//! of #256 lost the PowerPoint templates `ppt-master` had written into its own
//! `templates/`, and only found out afterwards.
//!
//! The updater cannot tell whose files are whose; that needs per-file provenance
//! and is a much larger change. But it does not need to. At the moment of the
//! swap both trees are on disk, so it can answer a narrower question that costs
//! nothing to compute and nothing to store:
//!
//! > which paths exist now, and are simply not in the new version?
//!
//! Those are the ones about to vanish. Some are the user's; some are files the
//! author deleted upstream. Saying "these will be removed" is true of both,
//! makes no claim about ownership, and is enough for a person to recognise their
//! own work and stop.
use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
// Only generated artifacts are excluded: a user's `.gitignore` is still
// worth mentioning when a replacement would remove it.
use super::content_hash::is_regenerable;
/// Paths that exist under `current` but not under `replacement`.
///
/// Reported by path alone: a file whose *contents* change still exists
/// afterwards, and warning about it would bury the ones that do not.
///
/// A directory missing from the replacement entirely is reported as one entry
/// with a trailing `/`, rather than every file beneath it. Without that, a
/// nested `.git` the user created would bury the dialog under thousands of
/// object files, and a whole removed `templates/` would read as unrelated
/// losses instead of one.
///
/// Sorted, so the same update always reads the same way.
pub fn removed_paths(current: &Path, replacement: &Path) -> Result<Vec<String>> {
// `is_dir()` follows symlinks and folds every error into "no". Only a
// genuine absence may answer "nothing will be lost"; anything else — a
// dangling link, a plain file, an unreadable path — has to say so.
match std::fs::symlink_metadata(current) {
Ok(md) if md.is_dir() => {}
Ok(_) => return Ok(vec![display_path(Path::new(""), false)]),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
Err(err) => {
return Err(anyhow::Error::from(err)
.context(format!("Cannot inspect {:?} before replacing it", current)));
}
}
let mut out = Vec::new();
collect(current, replacement, Path::new(""), &mut out)?;
out.sort();
Ok(out)
}
/// Coarse kind, so a path that changes shape counts as removed rather than
/// surviving: replacing a file with a directory of the same name still takes
/// the file's contents away.
fn kind_of(md: &std::fs::Metadata) -> u8 {
if md.file_type().is_symlink() {
0
} else if md.is_dir() {
1
} else {
2
}
}
fn collect(
current_root: &Path,
replacement_root: &Path,
prefix: &Path,
out: &mut Vec<String>,
) -> Result<()> {
let dir = current_root.join(prefix);
let entries = std::fs::read_dir(&dir).with_context(|| format!("Failed to read {:?}", dir))?;
for entry in entries {
let entry = entry.with_context(|| format!("Failed to read an entry in {:?}", dir))?;
let name = entry.file_name();
if is_regenerable(&name.to_string_lossy()) {
continue;
}
let relative = prefix.join(&name);
let is_dir = entry
.file_type()
.with_context(|| format!("Failed to inspect {:?}", entry.path()))?
.is_dir();
let current_md = std::fs::symlink_metadata(entry.path())
.with_context(|| format!("Failed to inspect {:?}", entry.path()))?;
match std::fs::symlink_metadata(replacement_root.join(&relative)) {
// Same path, same shape. Its contents may differ, which is what an
// update is for; only look deeper if it is a directory.
Ok(md) if kind_of(&md) == kind_of(¤t_md) => {
if is_dir {
collect(current_root, replacement_root, &relative, out)?;
}
}
// Same path, different shape: whatever is here now does not survive.
Ok(_) => out.push(display_path(&relative, is_dir)),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
out.push(display_path(&relative, is_dir));
}
// Neither present nor absent as far as we can tell. Refuse rather
// than guess: a wrong "nothing will be lost" is the failure this
// exists to prevent, and a wrong warning trains people to click
// through the real ones.
Err(err) => {
return Err(anyhow::Error::from(err).context(format!(
"Cannot tell whether {:?} survives the update",
relative
)));
}
}
}
Ok(())
}
fn display_path(relative: &Path, is_dir: bool) -> String {
// `\` is a legitimate filename character on unix, so only Windows'
// separators are normalised for display.
#[cfg(windows)]
let mut shown = relative.to_string_lossy().replace('\\', "/");
#[cfg(not(windows))]
let mut shown = relative.to_string_lossy().into_owned();
if shown.is_empty() {
shown.push('.');
}
if is_dir {
shown.push('/');
}
shown
}
/// Convenience for callers holding paths as strings.
pub fn removed_paths_between(current: &str, replacement: &Path) -> Result<Vec<String>> {
removed_paths(&PathBuf::from(current), replacement)
}
#[cfg(test)]
mod tests;