mise-util 2026.11.14

Environment, directory, and filesystem helpers shared by mise's crates
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//! A content-addressed store of finished downloads.
//!
//! Files are kept by their BLAKE3 hash under `blobs/<hash>/<name>`, under the
//! name they were downloaded as, since installers read the format and the
//! binary's name from it. Two small indexes point
//! into them:
//!
//! - `urls/`: a request (URL and headers) to the blob it last returned and the
//!   validator (`ETag` or `Last-Modified`) the server sent with it, so a later
//!   request can ask the server whether it changed and take `304 Not Modified`
//!   as an answer.
//! - `pins/`: a checksum a tool pinned to a blob, so a download can be reused
//!   with no request at all, for one of the URLs it was downloaded from.
//!
//! Nothing in an index is trusted on its own. A blob is hashed again before
//! it is used, against the pin when there is one, and an entry that no longer
//! matches is dropped.

use std::path::{Path, PathBuf};
use std::time::SystemTime;

use eyre::Result;
use serde::{Deserialize, Serialize};

use super::DownloadValidator;
use crate::file;

/// The size `download_cache_max_size` falls back to when it can't be read.
const DEFAULT_MAX_CACHE_BYTES: u64 = 2 * 1024 * 1024 * 1024;

/// Blobs are evicted, least recently used first, past this total size.
/// `download_cache_max_size = "0"` is no limit.
pub(super) fn max_cache_bytes() -> u64 {
    let setting = mise_settings::Settings::get();
    match setting
        .download_cache_max_size
        .parse::<bytesize::ByteSize>()
    {
        Ok(size) if size.as_u64() == 0 => u64::MAX,
        Ok(size) => size.as_u64(),
        Err(err) => {
            debug!(
                "invalid download_cache_max_size {:?}: {err}",
                setting.download_cache_max_size
            );
            DEFAULT_MAX_CACHE_BYTES
        }
    }
}

/// How long after a blob was last used another install may still be placing it.
const IN_USE: std::time::Duration = std::time::Duration::from_secs(10 * 60);

/// Algorithms a pin may use. `md5` and `sha1` are not collision resistant
/// enough to name content by.
const PIN_ALGOS: &[&str] = &["sha256", "sha384", "sha512", "blake3"];

