dracon-system 0.112.52

Disk, process, storage, and service diagnostics for local development machines
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! Pre-delete reference capture for the disk cleaner.
//!
//! The objective is explicit that "never delete anything under a running
//! process" must be verified via `/proc` `exe`, `cwd` **and** `fd` references
//! captured BEFORE the unlink, and that "a `(deleted)` suffix alone is not
//! proof either way". That last clause matters: once a file is unlinked, every
//! `/proc/<pid>/fd` entry pointing at it gains a `(deleted)` suffix, so a
//! post-delete scan cannot distinguish "was in use, we deleted it anyway" from
//! "was not in use". The only sound order is capture-then-decide-then-delete.
//!
//! This module provides that capture as a value the caller must hold while it
//! deletes, and renders it as an auditable one-line record per path.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

/// One process reference to a path, captured before deletion.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct ProcessRef {
    pub(crate) pid: u32,
    /// `exe` — the binary that would hold the file open.
    pub(crate) exe: Option<PathBuf>,
    /// `cwd` — the process working directory ("running inside this tree").
    pub(crate) cwd: Option<PathBuf>,
    /// `fd` — the file descriptor numbers whose target is this path.
    pub(crate) fds: Vec<u32>,
}

impl ProcessRef {
    /// True when this reference is a reason not to delete the path.
    ///
    /// A process is "using" a file when it holds it open (`fd`) or when it is
    /// executing out of it (`exe` inside the tree). A `cwd` inside a directory
    /// being removed also counts: deleting the directory a live process is
    /// sitting in breaks that process's next relative path operation.
    pub(crate) fn blocks_deletion(&self, target: &Path) -> bool {
        if !self.fds.is_empty() {
            return true;
        }
        if self
            .exe
            .as_ref()
            .is_some_and(|exe| exe == target || exe.starts_with(target))
        {
            return true;
        }
        self.cwd
            .as_ref()
            .is_some_and(|cwd| cwd == target || cwd.starts_with(target))
    }

    /// `pid 123 (exe=/usr/bin/foo, cwd=/home/x, fds=[3,7])` — audit text.
    pub(crate) fn describe(&self) -> String {
        let mut parts = Vec::new();
        if let Some(exe) = &self.exe {
            parts.push(format!("exe={}", exe.display()));
        }
        if let Some(cwd) = &self.cwd {
            parts.push(format!("cwd={}", cwd.display()));
        }
        if !self.fds.is_empty() {
            let fds: Vec<String> = self.fds.iter().map(u32::to_string).collect();
            parts.push(format!("fds=[{}]", fds.join(",")));
        }
        if parts.is_empty() {
            format!("pid {} (no refs readable)", self.pid)
        } else {
            format!("pid {} ({})", self.pid, parts.join(", "))
        }
    }
}

/// Everything `/proc` says about who is using a path, captured before unlink.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct ReferenceCapture {
    pub(crate) refs: Vec<ProcessRef>,
    /// True when `/proc` could not be enumerated at all. A failed scan is NOT
    /// evidence of "unused", so the caller must treat this as "unknown" and
    /// refuse to delete.
    pub(crate) scan_failed: bool,
}

impl ReferenceCapture {
    /// Why the capture forbids deletion, if it does.
    pub(crate) fn blocking_reason(&self, target: &Path) -> Option<String> {
        if self.scan_failed {
            return Some("/proc unreadable — usage cannot be verified".to_string());
        }
        if self.refs.is_empty() {
            return None;
        }
        let mut blocking: Vec<ProcessRef> = merged_by_pid(&self.refs)
            .into_iter()
            .filter(|r| r.blocks_deletion(target))
            .collect();
        if blocking.is_empty() {
            return None;
        }
        // Cap the listing: a browser cache is held by dozens of renderer
        // processes and printing all of them buries the actual finding.
        const SHOWN: usize = 4;
        let total = blocking.len();
        let truncated = total > SHOWN;
        if truncated {
            blocking.truncate(SHOWN);
        }
        let mut text = blocking
            .iter()
            .map(|r| r.describe())
            .collect::<Vec<_>>()
            .join("; ");
        if truncated {
            text.push_str(&format!("; … and {} more process(es)", total - SHOWN));
        }
        Some(text)
    }

