Skip to main content

magi/
disk.rs

1//! Disk accounting: how much magi's own directories occupy, how much space is
2//! left on the volume they live on, and how the shared build cache is pruned.
3//!
4//! The whole module grew out of one incident: a machine with 951.8 GB of disk
5//! ran a handful of competitions and ended up with 6.7 GB free and a pile of
6//! multi-gigabyte `target/` directories. Every function here exists to keep
7//! that from being a discovery, and every number is substituted at a pure
8//! boundary so the policy can be tested without asking the OS anything.
9
10use std::path::{Path, PathBuf};
11
12use anyhow::{Context as _, Result};
13
14#[cfg(unix)]
15use {crate::proc::Quiet as _, anyhow::bail};
16
17/// Are `free` bytes above the floor for starting a run?
18///
19/// Pure on purpose: the threshold logic is asserted against injected numbers,
20/// and the only place the machine is actually asked anything is [`free_bytes`].
21pub fn enough_space(free: u64, min_free: u64) -> bool {
22    free >= min_free
23}
24
25/// Why a run must not start, given measured free bytes and the floor —
26/// `None` means the gate is open. Pure, so the policy is asserted directly.
27///
28/// A gate that cannot measure also closes (see [`crate::daemon::disk_gate`]):
29/// starting a run on a disk that may already be full is the incident this
30/// whole module exists to prevent.
31pub fn gate(free: u64, min_free: u64) -> Option<String> {
32    gate_in(free, min_free, "en")
33}
34
35/// Prefix of [`gate_in`]'s reason, per language. [`is_gate_reason`] matches
36/// both, so translating the text cannot stop triage's auto-requeue.
37const SHORT_EN: &str = "not enough free space to start a run:";
38const SHORT_JA: &str = "run を開始するための空き容量が不足しています:";
39/// Prefix of a resource hold after emergency writer termination.
40pub const RUNNING_PRESSURE: &str = "disk pressure while a run was writing: ";
41/// Prefix of a failed free-space measurement.
42pub const UNMEASURED_EN: &str = "could not measure free space on ";
43/// See [`UNMEASURED_EN`].
44pub const UNMEASURED_JA: &str = "空き容量を測定できませんでした: ";
45
46/// [`gate`], with the reason in `language` (`en`, or Japanese; anything else
47/// is English).
48pub fn gate_in(free: u64, min_free: u64, language: &str) -> Option<String> {
49    if enough_space(free, min_free) {
50        None
51    } else if crate::lang::is_japanese(language) {
52        Some(format!(
53            "{SHORT_JA} 空き {free} バイト、`[disk] min_free_bytes` の要求は {min_free} バイト"
54        ))
55    } else {
56        Some(format!(
57            "{SHORT_EN} {free} bytes free, \
58             {min_free} required by `[disk] min_free_bytes`"
59        ))
60    }
61}
62
63/// The reason for a measurement that failed, in `language`.
64pub fn unmeasured_in(
65    path: &std::path::Path,
66    err: &dyn std::fmt::Display,
67    language: &str,
68) -> String {
69    if crate::lang::is_japanese(language) {
70        format!(
71            "{UNMEASURED_JA}{} ({err}); ディスクゲートは測定できないまま run を開始させません",
72            path.display()
73        )
74    } else {
75        format!(
76            "{UNMEASURED_EN}{} ({err}); the disk gate refuses \
77             to let a run start blind",
78            path.display()
79        )
80    }
81}
82
83/// Is `reason` one of this module's disk-gate reasons, in either language?
84/// What triage reads back to decide a hold is disk pressure.
85pub fn is_gate_reason(reason: &str) -> bool {
86    fn numbers(text: &str, middle: &str, suffix: &str) -> bool {
87        text.strip_suffix(suffix)
88            .and_then(|s| s.split_once(middle))
89            .is_some_and(|(a, b)| {
90                !a.is_empty()
91                    && !b.is_empty()
92                    && a.bytes().all(|c| c.is_ascii_digit())
93                    && b.bytes().all(|c| c.is_ascii_digit())
94                    && matches!((a.parse::<u64>(), b.parse::<u64>()), (Ok(free), Ok(required)) if free < required)
95            })
96    }
97    reason
98        .strip_prefix(&format!("{SHORT_EN} "))
99        .is_some_and(|s| numbers(s, " bytes free, ", " required by `[disk] min_free_bytes`"))
100        || reason
101            .strip_prefix(&format!("{SHORT_JA} 空き "))
102            .is_some_and(|s| numbers(s, " バイト、`[disk] min_free_bytes` の要求は ", " バイト"))
103        || [
104            (
105                UNMEASURED_EN,
106                "); the disk gate refuses to let a run start blind",
107            ),
108            (
109                UNMEASURED_JA,
110                "); ディスクゲートは測定できないまま run を開始させません",
111            ),
112        ]
113        .iter()
114        .any(|(prefix, suffix)| {
115            reason
116                .strip_prefix(prefix)
117                .and_then(|s| s.strip_suffix(suffix))
118                .is_some_and(|s| {
119                    s.split_once(" (").is_some_and(|(path, error)| {
120                        !path.is_empty() && !error.is_empty() && !s.contains('\n')
121                    })
122                })
123        })
124}
125
126/// Is `size` past `limit`? One comparison, shared by the janitor and the
127/// health view, so both answer "is the cache over its cap" identically.
128pub fn over_limit(size: u64, limit: u64) -> bool {
129    size > limit
130}
131
132/// A cache's target size under its cap and the volume's free-space floor.
133/// Pressure can reclaim idle cache files below the ordinary size cap.
134pub fn pressure_limit(size: u64, cap: u64, min_free: u64, free: u64) -> u64 {
135    cap.min(size.saturating_sub(min_free.saturating_sub(free)))
136}
137
138/// The path a rendered command sets `CARGO_TARGET_DIR=` to, if any.
139///
140/// magi never computes the cache path itself. The operator's `magi.toml` is
141/// the only place that knows it, and by the time a [`crate::config::Config`]
142/// exists that template has been rendered — so the concrete path is read back
143/// out of the verify commands (`CARGO_TARGET_DIR={{ vars.cache }}/magi-target
144/// cargo …` becomes `C:\…\Temp\magi-target`). This is what lets the janitor
145/// prune exactly the directory the gate and the seats build into. `None` when
146/// no command sets the variable: there is then no cache to aggregate or prune,
147/// and agents build wherever the repository's own defaults put them.
148///
149/// The value may be quoted with `'` or `"`; both are understood, as is no
150/// quoting (up to the next whitespace).
151pub fn extract_cargo_target_dir(command: &str) -> Option<PathBuf> {
152    const KEY: &str = "CARGO_TARGET_DIR=";
153    let rest = command.split_once(KEY)?.1.trim_start();
154    let value = if let Some(s) = rest.strip_prefix('\'') {
155        s.split('\'').next().unwrap_or("")
156    } else if let Some(s) = rest.strip_prefix('"') {
157        s.split('"').next().unwrap_or("")
158    } else {
159        let end = rest.find(char::is_whitespace).unwrap_or(rest.len());
160        &rest[..end]
161    };
162    if value.is_empty() {
163        None
164    } else {
165        Some(PathBuf::from(value))
166    }
167}
168
169/// Free bytes on the volume containing `path`.
170///
171/// Unix parses `df`; Windows asks the volume directly through kernel32, without
172/// starting a subprocess on the synchronous health-request path.
173pub fn free_bytes(path: &Path) -> Result<u64> {
174    free_bytes_by_os(path)
175}
176
177/// Free bytes on the volume containing `path`.
178#[cfg(unix)]
179fn free_bytes_by_os(path: &Path) -> Result<u64> {
180    let out = std::process::Command::new("df")
181        .args(["-k", "-P"])
182        .arg(path)
183        .quiet()
184        .output()
185        .with_context(|| format!("run `df` for {}", path.display()))?;
186    if !out.status.success() {
187        bail!(
188            "`df` failed: {}",
189            String::from_utf8_lossy(&out.stderr).trim()
190        );
191    }
192    let text = String::from_utf8_lossy(&out.stdout);
193    text.lines()
194        .skip(1)
195        .find_map(parse_df_available)
196        .with_context(|| format!("parse `df` output for {}", path.display()))
197}
198
199/// Free bytes on the volume containing `path`.
200#[cfg(windows)]
201fn free_bytes_by_os(path: &Path) -> Result<u64> {
202    let abs = std::path::absolute(path)
203        .with_context(|| format!("absolute path for {}", path.display()))?;
204    let root = volume_root(&abs)
205        .with_context(|| format!("no volume root in {} to measure", abs.display()))?;
206    windows_free_bytes(Path::new(&root))
207}
208
209pub(crate) fn managed_free_bytes(path: &Path) -> Result<u64> {
210    #[cfg(windows)]
211    return windows_free_bytes(path);
212    #[cfg(not(windows))]
213    free_bytes(path)
214}
215
216#[cfg(windows)]
217fn windows_free_bytes(path: &Path) -> Result<u64> {
218    use std::os::windows::ffi::OsStrExt as _;
219    #[link(name = "kernel32")]
220    unsafe extern "system" {
221        fn GetDiskFreeSpaceExW(
222            directory: *const u16,
223            available: *mut u64,
224            total: *mut u64,
225            free: *mut u64,
226        ) -> i32;
227    }
228    let wide: Vec<u16> = path.as_os_str().encode_wide().chain(Some(0)).collect();
229    let mut available = 0_u64;
230    // SAFETY: wide is a live NUL-terminated UTF-16 root, available is writable,
231    // and the optional totals are null. The API retains none of these pointers.
232    if unsafe {
233        GetDiskFreeSpaceExW(
234            wide.as_ptr(),
235            &mut available,
236            std::ptr::null_mut(),
237            std::ptr::null_mut(),
238        )
239    } == 0
240    {
241        let error = std::io::Error::last_os_error();
242        return Err(error).with_context(|| format!("query free bytes for {}", path.display()));
243    }
244    // Caller-available bytes, not total free bytes: preserve quota semantics.
245    Ok(available)
246}
247
248/// One `df -k -P` data row: `Filesystem 1024-blocks Used Available …`.
249///
250/// The value is 1024-byte blocks, so the parse returns bytes.
251pub fn parse_df_available(line: &str) -> Option<u64> {
252    let mut fields = line.split_whitespace();
253    fields.next()?; // filesystem
254    fields.next()?; // 1024-blocks
255    fields.next()?; // used
256    let blocks: u64 = fields.next()?.parse().ok()?;
257    Some(blocks.saturating_mul(1024))
258}
259
260/// The drive root used for querying an absolute Windows path:
261/// `C:\`, never `C:\Users\...` and never a verbatim `\\?\C:\...`.
262///
263/// Pure and platform-independent so the verbatim form - which is what the
264/// queue stores and what closed the disk gate on every task - is asserted
265/// without a Windows runner. `None` when there is no drive letter to name:
266/// UNC shares remain unsupported, rather than inventing a drive volume.
267pub fn volume_root(path: &Path) -> Option<String> {
268    let text = path.to_str()?;
269    // Verbatim (`\\?\C:\x`) and verbatim-UNC (`\\?\UNC\server\share`) prefixes.
270    let bare = text
271        .strip_prefix(r"\\?\")
272        .or_else(|| text.strip_prefix("//?/"))
273        .unwrap_or(text);
274    let mut chars = bare.chars();
275    let letter = chars.next()?;
276    if !letter.is_ascii_alphabetic() || chars.next()? != ':' {
277        return None;
278    }
279    Some(format!(r"{letter}:\"))
280}
281
282/// Total bytes under `path`, without following symlinks.
283///
284/// A symlinked directory counts as the link itself, not its contents: mutable
285/// worktrees are real directories, and following an accidental link into a
286/// clone of the repository would count the same bytes twice.
287pub fn dir_size(path: &Path) -> u64 {
288    let Ok(meta) = std::fs::symlink_metadata(path) else {
289        return 0;
290    };
291    if meta.is_file() {
292        return meta.len();
293    }
294    if !meta.is_dir() {
295        return 0;
296    }
297    let mut total = 0u64;
298    let mut stack = vec![path.to_path_buf()];
299    while let Some(dir) = stack.pop() {
300        let Ok(rd) = std::fs::read_dir(&dir) else {
301            continue;
302        };
303        for entry in rd.flatten() {
304            // `DirEntry::metadata` reports the entry itself, so a symlink is
305            // never traversed.
306            let Ok(meta) = entry.metadata() else {
307                continue;
308            };
309            if meta.is_dir() {
310                stack.push(entry.path());
311            } else if meta.is_file() {
312                total += meta.len();
313            }
314        }
315    }
316    total
317}
318
319/// What a prune removed, for the report.
320#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
321pub struct Prune {
322    /// Bytes actually freed.
323    pub freed: u64,
324    /// Files deleted.
325    pub files: usize,
326    /// Bytes still under the directory afterwards.
327    pub remaining: u64,
328}
329
330/// What [`plan_prune`] would remove, without removing it.
331///
332/// Oldest-first, the same order [`prune_dir`] deletes in — a caller previewing
333/// the plan (`magi cache prune --dry-run`) must see exactly what an immediate
334/// `prune_dir` call would do, not an approximation of it.
335#[derive(Debug, Clone, Default, PartialEq, Eq)]
336pub struct PrunePlan {
337    /// Files this plan would delete, oldest-first, paired with their size.
338    pub files: Vec<(PathBuf, u64)>,
339    /// Bytes this plan would free, assuming every listed file is removable.
340    pub freed: u64,
341    /// Bytes that would remain under the directory once the plan applies
342    /// cleanly.
343    pub remaining: u64,
344}
345
346/// The selection [`prune_dir`] would act on *if every one of these deletions
347/// succeeds*, computed without touching disk.
348///
349/// Read-only on purpose: a preview must never take the cache's lease (see
350/// `cache::maintenance_prune`'s doc) — it does not write anything, so it
351/// cannot race a build the way a real prune would, and a caller only wanting
352/// to show an operator a plan should not have to wait out contention to do
353/// it. This is necessarily an idealized selection, not a prediction of every
354/// file [`prune_dir`] will end up touching: a build finishing between the
355/// preview and a real prune can change what gets deleted, and so can a single
356/// locked file on the real pass, which - unlike this preview - has to keep
357/// reaching past its own plan for another (newer) file when an older one it
358/// counted on turns out to be unremovable. The CLI surface that shows this
359/// preview says so.
360#[must_use]
361pub fn plan_prune(dir: &Path, limit: u64) -> PrunePlan {
362    let Some(tree) = Tree::of(dir) else {
363        return PrunePlan::default();
364    };
365    let mut total = tree.total;
366    if !over_limit(total, limit) {
367        return PrunePlan {
368            files: Vec::new(),
369            freed: 0,
370            remaining: total,
371        };
372    }
373    let mut freed = 0u64;
374    let mut files = Vec::new();
375    for (_, size, path) in &tree.files {
376        if !over_limit(total, limit) {
377            break;
378        }
379        total = total.saturating_sub(*size);
380        freed += *size;
381        files.push((path.clone(), *size));
382    }
383    PrunePlan {
384        files,
385        freed,
386        remaining: total,
387    }
388}
389
390/// Delete files under `dir` oldest-first until its size is at or below `limit`.
391///
392/// The comparison is [`over_limit`], so a directory exactly at the cap is left
393/// alone. Oldest-first keeps the newest generation of artifacts — the one the
394/// next run reuses — and sheds the generations that only compile history. A
395/// deleted file costs the next build a rebuild of that one unit; deleting the
396/// whole directory would cost it everything, which is precisely the work
397/// [`prune_dir`] is keeping for it.
398///
399/// Empty directories left behind are swept depth-first, so cargo's deep
400/// `fingerprint`/`deps` trees do not outlive the files that made them.
401///
402/// Nothing is deleted when the directory is missing.
403pub fn prune_dir(dir: &Path, limit: u64) -> Result<Prune> {
404    let Some(tree) = Tree::of(dir) else {
405        return Ok(Prune {
406            freed: 0,
407            files: 0,
408            remaining: 0,
409        });
410    };
411    let mut total = tree.total;
412    if !over_limit(total, limit) {
413        return Ok(Prune {
414            freed: 0,
415            files: 0,
416            remaining: total,
417        });
418    }
419    let mut freed = 0u64;
420    let mut removed = 0usize;
421    // Deliberately walks every file in `tree`, not a fixed plan computed up
422    // front: `total` only drops on a successful removal, so a file that is
423    // being read elsewhere (a concurrent build, a snapshot) and fails to
424    // delete on Windows costs this pass nothing but that one file - it is
425    // skipped, and the loop keeps reaching for the next-oldest file until the
426    // real, achieved total is at or below `limit` or there is nothing left to
427    // try. A version of this that instead deleted only a pre-computed
428    // selection would stop short of the cap on the first locked file, every
429    // pass, on exactly the machines where locked files are common.
430    for (_, size, path) in &tree.files {
431        if !over_limit(total, limit) {
432            break;
433        }
434        if std::fs::remove_file(path).is_ok() {
435            total = total.saturating_sub(*size);
436            freed += *size;
437            removed += 1;
438        }
439    }
440    strip_empty_dirs(&tree.dirs);
441    Ok(Prune {
442        freed,
443        files: removed,
444        remaining: total,
445    })
446}
447
448/// The one file a prune never selects, when it sits directly in the pruned root.
449///
450/// cargo writes `CACHEDIR.TAG` once, when it creates a target directory, so in
451/// a long-lived cache it is the oldest file and an oldest-first prune reaches it
452/// first. Without it `cargo clean -p` refuses ("missing or invalid
453/// `CACHEDIR.TAG` file"). The tag belongs to the cargo that made it, not to the
454/// prune. It still counts toward the directory's size; a same-named file in a
455/// subdirectory is an ordinary candidate.
456const PRESERVED_AT_ROOT: &str = "CACHEDIR.TAG";
457
458/// Files and directories under one root, walked up-front.
459///
460/// `files` are the deletion candidates; `total` also counts what is preserved.
461struct Tree {
462    total: u64,
463    files: Vec<(u128, u64, PathBuf)>,
464    dirs: Vec<(usize, PathBuf)>,
465}
466
467impl Tree {
468    /// Walk `dir`, collecting files (mtime-nanoseconds, size, path) and
469    /// directories (depth, path). `None` when the directory does not exist.
470    fn of(dir: &Path) -> Option<Tree> {
471        if dir.symlink_metadata().ok()?.is_dir() {
472            Some(Tree::from_dir(dir))
473        } else {
474            None
475        }
476    }
477
478    fn from_dir(dir: &Path) -> Tree {
479        let mut total = 0u64;
480        let mut files = Vec::new();
481        let mut dirs = Vec::new();
482        // Depth-first so directories are recorded before their contents; the
483        // dir list is then sorted by descending depth for the sweep.
484        let mut stack: Vec<(usize, PathBuf)> = vec![(0, dir.to_path_buf())];
485        while let Some((depth, d)) = stack.pop() {
486            let Ok(rd) = std::fs::read_dir(&d) else {
487                continue;
488            };
489            for entry in rd.flatten() {
490                let path = entry.path();
491                let Ok(meta) = path.symlink_metadata() else {
492                    continue;
493                };
494                #[cfg(windows)]
495                {
496                    use std::os::windows::fs::MetadataExt as _;
497                    if meta.file_attributes() & 0x400 != 0 {
498                        continue;
499                    }
500                }
501                if meta.is_dir() {
502                    dirs.push((depth + 1, path.clone()));
503                    stack.push((depth + 1, path));
504                } else if meta.is_file() {
505                    let size = meta.len();
506                    total += size;
507                    if depth == 0
508                        && (entry.file_name() == PRESERVED_AT_ROOT
509                            || entry.file_name() == crate::build_target::TARGET_PROOF)
510                    {
511                        continue;
512                    }
513                    let mtime = meta
514                        .modified()
515                        .ok()
516                        .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
517                        .map(|d| d.as_nanos())
518                        .unwrap_or(0);
519                    files.push((mtime, size, path));
520                }
521            }
522        }
523        // Oldest first, and on a tie the larger file: a whole generation of
524        // cargo artifacts is written within one filesystem timestamp tick, so
525        // mtime alone leaves the order to `read_dir` and the sort's
526        // instability - the same cache pruned twice would shed different
527        // files, and a test over two same-tick files passed on one platform
528        // and failed on another. Larger-first also reaches the cap in fewer
529        // deletions, which is fewer rebuilt units for the next run.
530        files.sort_unstable_by(|a, b| a.0.cmp(&b.0).then(b.1.cmp(&a.1)).then(a.2.cmp(&b.2)));
531        Tree { total, files, dirs }
532    }
533}
534
535/// Remove empty directories, deepest first, never the root itself.
536fn strip_empty_dirs(dirs: &[(usize, PathBuf)]) {
537    let mut by_depth: Vec<&PathBuf> = dirs.iter().map(|(_, d)| d).collect();
538    by_depth.sort_unstable_by_key(|d| std::cmp::Reverse(d.iter().count()));
539    for d in by_depth {
540        let _ = std::fs::remove_dir(d);
541    }
542}
543
544#[cfg(test)]
545mod tests {
546    #[test]
547    fn gate_reasons_follow_the_language_and_stay_recognisable() {
548        let en = gate_in(1, 10, "en").unwrap();
549        let ja = gate_in(1, 10, "日本語").unwrap();
550        assert_eq!(gate_in(1, 10, "fr").unwrap(), en);
551        assert_eq!(en, gate(1, 10).unwrap());
552        assert!(ja.starts_with("run を開始"), "{ja}");
553        assert!(is_gate_reason(&en) && is_gate_reason(&ja));
554        let p = std::path::Path::new("/x");
555        let m_ja = unmeasured_in(p, &"boom", "ja");
556        assert!(m_ja.contains("測定できませんでした") && is_gate_reason(&m_ja));
557        assert!(is_gate_reason(&unmeasured_in(p, &"boom", "en")));
558        assert!(!is_gate_reason("something else"));
559    }
560
561    #[test]
562    fn disk_wait_legacy_reasons_require_the_complete_gate_format() {
563        let observed = "run を開始するための空き容量が不足しています: 空き 1477050368 バイト、`[disk] min_free_bytes` の要求は 3221225472 バイト";
564        assert!(is_gate_reason(observed));
565        assert!(is_gate_reason(
566            &gate_in(1477050368, 3221225472, "en").unwrap()
567        ));
568        for ambiguous in [
569            format!("{observed}\nowner: hold"),
570            format!("{SHORT_EN} inspect manually"),
571            format!("{RUNNING_PRESSURE}unknown"),
572            format!("{UNMEASURED_EN}/tmp (error)"),
573        ] {
574            assert!(!is_gate_reason(&ambiguous), "{ambiguous}");
575        }
576    }
577
578    use super::*;
579    use std::fs;
580
581    #[test]
582    fn the_free_space_predicate_is_the_boundary() {
583        assert!(enough_space(100, 100));
584        assert!(enough_space(101, 100));
585        assert!(!enough_space(99, 100));
586        // A zero floor disables the gate: the operator opted out.
587        assert!(enough_space(0, 0));
588    }
589
590    #[test]
591    fn the_gate_text_conveys_both_numbers_and_opens_with_room() {
592        assert_eq!(
593            gate(9, 10).expect("closed"),
594            "not enough free space to start a run: 9 bytes free, 10 required by `[disk] min_free_bytes`"
595        );
596        assert_eq!(gate(10, 10), None, "exactly at the floor is open");
597        assert_eq!(gate(10_000, 0), None, "a zero floor is an opt-out");
598    }
599
600    #[test]
601    fn over_limit_uses_strict_greater_than() {
602        assert!(over_limit(11, 10));
603        assert!(!over_limit(10, 10));
604        assert!(!over_limit(9, 10));
605    }
606
607    #[test]
608    fn df_row_parses_1024_blocks_into_bytes() {
609        let row = "/dev/sda1 976762584 808522388 168240196 83% /home";
610        assert_eq!(parse_df_available(row), Some(168_240_196 * 1024));
611        assert_eq!(parse_df_available("garbage"), None);
612        assert_eq!(parse_df_available("a b c x"), None);
613    }
614
615    /// The volume query takes a drive root; the queue hands out verbatim paths.
616    #[test]
617    fn the_volume_root_is_a_drive_not_the_path_it_came_from() {
618        // The form that closed the gate on every queued task: the queue
619        // records the repo as `\\?\C:\...`.
620        assert_eq!(
621            volume_root(Path::new(
622                r"\\?\C:\Users\yukimemi\src\github.com\yukimemi\magi"
623            )),
624            Some(r"C:\".to_owned())
625        );
626        assert_eq!(
627            volume_root(Path::new(r"C:\Users\yukimemi")),
628            Some(r"C:\".to_owned())
629        );
630        assert_eq!(volume_root(Path::new(r"D:\")), Some(r"D:\".to_owned()));
631        // Forward slashes reach magi from configs written by hand.
632        assert_eq!(
633            volume_root(Path::new("C:/Users/yukimemi/src")),
634            Some(r"C:\".to_owned())
635        );
636        // No drive to name: UNC shares are unsupported, and a POSIX path has no
637        // drive volume. The caller has to report that it cannot measure.
638        assert_eq!(volume_root(Path::new(r"\\server\share\dir")), None);
639        assert_eq!(volume_root(Path::new(r"\\?\UNC\server\share")), None);
640        assert_eq!(volume_root(Path::new("/home/yukimemi")), None);
641    }
642
643    #[test]
644    fn the_cache_dir_is_read_back_out_of_a_rendered_command() {
645        let cmd = r"CARGO_TARGET_DIR=C:\Users\me\Temp\magi-target cargo make check";
646        assert_eq!(
647            extract_cargo_target_dir(cmd),
648            Some(PathBuf::from(r"C:\Users\me\Temp\magi-target"))
649        );
650        // Quoted forms survive spaces; a config with none stays None.
651        assert_eq!(
652            extract_cargo_target_dir(r"CARGO_TARGET_DIR='/tmp/a b' cargo test"),
653            Some(PathBuf::from("/tmp/a b"))
654        );
655        assert_eq!(
656            extract_cargo_target_dir(r#"CARGO_TARGET_DIR="/tmp/qq" cargo test"#),
657            Some(PathBuf::from("/tmp/qq"))
658        );
659        assert_eq!(extract_cargo_target_dir("cargo make check"), None);
660        assert_eq!(extract_cargo_target_dir("CARGO_TARGET_DIR="), None);
661        // Second occurrence is irrelevant: the first is what the build used
662        // (a command's environment applies once).
663        let two = "CARGO_TARGET_DIR=/first and CARGO_TARGET_DIR=/second cargo x";
664        assert_eq!(extract_cargo_target_dir(two), Some(PathBuf::from("/first")));
665    }
666
667    #[test]
668    fn dir_size_is_zero_for_missing_and_counts_files_without_following_links() {
669        let t = tempfile::TempDir::new().expect("temp");
670        assert_eq!(dir_size(&t.path().join("nope")), 0);
671        fs::write(t.path().join("a"), b"12345").expect("write");
672        fs::create_dir(t.path().join("sub")).expect("dir");
673        fs::write(t.path().join("sub").join("b"), b"678").expect("write");
674        assert_eq!(dir_size(t.path()), 8);
675        #[cfg(unix)]
676        {
677            std::os::unix::fs::symlink(t.path().join("sub"), t.path().join("link"))
678                .expect("symlink");
679            assert_eq!(dir_size(t.path()), 8, "a link is counted as a link");
680        }
681    }
682
683    #[test]
684    fn prune_deletes_oldest_first_until_the_cap_is_met() {
685        let t = tempfile::TempDir::new().expect("temp");
686        let old = t.path().join("old");
687        fs::write(&old, b"yyyy").expect("write");
688        // Give the older file a measurably older mtime; a second is past the
689        // granularity of the filesystems magi runs on.
690        std::thread::sleep(std::time::Duration::from_millis(1_200));
691        fs::write(t.path().join("new"), b"xxxxx").expect("write");
692
693        // Cap above the total: nothing moves.
694        let keep = prune_dir(t.path(), 9).expect("prune");
695        assert_eq!(
696            keep,
697            Prune {
698                freed: 0,
699                files: 0,
700                remaining: 9
701            }
702        );
703
704        // Cap below: the oldest file goes, the new one stays.
705        let pruned = prune_dir(t.path(), 6).expect("prune");
706        assert!(pruned.freed > 0);
707        assert_eq!(pruned.files, 1);
708        assert_eq!(pruned.remaining, 5);
709        assert!(!old.exists(), "the older file is the one shed");
710        assert!(t.path().join("new").exists());
711    }
712
713    #[test]
714    fn plan_prune_selects_what_prune_dir_would_delete_without_deleting_it() {
715        let t = tempfile::TempDir::new().expect("temp");
716        let old = t.path().join("old");
717        fs::write(&old, b"yyyy").expect("write");
718        std::thread::sleep(std::time::Duration::from_millis(1_200));
719        fs::write(t.path().join("new"), b"xxxxx").expect("write");
720
721        let plan = plan_prune(t.path(), 6);
722        assert_eq!(plan.files, vec![(old.clone(), 4)]);
723        assert_eq!(plan.freed, 4);
724        assert_eq!(plan.remaining, 5);
725        assert!(old.exists(), "a plan never deletes anything");
726        assert!(t.path().join("new").exists());
727
728        // Applying `prune_dir` afterwards removes exactly what the plan named.
729        let pruned = prune_dir(t.path(), 6).expect("prune");
730        assert_eq!(pruned.freed, plan.freed);
731        assert_eq!(pruned.remaining, plan.remaining);
732        assert!(!old.exists());
733    }
734
735    #[test]
736    fn a_root_cachedir_tag_survives_even_as_the_oldest_file() {
737        // cargo writes the tag once, so in a long-lived cache it is the oldest
738        // file and used to be the first thing an over-cap prune deleted.
739        let t = tempfile::TempDir::new().expect("temp");
740        let tag = t.path().join("CACHEDIR.TAG");
741        fs::write(&tag, b"Signature: x").expect("write");
742        let f = fs::File::options().write(true).open(&tag).expect("open");
743        f.set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(1))
744            .expect("mtime");
745        drop(f);
746        fs::write(t.path().join("a"), b"aaaa").expect("write");
747        fs::create_dir(t.path().join("sub")).expect("mkdir");
748        // A same-named file below the root is an ordinary candidate.
749        let nested = t.path().join("sub").join("CACHEDIR.TAG");
750        fs::write(&nested, b"nn").expect("write");
751
752        let limit = 12; // exactly the tag: every other file must go
753        let plan = plan_prune(t.path(), limit);
754        assert!(plan.files.iter().all(|(p, _)| p != &tag));
755        assert!(plan.remaining <= limit);
756
757        let pruned = prune_dir(t.path(), limit).expect("prune");
758        assert!(tag.exists(), "the root tag is never pruned");
759        assert!(pruned.remaining <= limit);
760        assert_eq!(pruned.freed, plan.freed);
761        assert_eq!(pruned.remaining, plan.remaining);
762        assert!(!nested.exists());
763    }
764
765    #[test]
766    fn plan_prune_is_empty_under_the_cap_and_for_a_missing_dir() {
767        let t = tempfile::TempDir::new().expect("temp");
768        fs::write(t.path().join("a"), b"12345").expect("write");
769        let plan = plan_prune(t.path(), 100);
770        assert_eq!(
771            plan,
772            PrunePlan {
773                files: Vec::new(),
774                freed: 0,
775                remaining: 5,
776            }
777        );
778
779        assert_eq!(
780            plan_prune(&t.path().join("absent"), 0),
781            PrunePlan::default()
782        );
783    }
784
785    /// A file `prune_dir` cannot delete - locked by a concurrent reader on
786    /// Windows, the exact scenario the function's own doc calls out - must
787    /// not make the pass stop short of the cap. The achieved total only
788    /// drops on a successful removal, so the loop has to keep reaching for
789    /// newer files until *that* total clears `limit`, not stop once a
790    /// precomputed selection runs out.
791    #[cfg(windows)]
792    #[test]
793    fn prune_keeps_reaching_past_an_undeletable_file_to_still_reach_the_cap() {
794        use std::os::windows::fs::OpenOptionsExt as _;
795
796        let t = tempfile::TempDir::new().expect("temp");
797        let old = t.path().join("old");
798        fs::write(&old, b"yyyy").expect("write");
799        std::thread::sleep(std::time::Duration::from_millis(1_200));
800        let mid = t.path().join("mid");
801        fs::write(&mid, b"zzzz").expect("write");
802        std::thread::sleep(std::time::Duration::from_millis(1_200));
803        let new = t.path().join("new");
804        fs::write(&new, b"xxxxx").expect("write");
805
806        // A share mode of 0 denies every other handle, including a delete -
807        // standing in for a file a concurrent build still has open, which is
808        // exactly the case `prune_dir`'s own doc calls out.
809        let lock = std::fs::OpenOptions::new()
810            .read(true)
811            .share_mode(0)
812            .open(&old)
813            .expect("lock the old file exclusively");
814
815        let pruned = prune_dir(t.path(), 8).expect("prune");
816        drop(lock);
817
818        assert!(old.exists(), "the locked file could not be deleted");
819        assert!(!mid.exists(), "the next-oldest file was tried and removed");
820        assert!(
821            !new.exists(),
822            "pruning kept reaching for newer files until the cap was actually met, \
823             not just until a fixed selection ran out"
824        );
825        assert!(
826            pruned.remaining <= 8,
827            "the achieved total must reach the cap: {pruned:?}"
828        );
829    }
830
831    #[test]
832    fn prune_leaves_a_missing_dir_alone() {
833        let t = tempfile::TempDir::new().expect("temp");
834        let out = prune_dir(&t.path().join("absent"), 1).expect("prune");
835        assert_eq!(out, Prune::default());
836    }
837
838    #[test]
839    fn prune_sweeps_directories_the_files_leave_empty() {
840        let t = tempfile::TempDir::new().expect("temp");
841        let deep = t.path().join("a").join("b").join("c");
842        fs::create_dir_all(&deep).expect("dirs");
843        fs::write(deep.join("f"), b"1234").expect("write");
844        let out = prune_dir(t.path(), 0).expect("prune");
845        assert_eq!(out.files, 1);
846        assert_eq!(out.remaining, 0);
847        assert!(!t.path().join("a").exists(), "empty chain swept");
848    }
849}
850
851#[cfg(test)]
852mod pressure_tests {
853    #[test]
854    fn pressure_reclaims_below_cap_without_underflow() {
855        assert_eq!(super::pressure_limit(100, 200, 80, 30), 50);
856        assert_eq!(super::pressure_limit(100, 200, 80, 100), 100);
857        assert_eq!(super::pressure_limit(100, 200, 800, 0), 0);
858        assert_eq!(super::pressure_limit(100, 50, 0, 0), 50);
859    }
860}