-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathclean.rs
More file actions
352 lines (317 loc) · 11.8 KB
/
clean.rs
File metadata and controls
352 lines (317 loc) · 11.8 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use std::{
collections::{BTreeMap, HashMap},
fmt,
path::{Path, PathBuf},
time::SystemTime,
};
use color_eyre::eyre::{bail, eyre, Context, ContextCompat};
use nix::errno::Errno;
use nix::{
fcntl::AtFlags,
unistd::{faccessat, AccessFlags},
};
use regex::Regex;
use tracing::{debug, info, instrument, span, warn, Level};
use uzers::os::unix::UserExt;
use crate::{commands::Command, interface, Result};
// Nix impl:
// https://github.com/NixOS/nix/blob/master/src/nix-collect-garbage/nix-collect-garbage.cc
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
struct Generation {
number: u32,
last_modified: SystemTime,
path: PathBuf,
}
type ToBeRemoved = bool;
// BTreeMap to automatically sort generations by id
type GenerationsTagged = BTreeMap<Generation, ToBeRemoved>;
type ProfilesTagged = HashMap<PathBuf, GenerationsTagged>;
impl interface::CleanMode {
pub fn run(&self) -> Result<()> {
let mut profiles = Vec::new();
let mut gcroots_tagged: HashMap<PathBuf, ToBeRemoved> = HashMap::new();
let now = SystemTime::now();
let mut is_profile_clean = false;
// What profiles to clean depending on the call mode
let uid = nix::unistd::Uid::effective();
let args = match self {
Self::Profile(args) => {
profiles.push(args.profile.clone());
is_profile_clean = true;
&args.common
}
Self::All(args) => {
if !uid.is_root() {
crate::self_elevate();
}
profiles.extend(profiles_in_dir("/nix/var/nix/profiles"));
for read_dir in PathBuf::from("/nix/var/nix/profiles/per-user").read_dir()? {
let path = read_dir?.path();
profiles.extend(profiles_in_dir(path));
}
// Most unix systems start regular users at uid 1000+, but macos is special at 501+
// https://en.wikipedia.org/wiki/User_identifier
let uid_min = if cfg!(target_os = "macos") { 501 } else { 1000 };
let uid_max = uid_min + 100;
debug!("Scanning XDG profiles for users 0, ${uid_min}-${uid_max}");
for user in unsafe { uzers::all_users() } {
if user.uid() >= uid_min && user.uid() < uid_max || user.uid() == 0 {
debug!(?user, "Adding XDG profiles for user");
profiles.extend(profiles_in_dir(
user.home_dir().join(".local/state/nix/profiles"),
));
}
}
args
}
Self::User(args) => {
if uid.is_root() {
bail!("nh clean user: don't run me as root!");
}
let user = nix::unistd::User::from_uid(uid)?.unwrap();
profiles.extend(profiles_in_dir(
PathBuf::from(std::env::var("HOME")?).join(".local/state/nix/profiles"),
));
profiles.extend(profiles_in_dir(
PathBuf::from("/nix/var/nix/profiles/per-user").join(user.name),
));
args
}
};
// Use mutation to raise errors as they come
let mut profiles_tagged = ProfilesTagged::new();
for p in profiles {
profiles_tagged.insert(
p.clone(),
cleanable_generations(&p, args.keep, args.keep_since)?,
);
}
// Query gcroots
let filename_tests = [r".*/.direnv/.*", r".*result.*"];
let regexes = filename_tests
.into_iter()
.map(Regex::new)
.collect::<Result<Vec<_>, regex::Error>>()?;
if !is_profile_clean && !args.nogcroots {
for elem in PathBuf::from("/nix/var/nix/gcroots/auto")
.read_dir()
.wrap_err("Reading auto gcroots dir")?
{
let src = elem.wrap_err("Reading auto gcroots element")?.path();
let dst = src.read_link().wrap_err("Reading symlink destination")?;
let span = span!(Level::TRACE, "gcroot detection", ?dst);
let _entered = span.enter();
debug!(?src);
if !regexes
.iter()
.any(|next| next.is_match(&dst.to_string_lossy()))
{
debug!("dst doesn't match any gcroot regex, skipping");
continue;
};
// Use .exists to not travel symlinks
if match faccessat(
None,
&dst,
AccessFlags::F_OK | AccessFlags::W_OK,
AtFlags::AT_SYMLINK_NOFOLLOW,
) {
Ok(()) => true,
Err(errno) => match errno {
Errno::EACCES | Errno::ENOENT => false,
_ => {
bail!(eyre!("Checking access for gcroot {:?}, unknown error", dst)
.wrap_err(errno))
}
},
} {
let dur = now.duration_since(
dst.symlink_metadata()
.wrap_err("Reading gcroot metadata")?
.modified()?,
);
debug!(?dur);
match dur {
Err(err) => {
warn!(?err, ?now, "Failed to compare time!");
}
Ok(val) if val <= args.keep_since.into() => {
gcroots_tagged.insert(dst, false);
}
Ok(_) => {
gcroots_tagged.insert(dst, true);
}
}
} else {
debug!("dst doesn't exist or is not writable, skipping");
}
}
}
// Present the user the information about the paths to clean
use owo_colors::OwoColorize;
println!();
println!("{}", "Welcome to nh clean".bold());
println!("Keeping {} generation(s)", args.keep.green());
println!("Keeping paths newer than {}", args.keep_since.green());
println!();
println!("legend:");
println!("{}: path to be kept", "OK".green());
println!("{}: path to be removed", "DEL".red());
println!();
if !gcroots_tagged.is_empty() {
println!(
"{}",
"gcroots (matching the following regex patterns)"
.blue()
.bold()
);
for re in regexes {
println!("- {} {}", "RE".purple(), re);
}
for (path, tbr) in &gcroots_tagged {
if *tbr {
println!("- {} {}", "DEL".red(), path.to_string_lossy());
} else {
println!("- {} {}", "OK ".green(), path.to_string_lossy());
}
}
println!();
}
for (profile, generations_tagged) in &profiles_tagged {
println!("{}", profile.to_string_lossy().blue().bold());
for (gen, tbr) in generations_tagged.iter().rev() {
if *tbr {
println!("- {} {}", "DEL".red(), gen.path.to_string_lossy());
} else {
println!("- {} {}", "OK ".green(), gen.path.to_string_lossy());
};
}
println!();
}
// Clean the paths
if args.ask {
info!("Confirm the cleanup plan?");
if !dialoguer::Confirm::new().default(false).interact()? {
bail!("User rejected the cleanup plan");
}
}
if !args.dry {
for (path, tbr) in &gcroots_tagged {
if *tbr {
remove_path_nofail(path);
}
}
for generations_tagged in profiles_tagged.values() {
for (gen, tbr) in generations_tagged.iter().rev() {
if *tbr {
remove_path_nofail(&gen.path);
}
}
}
}
if !args.nogc {
Command::new("nix")
.args(["store", "gc"])
.dry(args.dry)
.message("Performing garbage collection on the nix store")
.run()?;
}
Ok(())
}
}
#[instrument(ret, level = "debug")]
fn profiles_in_dir<P: AsRef<Path> + fmt::Debug>(dir: P) -> Vec<PathBuf> {
let mut res = Vec::new();
let dir = dir.as_ref();
match dir.read_dir() {
Ok(read_dir) => {
for entry in read_dir {
match entry {
Ok(e) => {
let path = e.path();
if let Ok(dst) = path.read_link() {
let name = dst
.file_name()
.expect("Failed to get filename")
.to_string_lossy();
let generation_regex = Regex::new(r"^(.*)-(\d+)-link$").unwrap();
if generation_regex.captures(&name).is_some() {
res.push(path);
}
}
}
Err(error) => {
warn!(?dir, ?error, "Failed to read folder element");
}
}
}
}
Err(error) => {
warn!(?dir, ?error, "Failed to read profiles directory");
}
}
res
}
#[instrument(err, level = "debug")]
fn cleanable_generations(
profile: &Path,
keep: u32,
keep_since: humantime::Duration,
) -> Result<GenerationsTagged> {
let name = profile
.file_name()
.context("Checking profile's name")?
.to_str()
.unwrap();
let generation_regex = Regex::new(&format!(r"^{name}-(\d+)-link"))?;
let mut result = GenerationsTagged::new();
for entry in profile
.parent()
.context("Reading profile's parent dir")?
.read_dir()
.context("Reading profile's generations")?
{
let path = entry?.path();
let captures = generation_regex.captures(path.file_name().unwrap().to_str().unwrap());
if let Some(caps) = captures {
if let Some(number) = caps.get(1) {
let last_modified = path
.symlink_metadata()
.context("Checking symlink metadata")?
.modified()
.context("Reading modified time")?;
result.insert(
Generation {
number: number.as_str().parse().unwrap(),
last_modified,
path: path.clone(),
},
true,
);
}
}
}
let now = SystemTime::now();
for (gen, tbr) in &mut result {
match now.duration_since(gen.last_modified) {
Err(err) => {
warn!(?err, ?now, ?gen, "Failed to compare time!");
}
Ok(val) if val <= keep_since.into() => {
*tbr = false;
}
Ok(_) => {}
}
}
for (_, tbr) in result.iter_mut().rev().take(keep as _) {
*tbr = false;
}
debug!("{:#?}", result);
Ok(result)
}
fn remove_path_nofail(path: &Path) {
info!("Removing {}", path.to_string_lossy());
if let Err(err) = std::fs::remove_file(path) {
warn!(?path, ?err, "Failed to remove path");
}
}