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    #[link(name = "kernel32")]
203    unsafe extern "system" {
204        fn GetDiskFreeSpaceExW(
205            directory: *const u16,
206            available: *mut u64,
207            total: *mut u64,
208            free: *mut u64,
209        ) -> i32;
210    }
211    // Keep the existing drive-root contract, including verbatim drive paths
212    // and refusal of UNC shares; do not depend on the queried directory existing.
213    let abs = std::path::absolute(path)
214        .with_context(|| format!("absolute path for {}", path.display()))?;
215    let root = volume_root(&abs)
216        .with_context(|| format!("no volume root in {} to measure", abs.display()))?;
217    // Encode the complete root and append NUL explicitly, without assuming
218    // a fixed root length when constructing the native API buffer.
219    let wide: Vec<u16> = root.encode_utf16().chain(std::iter::once(0)).collect();
220    let mut available = 0_u64;
221    // SAFETY: wide is a live NUL-terminated UTF-16 root, available is writable,
222    // and the optional totals are null. The API retains none of these pointers.
223    if unsafe {
224        GetDiskFreeSpaceExW(
225            wide.as_ptr(),
226            &mut available,
227            std::ptr::null_mut(),
228            std::ptr::null_mut(),
229        )
230    } == 0
231    {
232        let error = std::io::Error::last_os_error();
233        return Err(error).with_context(|| format!("query free bytes for {}", abs.display()));
234    }
235    // Caller-available bytes, not total free bytes: preserve quota semantics.
236    Ok(available)
237}
238
239/// One `df -k -P` data row: `Filesystem 1024-blocks Used Available …`.
240///
241/// The value is 1024-byte blocks, so the parse returns bytes.
242pub fn parse_df_available(line: &str) -> Option<u64> {
243    let mut fields = line.split_whitespace();
244    fields.next()?; // filesystem
245    fields.next()?; // 1024-blocks
246    fields.next()?; // used
247    let blocks: u64 = fields.next()?.parse().ok()?;
248    Some(blocks.saturating_mul(1024))
249}
250
251/// The drive root used for querying an absolute Windows path:
252/// `C:\`, never `C:\Users\...` and never a verbatim `\\?\C:\...`.
253///
254/// Pure and platform-independent so the verbatim form - which is what the
255/// queue stores and what closed the disk gate on every task - is asserted
256/// without a Windows runner. `None` when there is no drive letter to name:
257/// UNC shares remain unsupported, rather than inventing a drive volume.
258pub fn volume_root(path: &Path) -> Option<String> {
259    let text = path.to_str()?;
260    // Verbatim (`\\?\C:\x`) and verbatim-UNC (`\\?\UNC\server\share`) prefixes.
261    let bare = text
262        .strip_prefix(r"\\?\")
263        .or_else(|| text.strip_prefix("//?/"))
264        .unwrap_or(text);
265    let mut chars = bare.chars();
266    let letter = chars.next()?;
267    if !letter.is_ascii_alphabetic() || chars.next()? != ':' {
268        return None;
269    }
270    Some(format!(r"{letter}:\"))
271}
272
273/// Total bytes under `path`, without following symlinks.
274///
275/// A symlinked directory counts as the link itself, not its contents: mutable
276/// worktrees are real directories, and following an accidental link into a
277/// clone of the repository would count the same bytes twice.
278pub fn dir_size(path: &Path) -> u64 {
279    let Ok(meta) = std::fs::symlink_metadata(path) else {
280        return 0;
281    };
282    if meta.is_file() {
283        return meta.len();
284    }
285    if !meta.is_dir() {
286        return 0;
287    }
288    let mut total = 0u64;
289    let mut stack = vec![path.to_path_buf()];
290    while let Some(dir) = stack.pop() {
291        let Ok(rd) = std::fs::read_dir(&dir) else {
292            continue;
293        };
294        for entry in rd.flatten() {
295            // `DirEntry::metadata` reports the entry itself, so a symlink is
296            // never traversed.
297            let Ok(meta) = entry.metadata() else {
298                continue;
299            };
300            if meta.is_dir() {
301                stack.push(entry.path());
302            } else if meta.is_file() {
303                total += meta.len();
304            }
305        }
306    }
307    total
308}
309
310/// What a prune removed, for the report.
311#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
312pub struct Prune {
313    /// Bytes actually freed.
314    pub freed: u64,
315    /// Files deleted.
316    pub files: usize,
317    /// Bytes still under the directory afterwards.
318    pub remaining: u64,
319}
320
321/// What [`plan_prune`] would remove, without removing it.
322///
323/// Oldest-first, the same order [`prune_dir`] deletes in — a caller previewing
324/// the plan (`magi cache prune --dry-run`) must see exactly what an immediate
325/// `prune_dir` call would do, not an approximation of it.
326#[derive(Debug, Clone, Default, PartialEq, Eq)]
327pub struct PrunePlan {
328    /// Files this plan would delete, oldest-first, paired with their size.
329    pub files: Vec<(PathBuf, u64)>,
330    /// Bytes this plan would free, assuming every listed file is removable.
331    pub freed: u64,
332    /// Bytes that would remain under the directory once the plan applies
333    /// cleanly.
334    pub remaining: u64,
335}
336
337/// The selection [`prune_dir`] would act on *if every one of these deletions
338/// succeeds*, computed without touching disk.
339///
340/// Read-only on purpose: a preview must never take the cache's lease (see
341/// `cache::maintenance_prune`'s doc) — it does not write anything, so it
342/// cannot race a build the way a real prune would, and a caller only wanting
343/// to show an operator a plan should not have to wait out contention to do
344/// it. This is necessarily an idealized selection, not a prediction of every
345/// file [`prune_dir`] will end up touching: a build finishing between the
346/// preview and a real prune can change what gets deleted, and so can a single
347/// locked file on the real pass, which - unlike this preview - has to keep
348/// reaching past its own plan for another (newer) file when an older one it
349/// counted on turns out to be unremovable. The CLI surface that shows this
350/// preview says so.
351#[must_use]
352pub fn plan_prune(dir: &Path, limit: u64) -> PrunePlan {
353    let Some(tree) = Tree::of(dir) else {
354        return PrunePlan::default();
355    };
356    let mut total = tree.total;
357    if !over_limit(total, limit) {
358        return PrunePlan {
359            files: Vec::new(),
360            freed: 0,
361            remaining: total,
362        };
363    }
364    let mut freed = 0u64;
365    let mut files = Vec::new();
366    for (_, size, path) in &tree.files {
367        if !over_limit(total, limit) {
368            break;
369        }
370        total = total.saturating_sub(*size);
371        freed += *size;
372        files.push((path.clone(), *size));
373    }
374    PrunePlan {
375        files,
376        freed,
377        remaining: total,
378    }
379}
380
381/// Delete files under `dir` oldest-first until its size is at or below `limit`.
382///
383/// The comparison is [`over_limit`], so a directory exactly at the cap is left
384/// alone. Oldest-first keeps the newest generation of artifacts — the one the
385/// next run reuses — and sheds the generations that only compile history. A
386/// deleted file costs the next build a rebuild of that one unit; deleting the
387/// whole directory would cost it everything, which is precisely the work
388/// [`prune_dir`] is keeping for it.
389///
390/// Empty directories left behind are swept depth-first, so cargo's deep
391/// `fingerprint`/`deps` trees do not outlive the files that made them.
392///
393/// Nothing is deleted when the directory is missing.
394pub fn prune_dir(dir: &Path, limit: u64) -> Result<Prune> {
395    let Some(tree) = Tree::of(dir) else {
396        return Ok(Prune {
397            freed: 0,
398            files: 0,
399            remaining: 0,
400        });
401    };
402    let mut total = tree.total;
403    if !over_limit(total, limit) {
404        return Ok(Prune {
405            freed: 0,
406            files: 0,
407            remaining: total,
408        });
409    }
410    let mut freed = 0u64;
411    let mut removed = 0usize;
412    // Deliberately walks every file in `tree`, not a fixed plan computed up
413    // front: `total` only drops on a successful removal, so a file that is
414    // being read elsewhere (a concurrent build, a snapshot) and fails to
415    // delete on Windows costs this pass nothing but that one file - it is
416    // skipped, and the loop keeps reaching for the next-oldest file until the
417    // real, achieved total is at or below `limit` or there is nothing left to
418    // try. A version of this that instead deleted only a pre-computed
419    // selection would stop short of the cap on the first locked file, every
420    // pass, on exactly the machines where locked files are common.
421    for (_, size, path) in &tree.files {
422        if !over_limit(total, limit) {
423            break;
424        }
425        if std::fs::remove_file(path).is_ok() {
426            total = total.saturating_sub(*size);
427            freed += *size;
428            removed += 1;
429        }
430    }
431    strip_empty_dirs(&tree.dirs);
432    Ok(Prune {
433        freed,
434        files: removed,
435        remaining: total,
436    })
437}
438
439/// The one file a prune never selects, when it sits directly in the pruned root.
440///
441/// cargo writes `CACHEDIR.TAG` once, when it creates a target directory, so in
442/// a long-lived cache it is the oldest file and an oldest-first prune reaches it
443/// first. Without it `cargo clean -p` refuses ("missing or invalid
444/// `CACHEDIR.TAG` file"). The tag belongs to the cargo that made it, not to the
445/// prune. It still counts toward the directory's size; a same-named file in a
446/// subdirectory is an ordinary candidate.
447const PRESERVED_AT_ROOT: &str = "CACHEDIR.TAG";
448
449/// Files and directories under one root, walked up-front.
450///
451/// `files` are the deletion candidates; `total` also counts what is preserved.
452struct Tree {
453    total: u64,
454    files: Vec<(u128, u64, PathBuf)>,
455    dirs: Vec<(usize, PathBuf)>,
456}
457
458impl Tree {
459    /// Walk `dir`, collecting files (mtime-nanoseconds, size, path) and
460    /// directories (depth, path). `None` when the directory does not exist.
461    fn of(dir: &Path) -> Option<Tree> {
462        if dir.symlink_metadata().ok()?.is_dir() {
463            Some(Tree::from_dir(dir))
464        } else {
465            None
466        }
467    }
468
469    fn from_dir(dir: &Path) -> Tree {
470        let mut total = 0u64;
471        let mut files = Vec::new();
472        let mut dirs = Vec::new();
473        // Depth-first so directories are recorded before their contents; the
474        // dir list is then sorted by descending depth for the sweep.
475        let mut stack: Vec<(usize, PathBuf)> = vec![(0, dir.to_path_buf())];
476        while let Some((depth, d)) = stack.pop() {
477            let Ok(rd) = std::fs::read_dir(&d) else {
478                continue;
479            };
480            for entry in rd.flatten() {
481                let Ok(meta) = entry.metadata() else {
482                    continue;
483                };
484                let path = entry.path();
485                if meta.is_dir() {
486                    dirs.push((depth + 1, path.clone()));
487                    stack.push((depth + 1, path));
488                } else if meta.is_file() {
489                    let size = meta.len();
490                    total += size;
491                    if depth == 0 && entry.file_name() == PRESERVED_AT_ROOT {
492                        continue;
493                    }
494                    let mtime = meta
495                        .modified()
496                        .ok()
497                        .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
498                        .map(|d| d.as_nanos())
499                        .unwrap_or(0);
500                    files.push((mtime, size, path));
501                }
502            }
503        }
504        // Oldest first, and on a tie the larger file: a whole generation of
505        // cargo artifacts is written within one filesystem timestamp tick, so
506        // mtime alone leaves the order to `read_dir` and the sort's
507        // instability - the same cache pruned twice would shed different
508        // files, and a test over two same-tick files passed on one platform
509        // and failed on another. Larger-first also reaches the cap in fewer
510        // deletions, which is fewer rebuilt units for the next run.
511        files.sort_unstable_by(|a, b| a.0.cmp(&b.0).then(b.1.cmp(&a.1)).then(a.2.cmp(&b.2)));
512        Tree { total, files, dirs }
513    }
514}
515
516/// Remove empty directories, deepest first, never the root itself.
517fn strip_empty_dirs(dirs: &[(usize, PathBuf)]) {
518    let mut by_depth: Vec<&PathBuf> = dirs.iter().map(|(_, d)| d).collect();
519    by_depth.sort_unstable_by_key(|d| std::cmp::Reverse(d.iter().count()));
520    for d in by_depth {
521        let _ = std::fs::remove_dir(d);
522    }
523}
524
525#[cfg(test)]
526mod tests {
527    #[test]
528    fn gate_reasons_follow_the_language_and_stay_recognisable() {
529        let en = gate_in(1, 10, "en").unwrap();
530        let ja = gate_in(1, 10, "日本語").unwrap();
531        assert_eq!(gate_in(1, 10, "fr").unwrap(), en);
532        assert_eq!(en, gate(1, 10).unwrap());
533        assert!(ja.starts_with("run を開始"), "{ja}");
534        assert!(is_gate_reason(&en) && is_gate_reason(&ja));
535        let p = std::path::Path::new("/x");
536        let m_ja = unmeasured_in(p, &"boom", "ja");
537        assert!(m_ja.contains("測定できませんでした") && is_gate_reason(&m_ja));
538        assert!(is_gate_reason(&unmeasured_in(p, &"boom", "en")));
539        assert!(!is_gate_reason("something else"));
540    }
541
542    #[test]
543    fn disk_wait_legacy_reasons_require_the_complete_gate_format() {
544        let observed = "run を開始するための空き容量が不足しています: 空き 1477050368 バイト、`[disk] min_free_bytes` の要求は 3221225472 バイト";
545        assert!(is_gate_reason(observed));
546        assert!(is_gate_reason(
547            &gate_in(1477050368, 3221225472, "en").unwrap()
548        ));
549        for ambiguous in [
550            format!("{observed}\nowner: hold"),
551            format!("{SHORT_EN} inspect manually"),
552            format!("{RUNNING_PRESSURE}unknown"),
553            format!("{UNMEASURED_EN}/tmp (error)"),
554        ] {
555            assert!(!is_gate_reason(&ambiguous), "{ambiguous}");
556        }
557    }
558
559    use super::*;
560    use std::fs;
561
562    #[test]
563    fn the_free_space_predicate_is_the_boundary() {
564        assert!(enough_space(100, 100));
565        assert!(enough_space(101, 100));
566        assert!(!enough_space(99, 100));
567        // A zero floor disables the gate: the operator opted out.
568        assert!(enough_space(0, 0));
569    }
570
571    #[test]
572    fn the_gate_text_conveys_both_numbers_and_opens_with_room() {
573        assert_eq!(
574            gate(9, 10).expect("closed"),
575            "not enough free space to start a run: 9 bytes free, 10 required by `[disk] min_free_bytes`"
576        );
577        assert_eq!(gate(10, 10), None, "exactly at the floor is open");
578        assert_eq!(gate(10_000, 0), None, "a zero floor is an opt-out");
579    }
580
581    #[test]
582    fn over_limit_uses_strict_greater_than() {
583        assert!(over_limit(11, 10));
584        assert!(!over_limit(10, 10));
585        assert!(!over_limit(9, 10));
586    }
587
588    #[test]
589    fn df_row_parses_1024_blocks_into_bytes() {
590        let row = "/dev/sda1 976762584 808522388 168240196 83% /home";
591        assert_eq!(parse_df_available(row), Some(168_240_196 * 1024));
592        assert_eq!(parse_df_available("garbage"), None);
593        assert_eq!(parse_df_available("a b c x"), None);
594    }
595
596    /// The volume query takes a drive root; the queue hands out verbatim paths.
597    #[test]
598    fn the_volume_root_is_a_drive_not_the_path_it_came_from() {
599        // The form that closed the gate on every queued task: the queue
600        // records the repo as `\\?\C:\...`.
601        assert_eq!(
602            volume_root(Path::new(
603                r"\\?\C:\Users\yukimemi\src\github.com\yukimemi\magi"
604            )),
605            Some(r"C:\".to_owned())
606        );
607        assert_eq!(
608            volume_root(Path::new(r"C:\Users\yukimemi")),
609            Some(r"C:\".to_owned())
610        );
611        assert_eq!(volume_root(Path::new(r"D:\")), Some(r"D:\".to_owned()));
612        // Forward slashes reach magi from configs written by hand.
613        assert_eq!(
614            volume_root(Path::new("C:/Users/yukimemi/src")),
615            Some(r"C:\".to_owned())
616        );
617        // No drive to name: UNC shares are unsupported, and a POSIX path has no
618        // drive volume. The caller has to report that it cannot measure.
619        assert_eq!(volume_root(Path::new(r"\\server\share\dir")), None);
620        assert_eq!(volume_root(Path::new(r"\\?\UNC\server\share")), None);
621        assert_eq!(volume_root(Path::new("/home/yukimemi")), None);
622    }
623
624    #[test]
625    fn the_cache_dir_is_read_back_out_of_a_rendered_command() {
626        let cmd = r"CARGO_TARGET_DIR=C:\Users\me\Temp\magi-target cargo make check";
627        assert_eq!(
628            extract_cargo_target_dir(cmd),
629            Some(PathBuf::from(r"C:\Users\me\Temp\magi-target"))
630        );
631        // Quoted forms survive spaces; a config with none stays None.
632        assert_eq!(
633            extract_cargo_target_dir(r"CARGO_TARGET_DIR='/tmp/a b' cargo test"),
634            Some(PathBuf::from("/tmp/a b"))
635        );
636        assert_eq!(
637            extract_cargo_target_dir(r#"CARGO_TARGET_DIR="/tmp/qq" cargo test"#),
638            Some(PathBuf::from("/tmp/qq"))
639        );
640        assert_eq!(extract_cargo_target_dir("cargo make check"), None);
641        assert_eq!(extract_cargo_target_dir("CARGO_TARGET_DIR="), None);
642        // Second occurrence is irrelevant: the first is what the build used
643        // (a command's environment applies once).
644        let two = "CARGO_TARGET_DIR=/first and CARGO_TARGET_DIR=/second cargo x";
645        assert_eq!(extract_cargo_target_dir(two), Some(PathBuf::from("/first")));
646    }
647
648    #[test]
649    fn dir_size_is_zero_for_missing_and_counts_files_without_following_links() {
650        let t = tempfile::TempDir::new().expect("temp");
651        assert_eq!(dir_size(&t.path().join("nope")), 0);
652        fs::write(t.path().join("a"), b"12345").expect("write");
653        fs::create_dir(t.path().join("sub")).expect("dir");
654        fs::write(t.path().join("sub").join("b"), b"678").expect("write");
655        assert_eq!(dir_size(t.path()), 8);
656        #[cfg(unix)]
657        {
658            std::os::unix::fs::symlink(t.path().join("sub"), t.path().join("link"))
659                .expect("symlink");
660            assert_eq!(dir_size(t.path()), 8, "a link is counted as a link");
661        }
662    }
663
664    #[test]
665    fn prune_deletes_oldest_first_until_the_cap_is_met() {
666        let t = tempfile::TempDir::new().expect("temp");
667        let old = t.path().join("old");
668        fs::write(&old, b"yyyy").expect("write");
669        // Give the older file a measurably older mtime; a second is past the
670        // granularity of the filesystems magi runs on.
671        std::thread::sleep(std::time::Duration::from_millis(1_200));
672        fs::write(t.path().join("new"), b"xxxxx").expect("write");
673
674        // Cap above the total: nothing moves.
675        let keep = prune_dir(t.path(), 9).expect("prune");
676        assert_eq!(
677            keep,
678            Prune {
679                freed: 0,
680                files: 0,
681                remaining: 9
682            }
683        );
684
685        // Cap below: the oldest file goes, the new one stays.
686        let pruned = prune_dir(t.path(), 6).expect("prune");
687        assert!(pruned.freed > 0);
688        assert_eq!(pruned.files, 1);
689        assert_eq!(pruned.remaining, 5);
690        assert!(!old.exists(), "the older file is the one shed");
691        assert!(t.path().join("new").exists());
692    }
693
694    #[test]
695    fn plan_prune_selects_what_prune_dir_would_delete_without_deleting_it() {
696        let t = tempfile::TempDir::new().expect("temp");
697        let old = t.path().join("old");
698        fs::write(&old, b"yyyy").expect("write");
699        std::thread::sleep(std::time::Duration::from_millis(1_200));
700        fs::write(t.path().join("new"), b"xxxxx").expect("write");
701
702        let plan = plan_prune(t.path(), 6);
703        assert_eq!(plan.files, vec![(old.clone(), 4)]);
704        assert_eq!(plan.freed, 4);
705        assert_eq!(plan.remaining, 5);
706        assert!(old.exists(), "a plan never deletes anything");
707        assert!(t.path().join("new").exists());
708
709        // Applying `prune_dir` afterwards removes exactly what the plan named.
710        let pruned = prune_dir(t.path(), 6).expect("prune");
711        assert_eq!(pruned.freed, plan.freed);
712        assert_eq!(pruned.remaining, plan.remaining);
713        assert!(!old.exists());
714    }
715
716    #[test]
717    fn a_root_cachedir_tag_survives_even_as_the_oldest_file() {
718        // cargo writes the tag once, so in a long-lived cache it is the oldest
719        // file and used to be the first thing an over-cap prune deleted.
720        let t = tempfile::TempDir::new().expect("temp");
721        let tag = t.path().join("CACHEDIR.TAG");
722        fs::write(&tag, b"Signature: x").expect("write");
723        let f = fs::File::options().write(true).open(&tag).expect("open");
724        f.set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(1))
725            .expect("mtime");
726        drop(f);
727        fs::write(t.path().join("a"), b"aaaa").expect("write");
728        fs::create_dir(t.path().join("sub")).expect("mkdir");
729        // A same-named file below the root is an ordinary candidate.
730        let nested = t.path().join("sub").join("CACHEDIR.TAG");
731        fs::write(&nested, b"nn").expect("write");
732
733        let limit = 12; // exactly the tag: every other file must go
734        let plan = plan_prune(t.path(), limit);
735        assert!(plan.files.iter().all(|(p, _)| p != &tag));
736        assert!(plan.remaining <= limit);
737
738        let pruned = prune_dir(t.path(), limit).expect("prune");
739        assert!(tag.exists(), "the root tag is never pruned");
740        assert!(pruned.remaining <= limit);
741        assert_eq!(pruned.freed, plan.freed);
742        assert_eq!(pruned.remaining, plan.remaining);
743        assert!(!nested.exists());
744    }
745
746    #[test]
747    fn plan_prune_is_empty_under_the_cap_and_for_a_missing_dir() {
748        let t = tempfile::TempDir::new().expect("temp");
749        fs::write(t.path().join("a"), b"12345").expect("write");
750        let plan = plan_prune(t.path(), 100);
751        assert_eq!(
752            plan,
753            PrunePlan {
754                files: Vec::new(),
755                freed: 0,
756                remaining: 5,
757            }
758        );
759
760        assert_eq!(
761            plan_prune(&t.path().join("absent"), 0),
762            PrunePlan::default()
763        );
764    }
765
766    /// A file `prune_dir` cannot delete - locked by a concurrent reader on
767    /// Windows, the exact scenario the function's own doc calls out - must
768    /// not make the pass stop short of the cap. The achieved total only
769    /// drops on a successful removal, so the loop has to keep reaching for
770    /// newer files until *that* total clears `limit`, not stop once a
771    /// precomputed selection runs out.
772    #[cfg(windows)]
773    #[test]
774    fn prune_keeps_reaching_past_an_undeletable_file_to_still_reach_the_cap() {
775        use std::os::windows::fs::OpenOptionsExt as _;
776
777        let t = tempfile::TempDir::new().expect("temp");
778        let old = t.path().join("old");
779        fs::write(&old, b"yyyy").expect("write");
780        std::thread::sleep(std::time::Duration::from_millis(1_200));
781        let mid = t.path().join("mid");
782        fs::write(&mid, b"zzzz").expect("write");
783        std::thread::sleep(std::time::Duration::from_millis(1_200));
784        let new = t.path().join("new");
785        fs::write(&new, b"xxxxx").expect("write");
786
787        // A share mode of 0 denies every other handle, including a delete -
788        // standing in for a file a concurrent build still has open, which is
789        // exactly the case `prune_dir`'s own doc calls out.
790        let lock = std::fs::OpenOptions::new()
791            .read(true)
792            .share_mode(0)
793            .open(&old)
794            .expect("lock the old file exclusively");
795
796        let pruned = prune_dir(t.path(), 8).expect("prune");
797        drop(lock);
798
799        assert!(old.exists(), "the locked file could not be deleted");
800        assert!(!mid.exists(), "the next-oldest file was tried and removed");
801        assert!(
802            !new.exists(),
803            "pruning kept reaching for newer files until the cap was actually met, \
804             not just until a fixed selection ran out"
805        );
806        assert!(
807            pruned.remaining <= 8,
808            "the achieved total must reach the cap: {pruned:?}"
809        );
810    }
811
812    #[test]
813    fn prune_leaves_a_missing_dir_alone() {
814        let t = tempfile::TempDir::new().expect("temp");
815        let out = prune_dir(&t.path().join("absent"), 1).expect("prune");
816        assert_eq!(out, Prune::default());
817    }
818
819    #[test]
820    fn prune_sweeps_directories_the_files_leave_empty() {
821        let t = tempfile::TempDir::new().expect("temp");
822        let deep = t.path().join("a").join("b").join("c");
823        fs::create_dir_all(&deep).expect("dirs");
824        fs::write(deep.join("f"), b"1234").expect("write");
825        let out = prune_dir(t.path(), 0).expect("prune");
826        assert_eq!(out.files, 1);
827        assert_eq!(out.remaining, 0);
828        assert!(!t.path().join("a").exists(), "empty chain swept");
829    }
830}
831
832#[cfg(test)]
833mod pressure_tests {
834    #[test]
835    fn pressure_reclaims_below_cap_without_underflow() {
836        assert_eq!(super::pressure_limit(100, 200, 80, 30), 50);
837        assert_eq!(super::pressure_limit(100, 200, 80, 100), 100);
838        assert_eq!(super::pressure_limit(100, 200, 800, 0), 0);
839        assert_eq!(super::pressure_limit(100, 50, 0, 0), 50);
840    }
841}