1mod 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
62pub const MAPPED_CHARGE_DIVISOR: usize = 8;
75
76#[derive(Debug, Clone)]
85pub struct ApkKey {
86 id: String,
87 archives: Arc<[ArchiveKey]>,
89}
90
91#[derive(Debug)]
93struct ArchiveKey {
94 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
114fn is_key_char(c: char) -> bool {
116 c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.'
117}
118const STEM_MAX: usize = 40;
120const HASH_HEX_LEN: usize = 16;
122
123impl ApkKey {
124 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 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 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 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 pub fn as_str(&self) -> &str {
168 &self.id
169 }
170
171 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
184fn 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#[non_exhaustive]
203pub enum DexData {
204 Owned(Vec<u8>),
206 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 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 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#[derive(Debug, thiserror::Error)]
251#[non_exhaustive]
252pub enum CacheError {
253 #[error("cache io error at {path}: {source}")]
255 Io {
256 path: PathBuf,
258 source: std::io::Error,
260 },
261 #[error("{path} is not a asc cache directory (no {tag} marker); refusing to touch it")]
264 NotACacheDir {
265 path: PathBuf,
267 tag: &'static str,
269 },
270 #[error("{path}: too little free disk space left to cache {bytes} bytes")]
273 LowDiskSpace {
274 path: PathBuf,
276 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
285pub struct DiskCache {
287 dir: PathBuf,
288 cap: DiskCapacity,
289 min_dex_bytes: u64,
290 min_derived_cost: Duration,
291 tiers: [bool; 3],
293 written: std::sync::atomic::AtomicU64,
295 boot: Option<SystemTime>,
298}
299
300#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
302#[non_exhaustive]
303pub enum Tier {
304 Dex,
306 Derived,
308 Rendered,
310}
311
312impl Tier {
313 pub const ALL: [Tier; 3] = [Tier::Dex, Tier::Derived, Tier::Rendered];
315
316 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 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
335const DERIVED_DIR: &str = "derived";
337const RENDERED_DIR: &str = "rendered";
339const SOURCES_DIR: &str = "sources";
341pub const SPILL_DIR: &str = "out";
344pub const DECOMPILERS_DIR: &str = "decompilers";
347
348pub const RENDERED_MAX: u64 = 128 << 20;
351pub const RENDERED_BUDGET_DIVISOR: u64 = 8;
353pub const DERIVED_BUDGET_DIVISOR: u64 = 2;
355
356pub const DEFAULT_MIN_DEX_BYTES: u64 = 4 << 20;
359pub const DEFAULT_MIN_DERIVED_COST: Duration = Duration::from_millis(2);
362
363const DERIVED_MAGIC: &[u8; 8] = b"ASCL2\0\0\0";
366const DERIVED_HEADER: usize = 64;
367const DERIVED_FORMAT: u32 = 2;
369const RENDERED_MAGIC: &[u8; 8] = b"ASCL3\0\0\0";
370const RENDERED_FORMAT: u32 = 2;
372const L1_MAGIC: &[u8; 4] = b"ASC1";
375const L1_TRAILER: u64 = 8;
376
377#[non_exhaustive]
380pub struct Derived {
381 map: memmap2::Mmap,
382 pub build_cost: Duration,
384}
385
386#[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#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
414#[non_exhaustive]
415pub struct TierStats {
416 pub apks: usize,
418 pub files: usize,
420 pub bytes: u64,
422}
423
424struct Item {
426 tier: Tier,
427 key: String,
428 path: PathBuf,
429 mtime: SystemTime,
430 files: usize,
431 bytes: u64,
432 cost_us: u64,
434}
435
436#[derive(Debug, Clone, Copy, PartialEq, Eq)]
438pub enum DiskCapacity {
439 Auto(AutoDisk),
441 Fixed(u64),
443}
444
445impl Default for DiskCapacity {
446 fn default() -> DiskCapacity {
447 DiskCapacity::Auto(AutoDisk::default())
448 }
449}
450
451pub const AUTO_DISK_UNKNOWN: u64 = 4 << 30;
453pub const AUTO_DISK_RESERVE_MIN: u64 = 1 << 30;
456pub const AUTO_DISK_RESERVE_PERCENT: u64 = 5;
459
460pub fn auto_disk_reserve(total: u64) -> u64 {
463 AUTO_DISK_RESERVE_MIN.max(total / 100 * AUTO_DISK_RESERVE_PERCENT)
464}
465
466#[derive(Debug, Clone, Copy, PartialEq, Eq)]
470#[non_exhaustive]
471pub struct AutoDisk {
472 pub percent: u8,
474 pub ceiling: u64,
476}
477
478impl AutoDisk {
479 pub const PERCENT: u8 = 20;
481 pub const CEILING: u64 = 32 << 30;
484
485 pub fn new(percent: u8, ceiling: u64) -> AutoDisk {
487 AutoDisk { percent, ceiling }
488 }
489
490 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
511pub 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 if unsafe { libc::statvfs(c.as_ptr(), st.as_mut_ptr()) } != 0 {
530 return None;
531 }
532 let st = unsafe { st.assume_init() };
534 #[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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
547#[non_exhaustive]
548pub struct DiskBudget {
549 pub capacity: DiskCapacity,
551 pub cap_bytes: u64,
553 pub used: u64,
555 pub space: Option<(u64, u64)>,
557}
558
559const STALE_TMP: Duration = Duration::from_secs(10 * 60);
561
562pub 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
574fn 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 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 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 pub fn with_tiers(mut self, tiers: &[Tier]) -> DiskCache {
620 self.tiers = Tier::ALL.map(|t| tiers.contains(&t));
621 self
622 }
623
624 pub fn uses(&self, tier: Tier) -> bool {
626 self.tiers[tier as usize]
627 }
628
629 pub fn with_min_dex_bytes(mut self, bytes: u64) -> DiskCache {
631 self.min_dex_bytes = bytes;
632 self
633 }
634
635 pub fn min_dex_bytes(&self) -> u64 {
639 self.min_dex_bytes
640 }
641
642 pub fn admits_dex(&self, len: u64) -> bool {
645 len >= self.min_dex_bytes
646 }
647
648 pub fn with_min_derived_cost(mut self, cost: Duration) -> DiskCache {
651 self.min_derived_cost = cost;
652 self
653 }
654
655 pub fn admits_derived(&self, cost: Duration) -> bool {
658 cost >= self.min_derived_cost
659 }
660
661 pub fn open_default() -> Option<DiskCache> {
663 Self::default_dir().map(Self::open)
664 }
665
666 pub fn with_capacity(self, cap_bytes: u64) -> DiskCache {
668 self.with_capacity_mode(DiskCapacity::Fixed(cap_bytes))
669 }
670
671 pub fn with_capacity_mode(mut self, cap: DiskCapacity) -> DiskCache {
673 self.cap = cap;
674 self
675 }
676
677 pub fn capacity_mode(&self) -> DiskCapacity {
679 self.cap
680 }
681
682 pub fn dir(&self) -> &Path {
684 &self.dir
685 }
686
687 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 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 fn predates_boot(&self, meta: &fs::Metadata) -> bool {
709 predates_boot(meta.modified().ok(), self.boot, SystemTime::now())
710 }
711
712 fn discard<T>(&self, path: &Path) -> Option<T> {
714 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 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 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 drop(map);
766 return self.discard(&path);
767 }
768 let _ = touch_file(&file, &path);
769 }
770 let _ = touch_dir(path.parent()?);
772 Some(DexData::Mapped(map))
773 }
774
775 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 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 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 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 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 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 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 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 fn write_atomic(&self, path: &Path, parts: &[&[u8]]) -> Result<(), CacheError> {
933 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 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 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 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 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 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 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 fn ensure_tag(&self) -> Result<(), CacheError> {
1031 if self.has_tag() {
1032 return Ok(());
1033 }
1034 let tag = self.dir.join(CACHEDIR_TAG);
1035 match fs::OpenOptions::new().write(true).create_new(true).open(&tag) {
1037 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 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 fn apk_dirs(&self) -> Vec<(SystemTime, PathBuf, usize, u64)> {
1083 apk_dirs_in(&self.dir)
1084 }
1085
1086 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 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
1135fn 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 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 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 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 pub fn evict_lazily(&self) {
1240 if !self.recently_evicted() {
1241 self.evict();
1242 }
1243 }
1244
1245 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
1263const EVICT_MARKER: &str = ".last-evict";
1265const EVICT_INTERVAL: Duration = Duration::from_secs(5);
1266const EVICT_WRITE_DIVISOR: u64 = 16;
1267
1268fn 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
1301const TIER_DIRS: [&str; 3] = [DERIVED_DIR, RENDERED_DIR, SOURCES_DIR];
1303
1304fn safe_name(name: &str) -> String {
1306 name.chars().map(|c| if is_key_char(c) { c } else { '_' }).collect()
1307}
1308
1309pub fn rendered_key(producer: &str, version: &str, query: &str) -> String {
1313 format!("{producer}\0{version}\0{RENDERED_FORMAT}\0{query}")
1314}
1315
1316fn map_file(file: &fs::File) -> Option<memmap2::Mmap> {
1318 unsafe { memmap2::MmapOptions::new().map(file) }.ok()
1321}
1322
1323fn map_prefix(file: &fs::File, len: u64) -> Option<memmap2::Mmap> {
1325 let len = usize::try_from(len).ok()?;
1326 unsafe { memmap2::MmapOptions::new().len(len).map(file) }.ok()
1328}
1329
1330fn 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
1342fn 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 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 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
1384fn 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
1394fn 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
1403fn 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
1421fn 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 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 tick: u64,
1442}
1443
1444pub struct MemoryCache<T> {
1447 cap_bytes: usize,
1448 used: usize,
1449 reserved: usize,
1451 clock: u64,
1452 map: HashMap<ApkKey, HashMap<String, Slot<T>>>,
1453}
1454
1455impl<T> MemoryCache<T> {
1456 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 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 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 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 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 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 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 pub fn used_bytes(&self) -> usize {
1548 self.used
1549 }
1550
1551 pub fn capacity_bytes(&self) -> usize {
1553 self.cap_bytes
1554 }
1555
1556 pub fn reserved_bytes(&self) -> usize {
1559 self.reserved
1560 }
1561
1562 pub fn set_reserved(&mut self, bytes: usize) {
1565 self.reserved = bytes;
1566 self.evict_over_cap();
1567 }
1568
1569 pub fn set_capacity(&mut self, cap_bytes: usize) {
1572 self.cap_bytes = cap_bytes;
1573 self.evict_over_cap();
1574 }
1575
1576 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 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 pub fn len(&self) -> usize {
1588 self.map.values().map(HashMap::len).sum()
1589 }
1590
1591 pub fn is_empty(&self) -> bool {
1593 self.len() == 0
1594 }
1595
1596 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 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 assert_eq!(AutoDisk::default().bytes_for(None, 0), AUTO_DISK_UNKNOWN);
1664 assert_eq!(AutoDisk::default().bytes_for(Some((500 * GIB, 1000 * GIB)), 0), AutoDisk::CEILING);
1666 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 assert_eq!(AutoDisk::default().bytes_for(Some((100 * GIB, 500 * GIB)), 0), 100 * GIB / 100 * 20);
1673 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 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 assert_eq!(AutoDisk::default().bytes_for(Some((GIB / 2, 100 * GIB)), 6 * GIB), (6 * GIB + GIB / 2) / 100 * 20);
1686 assert_eq!(AutoDisk::default().bytes_for(Some((GIB / 2, 100 * GIB)), 5 * GIB + GIB / 2), GIB);
1689 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 assert_eq!(b.space.is_some(), cfg!(unix));
1705 let auto = DiskCache::open(dir.path()).with_capacity_mode(DiskCapacity::default());
1706 let ab = auto.budget();
1709 assert_eq!(ab.cap_bytes, AutoDisk::default().bytes_for(ab.space, ab.used));
1710 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 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")); assert!(is_cache_dir_name("my.app_v2-0123456789abcdef"));
1739 assert!(!is_cache_dir_name("Documents"));
1740 assert!(!is_cache_dir_name("app-0123456789ABCDEF")); assert!(!is_cache_dir_name("app-0123456789abcde")); assert!(!is_cache_dir_name("app 1-0123456789abcdef")); 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 cache.put(&key, "classes.dex", &[0; 8]).unwrap();
1753 assert!(dir.path().join(CACHEDIR_TAG).is_file());
1754 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 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 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 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 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 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 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 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 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 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 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()); m.insert(&k, "c", Arc::new(vec![0; 4]), 4); 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 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 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 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 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); 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()); 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); 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 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")); }
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 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 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 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 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 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 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 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 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 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 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 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 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 let cache = cache.with_capacity(50);
2176 cache.evict_lazily();
2177 assert_eq!(cache.stats().1, 1);
2178 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 let cache = DiskCache::open(dir.path()).with_capacity(1000);
2200 let k = |i: u32| ApkKey::from_id(format!("app-{i:016x}"));
2201 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 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 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 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 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 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 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}