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