use std::fs; use std::path::Path; use crate::encoding::{Reader, Writer}; use crate::error::Result; use crate::object::id::Id; use crate::store::layout; #[derive(Clone)] pub struct CacheEntry { pub path: Vec, pub size: u64, pub mtime_ns: u64, pub inode: u64, pub kind: u8, pub executable: bool, pub target: Id, pub trusted: bool, } pub struct Cache { pub entries: Vec, pub written_ms: i64, } pub fn load(root: &Path) -> Cache { let mut cache = Cache { entries: Vec::new(), written_ms: 0, }; let Ok(data) = fs::read(layout::working_copy_dir(root).join("stat-cache")) else { return cache; }; if data.len() < 13 || &data[..4] != b"VST1" { return cache; } let mut reader = Reader::new(&data[4..]); let Ok(version) = reader.u8() else { return cache; }; if version != 49 && version != 50 { return cache; } let Ok(written) = reader.u64le() else { return cache; }; cache.written_ms = written as i64; let Ok(count) = reader.varint() else { return cache; }; for _ in 0..count { let Ok(path) = reader.bytes(65_536) else { break; }; let path = path.to_vec(); let (Ok(size), Ok(mtime), Ok(inode), Ok(kind), Ok(executable)) = ( reader.u64le(), reader.u64le(), reader.u64le(), reader.u8(), reader.u8(), ) else { break; }; let trusted = if version >= 50 { match reader.u8() { Ok(value) => value != 0, Err(_) => break, } } else { false }; let Ok(target) = reader.id() else { break; }; cache.entries.push(CacheEntry { path, size, mtime_ns: mtime, inode, kind, executable: executable != 0, target, trusted, }); } cache .entries .sort_by(|left, right| left.path.cmp(&right.path)); cache } pub fn save(root: &Path, entries: &mut [CacheEntry], written_ms: i64) -> Result<()> { entries.sort_by(|left, right| left.path.cmp(&right.path)); let mut writer = Writer::with_capacity(4096); writer.raw(b"VST1"); writer.u8(50); writer.u64le(written_ms as u64); writer.varint(entries.len() as u64); for entry in entries { writer.bytes(&entry.path); writer.u64le(entry.size); writer.u64le(entry.mtime_ns); writer.u64le(entry.inode); writer.u8(entry.kind); writer.u8(u8::from(entry.executable)); writer.u8(u8::from(entry.trusted)); writer.id(entry.target); } let path = layout::working_copy_dir(root).join("stat-cache"); // The stat cache is rebuildable; matching C++, write it without fsync so a // cache refresh does not pay two sync barriers. crate::core::write_file(&path, &writer.into_vec()); Ok(()) } pub fn lookup<'a>(cache: &'a Cache, path: &[u8]) -> Option<&'a CacheEntry> { cache .entries .binary_search_by(|entry| entry.path.as_slice().cmp(path)) .ok() .map(|index| &cache.entries[index]) } pub fn matches( entry: &CacheEntry, info: &crate::core::StatInfo, executable: bool, now_ms: i64, written_ms: i64, ) -> bool { if entry.size != info.size || entry.mtime_ns != info.mtime_ns || entry.inode != info.inode || entry.executable != executable || entry.kind != info.kind { return false; } if entry.trusted { return true; } let now_ns = now_ms.saturating_mul(1_000_000); let written_ns = written_ms.saturating_mul(1_000_000); const WINDOW: i64 = 2_000_000_000; if info.mtime_ns as i64 >= written_ns - WINDOW { return false; } if now_ns - (info.mtime_ns as i64) < WINDOW { return false; } true }