#[derive(Debug, Clone, Serialize, Deserialize)]
struct UrlEntry {
    validator: DownloadValidator,
    blake3: String,
    effective_filename: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct PinEntry {
    blake3: String,
    effective_filename: Option<String>,
    /// The URLs this blob was downloaded from under the pin. A pin is reused
    /// only for one of them: a checksum from a lockfile or from release
    /// metadata must not hand back a file another URL served, such as another
    /// tool's archive or an older release. Entries written before this was
    /// recorded have none and are fetched once more.
    #[serde(default)]
    urls: Vec<String>,
}

/// Tests that don't ask for a directory never touch the user's cache.
#[cfg(test)]
pub(super) static TEST_ROOT: std::sync::Mutex<Option<PathBuf>> = std::sync::Mutex::new(None);

fn root() -> Option<PathBuf> {
    #[cfg(test)]
    {
        TEST_ROOT.lock().unwrap().clone()
    }
    #[cfg(not(test))]
    {
        Some(crate::dirs::CACHE.join("downloads-cas"))
    }
}

fn is_hex(value: &str) -> bool {
    !value.is_empty() && value.bytes().all(|b| b.is_ascii_hexdigit())
}

fn blob_dir(root: &Path, blake3: &str) -> PathBuf {
    root.join("blobs").join(blake3)
}

/// The file a blob directory holds, whatever name it was stored under.
fn blob_file(dir: &Path) -> Option<PathBuf> {
    std::fs::read_dir(dir)
        .ok()?
        .flatten()
        .map(|entry| entry.path())
        .find(|path| path.is_file())
}

/// Where an in-flight download is written: stable per request, so a partial
/// one can be resumed, and on the store's filesystem so it renames into place.
pub(super) fn staging_path(request_hash: &str, name: &str) -> Option<PathBuf> {
    Some(root()?.join("incoming").join(request_hash).join(name))
}

fn url_path(root: &Path, request_hash: &str) -> PathBuf {
    root.join("urls").join(format!("{request_hash}.json"))
}

/// Split a pin into its algorithm and lowercase digest, or `None` when it
/// could not safely name a file.
fn parse_pin(pin: &str) -> Option<(&str, String)> {
    let (algo, hex) = pin.split_once(':')?;
    (PIN_ALGOS.contains(&algo) && is_hex(hex)).then(|| (algo, hex.to_lowercase()))
}

/// Whether `path` hashes to `pin`, whichever algorithm it names. The caller
/// checks the same pin after the download, so one it can't check fails here
/// too.
fn matches_pin(path: &Path, pin: &str) -> bool {
    pin.split_once(':')
        .is_some_and(|(algo, hex)| crate::hash::ensure_checksum(path, hex, None, algo).is_ok())
}

fn pin_path(root: &Path, algo: &str, hex: &str) -> PathBuf {
    root.join("pins").join(algo).join(format!("{hex}.json"))
}

/// A stored download ready to be copied out.
pub(super) struct Hit {
    blob: PathBuf,
    pub(super) effective_filename: Option<String>,
    /// The index entry that led here, dropped if the blob doesn't verify.
    entry: PathBuf,
    verify: Verify,
}

enum Verify {
    /// The blob must hash to its own name, and to the tool's pinned checksum
    /// when there is one.
    Blake3 { name: String, pin: Option<String> },
    /// The blob must hash to the tool's pinned checksum.
    Pin { algo: String, hex: String },
}

/// What the server was last asked about, for a conditional request.
pub(super) struct Cached {
    pub(super) validator: DownloadValidator,
    hit: Hit,
}

impl Cached {
    pub(super) fn into_hit(self) -> Hit {
        self.hit
    }