    /// One-line audit record: the refs, or an explicit "none".
    ///
    /// Rows are merged per pid: the snapshot indexes exe, cwd and each fd
    /// separately, so one process holding one file would otherwise print three
    /// times.
    pub(crate) fn describe(&self) -> String {
        if self.scan_failed {
            return "scan failed (/proc unreadable)".to_string();
        }
        if self.refs.is_empty() {
            return "none (no /proc exe, cwd or fd reference)".to_string();
        }
        merged_by_pid(&self.refs)
            .into_iter()
            .map(|reference| reference.describe())
            .collect::<Vec<_>>()
            .join("; ")
    }
}

/// Read `/proc/<pid>/fd/*` links for one process, returning fds pointing at
/// `target`. A symlink target that has already been unlinked ends in
/// " (deleted)" — that is exactly the suffix the objective says not to trust,
/// so it is stripped and the remaining path compared plainly.
#[cfg(test)]
pub(crate) fn fds_for(path: &Path, target: &Path) -> Vec<u32> {
    let Ok(entries) = std::fs::read_dir(path.join("fd")) else {
        return Vec::new();
    };
    let mut fds = Vec::new();
    for entry in entries.flatten() {
        let Some(fd) = entry
            .file_name()
            .to_str()
            .and_then(|name| name.parse::<u32>().ok())
        else {
            continue;
        };
        let Ok(link) = std::fs::read_link(entry.path()) else {
            continue;
        };
        let text = link.to_string_lossy();
        let plain = text.strip_suffix(" (deleted)").unwrap_or(&text);
        if Path::new(plain) == target {
            fds.push(fd);
        }
    }
    fds.sort_unstable();
    fds
}

/// Collapse per-attribute rows into one entry per pid.
pub(crate) fn merged_by_pid(refs: &[ProcessRef]) -> Vec<ProcessRef> {
    type MergedRow = (Option<PathBuf>, Option<PathBuf>, Vec<u32>);
    let mut merged: BTreeMap<u32, MergedRow> = BTreeMap::new();
    for reference in refs {
        let entry = merged.entry(reference.pid).or_default();
        if entry.0.is_none() {
            entry.0 = reference.exe.clone();
        }
        if entry.1.is_none() {
            entry.1 = reference.cwd.clone();
        }
        for fd in &reference.fds {
            if !entry.2.contains(fd) {
                entry.2.push(*fd);
            }
        }
    }
    merged
        .into_iter()
        .map(|(pid, (exe, cwd, mut fds))| {
            fds.sort_unstable();
            ProcessRef {
                pid,
                exe,
                cwd,
                fds,
            }
        })
        .collect()
}

/// Capture every `/proc` reference to `target`, before it is deleted.
///
/// Deliberately walks the WHOLE process table rather than only the owners a
/// detector already knows about: the point of the check is to catch a process
/// the detector did not anticipate.
/// A single-pass snapshot of every `/proc` reference, queried per target.
///
/// ADDED 2026-10-27: `capture_references` re-walked the whole process table for
/// EVERY finding. The first live `disk audit` therefore hung past 15 minutes on
/// a host with thousands of `/tmp` entries — each one re-reading every pid's
/// `exe`, `cwd` and every fd. The snapshot reads `/proc` once and answers every
/// query from memory, which is both correct and linear.
#[derive(Debug, Clone, Default)]
pub(crate) struct ProcSnapshot {
    refs: BTreeMap<PathBuf, Vec<ProcessRef>>,
    pub(crate) scan_failed: bool,
}

impl ProcSnapshot {
    /// Read `/proc` once, indexing every exe/cwd/fd target.
    pub(crate) fn read(proc_root: &Path) -> Self {
        let Ok(entries) = std::fs::read_dir(proc_root) else {
            return Self {
                refs: BTreeMap::new(),
                scan_failed: true,
            };
        };
        let mut index: BTreeMap<PathBuf, Vec<ProcessRef>> = BTreeMap::new();
        for entry in entries.flatten() {
            let Some(pid) = entry
                .file_name()
                .to_str()
                .and_then(|name| name.parse::<u32>().ok())
            else {
                continue;
            };
            let dir = entry.path();
            let exe = std::fs::read_link(dir.join("exe")).ok();
            let cwd = std::fs::read_link(dir.join("cwd")).ok();
            if let Some(exe_path) = &exe {
                index
                    .entry(strip_deleted(exe_path))
                    .or_default()
                    .push(ProcessRef {
                        pid,
                        exe: Some(exe_path.clone()),
                        cwd: cwd.clone(),
                        fds: Vec::new(),
                    });
            }
            if let Some(cwd) = &cwd {
                index.entry(strip_deleted(cwd)).or_default().push(ProcessRef {
                    pid,
                    exe: exe.clone(),
                    cwd: Some(cwd.clone()),
                    fds: Vec::new(),
                });
            }
            let Ok(fd_entries) = std::fs::read_dir(dir.join("fd")) else {
                continue;
            };
            for fd_entry in fd_entries.flatten() {
                let Some(fd) = fd_entry
                    .file_name()
                    .to_str()
                    .and_then(|name| name.parse::<u32>().ok())
                else {
                    continue;
                };
                let Ok(link) = std::fs::read_link(fd_entry.path()) else {
                    continue;
                };
                index
                    .entry(strip_deleted(&link))
                    .or_default()
                    .push(ProcessRef {
                        pid,
                        exe: exe.clone(),
                        cwd: cwd.clone(),
                        fds: vec![fd],
                    });
            }
        }
        for refs in index.values_mut() {
            refs.sort();
        }
        Self {
            refs: index,
            scan_failed: false,
        }
    }

