use crate::format::{fnv1a_64, CURRENT_FORMAT_VERSION, EXTRACTOR_VERSION};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::RwLock;
use std::time::SystemTime;
type DriverHashCache = HashMap<PathBuf, (u64, Option<SystemTime>, u64)>;
static DRIVER_HASH_CACHE: RwLock<Option<DriverHashCache>> = RwLock::new(None);
fn disk_cache_path(explicit_cache_dir: Option<&Path>) -> PathBuf {
if let Some(cd) = explicit_cache_dir {
cd.join("driver_hash.cache")
} else if let Ok(cd) = std::env::var("RIDX_CACHE_DIR") {
PathBuf::from(cd).join("driver_hash.cache")
} else if let Ok(home) = std::env::var("HOME") {
PathBuf::from(home).join(".cache").join("ridx").join("v3").join("driver_hash.cache")
} else {
std::env::temp_dir().join("ridx").join("v3").join("driver_hash.cache")
}
}
fn file_sig(meta: &fs::Metadata) -> (u64, u64, u64) {
let len = meta.len();
let mtime_secs = meta.modified().ok().and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok()).map(|d| d.as_secs()).unwrap_or(0);
#[cfg(unix)]
let ino = std::os::unix::fs::MetadataExt::ino(meta);
#[cfg(not(unix))]
let ino = 0u64;
(len, mtime_secs, ino)
}
fn lookup_disk_cache(explicit_cache_dir: Option<&Path>, p_str: &str, len: u64, mtime_secs: u64, ino: u64) -> Option<u64> {
let cp = disk_cache_path(explicit_cache_dir);
let content = fs::read_to_string(&cp).ok()?;
for line in content.lines() {
let parts: Vec<&str> = line.split('\t').collect();
if parts.len() == 5
&& parts[0] == p_str
&& parts[1].parse::<u64>().ok() == Some(len)
&& parts[2].parse::<u64>().ok() == Some(mtime_secs)
&& parts[3].parse::<u64>().ok() == Some(ino)
&& parts[4].len() == 16
{
if let Ok(hash) = u64::from_str_radix(parts[4], 16) {
return Some(hash);
}
}
}
None
}
fn append_disk_cache(explicit_cache_dir: Option<&Path>, p_str: &str, len: u64, mtime_secs: u64, ino: u64, hash: u64) {
let cp = disk_cache_path(explicit_cache_dir);
if let Some(parent) = cp.parent() {
let _ = fs::create_dir_all(parent);
}
if let Ok(content) = fs::read_to_string(&cp) {
let lines: Vec<&str> = content.lines().collect();
if lines.len() >= 64 {
let mut latest_by_path: HashMap<&str, &str> = HashMap::new();
for l in &lines {
if let Some((p, _)) = l.split_once('\t') {
latest_by_path.insert(p, l);
}
}
let tmp_path = cp.with_extension(format!("tmp.{}", std::process::id()));
if let Ok(mut f) = fs::File::create(&tmp_path) {
use std::io::Write;
for entry_line in latest_by_path.values() {
let _ = writeln!(f, "{}", entry_line);
}
let _ = f.flush();
let _ = fs::rename(&tmp_path, &cp);
}
}
}
if let Ok(mut f) = fs::OpenOptions::new().create(true).append(true).open(&cp) {
use std::io::Write;
let _ = writeln!(f, "{}\t{}\t{}\t{}\t{:016x}", p_str, len, mtime_secs, ino, hash);
}
}
fn hash_file_cached_with(p: &Path, explicit_cache_dir: Option<&Path>) -> Option<u64> {
let meta = fs::metadata(p).ok()?;
if !meta.is_file() {
return None;
}
let len = meta.len();
let mtime = meta.modified().ok();
if let Ok(guard) = DRIVER_HASH_CACHE.read() {
if let Some(map) = guard.as_ref() {
if let Some(&(cached_len, cached_mtime, cached_hash)) = map.get(p) {
if cached_len == len && cached_mtime == mtime {
return Some(cached_hash);
}
}
}
}
let (file_len, mtime_secs, ino) = file_sig(&meta);
let p_str = p.to_string_lossy();
if let Some(disk_hash) = lookup_disk_cache(explicit_cache_dir, &p_str, file_len, mtime_secs, ino) {
if let Ok(mut guard) = DRIVER_HASH_CACHE.write() {
let map = guard.get_or_insert_with(HashMap::new);
map.insert(p.to_path_buf(), (len, mtime, disk_hash));
}
return Some(disk_hash);
}
let bytes = fs::read(p).ok()?;
let hash = fnv1a_64(&bytes);
append_disk_cache(explicit_cache_dir, &p_str, file_len, mtime_secs, ino, hash);
if let Ok(mut guard) = DRIVER_HASH_CACHE.write() {
let map = guard.get_or_insert_with(HashMap::new);
map.insert(p.to_path_buf(), (len, mtime, hash));
}
Some(hash)
}
#[doc(hidden)]
pub fn clear_driver_hash_cache() {
clear_driver_hash_cache_in(None);
}
#[doc(hidden)]
pub fn clear_driver_hash_cache_in(cache_dir: Option<&Path>) {
if let Ok(mut guard) = DRIVER_HASH_CACHE.write() {
*guard = None;
}
if let Some(cd) = cache_dir {
let _ = fs::remove_file(cd.join("driver_hash.cache"));
} else if std::env::var("RIDX_CACHE_DIR").is_ok() {
let _ = fs::remove_file(disk_cache_path(None));
}
}
pub fn get_driver_hash() -> u64 {
get_driver_hash_with(None, None)
}
pub fn get_driver_hash_with(explicit_driver_bin: Option<&Path>, explicit_cache_dir: Option<&Path>) -> u64 {
if let Ok(override_val) = std::env::var("RIDX_DRIVER_HASH") {
let trimmed = override_val.trim();
if let Ok(val) = u64::from_str_radix(trimmed.trim_start_matches("0x"), 16) {
return val;
}
return fnv1a_64(trimmed.as_bytes());
}
if let Some(p) = explicit_driver_bin {
if let Some(hash) = hash_file_cached_with(p, explicit_cache_dir) {
return hash;
}
} else if let Ok(driver_bin) = std::env::var("RIDX_DRIVER_BIN") {
let p = Path::new(driver_bin.trim());
if let Some(hash) = hash_file_cached_with(p, explicit_cache_dir) {
return hash;
}
}
if let Ok(exe_path) = std::env::current_exe() {
if let Some(hash) = hash_file_cached_with(&exe_path, explicit_cache_dir) {
return hash;
}
}
let fallback = format!("{}:{}", CURRENT_FORMAT_VERSION, EXTRACTOR_VERSION);
fnv1a_64(fallback.as_bytes())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_lookup_disk_cache_rejects_truncated_hex() {
let temp_dir = tempfile::tempdir().unwrap();
let cache_file = temp_dir.path().join("driver_hash.cache");
fs::write(&cache_file, "/test/bin\t100\t200\t300\t123456789abc\n").unwrap();
let res = lookup_disk_cache(Some(temp_dir.path()), "/test/bin", 100, 200, 300);
assert_eq!(res, None, "Truncated hash must be rejected");
fs::write(&cache_file, "/test/bin\t100\t200\t300\t0123456789abcdef\n").unwrap();
let res = lookup_disk_cache(Some(temp_dir.path()), "/test/bin", 100, 200, 300);
assert_eq!(res, Some(0x0123456789abcdef));
}
#[test]
fn test_append_disk_cache_compaction_preserves_latest() {
let temp_dir = tempfile::tempdir().unwrap();
for i in 0..70 {
append_disk_cache(Some(temp_dir.path()), "/test/bin", 100, 200 + i, 300, 0x0123456789abcdef + i);
}
let res = lookup_disk_cache(Some(temp_dir.path()), "/test/bin", 100, 269, 300);
assert_eq!(res, Some(0x0123456789abcdef + 69));
}
}