    pub(super) fn effective_filename(&self) -> Option<String> {
        self.hit.effective_filename.clone()
    }
}

/// The download last kept for `request_hash`. With a `pin` it is only handed
/// back once it hashes to the pin too: an entry written by an unpinned
/// download of the same URL may hold bytes the pin would reject.
pub(super) fn lookup_url(request_hash: &str, pin: Option<&str>) -> Option<Cached> {
    let root = root()?;
    let entry = url_path(&root, request_hash);
    let url: UrlEntry = serde_json::from_slice(&std::fs::read(&entry).ok()?).ok()?;
    if !url.validator.is_valid() || !is_hex(&url.blake3) {
        return None;
    }
    let blob = blob_file(&blob_dir(&root, &url.blake3))?;
    Some(Cached {
        validator: url.validator,
        hit: Hit {
            blob,
            effective_filename: url.effective_filename,
            entry,
            verify: Verify::Blake3 {
                name: url.blake3,
                pin: pin.map(str::to_string),
            },
        },
    })
}

/// The download kept under `pin`, if it was downloaded from `url`.
pub(super) fn lookup_pin(pin: &str, url: &str) -> Option<Hit> {
    let root = root()?;
    let (algo, hex) = parse_pin(pin)?;
    let entry = pin_path(&root, algo, &hex);
    let pinned: PinEntry = serde_json::from_slice(&std::fs::read(&entry).ok()?).ok()?;
    if !is_hex(&pinned.blake3) || !pinned.urls.iter().any(|kept| kept == url) {
        return None;
    }
    let blob = blob_file(&blob_dir(&root, &pinned.blake3))?;
    Some(Hit {
        blob,
        effective_filename: pinned.effective_filename,
        entry,
        verify: Verify::Pin {
            algo: algo.to_string(),
            hex,
        },
    })
}

impl Hit {
    /// The stored file, once it hashes to what it is expected to. Otherwise
    /// drop what pointed at it and return `None`.
    pub(super) fn verified(&self) -> Option<PathBuf> {
        let intact = match &self.verify {
            Verify::Blake3 { name, pin } => {
                crate::hash::file_hash_blake3(&self.blob, None).is_ok_and(|hash| hash == *name)
                    && pin
                        .as_deref()
                        .is_none_or(|pin| matches_pin(&self.blob, pin))
            }
            Verify::Pin { algo, hex } => {
                crate::hash::ensure_checksum(&self.blob, hex, None, algo).is_ok()
            }
        };
        if !intact {
            let _ = file::remove_file(&self.entry);
            // A blob that doesn't hash to its own name is damaged, whichever
            // index led here. One that does merely isn't what the pin names.
            if let Some(dir) = self.blob.parent() {
                let name = dir.file_name().map(|n| n.to_string_lossy().to_string());
                let damaged = crate::hash::file_hash_blake3(&self.blob, None)
                    .is_ok_and(|hash| Some(hash) != name);
                if damaged {
                    let _ = file::remove_all(dir);
                }
            }
            return None;
        }
        // Marks the blob as recently used for eviction.
        if let Ok(f) = std::fs::File::options().write(true).open(&self.blob) {
            let _ = f.set_modified(SystemTime::now());
        }
        Some(self.blob.clone())
    }
}

/// Put `blob` at `dest` without ever leaving a partial file there. With `link`
/// the content is shared where the filesystem allows and copied where it
/// doesn't, such as across devices; without it the content is always copied.
pub(super) fn place(blob: &Path, dest: &Path, link: bool) -> Result<()> {
    let parent = dest.parent().unwrap_or(Path::new("."));
    file::create_dir_all(parent)?;
    // A directory of our own, so the name inside it is free to link to. A
    // `TempPath` would exist already, and Windows can't copy over a file that
    // is open.
    let scratch = tempfile::Builder::new()
        .prefix(".mise-place-")
        .tempdir_in(parent)
        .map_err(|err| placing(err, dest))?;
    let staged = scratch.path().join("file");
    if link {
        file::hard_link_or_copy(blob, &staged)?;
    } else {
        file::copy(blob, &staged)?;
    }
    // `persist` replaces an existing file on Windows too, where a plain rename
    // does not.
    tempfile::TempPath::try_from_path(&staged)
        .map_err(|err| placing(err, dest))?
        .persist(dest)
        .map_err(|err| placing(err.error, dest))?;
    Ok(())
}

/// Name the step and the path, with the Windows hint for an over-long path:
/// `tempfile` doesn't get the extended-length handling `std::fs` does, so this
/// is the call that fails first as a directory approaches `MAX_PATH`.
fn placing(err: std::io::Error, dest: &Path) -> eyre::Report {
    let msg = file::with_io_hint(
        format!(
            "failed to move the downloaded file into place: {}",
            file::display_path(dest)
        ),
        dest,
        &err,
    );
    eyre::Report::new(err).wrap_err(msg)
}

/// Write `contents` to `path` so a reader never sees a partial file.
fn write_atomic(path: &Path, contents: &[u8]) -> Result<()> {
    let parent = path.parent().expect("index paths have a parent");
    file::create_dir_all(parent)?;
    let tmp = tempfile::NamedTempFile::new_in(parent)?.into_temp_path();
    file::write(&tmp, contents)?;
    tmp.persist(path).map_err(|err| err.error)?;
    Ok(())
}

/// What kept a download: how to find it again.
pub(super) struct Keep<'a> {
    pub(super) request_hash: &'a str,
    /// The URL requested.
    pub(super) url: &'a str,
    /// The checksum the tool pinned.
    pub(super) pin: Option<&'a str>,
    pub(super) validator: Option<&'a DownloadValidator>,
    pub(super) effective_filename: Option<String>,
}