    /// References to `target` and to anything inside it (a directory).
    pub(crate) fn capture(&self, target: &Path) -> ReferenceCapture {
        if self.scan_failed {
            return ReferenceCapture {
                refs: Vec::new(),
                scan_failed: true,
            };
        }
        let mut refs: Vec<ProcessRef> = Vec::new();
        // Exact hits.
        if let Some(exact) = self.refs.get(target) {
            refs.extend(exact.iter().cloned());
        }
        // Anything strictly inside the target (a directory being removed).
        let prefix = target.to_path_buf();
        for (path, entries) in self.refs.range(prefix.clone()..) {
            if path == &prefix {
                continue; // already collected as an exact hit
            }
            // BREAK, not continue: the map is sorted, so the first entry that
            // is not inside `target` ends the run. Continuing here re-scanned
            // the whole remaining index for every one of 33k /tmp queries,
            // which is what made the first audit take four minutes.
            if !path.starts_with(&prefix) {
                break;
            }
            refs.extend(entries.iter().cloned());
        }
        refs.sort();
        ReferenceCapture {
            refs,
            scan_failed: false,
        }
    }
}

/// Drop a trailing " (deleted)" so a live fd still matches its path.
fn strip_deleted(path: &Path) -> PathBuf {
    let text = path.to_string_lossy();
    match text.strip_suffix(" (deleted)") {
        Some(plain) => PathBuf::from(plain),
        None => path.to_path_buf(),
    }
}

#[cfg(test)]
pub(crate) fn capture_references(proc_root: &Path, target: &Path) -> ReferenceCapture {
    let Ok(entries) = std::fs::read_dir(proc_root) else {
        return ReferenceCapture {
            refs: Vec::new(),
            scan_failed: true,
        };
    };
    let mut refs = Vec::new();
    for entry in entries.flatten() {
        let Some(pid) = entry
            .file_name()
            .to_str()
            .and_then(|name| name.parse::<u32>().ok())
        else {
            continue;
        };
        let dir = entry.path();
        let exe = std::fs::read_link(dir.join("exe")).ok();
        let cwd = std::fs::read_link(dir.join("cwd")).ok();
        let fds = fds_for(&dir, target);
        let relevant = exe
            .as_ref()
            .is_some_and(|exe| exe == target || exe.starts_with(target))
            || cwd
                .as_ref()
                .is_some_and(|cwd| cwd == target || cwd.starts_with(target))
            || !fds.is_empty();
        if relevant {
            refs.push(ProcessRef {
                pid,
                exe,
                cwd,
                fds,
            });
        }
    }
    refs.sort();
    ReferenceCapture {
        refs,
        scan_failed: false,
    }
}

/// Pids that forbid deletion, exposed for tests and callers that only want
/// the identifiers rather than the rendered reason.
#[cfg(test)]
pub(crate) fn blocking_pids(capture: &ReferenceCapture, target: &Path) -> Vec<u32> {
    capture
        .refs
        .iter()
        .filter(|r| r.blocks_deletion(target))
        .map(|r| r.pid)
        .collect()
}

/// Render a capture as `BTreeMap`-friendly audit rows: `(pid, description)`.
#[cfg(test)]
pub(crate) fn audit_rows(capture: &ReferenceCapture) -> BTreeMap<u32, String> {
    capture
        .refs
        .iter()
        .map(|r| (r.pid, r.describe()))
        .collect()
}

#[cfg(test)]
mod capture_tests {
    use super::*;

