Skip to main content

asc_cache/
lib.rs

1//! Caches for inflated DEX entries and what is derived from them.
2//!
3//! Inflating the largest DEX of an APK is the critical path of every query, and
4//! it produces the same bytes every time. Two caches remove that cost for
5//! repeated queries without introducing any preprocessing step: they are
6//! filled lazily by the first query that needs an entry.
7//!
8//! * [`DiskCache`] stores data under a cache directory keyed by the APK's
9//!   size, modification time and central directory ([`ApkKey`]), in three
10//!   tiers ([`Tier`]) that share one budget:
11//!   - **L1** inflated DEX entries ([`DiskCache::put`] / [`DiskCache::get`]),
12//!     cheap to rebuild and large: evicted first, largest-and-oldest first.
13//!     Entries below [`DiskCache::min_dex_bytes`] are not worth writing
14//!     (re-inflating them is cheaper than the write).
15//!   - **L2** derived indexes ([`DiskCache::put_derived`] /
16//!     [`DiskCache::get_derived`]): locator tables and other per-DEX or
17//!     per-APK structures, stored so they can be mapped and viewed in place.
18//!     Every process mapping one shares its pages instead of building a
19//!     private copy. Kept by rebuild cost per byte; evicted last.
20//!   - **L3** rendered results ([`DiskCache::put_rendered`] /
21//!     [`DiskCache::get_rendered`]): small outputs keyed by the query and the
22//!     tool version, plain LRU under a small budget of their own.
23//!
24//!   Later processes memory-map L1 and L2 files, so the operating system's
25//!   page cache keeps them hot. Writes are plain `put`s; eviction is a
26//!   separate [`DiskCache::evict`] step so a host can batch it (writing a
27//!   30 MiB entry and walking the whole cache directory after every one of
28//!   them is what a query must never wait on). The budget follows the free
29//!   space of the file system by default ([`AutoDisk`]).
30//!
31//!   Writes are never synced: everything here can be rebuilt, and a sync
32//!   per file costs more than the query that wrote it (macOS flushes the
33//!   drive's own cache on every one). A temporary file renamed into place
34//!   keeps readers from seeing half a file while the system runs; what a
35//!   crash can leave is a file whose pages never reached the disk. Every
36//!   file carries a CRC-32 of its contents, checked when a file last
37//!   written before the current boot (or at an unknown or future time) is
38//!   first read: a damaged one is deleted and read as a miss. L3 files are small and
39//!   always checked.
40//! * [`MemoryCache`] is a byte-bounded LRU of inflated entries for hosts that
41//!   stay alive between queries (MCP servers, the daemon).
42//! * [`ProcessRegistry`] lets resident hosts of one user see each other, so
43//!   their memory budgets add up to one machine-wide budget, and
44//!   [`auto_threads`] splits the CPUs between the busy ones.
45
46mod lock;
47mod procs;
48mod threads;
49
50pub use lock::LockExt;
51pub use procs::{ProcessRegistry, ProcessUsage};
52pub use threads::{Threads, auto_threads, auto_threads_for, cpus};
53
54use std::collections::{HashMap, HashSet};
55use std::fs;
56use std::io::{Read, Seek, SeekFrom, Write};
57use std::ops::Deref;
58use std::path::{Path, PathBuf};
59use std::sync::{Arc, OnceLock};
60use std::time::{Duration, SystemTime};
61
62/// A memory budget charges file-backed bytes (mapped from the disk cache,
63/// the APK or a HarmonyOS package) at this fraction of their size, heap bytes in full.
64///
65/// File pages are not what runs a machine out of memory: the kernel drops
66/// clean ones under pressure (they are re-read from the file) and every
67/// process mapping the same L1 / L2 file or APK shares one copy, so charging
68/// them in full made each of N processes evict as if it held its own copy,
69/// and summed to N times the real footprint in the process registry
70/// (`MemAvailable`, which the budget starts from, already counts reclaimable
71/// page cache). They still cost something (address space, page cache churn,
72/// the re-reads), so they are not free either: the LRU still bounds them, at
73/// up to this many times the budget.
74pub const MAPPED_CHARGE_DIVISOR: usize = 8;
75
76/// Identity of an APK for cache purposes.
77///
78/// A program of several archives has a key of its own (rendered results,
79/// L3, and [`MemoryCache`] go by it), and each archive its own key too: L1
80/// and L2 files of an entry live under its archive's key, by its name inside
81/// the archive ([`ApkKey::locate`]). Rewriting one jar of a program then
82/// costs that jar's entries only, and a jar read alone shares them with the
83/// same jar read as part of a program.
84#[derive(Debug, Clone)]
85pub struct ApkKey {
86    id: String,
87    /// Each archive of a program; empty for a lone archive.
88    archives: Arc<[ArchiveKey]>,
89}
90
91/// One archive of a program's [`ApkKey`].
92#[derive(Debug)]
93struct ArchiveKey {
94    /// What the program's names of its entries start with, before a `/`.
95    label: String,
96    path: PathBuf,
97    key: ApkKey,
98}
99
100impl PartialEq for ApkKey {
101    fn eq(&self, other: &ApkKey) -> bool {
102        self.id == other.id
103    }
104}
105
106impl Eq for ApkKey {}
107
108impl std::hash::Hash for ApkKey {
109    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
110        self.id.hash(state);
111    }
112}
113
114/// Characters a key stem (and a cached entry file name) may contain.
115fn is_key_char(c: char) -> bool {
116    c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.'
117}
118/// Longest stem kept from the APK file name.
119const STEM_MAX: usize = 40;
120/// Width of the hex hash that ends every key.
121const HASH_HEX_LEN: usize = 16;
122
123impl ApkKey {
124    /// Derive the key from the file size, mtime and a hash of the central
125    /// directory (so a rewritten archive with the same size is not confused
126    /// with the old one); for a program of several archives, from those of
127    /// each and its path, in order.
128    ///
129    /// # Errors
130    ///
131    /// The errors of [`asc_apk::Apk::directories`], and
132    /// [`asc_apk::ApkError::Io`] when an archive's metadata cannot be read.
133    pub fn for_apk(apk: &asc_apk::Apk) -> asc_apk::Result<ApkKey> {
134        let ids = apk.directories()?;
135        let mut key = 0u64;
136        let mut archives = Vec::with_capacity(if ids.len() > 1 { ids.len() } else { 0 });
137        for (i, id) in ids.iter().enumerate() {
138            let meta = fs::metadata(id.path).map_err(|source| asc_apk::ApkError::Io { path: id.path.to_path_buf(), source })?;
139            let mtime = meta.modified().ok().and_then(|t| t.duration_since(SystemTime::UNIX_EPOCH).ok()).map(|d| d.as_nanos()).unwrap_or(0);
140            let mut h = fnv64(id.directory);
141            h ^= meta.len().wrapping_mul(0x9E37_79B9_7F4A_7C15);
142            h ^= (mtime as u64).rotate_left(17);
143            if ids.len() > 1 {
144                // The key the archive has when read alone.
145                let stem = id.path.file_stem().map(|s| s.to_string_lossy()).unwrap_or_default();
146                let key = ApkKey::lone(&stem, h);
147                archives.push(ArchiveKey { label: id.label.to_string(), path: id.path.to_path_buf(), key });
148            }
149            // A lone archive keeps the key it always had.
150            key = if i == 0 { h } else { key.rotate_left(11) ^ h ^ fnv64(id.path.as_os_str().as_encoded_bytes()).rotate_left(29) };
151        }
152        Ok(ApkKey { archives: archives.into(), ..ApkKey::lone(&apk.stem(), key) })
153    }
154
155    /// The key of one archive: `stem` (sanitised) and its hash.
156    fn lone(stem: &str, hash: u64) -> ApkKey {
157        let stem: String = stem.chars().filter(|c| is_key_char(*c)).take(STEM_MAX).collect();
158        ApkKey::from_id(format!("{stem}-{hash:0width$x}", width = HASH_HEX_LEN))
159    }
160
161    fn from_id(id: String) -> ApkKey {
162        ApkKey { id, archives: Arc::new([]) }
163    }
164
165    /// The key as text: `<sanitised stem>-<16 hex digits>`, also the name of
166    /// the APK's cache directories.
167    pub fn as_str(&self) -> &str {
168        &self.id
169    }
170
171    /// The key and name the L1 / L2 files of the entry (or logical DEX) a
172    /// program calls `name` are stored under: its archive's, by its name
173    /// inside that archive. Other names (and a lone archive's) stay as
174    /// they are.
175    pub fn locate<'n>(&self, name: &'n str) -> (&ApkKey, &'n str) {
176        let found = asc_apk::archive_of(name).and_then(|label| {
177            let a = self.archives.iter().find(|a| a.label == label)?;
178            Some((&a.key, &name[label.len() + 1..]))
179        });
180        found.unwrap_or((self, name))
181    }
182}
183
184/// FNV-1a over 8-byte lanes: the central directory of a large APK runs to a
185/// few megabytes and is hashed once per process.
186fn fnv64(bytes: &[u8]) -> u64 {
187    const PRIME: u64 = 0x0100_0000_01b3;
188    let mut h: u64 = 0xcbf2_9ce4_8422_2325;
189    let (chunks, rest) = bytes.as_chunks::<8>();
190    for c in chunks {
191        h ^= u64::from_le_bytes(*c);
192        h = h.wrapping_mul(PRIME);
193    }
194    for &b in rest {
195        h ^= b as u64;
196        h = h.wrapping_mul(PRIME);
197    }
198    h
199}
200
201/// Inflated bytes of one entry, either owned or mapped from the disk cache.
202#[non_exhaustive]
203pub enum DexData {
204    /// Inflated into memory.
205    Owned(Vec<u8>),
206    /// Mapped read-only from a cache file or an uncompressed APK entry.
207    Mapped(memmap2::Mmap),
208}
209
210impl Deref for DexData {
211    type Target = [u8];
212    fn deref(&self) -> &[u8] {
213        match self {
214            DexData::Owned(v) => v,
215            DexData::Mapped(m) => m,
216        }
217    }
218}
219
220impl DexData {
221    /// `len` bytes at `offset` of the file at `path`, mapped read-only (any
222    /// offset: the mapping starts at the page holding it). For entries an
223    /// APK stores uncompressed, so a host keeping them gets file pages the
224    /// OS can share and reclaim instead of a heap copy. The file must not
225    /// change while mapped, the same contract as mapping the APK itself.
226    ///
227    /// # Errors
228    ///
229    /// The I/O error opening or mapping the file, or
230    /// [`std::io::ErrorKind::UnexpectedEof`] when the region runs past its end.
231    pub fn map_region(path: &Path, offset: u64, len: usize) -> std::io::Result<DexData> {
232        let file = fs::File::open(path)?;
233        if file.metadata()?.len() < offset.saturating_add(len as u64) {
234            return Err(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "region past the end of the file"));
235        }
236        // SAFETY: read-only mapping of a range checked to lie inside the file;
237        // see the contract above.
238        let map = unsafe { memmap2::MmapOptions::new().offset(offset).len(len).map(&file) }?;
239        Ok(DexData::Mapped(map))
240    }
241}
242
243impl AsRef<[u8]> for DexData {
244    fn as_ref(&self) -> &[u8] {
245        self
246    }
247}
248
249/// Why a disk cache operation failed.
250#[derive(Debug, thiserror::Error)]
251#[non_exhaustive]
252pub enum CacheError {
253    /// A file system operation failed.
254    #[error("cache io error at {path}: {source}")]
255    Io {
256        /// The file or directory operated on.
257        path: PathBuf,
258        /// The underlying error.
259        source: std::io::Error,
260    },
261    /// The directory exists but holds no cache marker, so it may hold
262    /// someone else's files.
263    #[error("{path} is not a asc cache directory (no {tag} marker); refusing to touch it")]
264    NotACacheDir {
265        /// The cache directory.
266        path: PathBuf,
267        /// The marker file name, [`CACHEDIR_TAG`].
268        tag: &'static str,
269    },
270    /// Writing would eat into the free space the automatic budget reserves
271    /// for the file system ([`auto_disk_reserve`]).
272    #[error("{path}: too little free disk space left to cache {bytes} bytes")]
273    LowDiskSpace {
274        /// The cache directory.
275        path: PathBuf,
276        /// Size of the write that was refused.
277        bytes: u64,
278    },
279}
280
281fn io_err(path: &Path) -> impl Fn(std::io::Error) -> CacheError + '_ {
282    move |source| CacheError::Io { path: path.to_path_buf(), source }
283}
284
285/// On-disk cache of inflated entries, derived indexes and rendered results.
286pub struct DiskCache {
287    dir: PathBuf,
288    cap: DiskCapacity,
289    min_dex_bytes: u64,
290    min_derived_cost: Duration,
291    /// Tiers written and read; the others behave as permanently empty.
292    tiers: [bool; 3],
293    /// Bytes this handle wrote since its last eviction walk.
294    written: std::sync::atomic::AtomicU64,
295    /// When the system booted ([`boot_time`]): files written before it are
296    /// checked on read.
297    boot: Option<SystemTime>,
298}
299
300/// The tiers of a [`DiskCache`].
301#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
302#[non_exhaustive]
303pub enum Tier {
304    /// L1: inflated DEX entries, `<dir>/<apk key>/<entry>`.
305    Dex,
306    /// L2: derived indexes, `<dir>/derived/<apk key>/<name>~<kind>.v<format>.<version>`.
307    Derived,
308    /// L3: rendered results, `<dir>/rendered/<apk key>/<hash of the query>`.
309    Rendered,
310}
311
312impl Tier {
313    /// Every tier, L1 first.
314    pub const ALL: [Tier; 3] = [Tier::Dex, Tier::Derived, Tier::Rendered];
315
316    /// Name in a tier list (`dex,derived,rendered`).
317    pub fn name(self) -> &'static str {
318        match self {
319            Tier::Dex => "dex",
320            Tier::Derived => "derived",
321            Tier::Rendered => "rendered",
322        }
323    }
324
325    /// Name for reports, with its level (`L1 dex`).
326    pub fn label(self) -> &'static str {
327        match self {
328            Tier::Dex => "L1 dex",
329            Tier::Derived => "L2 derived",
330            Tier::Rendered => "L3 rendered",
331        }
332    }
333}
334
335/// Subdirectory of the L2 tier.
336const DERIVED_DIR: &str = "derived";
337/// Subdirectory of the L3 tier.
338const RENDERED_DIR: &str = "rendered";
339/// Where each APK key's source path is recorded, to spot dead entries.
340const SOURCES_DIR: &str = "sources";
341/// Where the MCP server writes replies too long for the client (its own
342/// budget and pruning; this cache only recognizes and clears it).
343pub const SPILL_DIR: &str = "out";
344/// Subdirectory holding the working directories of decompiler backends
345/// (`decompilers/<name>/`); the cache neither evicts nor clears it.
346pub const DECOMPILERS_DIR: &str = "decompilers";
347
348/// Absolute cap of L3, which also never takes more than
349/// 1/[`RENDERED_BUDGET_DIVISOR`] of the total budget.
350pub const RENDERED_MAX: u64 = 128 << 20;
351/// L3 never takes more than 1/`RENDERED_BUDGET_DIVISOR` of the total budget.
352pub const RENDERED_BUDGET_DIVISOR: u64 = 8;
353/// L2 never takes more than 1/[`DERIVED_BUDGET_DIVISOR`] of the total budget.
354pub const DERIVED_BUDGET_DIVISOR: u64 = 2;
355
356/// Inflated DEX entries smaller than this are not written to L1 by default:
357/// re-inflating them costs less than writing (and later evicting) them.
358pub const DEFAULT_MIN_DEX_BYTES: u64 = 4 << 20;
359/// Derived indexes that took less than this to build are not written to L2
360/// by default: mapping and checking a file would save next to nothing.
361pub const DEFAULT_MIN_DERIVED_COST: Duration = Duration::from_millis(2);
362
363/// Header of an L2 file; the payload follows at [`DERIVED_HEADER`], so it is
364/// as aligned as the mapping (page-aligned) up to that.
365const DERIVED_MAGIC: &[u8; 8] = b"ASCL2\0\0\0";
366const DERIVED_HEADER: usize = 64;
367/// Layout version of the L2 header, part of every L2 file name.
368const DERIVED_FORMAT: u32 = 2;
369const RENDERED_MAGIC: &[u8; 8] = b"ASCL3\0\0\0";
370/// Layout version of L3 files, part of every L3 key.
371const RENDERED_FORMAT: u32 = 2;
372/// End of an L1 file, after the inflated bytes: this magic, then the
373/// CRC-32 of those bytes. Readers map only the bytes before it.
374const L1_MAGIC: &[u8; 4] = b"ASC1";
375const L1_TRAILER: u64 = 8;
376
377/// A derived index mapped from L2: the payload [`DiskCache::put_derived`]
378/// stored, 64-byte aligned.
379#[non_exhaustive]
380pub struct Derived {
381    map: memmap2::Mmap,
382    /// Rebuild cost recorded by the writer.
383    pub build_cost: Duration,
384}
385
386/// A validated L3 result viewed directly in its cache file.
387#[derive(Debug)]
388pub struct Rendered {
389    map: memmap2::Mmap,
390    body: usize,
391}
392
393impl AsRef<[u8]> for Rendered {
394    fn as_ref(&self) -> &[u8] {
395        &self.map[self.body..]
396    }
397}
398
399impl Deref for Derived {
400    type Target = [u8];
401    fn deref(&self) -> &[u8] {
402        &self.map[DERIVED_HEADER..]
403    }
404}
405
406impl AsRef<[u8]> for Derived {
407    fn as_ref(&self) -> &[u8] {
408        self
409    }
410}
411
412/// Per-tier usage reported by [`DiskCache::tier_stats`].
413#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
414#[non_exhaustive]
415pub struct TierStats {
416    /// APK keys with data in the tier.
417    pub apks: usize,
418    /// Files in the tier.
419    pub files: usize,
420    /// Bytes those files take.
421    pub bytes: u64,
422}
423
424/// One evictable unit: an L1 APK directory, or one L2 / L3 file.
425struct Item {
426    tier: Tier,
427    key: String,
428    path: PathBuf,
429    mtime: SystemTime,
430    files: usize,
431    bytes: u64,
432    /// Rebuild cost (L2 only).
433    cost_us: u64,
434}
435
436/// How a [`DiskCache`] is bounded.
437#[derive(Debug, Clone, Copy, PartialEq, Eq)]
438pub enum DiskCapacity {
439    /// Follow the free space of the cache's file system.
440    Auto(AutoDisk),
441    /// A fixed number of bytes.
442    Fixed(u64),
443}
444
445impl Default for DiskCapacity {
446    fn default() -> DiskCapacity {
447        DiskCapacity::Auto(AutoDisk::default())
448    }
449}
450
451/// The automatic disk budget where the free space cannot be read.
452pub const AUTO_DISK_UNKNOWN: u64 = 4 << 30;
453/// Smallest free space the automatic budget leaves on the file system; the
454/// reserve is the larger of this and [`AUTO_DISK_RESERVE_PERCENT`] of it.
455pub const AUTO_DISK_RESERVE_MIN: u64 = 1 << 30;
456/// Share of the file system's size, in percent, the automatic budget leaves
457/// free (at least [`AUTO_DISK_RESERVE_MIN`]).
458pub const AUTO_DISK_RESERVE_PERCENT: u64 = 5;
459
460/// Free space the automatic budget never writes into on a file system of
461/// `total` bytes.
462pub fn auto_disk_reserve(total: u64) -> u64 {
463    AUTO_DISK_RESERVE_MIN.max(total / 100 * AUTO_DISK_RESERVE_PERCENT)
464}
465
466/// The automatic disk budget: a share of what the cache could occupy (its
467/// own bytes plus the free space), at most a ceiling, and never more than
468/// leaves [`auto_disk_reserve`] free.
469#[derive(Debug, Clone, Copy, PartialEq, Eq)]
470#[non_exhaustive]
471pub struct AutoDisk {
472    /// Share of (free space + the cache's own bytes), in percent.
473    pub percent: u8,
474    /// Upper bound, in bytes.
475    pub ceiling: u64,
476}
477
478impl AutoDisk {
479    /// Default share of the room.
480    pub const PERCENT: u8 = 20;
481    /// Default ceiling: large enough for the inflated DEX of a phone's worth
482    /// of APKs (416 system and store APKs: 8.6 GB).
483    pub const CEILING: u64 = 32 << 30;
484
485    /// `percent` of the room, at most `ceiling` bytes.
486    pub fn new(percent: u8, ceiling: u64) -> AutoDisk {
487        AutoDisk { percent, ceiling }
488    }
489
490    /// The budget for a cache holding `used` bytes on a file system with
491    /// `(free, total)` bytes (`None`: unknown, e.g. on Windows, giving
492    /// [`AUTO_DISK_UNKNOWN`]). When the disk is already below the reserve,
493    /// the last bound falls under `used`, so eviction gives the shortfall
494    /// back. (No separate floor: with a reserve of at least 1 GiB, the
495    /// reserve is always the tighter bound on a disk too small for 20 % to
496    /// reach 256 MiB.)
497    pub fn bytes_for(&self, space: Option<(u64, u64)>, used: u64) -> u64 {
498        let Some((free, total)) = space else { return AUTO_DISK_UNKNOWN.min(self.ceiling) };
499        let room = used.saturating_add(free);
500        let budget = (room / 100 * u64::from(self.percent)).min(self.ceiling);
501        budget.min(room.saturating_sub(auto_disk_reserve(total)))
502    }
503}
504
505impl Default for AutoDisk {
506    fn default() -> AutoDisk {
507        AutoDisk { percent: AutoDisk::PERCENT, ceiling: AutoDisk::CEILING }
508    }
509}
510
511/// `(free, total)` bytes of the file system holding `path` (or its nearest
512/// existing ancestor). `None` where it cannot be determined.
513pub fn disk_space(path: &Path) -> Option<(u64, u64)> {
514    let mut p = path;
515    loop {
516        if p.exists() {
517            return fs_space(p);
518        }
519        p = p.parent()?;
520    }
521}
522
523#[cfg(unix)]
524fn fs_space(path: &Path) -> Option<(u64, u64)> {
525    use std::os::unix::ffi::OsStrExt;
526    let c = std::ffi::CString::new(path.as_os_str().as_bytes()).ok()?;
527    let mut st = std::mem::MaybeUninit::<libc::statvfs>::uninit();
528    // SAFETY: `c` is a NUL-terminated path and `st` a properly sized out buffer.
529    if unsafe { libc::statvfs(c.as_ptr(), st.as_mut_ptr()) } != 0 {
530        return None;
531    }
532    // SAFETY: statvfs succeeded, so it filled `st`.
533    let st = unsafe { st.assume_init() };
534    // The field types differ between platforms.
535    #[allow(clippy::useless_conversion, clippy::unnecessary_cast)]
536    let (frsize, bavail, blocks) = (st.f_frsize as u64, st.f_bavail as u64, st.f_blocks as u64);
537    Some((bavail.saturating_mul(frsize), blocks.saturating_mul(frsize)))
538}
539
540#[cfg(not(unix))]
541fn fs_space(_path: &Path) -> Option<(u64, u64)> {
542    None
543}
544
545/// What [`DiskCache::budget`] reports.
546#[derive(Debug, Clone, Copy, PartialEq, Eq)]
547#[non_exhaustive]
548pub struct DiskBudget {
549    /// How the cache is bounded.
550    pub capacity: DiskCapacity,
551    /// Bytes the cache may hold right now.
552    pub cap_bytes: u64,
553    /// Bytes it holds.
554    pub used: u64,
555    /// `(free, total)` of its file system, when known.
556    pub space: Option<(u64, u64)>,
557}
558
559/// Temporary files older than this are leftovers of a killed writer.
560const STALE_TMP: Duration = Duration::from_secs(10 * 60);
561
562/// Marker file written into the cache root (see <https://bford.info/cachedir/>).
563/// A directory is only used as a cache (written to, evicted, cleared) when it
564/// carries this marker, is empty or missing, or holds nothing but the cache's
565/// own APK directories (one written before the marker existed). Pointing
566/// `--cache-dir` at `$HOME` or a project tree therefore neither reclaims its
567/// subdirectories as "least recently used APKs" nor plants a tag that makes
568/// backup tools skip it.
569pub const CACHEDIR_TAG: &str = "CACHEDIR.TAG";
570const CACHEDIR_TAG_CONTENT: &str = "Signature: 8a477f597d28d172789f06886806bc55\n\
571# This file is a cache directory tag created by asc.\n\
572# For information about cache directory tags, see: https://bford.info/cachedir/\n";
573
574/// Whether `name` has the shape [`ApkKey::for_apk`] produces
575/// (`<stem>-<hash>`, stem of at most [`STEM_MAX`] [`is_key_char`]s, hash of
576/// [`HASH_HEX_LEN`] lowercase hex digits). Only such subdirectories are
577/// counted, evicted or cleared.
578fn is_cache_dir_name(name: &str) -> bool {
579    let Some((stem, hash)) = name.rsplit_once('-') else { return false };
580    hash.len() == HASH_HEX_LEN
581        && hash.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
582        && stem.len() <= STEM_MAX
583        && stem.chars().all(is_key_char)
584}
585
586impl DiskCache {
587    /// The default cache directory: `$XDG_CACHE_HOME/asc`, else on Windows
588    /// `%LOCALAPPDATA%\\asc\\cache`, else `~/.cache/asc`.
589    pub fn default_dir() -> Option<PathBuf> {
590        let var = |name: &str| std::env::var_os(name).filter(|v| !v.is_empty()).map(PathBuf::from);
591        if let Some(d) = var("XDG_CACHE_HOME") {
592            return Some(d.join("asc"));
593        }
594        if cfg!(windows)
595            && let Some(d) = var("LOCALAPPDATA")
596        {
597            return Some(d.join("asc").join("cache"));
598        }
599        var("HOME").map(|h| h.join(".cache").join("asc"))
600    }
601
602    /// A cache in `dir` with the default policy: automatic capacity, every
603    /// tier, [`DEFAULT_MIN_DEX_BYTES`]. Hosts apply the user's configuration
604    /// with the `with_*` methods.
605    pub fn open(dir: impl Into<PathBuf>) -> DiskCache {
606        DiskCache {
607            dir: dir.into(),
608            cap: DiskCapacity::default(),
609            min_dex_bytes: DEFAULT_MIN_DEX_BYTES,
610            min_derived_cost: DEFAULT_MIN_DERIVED_COST,
611            tiers: [true; 3],
612            written: Default::default(),
613            boot: boot_time(),
614        }
615    }
616
617    /// Use only `tiers`: reads of the others miss and writes to them are
618    /// skipped. Entries already stored in them still count and are evicted.
619    pub fn with_tiers(mut self, tiers: &[Tier]) -> DiskCache {
620        self.tiers = Tier::ALL.map(|t| tiers.contains(&t));
621        self
622    }
623
624    /// Whether `tier` is read and written ([`DiskCache::with_tiers`]).
625    pub fn uses(&self, tier: Tier) -> bool {
626        self.tiers[tier as usize]
627    }
628
629    /// Write inflated entries of at least `bytes` to L1 (0: all of them).
630    pub fn with_min_dex_bytes(mut self, bytes: u64) -> DiskCache {
631        self.min_dex_bytes = bytes;
632        self
633    }
634
635    /// Smallest inflated entry worth writing to L1 ([`DEFAULT_MIN_DEX_BYTES`]
636    /// unless configured otherwise). [`DiskCache::put`] itself
637    /// writes whatever it is given; hosts ask [`DiskCache::admits_dex`] first.
638    pub fn min_dex_bytes(&self) -> u64 {
639        self.min_dex_bytes
640    }
641
642    /// Whether an inflated entry of `len` bytes is worth an L1 file
643    /// ([`DiskCache::min_dex_bytes`]).
644    pub fn admits_dex(&self, len: u64) -> bool {
645        len >= self.min_dex_bytes
646    }
647
648    /// Write derived indexes that took at least `cost` to build to L2
649    /// (default [`DEFAULT_MIN_DERIVED_COST`]; zero: all of them).
650    pub fn with_min_derived_cost(mut self, cost: Duration) -> DiskCache {
651        self.min_derived_cost = cost;
652        self
653    }
654
655    /// Whether an index that took `cost` to build is worth an L2 file.
656    /// Like [`DiskCache::admits_dex`], a policy for hosts to apply.
657    pub fn admits_derived(&self, cost: Duration) -> bool {
658        cost >= self.min_derived_cost
659    }
660
661    /// Open the default directory, if one can be determined.
662    pub fn open_default() -> Option<DiskCache> {
663        Self::default_dir().map(Self::open)
664    }
665
666    /// A fixed capacity of `cap_bytes`.
667    pub fn with_capacity(self, cap_bytes: u64) -> DiskCache {
668        self.with_capacity_mode(DiskCapacity::Fixed(cap_bytes))
669    }
670
671    /// Bound the cache by `cap`.
672    pub fn with_capacity_mode(mut self, cap: DiskCapacity) -> DiskCache {
673        self.cap = cap;
674        self
675    }
676
677    /// How the cache is bounded.
678    pub fn capacity_mode(&self) -> DiskCapacity {
679        self.cap
680    }
681
682    /// The cache directory.
683    pub fn dir(&self) -> &Path {
684        &self.dir
685    }
686
687    /// Bytes the cache may hold when it holds `used`: the fixed capacity, or
688    /// the automatic budget for the file system's current free space.
689    pub fn capacity_for(&self, used: u64) -> u64 {
690        match self.cap {
691            DiskCapacity::Fixed(b) => b,
692            DiskCapacity::Auto(auto) => auto.bytes_for(disk_space(&self.dir), used),
693        }
694    }
695
696    /// Capacity, usage and free space right now (walks the cache directory).
697    pub fn budget(&self) -> DiskBudget {
698        let used = self.stats().2;
699        let space = disk_space(&self.dir);
700        let cap_bytes = match self.cap {
701            DiskCapacity::Fixed(b) => b,
702            DiskCapacity::Auto(auto) => auto.bytes_for(space, used),
703        };
704        DiskBudget { capacity: self.cap, cap_bytes, used, space }
705    }
706
707    /// Whether a cache file must be checked before use ([`predates_boot`]).
708    fn predates_boot(&self, meta: &fs::Metadata) -> bool {
709        predates_boot(meta.modified().ok(), self.boot, SystemTime::now())
710    }
711
712    /// Delete a damaged file; always a miss.
713    fn discard<T>(&self, path: &Path) -> Option<T> {
714        // A concurrent writer may have just renamed a sound file over it:
715        // deleting that one only costs a rebuild.
716        let _ = fs::remove_file(path);
717        None
718    }
719
720    fn entry_path(&self, key: &ApkKey, entry_name: &str) -> PathBuf {
721        let (key, entry_name) = key.locate(entry_name);
722        self.dir.join(key.as_str()).join(safe_name(entry_name))
723    }
724
725    fn derived_path(&self, key: &ApkKey, name: &str, kind: &str, version: u32) -> PathBuf {
726        let (key, name) = key.locate(name);
727        let file = format!("{}~{}.v{DERIVED_FORMAT}.{version}", safe_name(name), safe_name(kind));
728        self.dir.join(DERIVED_DIR).join(key.as_str()).join(file)
729    }
730
731    fn rendered_path(&self, key: &ApkKey, query: &str) -> PathBuf {
732        self.dir.join(RENDERED_DIR).join(key.as_str()).join(format!("{:016x}", fnv64(query.as_bytes())))
733    }
734
735    /// Map a cached entry if present and of the expected size (0: any).
736    /// A file from before the current boot is checked against its CRC-32
737    /// first; a damaged one is deleted.
738    ///
739    /// # Panics
740    ///
741    /// Never: the CRC is read from a trailer of fixed size.
742    pub fn get(&self, key: &ApkKey, entry_name: &str, expected_len: u64) -> Option<DexData> {
743        if !self.uses(Tier::Dex) {
744            return None;
745        }
746        let path = self.entry_path(key, entry_name);
747        let file = fs::File::open(&path).ok()?;
748        let meta = file.metadata().ok()?;
749        let len = meta.len().checked_sub(L1_TRAILER).filter(|&l| l > 0)?;
750        if expected_len != 0 && len != expected_len {
751            return None;
752        }
753        let mut trailer = [0u8; L1_TRAILER as usize];
754        (&file).seek(SeekFrom::Start(len)).ok()?;
755        (&file).read_exact(&mut trailer).ok()?;
756        // Not `populate`d: pre-faulting every entry of an APK costs more than
757        // faulting in the pages a query reads (getclass reads one class).
758        let map = map_prefix(&file, len)?;
759        if &trailer[..4] != L1_MAGIC {
760            return self.discard(&path);
761        }
762        if self.predates_boot(&meta) {
763            if crc32fast::hash(&map) != u32::from_le_bytes(trailer[4..].try_into().expect("4 bytes")) {
764                // Unmapped first: Windows refuses to remove a mapped file.
765                drop(map);
766                return self.discard(&path);
767            }
768            let _ = touch_file(&file, &path);
769        }
770        // Touch the APK directory so LRU eviction sees recent use.
771        let _ = touch_dir(path.parent()?);
772        Some(DexData::Mapped(map))
773    }
774
775    /// Store an inflated entry (atomic rename; concurrent writers are
776    /// harmless). Does not evict: call [`DiskCache::evict`] after a batch of
777    /// puts.
778    ///
779    /// # Errors
780    ///
781    /// [`CacheError::NotACacheDir`] when the directory is not this cache's,
782    /// [`CacheError::LowDiskSpace`] when the write would eat into the free
783    /// space reserve, [`CacheError::Io`] when writing fails.
784    pub fn put(&self, key: &ApkKey, entry_name: &str, bytes: &[u8]) -> Result<(), CacheError> {
785        if !self.uses(Tier::Dex) {
786            return Ok(());
787        }
788        let mut trailer = [0u8; L1_TRAILER as usize];
789        trailer[..4].copy_from_slice(L1_MAGIC);
790        trailer[4..].copy_from_slice(&crc32fast::hash(bytes).to_le_bytes());
791        self.write_atomic(&self.entry_path(key, entry_name), &[bytes, &trailer])
792    }
793
794    /// Map the L2 index `name` (e.g. a logical DEX name) of `kind` at the
795    /// producer's format `version`, as [`DiskCache::put_derived`] stored it.
796    /// A file of another version, a damaged header or a truncated payload
797    /// is a miss.
798    ///
799    /// # Panics
800    ///
801    /// Never: the header fields are read from a slice of the full header size.
802    pub fn get_derived(&self, key: &ApkKey, name: &str, kind: &str, version: u32) -> Option<Derived> {
803        if !self.uses(Tier::Derived) {
804            return None;
805        }
806        let path = self.derived_path(key, name, kind, version);
807        let file = fs::File::open(&path).ok()?;
808        let meta = file.metadata().ok()?;
809        let map = map_file(&file)?;
810        let header = map.get(..DERIVED_HEADER)?;
811        let u32_at = |o: usize| u32::from_le_bytes(header[o..o + 4].try_into().expect("4 bytes"));
812        let u64_at = |o: usize| u64::from_le_bytes(header[o..o + 8].try_into().expect("8 bytes"));
813        if &header[..8] != DERIVED_MAGIC
814            || u32_at(8) != DERIVED_FORMAT
815            || u32_at(12) != version
816            || u64_at(24) != (map.len() - DERIVED_HEADER) as u64
817        {
818            return None;
819        }
820        if self.predates_boot(&meta) && crc32fast::hash(&map[DERIVED_HEADER..]) != u32_at(32) {
821            drop(map);
822            return self.discard(&path);
823        }
824        let _ = touch_file(&file, &path);
825        Some(Derived { build_cost: Duration::from_micros(u64_at(16)), map })
826    }
827
828    /// Store an L2 index (see [`DiskCache::get_derived`]). `build_cost` is
829    /// what rebuilding it takes: eviction keeps the indexes that save the
830    /// most time per byte.
831    ///
832    /// # Errors
833    ///
834    /// As [`DiskCache::put`].
835    pub fn put_derived(
836        &self,
837        key: &ApkKey,
838        name: &str,
839        kind: &str,
840        version: u32,
841        build_cost: Duration,
842        payload: &[u8],
843    ) -> Result<(), CacheError> {
844        if !self.uses(Tier::Derived) {
845            return Ok(());
846        }
847        let mut header = [0u8; DERIVED_HEADER];
848        header[..8].copy_from_slice(DERIVED_MAGIC);
849        header[8..12].copy_from_slice(&DERIVED_FORMAT.to_le_bytes());
850        header[12..16].copy_from_slice(&version.to_le_bytes());
851        header[16..24].copy_from_slice(&(build_cost.as_micros() as u64).to_le_bytes());
852        header[24..32].copy_from_slice(&(payload.len() as u64).to_le_bytes());
853        header[32..36].copy_from_slice(&crc32fast::hash(payload).to_le_bytes());
854        self.write_atomic(&self.derived_path(key, name, kind, version), &[&header, payload])
855    }
856
857    /// The L3 result stored under `query` (the query, its options and the
858    /// producer's version: build it with [`rendered_key`]).
859    pub fn get_rendered(&self, key: &ApkKey, query: &str) -> Option<Vec<u8>> {
860        self.get_rendered_view(key, query).map(|view| view.as_ref().to_vec())
861    }
862
863    /// [`DiskCache::get_rendered`] without copying the payload out of its
864    /// cache file. The immutable file is mapped only after its full key and
865    /// CRC have been validated.
866    pub fn get_rendered_view(&self, key: &ApkKey, query: &str) -> Option<Rendered> {
867        if !self.uses(Tier::Rendered) {
868            return None;
869        }
870        let path = self.rendered_path(key, query);
871        let file = fs::File::open(&path).ok()?;
872        let data = map_file(&file)?;
873        let key_len = u32::from_le_bytes(data.get(8..12)?.try_into().ok()?) as usize;
874        let crc = u32::from_le_bytes(data.get(12..16)?.try_into().ok()?);
875        let body = 16usize.checked_add(key_len)?;
876        if data.get(..8)? != RENDERED_MAGIC || crc32fast::hash(&data[16..]) != crc {
877            drop(data);
878            return self.discard(&path);
879        }
880        // The full key is stored and compared, so a hash collision is a miss.
881        if data.get(16..body)? != query.as_bytes() {
882            return None;
883        }
884        let _ = touch_file(&file, &path);
885        Some(Rendered { map: data, body })
886    }
887
888    /// Store an L3 result (see [`DiskCache::get_rendered`]).
889    ///
890    /// # Errors
891    ///
892    /// As [`DiskCache::put`].
893    pub fn put_rendered(&self, key: &ApkKey, query: &str, payload: &[u8]) -> Result<(), CacheError> {
894        if !self.uses(Tier::Rendered) {
895            return Ok(());
896        }
897        let mut crc = crc32fast::Hasher::new();
898        crc.update(query.as_bytes());
899        crc.update(payload);
900        let mut header = Vec::with_capacity(16 + query.len());
901        header.extend_from_slice(RENDERED_MAGIC);
902        header.extend_from_slice(&(query.len() as u32).to_le_bytes());
903        header.extend_from_slice(&crc.finalize().to_le_bytes());
904        header.extend_from_slice(query.as_bytes());
905        self.write_atomic(&self.rendered_path(key, query), &[&header, payload])
906    }
907
908    /// Record where the APK of `key` lives, so eviction can tell the entries
909    /// of an APK deleted or replaced since (evicted first) from live ones.
910    /// Cheap to repeat; hosts call it once per APK. A program's archives are
911    /// recorded one by one, each under its own key.
912    ///
913    /// # Errors
914    ///
915    /// As [`DiskCache::put`].
916    pub fn note_source(&self, key: &ApkKey, apk_path: &Path) -> Result<(), CacheError> {
917        if !key.archives.is_empty() {
918            return key.archives.iter().try_for_each(|a| self.note_source(&a.key, &a.path));
919        }
920        let path = self.dir.join(SOURCES_DIR).join(key.as_str());
921        let Some(stamp) = source_stamp(apk_path) else { return Ok(()) };
922        if fs::read_to_string(&path).is_ok_and(|s| s == stamp) {
923            return Ok(());
924        }
925        self.write_atomic(&path, &[stamp.as_bytes()])
926    }
927
928    /// Write `parts` to `path` (atomic rename; concurrent writers are
929    /// harmless), refusing foreign directories and the free-space reserve.
930    /// Not synced: a file a crash damages fails its checksum on read (see
931    /// the module documentation).
932    fn write_atomic(&self, path: &Path, parts: &[&[u8]]) -> Result<(), CacheError> {
933        // Checked before anything is created: the tag must never land in a
934        // directory that holds someone else's files.
935        if !self.is_cache_dir() {
936            return Err(self.not_a_cache_dir());
937        }
938        let len: u64 = parts.iter().map(|p| p.len() as u64).sum();
939        // One statvfs per write: with the automatic budget, never write into
940        // the reserve the file system needs (another program may have filled
941        // the disk since the last eviction).
942        if matches!(self.cap, DiskCapacity::Auto(_))
943            && let Some((free, total)) = disk_space(&self.dir)
944            && free.saturating_sub(len) < auto_disk_reserve(total)
945        {
946            return Err(CacheError::LowDiskSpace { path: self.dir.clone(), bytes: len });
947        }
948        let parent = path.parent().expect("cache path has a parent");
949        fs::create_dir_all(parent).map_err(io_err(parent))?;
950        // Best effort: an unwritable root (shared cache, full disk) must not
951        // stop entries from being written into their own directories.
952        let _ = self.ensure_tag();
953        let tmp = parent.join(format!(".{}.{}.tmp", path.file_name().unwrap_or_default().to_string_lossy(), std::process::id()));
954        {
955            let mut f = fs::File::create(&tmp).map_err(io_err(&tmp))?;
956            for p in parts {
957                f.write_all(p).map_err(io_err(&tmp))?;
958            }
959        }
960        fs::rename(&tmp, path).map_err(io_err(path))?;
961        self.written.fetch_add(len, std::sync::atomic::Ordering::Relaxed);
962        Ok(())
963    }
964
965    /// `(APKs, files, bytes)` currently stored, over every tier (an APK
966    /// with data in several tiers counts once).
967    pub fn stats(&self) -> (usize, usize, u64) {
968        let items = self.items();
969        let keys: HashSet<&str> = items.iter().map(|i| i.key.as_str()).collect();
970        (keys.len(), items.iter().map(|i| i.files).sum(), items.iter().map(|i| i.bytes).sum())
971    }
972
973    /// Usage of each tier, in [`Tier::ALL`] order.
974    pub fn tier_stats(&self) -> [TierStats; 3] {
975        let items = self.items();
976        Tier::ALL.map(|tier| {
977            let of_tier = || items.iter().filter(move |i| i.tier == tier);
978            let keys: HashSet<&str> = of_tier().map(|i| i.key.as_str()).collect();
979            TierStats { apks: keys.len(), files: of_tier().map(|i| i.files).sum(), bytes: of_tier().map(|i| i.bytes).sum() }
980        })
981    }
982
983    /// Delete every cached entry of every tier (and nothing else). Returns
984    /// the number of bytes freed. Refuses to run on a directory that is not
985    /// a cache directory (see [`CACHEDIR_TAG`]).
986    ///
987    /// # Errors
988    ///
989    /// [`CacheError::NotACacheDir`] for a foreign directory, and
990    /// [`CacheError::Io`] when a file or directory cannot be removed.
991    pub fn clear(&self) -> Result<u64, CacheError> {
992        if !self.dir.exists() {
993            return Ok(0);
994        }
995        if !self.is_cache_dir() {
996            return Err(self.not_a_cache_dir());
997        }
998        let mut bytes = 0;
999        for item in self.items() {
1000            remove_item(&item).map_err(io_err(&item.path))?;
1001            bytes += item.bytes;
1002        }
1003        for sub in [DERIVED_DIR, RENDERED_DIR, SOURCES_DIR, SPILL_DIR] {
1004            let path = self.dir.join(sub);
1005            if path.is_dir() {
1006                fs::remove_dir_all(&path).map_err(io_err(&path))?;
1007            }
1008        }
1009        Ok(bytes)
1010    }
1011
1012    /// `Ok` when the directory may be used as this cache (see
1013    /// [`CACHEDIR_TAG`]), [`CacheError::NotACacheDir`] otherwise. Hosts call
1014    /// this up front: [`DiskCache::put`] refuses such a directory too, but its
1015    /// errors are usually swallowed by a background writer.
1016    ///
1017    /// # Errors
1018    ///
1019    /// [`CacheError::NotACacheDir`] when the directory exists without the
1020    /// marker.
1021    pub fn check_usable(&self) -> Result<(), CacheError> {
1022        if self.is_cache_dir() { Ok(()) } else { Err(self.not_a_cache_dir()) }
1023    }
1024
1025    fn not_a_cache_dir(&self) -> CacheError {
1026        CacheError::NotACacheDir { path: self.dir.clone(), tag: CACHEDIR_TAG }
1027    }
1028
1029    /// Write the [`CACHEDIR_TAG`] marker if it is missing.
1030    fn ensure_tag(&self) -> Result<(), CacheError> {
1031        if self.has_tag() {
1032            return Ok(());
1033        }
1034        let tag = self.dir.join(CACHEDIR_TAG);
1035        // `create_new` so two first writers cannot race; whoever loses is done.
1036        match fs::OpenOptions::new().write(true).create_new(true).open(&tag) {
1037            // A truncated tag would still count as one; remove it so the next
1038            // put retries.
1039            Ok(mut f) => f.write_all(CACHEDIR_TAG_CONTENT.as_bytes()).map_err(|e| {
1040                drop(f);
1041                let _ = fs::remove_file(&tag);
1042                io_err(&tag)(e)
1043            }),
1044            Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Ok(()),
1045            Err(e) => Err(io_err(&tag)(e)),
1046        }
1047    }
1048
1049    fn has_tag(&self) -> bool {
1050        self.dir.join(CACHEDIR_TAG).is_file()
1051    }
1052
1053    /// Whether the directory may be treated as this cache's own: it carries
1054    /// the tag, does not exist yet, or contains nothing but APK directories
1055    /// of the cache's naming shape (plus Finder's `.DS_Store`), which is what
1056    /// a cache written before the tag was introduced looks like. Any other
1057    /// file, dot-files included, means the directory is someone else's.
1058    fn is_cache_dir(&self) -> bool {
1059        if self.has_tag() {
1060            return true;
1061        }
1062        match fs::read_dir(&self.dir) {
1063            Ok(rd) => rd.flatten().all(|d| {
1064                let name = d.file_name();
1065                let name = name.to_string_lossy();
1066                match d.file_type() {
1067                    Ok(t) if t.is_dir() => {
1068                        is_cache_dir_name(&name) || TIER_DIRS.contains(&&*name) || name == SPILL_DIR || name == DECOMPILERS_DIR
1069                    }
1070                    Ok(t) if t.is_file() => name == ".DS_Store" || name == EVICT_MARKER,
1071                    _ => false,
1072                }
1073            }),
1074            Err(e) => e.kind() == std::io::ErrorKind::NotFound,
1075        }
1076    }
1077
1078    /// `(mtime, path, file count, bytes)` per APK directory (subdirectories
1079    /// whose name matches [`is_cache_dir_name`]; anything else in the
1080    /// directory is ignored); also removes temporary files left behind by a
1081    /// killed writer.
1082    fn apk_dirs(&self) -> Vec<(SystemTime, PathBuf, usize, u64)> {
1083        apk_dirs_in(&self.dir)
1084    }
1085
1086    /// Every evictable unit of every tier.
1087    fn items(&self) -> Vec<Item> {
1088        let mut out: Vec<Item> = self
1089            .apk_dirs()
1090            .into_iter()
1091            .map(|(mtime, path, files, bytes)| Item {
1092                tier: Tier::Dex,
1093                key: path.file_name().unwrap_or_default().to_string_lossy().into_owned(),
1094                path,
1095                mtime,
1096                files,
1097                bytes,
1098                cost_us: 0,
1099            })
1100            .collect();
1101        for (tier, sub) in [(Tier::Derived, DERIVED_DIR), (Tier::Rendered, RENDERED_DIR)] {
1102            for (_, dir, _, _) in apk_dirs_in(&self.dir.join(sub)) {
1103                let key = dir.file_name().unwrap_or_default().to_string_lossy().into_owned();
1104                let Ok(rd) = fs::read_dir(&dir) else { continue };
1105                for f in rd.flatten() {
1106                    let Ok(m) = f.metadata() else { continue };
1107                    if !m.is_file() || f.file_name().to_string_lossy().ends_with(".tmp") {
1108                        continue;
1109                    }
1110                    let cost_us = if tier == Tier::Derived { derived_cost_us(&f.path()) } else { 0 };
1111                    let mtime = m.modified().unwrap_or(SystemTime::UNIX_EPOCH);
1112                    out.push(Item { tier, key: key.clone(), path: f.path(), mtime, files: 1, bytes: m.len(), cost_us });
1113                }
1114            }
1115        }
1116        out
1117    }
1118
1119    /// APK keys whose recorded source ([`DiskCache::note_source`]) is gone
1120    /// or has changed since.
1121    fn dead_keys(&self) -> HashSet<String> {
1122        let mut out = HashSet::new();
1123        let Ok(rd) = fs::read_dir(self.dir.join(SOURCES_DIR)) else { return out };
1124        for f in rd.flatten() {
1125            let Ok(stamp) = fs::read_to_string(f.path()) else { continue };
1126            let Some(path) = stamp.lines().nth(2) else { continue };
1127            if source_stamp(Path::new(path)).as_deref() != Some(stamp.as_str()) {
1128                out.insert(f.file_name().to_string_lossy().into_owned());
1129            }
1130        }
1131        out
1132    }
1133}
1134
1135/// `(mtime, path, file count, bytes)` per APK directory directly under
1136/// `root` (subdirectories whose name matches [`is_cache_dir_name`]; anything
1137/// else is ignored); also removes temporary files left behind by a killed
1138/// writer.
1139fn apk_dirs_in(root: &Path) -> Vec<(SystemTime, PathBuf, usize, u64)> {
1140    let mut out = Vec::new();
1141    let Ok(rd) = fs::read_dir(root) else { return out };
1142    let now = SystemTime::now();
1143    for d in rd.flatten() {
1144        let path = d.path();
1145        if !is_cache_dir_name(&d.file_name().to_string_lossy()) || !path.is_dir() {
1146            continue;
1147        }
1148        let mtime = d.metadata().and_then(|m| m.modified()).unwrap_or(SystemTime::UNIX_EPOCH);
1149        let (mut files, mut size) = (0usize, 0u64);
1150        if let Ok(fd) = fs::read_dir(&path) {
1151            for f in fd.flatten() {
1152                let Ok(m) = f.metadata() else { continue };
1153                let name = f.file_name();
1154                let is_tmp = name.to_string_lossy().ends_with(".tmp");
1155                if is_tmp && m.modified().ok().and_then(|t| now.duration_since(t).ok()).is_some_and(|age| age > STALE_TMP) {
1156                    let _ = fs::remove_file(f.path());
1157                    continue;
1158                }
1159                files += 1;
1160                size += m.len();
1161            }
1162        }
1163        out.push((mtime, path, files, size));
1164    }
1165    out
1166}
1167
1168impl DiskCache {
1169    /// Evict until under capacity ([`DiskCache::capacity_for`],
1170    /// re-evaluated against the current free space). Does nothing in a
1171    /// directory that is not a cache directory (see [`CACHEDIR_TAG`]).
1172    ///
1173    /// L3 is first trimmed to its own budget (the smaller of
1174    /// [`RENDERED_MAX`] and 1/[`RENDERED_BUDGET_DIVISOR`] of the total) and
1175    /// L2 to 1/[`DERIVED_BUDGET_DIVISOR`]. Over the total, entries of APKs
1176    /// that are gone ([`DiskCache::note_source`]) go first, then L1 (the
1177    /// largest, least recently used APK directories first), then L3 (least
1178    /// recently used first), then L2 (least rebuild time saved per byte
1179    /// first, discounted by age).
1180    pub fn evict(&self) {
1181        if !self.is_cache_dir() {
1182            return;
1183        }
1184        self.written.store(0, std::sync::atomic::Ordering::Relaxed);
1185        let _ = fs::File::create(self.dir.join(EVICT_MARKER));
1186        let items = self.items();
1187        let mut total: u64 = items.iter().map(|i| i.bytes).sum();
1188        let cap = self.capacity_for(total);
1189        let caps = [cap, cap / DERIVED_BUDGET_DIVISOR, (cap / RENDERED_BUDGET_DIVISOR).min(RENDERED_MAX)];
1190        let mut tier_used = [0u64; 3];
1191        for i in &items {
1192            tier_used[i.tier as usize] += i.bytes;
1193        }
1194        if total > cap || tier_used[1] > caps[1] || tier_used[2] > caps[2] {
1195            let order = eviction_order(&items, &self.dead_keys());
1196            let mut gone = vec![false; items.len()];
1197            let mut remove = |idx: usize, total: &mut u64, tier_used: &mut [u64; 3]| {
1198                if !gone[idx] && remove_item(&items[idx]).is_ok() {
1199                    gone[idx] = true;
1200                    *total = total.saturating_sub(items[idx].bytes);
1201                    let t = items[idx].tier as usize;
1202                    tier_used[t] = tier_used[t].saturating_sub(items[idx].bytes);
1203                }
1204            };
1205            // The per-tier budgets of L3 and L2, each in its own order.
1206            for tier in [Tier::Rendered, Tier::Derived] {
1207                for &idx in order.iter().filter(|&&i| items[i].tier == tier) {
1208                    if tier_used[tier as usize] <= caps[tier as usize] {
1209                        break;
1210                    }
1211                    remove(idx, &mut total, &mut tier_used);
1212                }
1213            }
1214            for &idx in &order {
1215                if total <= cap {
1216                    break;
1217                }
1218                remove(idx, &mut total, &mut tier_used);
1219            }
1220        }
1221        // Drop the source records of keys with nothing left.
1222        if let Ok(rd) = fs::read_dir(self.dir.join(SOURCES_DIR)) {
1223            let live: HashSet<String> = self.items().into_iter().map(|i| i.key).collect();
1224            for f in rd.flatten() {
1225                if !live.contains(&*f.file_name().to_string_lossy()) {
1226                    let _ = fs::remove_file(f.path());
1227                }
1228            }
1229        }
1230    }
1231}
1232
1233impl DiskCache {
1234    /// [`DiskCache::evict`], skipped when another walk happened less than
1235    /// 5 s ago and this handle has written less than 1/16 of the budget
1236    /// since its own last walk. For hosts that evict after every burst of
1237    /// writes: a process per query would otherwise walk the whole cache
1238    /// thousands of times over a batch.
1239    pub fn evict_lazily(&self) {
1240        if !self.recently_evicted() {
1241            self.evict();
1242        }
1243    }
1244
1245    /// Whether a walk can be skipped: some process walked the cache less
1246    /// than [`EVICT_INTERVAL`] ago and this handle has written less than
1247    /// 1/[`EVICT_WRITE_DIVISOR`] of the budget since its own last walk. A
1248    /// walk reads every file's metadata (and every L2 header), which a
1249    /// process per query would otherwise repeat thousands of times over a
1250    /// batch; the overshoot is bounded, and every automatic-budget write
1251    /// still checks the free-space reserve.
1252    fn recently_evicted(&self) -> bool {
1253        let written = self.written.load(std::sync::atomic::Ordering::Relaxed);
1254        let fresh = fs::metadata(self.dir.join(EVICT_MARKER))
1255            .and_then(|m| m.modified())
1256            .ok()
1257            .and_then(|t| SystemTime::now().duration_since(t).ok())
1258            .is_some_and(|age| age < EVICT_INTERVAL);
1259        fresh && written < self.capacity_for(0) / EVICT_WRITE_DIVISOR
1260    }
1261}
1262
1263/// Touched by every eviction walk; see [`DiskCache::evict`].
1264const EVICT_MARKER: &str = ".last-evict";
1265const EVICT_INTERVAL: Duration = Duration::from_secs(5);
1266const EVICT_WRITE_DIVISOR: u64 = 16;
1267
1268/// Indexes of `items` in eviction order: entries of dead APKs first
1269/// (largest first), then L1 by `age × size` (descending), then L3 by age
1270/// (descending), then L2 by `rebuild µs / byte / (1 + age in hours)`
1271/// (ascending). Ages count from the newest item (plus one second), so clock
1272/// skew between writers cannot invert the order; ties go oldest first.
1273fn eviction_order(items: &[Item], dead: &HashSet<String>) -> Vec<usize> {
1274    let newest = items.iter().map(|i| i.mtime).max().unwrap_or(SystemTime::UNIX_EPOCH);
1275    let age_secs = |i: &Item| newest.duration_since(i.mtime).unwrap_or_default().as_secs_f64() + 1.0;
1276    let score = |i: &Item| -> (u8, f64) {
1277        if dead.contains(&i.key) {
1278            return (0, -(i.bytes as f64));
1279        }
1280        match i.tier {
1281            Tier::Dex => (1, -(age_secs(i) * i.bytes as f64)),
1282            Tier::Rendered => (2, -age_secs(i)),
1283            Tier::Derived => (3, i.cost_us as f64 / i.bytes.max(1) as f64 / (1.0 + age_secs(i) / 3600.0)),
1284        }
1285    };
1286    let mut order: Vec<usize> = (0..items.len()).collect();
1287    order.sort_by(|&a, &b| {
1288        let (sa, sb) = (score(&items[a]), score(&items[b]));
1289        sa.0.cmp(&sb.0).then(sa.1.total_cmp(&sb.1)).then(items[a].mtime.cmp(&items[b].mtime))
1290    });
1291    order
1292}
1293
1294fn remove_item(item: &Item) -> std::io::Result<()> {
1295    match item.tier {
1296        Tier::Dex => fs::remove_dir_all(&item.path),
1297        _ => fs::remove_file(&item.path),
1298    }
1299}
1300
1301/// Subdirectories of the cache root that belong to it besides APK directories.
1302const TIER_DIRS: [&str; 3] = [DERIVED_DIR, RENDERED_DIR, SOURCES_DIR];
1303
1304/// `name` with every character a cache file name may not hold replaced.
1305fn safe_name(name: &str) -> String {
1306    name.chars().map(|c| if is_key_char(c) { c } else { '_' }).collect()
1307}
1308
1309/// The key an L3 result is stored under: the producer (e.g. `findrefs`)
1310/// and its version, the L3 layout version and the query, so a tool upgrade
1311/// never serves output of the old one.
1312pub fn rendered_key(producer: &str, version: &str, query: &str) -> String {
1313    format!("{producer}\0{version}\0{RENDERED_FORMAT}\0{query}")
1314}
1315
1316/// Read-only mapping of a cache file.
1317fn map_file(file: &fs::File) -> Option<memmap2::Mmap> {
1318    // SAFETY: read-only mapping of a file renamed into place atomically; a
1319    // concurrent overwrite replaces the directory entry, not the mapped inode.
1320    unsafe { memmap2::MmapOptions::new().map(file) }.ok()
1321}
1322
1323/// The first `len` bytes of `file`, mapped as [`map_file`] does.
1324fn map_prefix(file: &fs::File, len: u64) -> Option<memmap2::Mmap> {
1325    let len = usize::try_from(len).ok()?;
1326    // SAFETY: as in `map_file`; the caller checked `len` against the file.
1327    unsafe { memmap2::MmapOptions::new().len(len).map(file) }.ok()
1328}
1329
1330/// Whether a file last modified at `mtime` may predate the current boot,
1331/// and so may hold pages a crash kept from reaching the disk. Unknown
1332/// times count as old, and so does a time in the future (a clock moved
1333/// back since): checking a sound file costs one read, trusting a damaged
1334/// one a wrong answer.
1335fn predates_boot(mtime: Option<SystemTime>, boot: Option<SystemTime>, now: SystemTime) -> bool {
1336    match (mtime, boot) {
1337        (Some(m), Some(b)) => m < b || m > now,
1338        _ => true,
1339    }
1340}
1341
1342/// When the system booted, read once per process.
1343fn boot_time() -> Option<SystemTime> {
1344    static BOOT: OnceLock<Option<SystemTime>> = OnceLock::new();
1345    *BOOT.get_or_init(read_boot_time)
1346}
1347
1348#[cfg(any(target_os = "linux", target_os = "android"))]
1349fn read_boot_time() -> Option<SystemTime> {
1350    let stat = fs::read_to_string("/proc/stat").ok()?;
1351    let secs = stat.lines().find_map(|l| l.strip_prefix("btime "))?.trim().parse().ok()?;
1352    Some(SystemTime::UNIX_EPOCH + Duration::from_secs(secs))
1353}
1354
1355#[cfg(target_vendor = "apple")]
1356fn read_boot_time() -> Option<SystemTime> {
1357    let mut tv = libc::timeval { tv_sec: 0, tv_usec: 0 };
1358    let mut len = std::mem::size_of::<libc::timeval>();
1359    // SAFETY: `kern.boottime` is a `struct timeval`; the kernel writes at most
1360    // `len` bytes into `tv` and sets `len` to what it wrote.
1361    let rc = unsafe { libc::sysctlbyname(c"kern.boottime".as_ptr(), (&raw mut tv).cast(), &mut len, std::ptr::null_mut(), 0) };
1362    if rc != 0 || len != std::mem::size_of::<libc::timeval>() {
1363        return None;
1364    }
1365    Some(
1366        SystemTime::UNIX_EPOCH
1367            + Duration::from_secs(u64::try_from(tv.tv_sec).ok()?)
1368            + Duration::from_micros(u64::try_from(tv.tv_usec).ok()?),
1369    )
1370}
1371
1372#[cfg(windows)]
1373fn read_boot_time() -> Option<SystemTime> {
1374    // SAFETY: GetTickCount64 takes no arguments and only reads the uptime.
1375    let uptime = Duration::from_millis(unsafe { windows_sys::Win32::System::SystemInformation::GetTickCount64() });
1376    SystemTime::now().checked_sub(uptime)
1377}
1378
1379#[cfg(not(any(target_os = "linux", target_os = "android", target_vendor = "apple", windows)))]
1380fn read_boot_time() -> Option<SystemTime> {
1381    None
1382}
1383
1384/// Rebuild cost recorded in an L2 file's header (0 when unreadable).
1385fn derived_cost_us(path: &Path) -> u64 {
1386    use std::io::Read;
1387    let mut header = [0u8; 24];
1388    match fs::File::open(path).and_then(|mut f| f.read_exact(&mut header)) {
1389        Ok(()) if &header[..8] == DERIVED_MAGIC => u64::from_le_bytes(header[16..24].try_into().expect("8 bytes")),
1390        _ => 0,
1391    }
1392}
1393
1394/// `size\nmtime\npath` of an APK, as [`DiskCache::note_source`] records it.
1395fn source_stamp(apk: &Path) -> Option<String> {
1396    let abs = fs::canonicalize(apk).ok()?;
1397    let meta = fs::metadata(&abs).ok()?;
1398    let mtime = meta.modified().ok()?.duration_since(SystemTime::UNIX_EPOCH).ok()?.as_nanos();
1399    let path = abs.to_str()?;
1400    (!path.contains('\n')).then(|| format!("{}\n{mtime}\n{path}", meta.len()))
1401}
1402
1403/// `path` (a file or a directory) opened so its times can be set: Unix
1404/// sets them through any handle the owner holds, Windows only through one
1405/// with FILE_WRITE_ATTRIBUTES access (and opens a directory only with
1406/// FILE_FLAG_BACKUP_SEMANTICS).
1407fn open_for_times(path: &Path) -> std::io::Result<fs::File> {
1408    #[cfg(windows)]
1409    {
1410        use std::os::windows::fs::OpenOptionsExt;
1411        const FILE_WRITE_ATTRIBUTES: u32 = 0x0100;
1412        const FILE_FLAG_BACKUP_SEMANTICS: u32 = 0x0200_0000;
1413        fs::OpenOptions::new().access_mode(FILE_WRITE_ATTRIBUTES).custom_flags(FILE_FLAG_BACKUP_SEMANTICS).open(path)
1414    }
1415    #[cfg(not(windows))]
1416    {
1417        fs::File::open(path)
1418    }
1419}
1420
1421/// Set the modification time of `file` (opened read-only at `path`) to now.
1422fn touch_file(file: &fs::File, path: &Path) -> std::io::Result<()> {
1423    if cfg!(windows) { open_for_times(path)?.set_modified(SystemTime::now()) } else { file.set_modified(SystemTime::now()) }
1424}
1425
1426fn touch_dir(dir: &Path) -> std::io::Result<()> {
1427    let now = SystemTime::now();
1428    if open_for_times(dir).and_then(|f| f.set_modified(now)).is_ok() {
1429        return Ok(());
1430    }
1431    // Portable fallback: update the directory mtime by creating and removing a marker.
1432    let marker = dir.join(".touch");
1433    fs::File::create(&marker)?;
1434    fs::remove_file(&marker)
1435}
1436
1437struct Slot<T> {
1438    value: Arc<T>,
1439    size: usize,
1440    /// Logical clock of the last access; the smallest is the LRU victim.
1441    tick: u64,
1442}
1443
1444/// Byte-bounded LRU of inflated entries plus any per-entry state the host
1445/// wants to keep alongside (`T`).
1446pub struct MemoryCache<T> {
1447    cap_bytes: usize,
1448    used: usize,
1449    /// Bytes the host holds elsewhere against the same capacity.
1450    reserved: usize,
1451    clock: u64,
1452    map: HashMap<ApkKey, HashMap<String, Slot<T>>>,
1453}
1454
1455impl<T> MemoryCache<T> {
1456    /// An empty cache holding at most `cap_bytes` bytes of entries.
1457    pub fn new(cap_bytes: usize) -> Self {
1458        MemoryCache { cap_bytes, used: 0, reserved: 0, clock: 0, map: HashMap::new() }
1459    }
1460
1461    fn tick(&mut self) -> u64 {
1462        self.clock += 1;
1463        self.clock
1464    }
1465
1466    /// The entry cached under `entry_name`, marked most recently used.
1467    pub fn get(&mut self, key: &ApkKey, entry_name: &str) -> Option<Arc<T>> {
1468        let tick = self.tick();
1469        let slot = self.map.get_mut(key)?.get_mut(entry_name)?;
1470        slot.tick = tick;
1471        Some(Arc::clone(&slot.value))
1472    }
1473
1474    /// Cache `value` under `entry_name`, charged at `size` bytes, replacing
1475    /// any previous entry and evicting least recently used ones past the
1476    /// capacity.
1477    pub fn insert(&mut self, key: &ApkKey, entry_name: &str, value: Arc<T>, size: usize) {
1478        let tick = self.tick();
1479        let per_apk = self.map.entry(key.clone()).or_default();
1480        if let Some(old) = per_apk.insert(entry_name.to_string(), Slot { value, size, tick }) {
1481            self.used -= old.size;
1482        }
1483        self.used += size;
1484        self.evict_over_cap();
1485    }
1486
1487    /// Re-charge the entry cached under `entry_name` at `size_of` its current
1488    /// value, e.g. once per-entry state built after insertion has grown it,
1489    /// and evict least recently used entries if that exceeds the capacity.
1490    /// The value is measured under the cache's lock, whatever replaced the
1491    /// one a caller last saw, so the charge always matches what the slot
1492    /// holds. Returns whether the charge changed.
1493    pub fn recharge(&mut self, key: &ApkKey, entry_name: &str, size_of: impl FnOnce(&T) -> usize) -> bool {
1494        let Some(slot) = self.map.get_mut(key).and_then(|m| m.get_mut(entry_name)) else { return false };
1495        let size = size_of(&slot.value);
1496        let old = std::mem::replace(&mut slot.size, size);
1497        if old == size {
1498            return false;
1499        }
1500        self.used = self.used - old + size;
1501        self.evict_over_cap();
1502        true
1503    }
1504
1505    /// Swap the entry cached under `entry_name` for `new` when it is still
1506    /// `current`, keeping its size and recency. Returns whether it swapped.
1507    pub fn replace(&mut self, key: &ApkKey, entry_name: &str, current: &Arc<T>, new: Arc<T>) -> bool {
1508        match self.map.get_mut(key).and_then(|m| m.get_mut(entry_name)) {
1509            Some(slot) if Arc::ptr_eq(&slot.value, current) => {
1510                slot.value = new;
1511                true
1512            }
1513            _ => false,
1514        }
1515    }
1516
1517    /// Size the entry cached under `entry_name` is charged at.
1518    pub fn charged_bytes(&self, key: &ApkKey, entry_name: &str) -> Option<usize> {
1519        self.map.get(key)?.get(entry_name).map(|s| s.size)
1520    }
1521
1522    /// Evict least recently used entries until under capacity (always
1523    /// keeping the last one: a single entry larger than the cap stays usable).
1524    fn evict_over_cap(&mut self) {
1525        while self.used + self.reserved > self.cap_bytes && self.len() > 1 {
1526            let Some((k, name)) = self.lru() else { break };
1527            if let Some(per_apk) = self.map.get_mut(&k) {
1528                if let Some(old) = per_apk.remove(&name) {
1529                    self.used -= old.size;
1530                }
1531                if per_apk.is_empty() {
1532                    self.map.remove(&k);
1533                }
1534            }
1535        }
1536    }
1537
1538    fn lru(&self) -> Option<(ApkKey, String)> {
1539        self.map
1540            .iter()
1541            .flat_map(|(k, per)| per.iter().map(move |(n, s)| (s.tick, k, n)))
1542            .min_by_key(|(t, _, _)| *t)
1543            .map(|(_, k, n)| (k.clone(), n.clone()))
1544    }
1545
1546    /// Bytes charged for the cached entries (the reserve not included).
1547    pub fn used_bytes(&self) -> usize {
1548        self.used
1549    }
1550
1551    /// Bytes the entries and the reserve may take together.
1552    pub fn capacity_bytes(&self) -> usize {
1553        self.cap_bytes
1554    }
1555
1556    /// Bytes counted against the capacity besides the entries
1557    /// ([`MemoryCache::set_reserved`]).
1558    pub fn reserved_bytes(&self) -> usize {
1559        self.reserved
1560    }
1561
1562    /// Count `bytes` the host holds elsewhere against the capacity, evicting
1563    /// least recently used entries to make room.
1564    pub fn set_reserved(&mut self, bytes: usize) {
1565        self.reserved = bytes;
1566        self.evict_over_cap();
1567    }
1568
1569    /// Change the capacity, evicting least recently used entries if the
1570    /// cache now holds more.
1571    pub fn set_capacity(&mut self, cap_bytes: usize) {
1572        self.cap_bytes = cap_bytes;
1573        self.evict_over_cap();
1574    }
1575
1576    /// The entry cached under `entry_name`, without touching its recency.
1577    pub fn peek(&self, key: &ApkKey, entry_name: &str) -> Option<Arc<T>> {
1578        self.map.get(key)?.get(entry_name).map(|s| Arc::clone(&s.value))
1579    }
1580
1581    /// Every cached value, in no particular order (recency is not touched).
1582    pub fn values(&self) -> impl Iterator<Item = &Arc<T>> {
1583        self.map.values().flat_map(|per| per.values().map(|s| &s.value))
1584    }
1585
1586    /// Number of cached entries, over every APK.
1587    pub fn len(&self) -> usize {
1588        self.map.values().map(HashMap::len).sum()
1589    }
1590
1591    /// Whether no entry is cached.
1592    pub fn is_empty(&self) -> bool {
1593        self.len() == 0
1594    }
1595
1596    /// Drop every entry cached for `key` and return the bytes released.
1597    pub fn remove_apk(&mut self, key: &ApkKey) -> usize {
1598        let freed: usize = self.map.remove(key).map(|per| per.values().map(|s| s.size).sum()).unwrap_or(0);
1599        self.used -= freed;
1600        freed
1601    }
1602
1603    /// Drop every entry (the reserve is kept).
1604    pub fn clear(&mut self) {
1605        self.map.clear();
1606        self.used = 0;
1607    }
1608}
1609
1610#[cfg(test)]
1611mod tests {
1612    use super::*;
1613
1614    #[test]
1615    fn regions_of_a_file_map_at_any_offset() {
1616        let dir = tempfile::tempdir().unwrap();
1617        let path = dir.path().join("app.apk");
1618        let bytes: Vec<u8> = (0..20_000u32).map(|i| (i % 251) as u8).collect();
1619        fs::write(&path, &bytes).unwrap();
1620        for (off, len) in [(0, 10), (4097, 3000), (12_345, 7655), (19_999, 1)] {
1621            let d = DexData::map_region(&path, off as u64, len).unwrap();
1622            assert!(matches!(d, DexData::Mapped(_)));
1623            assert_eq!(&*d, &bytes[off..off + len]);
1624        }
1625        assert!(DexData::map_region(&path, 19_999, 2).is_err());
1626        assert!(DexData::map_region(&dir.path().join("missing"), 0, 1).is_err());
1627    }
1628
1629    #[test]
1630    fn memory_remove_apk_releases_only_that_apk() {
1631        let (a, b) = (ApkKey::from_id("a-0123456789abcdef".into()), ApkKey::from_id("b-0123456789abcdef".into()));
1632        let mut mem = MemoryCache::new(1 << 20);
1633        mem.insert(&a, "classes.dex", Arc::new(1), 100);
1634        mem.insert(&a, "classes2.dex", Arc::new(2), 50);
1635        mem.insert(&b, "classes.dex", Arc::new(3), 7);
1636        assert_eq!(mem.remove_apk(&a), 150);
1637        assert_eq!(mem.remove_apk(&a), 0);
1638        assert_eq!((mem.used_bytes(), mem.len()), (7, 1));
1639        assert!(mem.get(&b, "classes.dex").is_some());
1640    }
1641
1642    #[test]
1643    fn disk_roundtrip_and_clear() {
1644        let dir = tempfile::tempdir().unwrap();
1645        let cache = DiskCache::open(dir.path()).with_capacity(1 << 20);
1646        let key = ApkKey::from_id("app-0123456789abcdef".into());
1647        assert!(cache.get(&key, "classes.dex", 3).is_none());
1648        cache.put(&key, "classes.dex", b"abc").unwrap();
1649        assert_eq!(&*cache.get(&key, "classes.dex", 3).unwrap(), b"abc");
1650        assert!(cache.get(&key, "classes.dex", 4).is_none());
1651        let (dirs, files, bytes) = cache.stats();
1652        assert_eq!((dirs, files, bytes), (1, 1, 3 + L1_TRAILER));
1653        assert_eq!(cache.clear().unwrap(), 3 + L1_TRAILER);
1654        assert!(cache.get(&key, "classes.dex", 3).is_none());
1655    }
1656
1657    const GIB: u64 = 1 << 30;
1658    const MIB: u64 = 1 << 20;
1659
1660    #[test]
1661    fn auto_disk_budget_follows_free_space() {
1662        // Unknown file system (Windows): the old fixed cap.
1663        assert_eq!(AutoDisk::default().bytes_for(None, 0), AUTO_DISK_UNKNOWN);
1664        // Large disk: capped at 32 GiB.
1665        assert_eq!(AutoDisk::default().bytes_for(Some((500 * GIB, 1000 * GIB)), 0), AutoDisk::CEILING);
1666        // A lower share and ceiling.
1667        let tight = AutoDisk { percent: 10, ceiling: 5 * GIB };
1668        assert_eq!(tight.bytes_for(Some((100 * GIB, 500 * GIB)), 0), 5 * GIB);
1669        assert_eq!(tight.bytes_for(Some((20 * GIB, 50 * GIB)), 0), 20 * GIB / 100 * 10);
1670        assert_eq!(tight.bytes_for(None, 0), 4 * GIB);
1671        // 100 GiB free: 20 GiB, past the old 4 GiB cap.
1672        assert_eq!(AutoDisk::default().bytes_for(Some((100 * GIB, 500 * GIB)), 0), 100 * GIB / 100 * 20);
1673        // Small disk: 20 % of free + own bytes.
1674        assert_eq!(AutoDisk::default().bytes_for(Some((10 * GIB, 20 * GIB)), 0), 10 * GIB / 100 * 20);
1675        assert_eq!(AutoDisk::default().bytes_for(Some((9 * GIB, 20 * GIB)), GIB), 10 * GIB / 100 * 20);
1676        // Never eats into the reserve (1 GiB here).
1677        assert_eq!(AutoDisk::default().bytes_for(Some((GIB + 100 * MIB, 16 * GIB)), 0), 100 * MIB);
1678        assert_eq!(AutoDisk::default().bytes_for(Some((GIB, 16 * GIB)), 0), 0);
1679    }
1680
1681    #[test]
1682    fn auto_disk_budget_shrinks_below_usage_on_a_nearly_full_disk() {
1683        // 100 GiB disk: 5 GiB reserve. 6 GiB cached, 0.5 GiB free: 20 % of
1684        // the 6.5 GiB the cache could hold, well below what it holds now.
1685        assert_eq!(AutoDisk::default().bytes_for(Some((GIB / 2, 100 * GIB)), 6 * GIB), (6 * GIB + GIB / 2) / 100 * 20);
1686        // 5.5 GiB cached, 0.5 GiB free: the reserve binds (1 GiB < 20 % of
1687        // 6 GiB), giving back the 4.5 GiB shortfall.
1688        assert_eq!(AutoDisk::default().bytes_for(Some((GIB / 2, 100 * GIB)), 5 * GIB + GIB / 2), GIB);
1689        // Nothing left to give: evict everything.
1690        assert_eq!(AutoDisk::default().bytes_for(Some((GIB / 2, 100 * GIB)), 3 * GIB), 0);
1691        assert_eq!(auto_disk_reserve(100 * GIB), 5 * GIB);
1692        assert_eq!(auto_disk_reserve(4 * GIB), GIB);
1693    }
1694
1695    #[test]
1696    fn explicit_disk_capacity_wins() {
1697        let dir = tempfile::tempdir().unwrap();
1698        let cache = DiskCache::open(dir.path()).with_capacity_mode(DiskCapacity::default()).with_capacity(7);
1699        assert_eq!(cache.capacity_mode(), DiskCapacity::Fixed(7));
1700        assert_eq!(cache.capacity_for(1 << 40), 7);
1701        let b = cache.budget();
1702        assert_eq!((b.capacity, b.cap_bytes, b.used), (DiskCapacity::Fixed(7), 7, 0));
1703        // The file system of a real directory is known on Unix.
1704        assert_eq!(b.space.is_some(), cfg!(unix));
1705        let auto = DiskCache::open(dir.path()).with_capacity_mode(DiskCapacity::default());
1706        // Compare against the space the budget itself measured: other tests write to the
1707        // same file system concurrently, so a second `disk_space` call may differ.
1708        let ab = auto.budget();
1709        assert_eq!(ab.cap_bytes, AutoDisk::default().bytes_for(ab.space, ab.used));
1710        // A directory that does not exist yet is measured on its parent.
1711        assert_eq!(disk_space(&dir.path().join("a/b")).is_some(), cfg!(unix));
1712    }
1713
1714    #[test]
1715    fn eviction_follows_a_lowered_capacity() {
1716        let dir = tempfile::tempdir().unwrap();
1717        let keys: Vec<ApkKey> = (0..3).map(|i| ApkKey::from_id(format!("app-000000000000000{i}"))).collect();
1718        let cache = DiskCache::open(dir.path()).with_capacity(100);
1719        for (i, k) in keys.iter().enumerate() {
1720            cache.put(k, "classes.dex", &[0; 10]).unwrap();
1721            let t = SystemTime::now() + Duration::from_secs(10 * i as u64);
1722            open_for_times(&dir.path().join(k.as_str())).unwrap().set_modified(t).unwrap();
1723        }
1724        cache.evict();
1725        assert_eq!(cache.stats(), (3, 3, 3 * (10 + L1_TRAILER)));
1726        // Same directory, smaller budget (as when the free space drops): the
1727        // next eviction pass trims to it, oldest first.
1728        let cache = cache.with_capacity(25);
1729        cache.evict();
1730        assert_eq!(cache.stats(), (1, 1, 10 + L1_TRAILER));
1731        assert!(cache.get(&keys[2], "classes.dex", 10).is_some());
1732    }
1733
1734    #[test]
1735    fn cache_dir_names_are_recognised() {
1736        assert!(is_cache_dir_name("app-0123456789abcdef"));
1737        assert!(is_cache_dir_name("-0123456789abcdef")); // empty stem
1738        assert!(is_cache_dir_name("my.app_v2-0123456789abcdef"));
1739        assert!(!is_cache_dir_name("Documents"));
1740        assert!(!is_cache_dir_name("app-0123456789ABCDEF")); // upper-case hex
1741        assert!(!is_cache_dir_name("app-0123456789abcde")); // 15 digits
1742        assert!(!is_cache_dir_name("app 1-0123456789abcdef")); // space
1743        assert!(!is_cache_dir_name(&format!("{}-0123456789abcdef", "a".repeat(41))));
1744    }
1745
1746    #[test]
1747    fn unrelated_directories_survive_evict_and_clear() {
1748        let dir = tempfile::tempdir().unwrap();
1749        let cache = DiskCache::open(dir.path()).with_capacity(4);
1750        let key = ApkKey::from_id("app-0123456789abcdef".into());
1751        // The first write tags the (empty) directory ...
1752        cache.put(&key, "classes.dex", &[0; 8]).unwrap();
1753        assert!(dir.path().join(CACHEDIR_TAG).is_file());
1754        // ... and whatever lands next to the cache afterwards is left alone.
1755        let thesis = dir.path().join("my-thesis");
1756        fs::create_dir(&thesis).unwrap();
1757        fs::write(thesis.join("chapter1.txt"), vec![0u8; 100]).unwrap();
1758        fs::write(dir.path().join("notes.txt"), b"keep").unwrap();
1759        // Only the cache directory is counted, even though the thesis is bigger.
1760        assert_eq!(cache.stats(), (1, 1, 8 + L1_TRAILER));
1761        cache.evict();
1762        assert!(thesis.join("chapter1.txt").is_file());
1763        assert!(cache.get(&key, "classes.dex", 8).is_none());
1764        cache.put(&key, "classes.dex", &[0; 8]).unwrap();
1765        assert_eq!(cache.clear().unwrap(), 8 + L1_TRAILER);
1766        assert!(thesis.join("chapter1.txt").is_file());
1767        assert!(dir.path().join("notes.txt").is_file());
1768        assert!(dir.path().join(CACHEDIR_TAG).is_file());
1769        assert!(dir.path().is_dir());
1770        assert_eq!(cache.stats(), (0, 0, 0));
1771    }
1772
1773    #[test]
1774    fn foreign_directories_are_refused_and_never_tagged() {
1775        let dir = tempfile::tempdir().unwrap();
1776        let cache = DiskCache::open(dir.path()).with_capacity(1);
1777        // A cache-shaped subdirectory next to unrelated content: not ours.
1778        let lookalike = dir.path().join("app-0123456789abcdef");
1779        fs::create_dir(&lookalike).unwrap();
1780        fs::write(lookalike.join("classes.dex"), [0; 16]).unwrap();
1781        fs::write(dir.path().join("notes.txt"), b"keep").unwrap();
1782        cache.evict();
1783        assert!(lookalike.join("classes.dex").is_file());
1784        assert!(matches!(cache.clear(), Err(CacheError::NotACacheDir { .. })));
1785        assert!(lookalike.join("classes.dex").is_file());
1786        // Writing refuses too, so no tag is planted that would make backup
1787        // tools skip the user's directory.
1788        let key = ApkKey::from_id("other-fedcba9876543210".into());
1789        assert!(matches!(cache.put(&key, "classes.dex", &[0; 4]), Err(CacheError::NotACacheDir { .. })));
1790        assert!(!dir.path().join(CACHEDIR_TAG).exists());
1791        assert!(!dir.path().join("other-fedcba9876543210").exists());
1792        // An empty or missing directory is fine.
1793        let empty = tempfile::tempdir().unwrap();
1794        assert_eq!(DiskCache::open(empty.path()).clear().unwrap(), 0);
1795        assert_eq!(DiskCache::open(empty.path().join("missing")).clear().unwrap(), 0);
1796    }
1797
1798    #[test]
1799    fn a_directory_holding_only_spilled_replies_is_still_a_cache() {
1800        // The MCP server may spill a reply before the first DEX is cached.
1801        let dir = tempfile::tempdir().unwrap();
1802        fs::create_dir(dir.path().join(SPILL_DIR)).unwrap();
1803        fs::write(dir.path().join(SPILL_DIR).join("ab.txt"), b"reply").unwrap();
1804        let cache = DiskCache::open(dir.path());
1805        assert!(cache.check_usable().is_ok());
1806        cache.put(&ApkKey::from_id("app-0123456789abcdef".into()), "classes.dex", &[0; 4]).unwrap();
1807        cache.clear().unwrap();
1808        assert!(!dir.path().join(SPILL_DIR).exists(), "clear removes spilled replies too");
1809    }
1810
1811    #[test]
1812    fn decompiler_working_directories_keep_a_cache_usable() {
1813        // A backend may start in `decompilers/<name>/` before anything is cached.
1814        let dir = tempfile::tempdir().unwrap();
1815        fs::create_dir_all(dir.path().join(DECOMPILERS_DIR).join("jeb")).unwrap();
1816        let cache = DiskCache::open(dir.path());
1817        assert!(cache.check_usable().is_ok());
1818        cache.put(&ApkKey::from_id("app-0123456789abcdef".into()), "classes.dex", &[0; 4]).unwrap();
1819        cache.clear().unwrap();
1820        assert!(dir.path().join(DECOMPILERS_DIR).join("jeb").exists(), "a backend's working directory is not the cache's to clear");
1821    }
1822
1823    #[test]
1824    fn legacy_cache_directories_are_adopted() {
1825        // Written by a build that predates the tag: only APK directories.
1826        let dir = tempfile::tempdir().unwrap();
1827        let cache = DiskCache::open(dir.path()).with_capacity(1);
1828        let legacy = dir.path().join("app-0123456789abcdef");
1829        fs::create_dir(&legacy).unwrap();
1830        fs::write(legacy.join("classes.dex"), [0; 16]).unwrap();
1831        assert_eq!(cache.stats(), (1, 1, 16));
1832        cache.evict();
1833        assert!(!legacy.exists(), "over capacity: evicted");
1834        fs::create_dir(&legacy).unwrap();
1835        fs::write(legacy.join("classes.dex"), [0; 16]).unwrap();
1836        assert_eq!(cache.clear().unwrap(), 16);
1837        // A put adopts it for good.
1838        let key = ApkKey::from_id("app-0123456789abcdef".into());
1839        cache.put(&key, "classes.dex", &[0; 4]).unwrap();
1840        assert!(dir.path().join(CACHEDIR_TAG).is_file());
1841    }
1842
1843    #[test]
1844    fn a_tag_that_is_not_a_file_is_not_a_tag() {
1845        let dir = tempfile::tempdir().unwrap();
1846        let cache = DiskCache::open(dir.path()).with_capacity(1);
1847        fs::create_dir(dir.path().join(CACHEDIR_TAG)).unwrap();
1848        fs::write(dir.path().join("notes.txt"), b"keep").unwrap();
1849        let key = ApkKey::from_id("app-0123456789abcdef".into());
1850        assert!(matches!(cache.put(&key, "classes.dex", &[0; 4]), Err(CacheError::NotACacheDir { .. })));
1851        assert!(matches!(cache.clear(), Err(CacheError::NotACacheDir { .. })));
1852    }
1853
1854    #[test]
1855    fn dot_files_make_a_directory_foreign() {
1856        // A checkout or config directory can hold nothing but dot-files.
1857        let dir = tempfile::tempdir().unwrap();
1858        let cache = DiskCache::open(dir.path());
1859        fs::write(dir.path().join(".gitignore"), b"*\n").unwrap();
1860        assert!(matches!(cache.check_usable(), Err(CacheError::NotACacheDir { .. })));
1861        let key = ApkKey::from_id("app-0123456789abcdef".into());
1862        assert!(matches!(cache.put(&key, "classes.dex", &[0; 4]), Err(CacheError::NotACacheDir { .. })));
1863        assert!(!dir.path().join(CACHEDIR_TAG).exists());
1864        // Finder metadata next to a legacy cache does not.
1865        let legacy = tempfile::tempdir().unwrap();
1866        fs::create_dir(legacy.path().join("app-0123456789abcdef")).unwrap();
1867        fs::write(legacy.path().join(".DS_Store"), b"").unwrap();
1868        assert!(DiskCache::open(legacy.path()).check_usable().is_ok());
1869    }
1870
1871    #[test]
1872    fn memory_lru_evicts_by_bytes() {
1873        let mut m: MemoryCache<Vec<u8>> = MemoryCache::new(10);
1874        let k = ApkKey::from_id("k".into());
1875        m.insert(&k, "a", Arc::new(vec![0; 4]), 4);
1876        m.insert(&k, "b", Arc::new(vec![0; 4]), 4);
1877        assert!(m.get(&k, "a").is_some()); // a is now most recent
1878        m.insert(&k, "c", Arc::new(vec![0; 4]), 4); // evicts b
1879        assert!(m.get(&k, "b").is_none());
1880        assert!(m.get(&k, "a").is_some());
1881        assert_eq!(m.used_bytes(), 8);
1882        assert_eq!(m.len(), 2);
1883        // Re-inserting an entry replaces its accounting.
1884        m.insert(&k, "a", Arc::new(vec![0; 2]), 2);
1885        assert_eq!(m.used_bytes(), 6);
1886        m.clear();
1887        assert!(m.is_empty());
1888    }
1889
1890    #[test]
1891    fn memory_lru_recharge_measures_the_current_value_and_evicts() {
1892        let mut m: MemoryCache<Vec<u8>> = MemoryCache::new(10);
1893        let k = ApkKey::from_id("k".into());
1894        m.insert(&k, "a", Arc::new(vec![0; 4]), 4);
1895        m.insert(&k, "b", Arc::new(vec![0; 7]), 4);
1896        // Growing b past the cap evicts a (least recently used).
1897        assert!(m.recharge(&k, "b", Vec::len));
1898        assert_eq!(m.used_bytes(), 7);
1899        assert!(m.get(&k, "a").is_none());
1900        assert_eq!(m.charged_bytes(&k, "b"), Some(7));
1901        assert!(!m.recharge(&k, "b", Vec::len));
1902        // Whatever replaced the entry is what gets measured.
1903        m.insert(&k, "b", Arc::new(vec![1; 9]), 4);
1904        assert!(m.recharge(&k, "b", Vec::len));
1905        assert_eq!(m.charged_bytes(&k, "b"), Some(9));
1906        assert!(!m.recharge(&k, "missing", Vec::len));
1907        // The last entry is kept even when it alone exceeds the cap.
1908        assert!(m.recharge(&k, "b", |_| 50));
1909        assert_eq!((m.len(), m.used_bytes()), (1, 50));
1910    }
1911
1912    #[test]
1913    fn memory_lru_capacity_can_shrink_and_peek_keeps_recency() {
1914        let mut m: MemoryCache<u8> = MemoryCache::new(10);
1915        let k = ApkKey::from_id("k".into());
1916        m.insert(&k, "a", Arc::new(1), 4);
1917        m.insert(&k, "b", Arc::new(2), 4);
1918        assert_eq!(*m.peek(&k, "a").unwrap(), 1); // a stays least recently used
1919        m.set_capacity(5);
1920        assert_eq!((m.capacity_bytes(), m.used_bytes()), (5, 4));
1921        assert!(m.peek(&k, "a").is_none());
1922        assert!(m.peek(&k, "b").is_some());
1923    }
1924
1925    #[test]
1926    fn memory_lru_reserved_bytes_make_room_under_the_cap() {
1927        let mut m: MemoryCache<u8> = MemoryCache::new(10);
1928        let k = ApkKey::from_id("k".into());
1929        m.insert(&k, "a", Arc::new(1), 4);
1930        m.insert(&k, "b", Arc::new(2), 4);
1931        m.set_reserved(3);
1932        assert_eq!((m.used_bytes(), m.reserved_bytes()), (4, 3), "a evicted to fit 3 reserved bytes");
1933        assert!(m.peek(&k, "a").is_none());
1934        m.insert(&k, "c", Arc::new(3), 3);
1935        assert_eq!(m.used_bytes(), 7);
1936        m.set_reserved(0);
1937        m.insert(&k, "d", Arc::new(4), 3);
1938        assert_eq!(m.used_bytes(), 10, "nothing reserved: the whole cap holds entries");
1939    }
1940
1941    #[test]
1942    fn memory_lru_replace_keeps_size_and_recency() {
1943        let mut m: MemoryCache<Vec<u8>> = MemoryCache::new(10);
1944        let k = ApkKey::from_id("k".into());
1945        let a = Arc::new(vec![0; 4]);
1946        m.insert(&k, "a", Arc::clone(&a), 4);
1947        m.insert(&k, "b", Arc::new(vec![0; 4]), 4);
1948        assert!(m.get(&k, "a").is_some()); // a most recent
1949        let a2 = Arc::new(vec![9; 4]);
1950        assert!(m.replace(&k, "a", &a, Arc::clone(&a2)));
1951        assert!(!m.replace(&k, "a", &a, Arc::new(vec![])), "stale handle");
1952        assert!(!m.replace(&k, "missing", &a, Arc::new(vec![])));
1953        assert_eq!(m.used_bytes(), 8);
1954        m.insert(&k, "c", Arc::new(vec![0; 4]), 4); // evicts b, not the replaced a
1955        assert!(Arc::ptr_eq(&m.get(&k, "a").unwrap(), &a2));
1956        assert!(m.get(&k, "b").is_none());
1957    }
1958
1959    #[test]
1960    fn fnv_lanes_match_scalar() {
1961        fn scalar(bytes: &[u8]) -> u64 {
1962            let mut h: u64 = 0xcbf2_9ce4_8422_2325;
1963            for &b in bytes {
1964                h ^= b as u64;
1965                h = h.wrapping_mul(0x0100_0000_01b3);
1966            }
1967            h
1968        }
1969        // The lane variant is a different function on purpose; it only has to
1970        // be stable and spread inputs. Check it distinguishes nearby inputs.
1971        let a = fnv64(b"PK\x01\x02classes.dex");
1972        let b = fnv64(b"PK\x01\x02classes2.dex");
1973        assert_ne!(a, b);
1974        assert_ne!(fnv64(b""), fnv64(b"\0"));
1975        assert_eq!(fnv64(b"abc"), scalar(b"abc")); // short inputs take the scalar path
1976    }
1977
1978    fn set_mtime(path: &Path, secs_from_now: i64) {
1979        let t = if secs_from_now >= 0 {
1980            SystemTime::now() + Duration::from_secs(secs_from_now as u64)
1981        } else {
1982            SystemTime::now() - Duration::from_secs((-secs_from_now) as u64)
1983        };
1984        open_for_times(path).unwrap().set_modified(t).unwrap();
1985    }
1986
1987    #[test]
1988    fn files_are_checked_when_they_predate_the_boot() {
1989        let now = SystemTime::now();
1990        let boot = Some(now - Duration::from_secs(60));
1991        assert!(predates_boot(Some(now - Duration::from_secs(61)), boot, now));
1992        assert!(!predates_boot(Some(now - Duration::from_secs(59)), boot, now));
1993        // A clock moved back, an unknown boot or mtime: checked.
1994        assert!(predates_boot(Some(now + Duration::from_secs(5)), boot, now));
1995        assert!(predates_boot(Some(now), None, now));
1996        assert!(predates_boot(None, boot, now));
1997    }
1998
1999    /// A cache whose system booted a minute ago.
2000    fn booted_cache(dir: &Path) -> DiskCache {
2001        let mut cache = DiskCache::open(dir).with_capacity(1 << 20);
2002        cache.boot = Some(SystemTime::now() - Duration::from_secs(60));
2003        cache
2004    }
2005
2006    /// Overwrite `damage` (offset, bytes) and cut the file to `cut`, then
2007    /// make it look written before the boot of [`booted_cache`].
2008    fn age(path: &Path, damage: Option<(u64, &[u8])>, cut: Option<u64>) {
2009        let f = fs::OpenOptions::new().write(true).open(path).unwrap();
2010        if let Some((at, bytes)) = damage {
2011            (&f).seek(SeekFrom::Start(at)).unwrap();
2012            (&f).write_all(bytes).unwrap();
2013        }
2014        if let Some(len) = cut {
2015            f.set_len(len).unwrap();
2016        }
2017        f.set_modified(SystemTime::now() - Duration::from_secs(120)).unwrap();
2018    }
2019
2020    #[test]
2021    fn l1_files_from_an_earlier_boot_are_checked() {
2022        let dir = tempfile::tempdir().unwrap();
2023        let cache = booted_cache(dir.path());
2024        let key = ApkKey::from_id("app-0123456789abcdef".into());
2025        let data: Vec<u8> = (0..4096u32).map(|i| i as u8).collect();
2026        let path = cache.entry_path(&key, "classes.dex");
2027        let n = data.len() as u64;
2028
2029        // Sound: served, and marked checked for this boot.
2030        cache.put(&key, "classes.dex", &data).unwrap();
2031        age(&path, None, None);
2032        assert_eq!(&*cache.get(&key, "classes.dex", n).unwrap(), &data[..]);
2033        assert!(!cache.predates_boot(&fs::metadata(&path).unwrap()));
2034
2035        // Zeroed pages, a flipped byte: deleted and missed.
2036        let flipped = [!data[7]];
2037        for (at, bytes) in [(1024, &[0u8; 1024][..]), (7, &flipped[..])] {
2038            cache.put(&key, "classes.dex", &data).unwrap();
2039            age(&path, Some((at, bytes)), None);
2040            assert!(cache.get(&key, "classes.dex", n).is_none(), "damage at {at}");
2041            assert!(!path.exists(), "damage at {at}");
2042        }
2043        // Cut short (looked up without a declared size, so the length alone
2044        // cannot tell): the trailer is gone.
2045        for cut in [n, n - 100] {
2046            cache.put(&key, "classes.dex", &data).unwrap();
2047            age(&path, None, Some(cut));
2048            assert!(cache.get(&key, "classes.dex", 0).is_none(), "cut to {cut}");
2049            assert!(!path.exists(), "cut to {cut}");
2050        }
2051
2052        // Written during this boot: trusted without a check.
2053        cache.put(&key, "classes.dex", &data).unwrap();
2054        let f = fs::OpenOptions::new().write(true).open(&path).unwrap();
2055        (&f).write_all(&[!data[0]]).unwrap();
2056        f.set_modified(SystemTime::now()).unwrap();
2057        assert_eq!(cache.get(&key, "classes.dex", n).unwrap()[0], !data[0]);
2058    }
2059
2060    #[test]
2061    fn l2_and_l3_files_carry_checksums() {
2062        let dir = tempfile::tempdir().unwrap();
2063        let cache = booted_cache(dir.path());
2064        let key = ApkKey::from_id("app-0123456789abcdef".into());
2065        let path = cache.derived_path(&key, "classes.dex", "locators", 1);
2066        let flip = DERIVED_HEADER as u64 + 3;
2067        cache.put_derived(&key, "classes.dex", "locators", 1, Duration::from_millis(7), b"tables").unwrap();
2068        age(&path, None, None);
2069        assert_eq!(&*cache.get_derived(&key, "classes.dex", "locators", 1).unwrap(), b"tables");
2070        age(&path, Some((flip, b"X")), None);
2071        assert!(cache.get_derived(&key, "classes.dex", "locators", 1).is_none());
2072        assert!(!path.exists());
2073
2074        // L3 is checked on every read.
2075        cache.put_rendered(&key, "q", b"rows").unwrap();
2076        assert_eq!(cache.get_rendered(&key, "q").unwrap(), b"rows");
2077        let path = cache.rendered_path(&key, "q");
2078        let len = fs::metadata(&path).unwrap().len();
2079        let f = fs::OpenOptions::new().write(true).open(&path).unwrap();
2080        (&f).seek(SeekFrom::Start(len - 1)).unwrap();
2081        (&f).write_all(b"X").unwrap();
2082        assert!(cache.get_rendered(&key, "q").is_none());
2083        assert!(!path.exists());
2084    }
2085
2086    #[test]
2087    fn derived_roundtrip_is_versioned_and_aligned() {
2088        let dir = tempfile::tempdir().unwrap();
2089        let cache = DiskCache::open(dir.path()).with_capacity(1 << 20);
2090        let key = ApkKey::from_id("app-0123456789abcdef".into());
2091        assert!(cache.get_derived(&key, "classes.dex", "locators", 1).is_none());
2092        cache.put_derived(&key, "classes.dex", "locators", 1, Duration::from_millis(7), b"tables").unwrap();
2093        let d = cache.get_derived(&key, "classes.dex", "locators", 1).unwrap();
2094        assert_eq!(&*d, b"tables");
2095        assert_eq!(d.build_cost, Duration::from_millis(7));
2096        assert_eq!(d.as_ptr() as usize % 64, 0, "payload as aligned as the mapping");
2097        drop(d);
2098        // Another producer version, kind or name is another file.
2099        assert!(cache.get_derived(&key, "classes.dex", "locators", 2).is_none());
2100        assert!(cache.get_derived(&key, "classes.dex", "callgraph", 1).is_none());
2101        assert!(cache.get_derived(&key, "classes2.dex", "locators", 1).is_none());
2102        // A truncated file is a miss.
2103        let path = cache.derived_path(&key, "classes.dex", "locators", 1);
2104        let full = fs::read(&path).unwrap();
2105        fs::write(&path, &full[..full.len() - 1]).unwrap();
2106        assert!(cache.get_derived(&key, "classes.dex", "locators", 1).is_none());
2107        fs::write(&path, &full[..10]).unwrap();
2108        assert!(cache.get_derived(&key, "classes.dex", "locators", 1).is_none());
2109        let [l1, l2, l3] = cache.tier_stats();
2110        assert_eq!((l1.files, l2.files, l3.files), (0, 1, 0));
2111    }
2112
2113    #[test]
2114    fn rendered_results_are_keyed_by_the_full_query() {
2115        let dir = tempfile::tempdir().unwrap();
2116        let cache = DiskCache::open(dir.path()).with_capacity(1 << 20);
2117        let key = ApkKey::from_id("app-0123456789abcdef".into());
2118        let q = rendered_key("findrefs", "1.0", "string\0token");
2119        assert!(cache.get_rendered(&key, &q).is_none());
2120        cache.put_rendered(&key, &q, b"rows").unwrap();
2121        assert_eq!(cache.get_rendered(&key, &q).unwrap(), b"rows");
2122        assert!(cache.get_rendered(&key, &rendered_key("findrefs", "1.1", "string\0token")).is_none(), "tool upgrade");
2123        // A file found under another query's hash (a collision) is a miss.
2124        let other = rendered_key("findrefs", "1.0", "string\0other");
2125        fs::rename(cache.rendered_path(&key, &q), cache.rendered_path(&key, &other)).unwrap();
2126        assert!(cache.get_rendered(&key, &other).is_none());
2127    }
2128
2129    #[test]
2130    fn stats_and_clear_cover_every_tier() {
2131        let dir = tempfile::tempdir().unwrap();
2132        let cache = DiskCache::open(dir.path()).with_capacity(1 << 20);
2133        let (a, b) = (ApkKey::from_id("a-0123456789abcdef".into()), ApkKey::from_id("b-0123456789abcdef".into()));
2134        cache.put(&a, "classes.dex", &[0; 10]).unwrap();
2135        cache.put_derived(&a, "classes.dex", "locators", 1, Duration::ZERO, &[0; 6]).unwrap();
2136        cache.put_rendered(&b, "q", &[0; 3]).unwrap();
2137        let (apks, files, bytes) = cache.stats();
2138        assert_eq!((apks, files), (2, 3));
2139        assert_eq!(bytes, (10 + L1_TRAILER) + (64 + 6) + (16 + 1 + 3));
2140        let [l1, l2, l3] = cache.tier_stats();
2141        assert_eq!((l1.apks, l2.apks, l3.apks), (1, 1, 1));
2142        // A cache with tier directories but no tag (lost) is still ours.
2143        fs::remove_file(dir.path().join(CACHEDIR_TAG)).unwrap();
2144        assert!(cache.check_usable().is_ok());
2145        assert_eq!(cache.clear().unwrap(), bytes);
2146        assert_eq!(cache.stats(), (0, 0, 0));
2147        assert!(!dir.path().join("derived").exists() && !dir.path().join("rendered").exists());
2148    }
2149
2150    #[test]
2151    fn disabled_tiers_miss_and_skip_writes() {
2152        let dir = tempfile::tempdir().unwrap();
2153        let cache = DiskCache::open(dir.path()).with_capacity(1 << 20).with_tiers(&[Tier::Derived]);
2154        let key = ApkKey::from_id("app-0123456789abcdef".into());
2155        cache.put(&key, "classes.dex", b"abc").unwrap();
2156        cache.put_rendered(&key, "q", b"rows").unwrap();
2157        cache.put_derived(&key, "classes.dex", "k", 1, Duration::ZERO, b"t").unwrap();
2158        assert_eq!(cache.tier_stats().map(|t| t.files), [0, 1, 0]);
2159        let all = DiskCache::open(dir.path()).with_capacity(1 << 20).with_tiers(&Tier::ALL);
2160        all.put(&key, "classes.dex", b"abc").unwrap();
2161        assert!(cache.get(&key, "classes.dex", 3).is_none());
2162        assert!(cache.uses(Tier::Derived) && !cache.uses(Tier::Dex));
2163    }
2164
2165    #[test]
2166    fn lazy_eviction_skips_a_walk_right_after_another() {
2167        let dir = tempfile::tempdir().unwrap();
2168        let cache = DiskCache::open(dir.path()).with_capacity(1 << 30);
2169        let key = ApkKey::from_id("app-0123456789abcdef".into());
2170        cache.put(&key, "classes.dex", &[0; 100]).unwrap();
2171        cache.evict();
2172        assert!(dir.path().join(EVICT_MARKER).is_file());
2173        assert!(cache.check_usable().is_ok(), "the marker is the cache's own");
2174        // Over a lowered budget: a lazy pass right after a walk leaves it ...
2175        let cache = cache.with_capacity(50);
2176        cache.evict_lazily();
2177        assert_eq!(cache.stats().1, 1);
2178        // ... until enough was written since (1/16 of the budget), or the walk is old.
2179        cache.put(&key, "classes2.dex", &[0; 100]).unwrap();
2180        cache.evict_lazily();
2181        assert_eq!(cache.stats().1, 0);
2182    }
2183
2184    #[test]
2185    fn small_dex_entries_are_not_admitted_by_default() {
2186        let dir = tempfile::tempdir().unwrap();
2187        let cache = DiskCache::open(dir.path());
2188        assert_eq!(cache.min_dex_bytes(), DEFAULT_MIN_DEX_BYTES);
2189        let cache = cache.with_min_dex_bytes(100);
2190        assert!(!cache.admits_dex(99));
2191        assert!(cache.admits_dex(100));
2192        assert!(cache.with_min_dex_bytes(0).admits_dex(0));
2193    }
2194
2195    #[test]
2196    fn eviction_trims_tiers_then_goes_l1_l3_l2() {
2197        let dir = tempfile::tempdir().unwrap();
2198        // Budget 1000: L2 may hold 500, L3 125.
2199        let cache = DiskCache::open(dir.path()).with_capacity(1000);
2200        let k = |i: u32| ApkKey::from_id(format!("app-{i:016x}"));
2201        // L3: four 64-byte results (+ header), the oldest beyond L3's own budget.
2202        for i in 0..4 {
2203            let q = format!("q{i}");
2204            cache.put_rendered(&k(0), &q, &[0; 64]).unwrap();
2205            set_mtime(&cache.rendered_path(&k(0), &q), -100 + i as i64);
2206        }
2207        cache.evict();
2208        let [_, _, l3] = cache.tier_stats();
2209        assert_eq!(l3.files, 1, "trimmed to its own budget, newest kept");
2210        assert!(cache.get_rendered(&k(0), "q3").is_some());
2211        // L2: a cheap and an expensive index per byte; L1: two APKs.
2212        cache.put_derived(&k(1), "classes.dex", "cheap", 1, Duration::from_micros(10), &[0; 136]).unwrap();
2213        cache.put_derived(&k(1), "classes.dex", "dear", 1, Duration::from_secs(1), &[0; 136]).unwrap();
2214        cache.put(&k(2), "classes.dex", &[0; 300]).unwrap();
2215        cache.put(&k(3), "classes.dex", &[0; 300]).unwrap();
2216        set_mtime(&dir.path().join(k(2).as_str()), -50);
2217        // 78 + 400 + 600 = 1078 > 1000: one L1 directory goes, the older.
2218        cache.evict();
2219        assert!(cache.get(&k(2), "classes.dex", 300).is_none());
2220        assert!(cache.get(&k(3), "classes.dex", 300).is_some());
2221        assert_eq!(cache.tier_stats().map(|t| t.files), [1, 2, 1]);
2222        // Budget 450 (L2 225, L3 56): L3 and the cheap L2 index exceed their
2223        // own budgets, then L1 goes to fit the total.
2224        let cache = cache.with_capacity(450);
2225        cache.evict();
2226        assert_eq!(cache.tier_stats().map(|t| t.files), [0, 1, 0]);
2227        assert!(cache.get_derived(&k(1), "classes.dex", "dear", 1).is_some());
2228    }
2229
2230    #[test]
2231    fn l1_eviction_weighs_size_with_age() {
2232        let dir = tempfile::tempdir().unwrap();
2233        let cache = DiskCache::open(dir.path()).with_capacity(600);
2234        let (big, small) = (ApkKey::from_id("big-0000000000000001".into()), ApkKey::from_id("small-0000000000000002".into()));
2235        // The big entry is newer but costs far more space per second of age.
2236        cache.put(&small, "classes.dex", &[0; 100]).unwrap();
2237        cache.put(&big, "classes.dex", &[0; 550]).unwrap();
2238        set_mtime(&dir.path().join(small.as_str()), -3);
2239        cache.evict();
2240        assert!(cache.get(&small, "classes.dex", 100).is_some());
2241        assert!(cache.get(&big, "classes.dex", 550).is_none());
2242    }
2243
2244    #[test]
2245    fn entries_of_deleted_apks_go_first() {
2246        let dir = tempfile::tempdir().unwrap();
2247        let apks = tempfile::tempdir().unwrap();
2248        let cache = DiskCache::open(dir.path()).with_capacity(1000);
2249        let (live, dead) = (ApkKey::from_id("live-0000000000000001".into()), ApkKey::from_id("dead-0000000000000002".into()));
2250        let (live_apk, dead_apk) = (apks.path().join("live.apk"), apks.path().join("dead.apk"));
2251        fs::write(&live_apk, b"PK").unwrap();
2252        fs::write(&dead_apk, b"PK").unwrap();
2253        cache.note_source(&live, &live_apk).unwrap();
2254        cache.note_source(&dead, &dead_apk).unwrap();
2255        cache.put(&live, "classes.dex", &[0; 400]).unwrap();
2256        // The dead APK's entry is the newest one, and its L2 index the dearest.
2257        cache.put_derived(&dead, "classes.dex", "locators", 1, Duration::from_secs(9), &[0; 100]).unwrap();
2258        cache.put(&dead, "classes.dex", &[0; 400]).unwrap();
2259        set_mtime(&dir.path().join(live.as_str()), -100);
2260        fs::remove_file(&dead_apk).unwrap();
2261        // 400 + 164 + 400 = 964: within budget, nothing goes.
2262        cache.evict();
2263        assert_eq!(cache.stats().1, 3);
2264        let cache = cache.with_capacity(500);
2265        cache.evict();
2266        assert!(cache.get(&live, "classes.dex", 400).is_some());
2267        assert!(cache.get(&dead, "classes.dex", 400).is_none());
2268        assert!(cache.get_derived(&dead, "classes.dex", "locators", 1).is_none());
2269        assert!(!dir.path().join("sources").join(dead.as_str()).exists(), "source record dropped with its entries");
2270        assert!(dir.path().join("sources").join(live.as_str()).exists());
2271    }
2272}