/// Keep `path`, just downloaded, by copying it into the store. Returns the
/// stored file, or `None` when there is nothing to index it by or it doesn't
/// match its pin.
pub(super) fn store(path: &Path, keep: Keep<'_>) -> Result<Option<PathBuf>> {
    keep_file(path, keep, false)
}

/// Keep `path`, which sits in the store's staging area, by moving it into the
/// store. Returns the stored file, or `None` as [`store`] does, with `path`
/// left where it was.
pub(super) fn ingest(path: &Path, keep: Keep<'_>) -> Result<Option<PathBuf>> {
    keep_file(path, keep, true)
}

fn keep_file(path: &Path, keep: Keep<'_>, move_in: bool) -> Result<Option<PathBuf>> {
    let Some(root) = root() else {
        return Ok(None);
    };
    // The caller checks the pin after the download and rejects the file.
    // Checking here too keeps a wrong file out of the store: indexed under its
    // URL, a `304 Not Modified` would hand it back to be rejected again, and
    // the right file would never be fetched.
    if keep.pin.is_some_and(|pin| !matches_pin(path, pin)) {
        debug!("not keeping a download that doesn't match its pin");
        return Ok(None);
    }
    let pin = keep.pin.and_then(parse_pin);
    if keep.validator.is_none() && pin.is_none() {
        return Ok(None);
    }
    let name = path.file_name().expect("downloads have a file name");
    let blake3 = crate::hash::file_hash_blake3(path, None)?;
    let dir = blob_dir(&root, &blake3);
    // An existing blob is reused only while it still hashes to its own name;
    // otherwise it is replaced by the file just downloaded.
    let existing = blob_file(&dir).filter(|existing| {
        let intact = crate::hash::file_hash_blake3(existing, None).is_ok_and(|h| h == blake3);
        if !intact {
            let _ = file::remove_all(&dir);
        }
        intact
    });
    let blob = match existing {
        Some(blob) => {
            if move_in {
                let _ = file::remove_file(path);
            }
            blob
        }
        None => {
            let blob = dir.join(name);
            // Built beside its final place so the rename can't cross a device.
            let blobs = root.join("blobs");
            file::create_dir_all(&blobs)?;
            let building = tempfile::Builder::new()
                .prefix(".building-")
                .tempdir_in(&blobs)?;
            let staged = building.path().join(name);
            if move_in {
                file::rename(path, &staged)?;
            } else {
                file::copy(path, &staged)?;
            }
            let building = building.keep();
            if let Err(err) = std::fs::rename(&building, &dir) {
                let _ = file::remove_all(&building);
                // Another process stored the same download first.
                if blob_file(&dir).is_none() {
                    return Err(err.into());
                }
            }
            blob_file(&dir).unwrap_or(blob)
        }
    };
    if let Some(validator) = keep.validator {
        let entry = UrlEntry {
            validator: validator.clone(),
            blake3: blake3.clone(),
            effective_filename: keep.effective_filename.clone(),
        };
        write_atomic(
            &url_path(&root, keep.request_hash),
            &serde_json::to_vec(&entry)?,
        )?;
    }
    // The blob is in place, and possibly moved out of staging, so a pin that
    // can't be recorded costs only its reuse, never the download.
    if let Some((algo, hex)) = pin
        && let Err(err) = record_pin(&root, algo, &hex, blake3, &keep)
    {
        debug!("not recording the pin of a kept download: {err:#}");
    }
    evict(&root, blob.parent());
    Ok(Some(blob))
}

/// How long recording a pin waits for another process recording one. Each
/// holds the lock for one small read and write, so this is only exceeded by a
/// lock that is stuck.
const PIN_LOCK_WAIT: std::time::Duration = std::time::Duration::from_secs(5);