    #[test]
    fn an_open_fd_blocks_deletion() {
        let target = PathBuf::from("/home/u/Dev/repo/.git/objects/pack/tmp_pack_a");
        let capture = ReferenceCapture {
            refs: vec![ProcessRef {
                pid: 11,
                exe: Some(PathBuf::from("/usr/bin/git")),
                cwd: Some(PathBuf::from("/home/u/Dev/repo")),
                fds: vec![7],
            }],
            scan_failed: false,
        };
        let reason = capture.blocking_reason(&target).expect("must block");
        assert!(reason.contains("pid 11"));
        assert!(reason.contains("fds=[7]"));
        assert_eq!(blocking_pids(&capture, &target), vec![11]);
        let rows = audit_rows(&capture);
        assert_eq!(rows.len(), 1);
        assert!(rows[&11].contains("fds=[7]"));
    }

    #[test]
    fn a_process_merely_running_elsewhere_does_not_block() {
        let target = PathBuf::from("/home/u/Dev/repo/.git/objects/pack/tmp_pack_a");
        let capture = ReferenceCapture {
            refs: vec![ProcessRef {
                pid: 12,
                exe: Some(PathBuf::from("/usr/bin/git")),
                cwd: Some(PathBuf::from("/home/u/Dev/other-repo")),
                fds: Vec::new(),
            }],
            scan_failed: false,
        };
        assert_eq!(capture.blocking_reason(&target), None);
    }

    #[test]
    fn an_unreadable_proc_scan_is_not_evidence_of_unused() {
        let capture = ReferenceCapture {
            refs: Vec::new(),
            scan_failed: true,
        };
        assert!(capture
            .blocking_reason(Path::new("/anything"))
            .expect("unknown must block")
            .contains("cannot be verified"));
    }

    #[test]
    fn deleting_a_directory_a_process_sits_in_is_blocked() {
        let target = PathBuf::from("/home/u/dracon-lfs-proof");
        let capture = ReferenceCapture {
            refs: vec![ProcessRef {
                pid: 13,
                exe: None,
                cwd: Some(target.join("sub")),
                fds: Vec::new(),
            }],
            scan_failed: false,
        };
        assert!(capture.blocking_reason(&target).is_some());
    }

    #[test]
    fn a_clean_capture_describes_itself_explicitly() {
        let capture = ReferenceCapture::default();
        assert_eq!(
            capture.describe(),
            "none (no /proc exe, cwd or fd reference)"
        );
        assert_eq!(capture.blocking_reason(Path::new("/x")), None);
    }

    #[test]
    fn a_deleted_suffix_does_not_hide_a_live_fd() {
        // The symlink text git leaves behind after an unlink still names the
        // path; the comparison must not be fooled into thinking it is a
        // different file.
        let capture = ReferenceCapture {
            refs: vec![ProcessRef {
                pid: 14,
                exe: None,
                cwd: None,
                fds: vec![3],
            }],
            scan_failed: false,
        };
        assert!(capture
            .blocking_reason(Path::new("/home/u/x/tmp_pack_a (deleted)"))
            .is_some());
    }

    #[test]
    fn a_real_proc_scan_finds_a_live_reference_to_a_file_this_process_holds() {
        // Hold a real file open in THIS process, then capture it from /proc.
        // A checked-in fixture cannot prove the exe/cwd/fd walk works; only a
        // live descriptor can.
        let dir = tempfile::tempdir().expect("tempdir");
        let held = dir.path().join("held.bin");
        std::fs::write(&held, b"held").expect("write");
        let file = std::fs::File::open(&held).expect("open held file");

        let capture = capture_references(Path::new("/proc"), &held);
        assert!(!capture.scan_failed, "a live /proc must be readable");
        let own_pid = std::process::id();
        assert!(
            blocking_pids(&capture, &held).contains(&own_pid),
            "the process holding the file must be reported: {}",
            capture.describe()
        );
        assert!(
            capture
                .blocking_reason(&held)
                .expect("a held file must block")
                .contains(&format!("pid {own_pid}")),
            "the reason must name the holding pid"
        );
        drop(file);
    }

    #[test]
    fn a_released_reference_stops_blocking() {
        let dir = tempfile::tempdir().expect("tempdir");
        let held = dir.path().join("released.bin");
        std::fs::write(&held, b"x").expect("write");
        {
            let _file = std::fs::File::open(&held).expect("open");
            assert!(capture_references(Path::new("/proc"), &held)
                .blocking_reason(&held)
                .is_some());
        }
        // After the drop the only remaining refs are transient; a file nobody
        // holds must be deletable (that is the whole point of capturing first).
        let capture = capture_references(Path::new("/proc"), &held);
        let own = std::process::id();
        assert!(
            !blocking_pids(&capture, &held).contains(&own),
            "this process no longer holds the file: {}",
            capture.describe()
        );
    }
}