/// A shorter wait for a test that holds the lock on purpose, so it doesn't sit
/// out the whole of [`PIN_LOCK_WAIT`].
#[cfg(test)]
pub(super) static TEST_PIN_LOCK_WAIT: std::sync::Mutex<Option<std::time::Duration>> =
    std::sync::Mutex::new(None);

fn pin_lock_wait() -> std::time::Duration {
    #[cfg(test)]
    if let Some(wait) = *TEST_PIN_LOCK_WAIT.lock().unwrap() {
        return wait;
    }
    PIN_LOCK_WAIT
}

/// Add `keep.url` to the URLs the pin `algo:hex` was kept from. Locked across
/// the read and the write, or two downloads of the same file from different
/// URLs would each write their own list and one URL would be lost.
fn record_pin(root: &Path, algo: &str, hex: &str, blake3: String, keep: &Keep<'_>) -> Result<()> {
    let lock_path = root.join("pins.lock");
    let started = std::time::Instant::now();
    let wait = pin_lock_wait();
    let _lock = loop {
        if let Some(lock) = crate::lock_file::LockFile::at(&lock_path).try_lock()? {
            break lock;
        }
        eyre::ensure!(
            started.elapsed() < wait,
            "the pin index stayed locked for {wait:?}"
        );
        std::thread::sleep(std::time::Duration::from_millis(10));
    };
    let path = pin_path(root, algo, hex);
    let mut urls = std::fs::read(&path)
        .ok()
        .and_then(|bytes| serde_json::from_slice::<PinEntry>(&bytes).ok())
        .filter(|existing| existing.blake3 == blake3)
        .map(|existing| existing.urls)
        .unwrap_or_default();
    if !urls.iter().any(|kept| kept == keep.url) {
        urls.push(keep.url.to_string());
    }
    let entry = PinEntry {
        blake3,
        effective_filename: keep.effective_filename.clone(),
        urls,
    };
    write_atomic(&path, &serde_json::to_vec(&entry)?)
}

/// Past `download_cache_max_size` the least recently used blobs go first, except
/// `keep`, which the caller is about to use, and any used within `IN_USE`: a
/// blob is verified, which refreshes its time, moments before another install
/// places it, so a recent one may be mid-use. Staged downloads that were never
/// finished go after a week.
fn evict(root: &Path, keep: Option<&Path>) {
    let week = std::time::Duration::from_secs(7 * 24 * 60 * 60);
    if let Ok(read) = std::fs::read_dir(root.join("incoming")) {
        for entry in read.flatten() {
            let stale = entry
                .metadata()
                .and_then(|m| m.modified())
                .ok()
                .and_then(|modified| modified.elapsed().ok())
                .is_some_and(|age| age > week);
            if stale {
                let _ = file::remove_all(entry.path());
            }
        }
    }
    let Ok(read) = std::fs::read_dir(root.join("blobs")) else {
        return;
    };
    let mut entries = Vec::new();
    let mut total = 0u64;
    for entry in read.flatten() {
        let dir = entry.path();
        if entry.file_name().to_string_lossy().starts_with('.')
            || !dir.is_dir()
            || keep == Some(dir.as_path())
        {
            continue;
        }
        // Names of one blob share a file where they can, so count the largest.
        let (mut size, mut used) = (0, SystemTime::UNIX_EPOCH);
        for file in std::fs::read_dir(&dir).into_iter().flatten().flatten() {
            if let Ok(meta) = file.metadata() {
                size = size.max(meta.len());
                used = used.max(meta.modified().unwrap_or(SystemTime::UNIX_EPOCH));
            }
        }
        total += size;
        if used.elapsed().is_ok_and(|age| age < IN_USE) {
            continue;
        }
        entries.push((used, size, dir));
    }
    entries.sort();
    let limit = max_cache_bytes();
    for (_, size, dir) in entries {
        if total <= limit {
            break;
        }
        if file::remove_all(&dir).is_ok() {
            total -= size;
        }
    }
}