use std::collections::BTreeMap;
use std::io::Read as _;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::time::SystemTime;
use serde_json::{json, Value as Json};
pub const SOURCES_META: &str = "memfork.sources";
pub const MAX_SOURCES: usize = 32;
const MAX_PATH_BYTES: usize = 1024;
pub const MAX_HASHED_BYTES: u64 = 8 * 1024 * 1024;
pub const MAX_BYTES_PER_CHECK: u64 = 64 * 1024 * 1024;
pub fn normalise(paths: &[String]) -> Result<Vec<String>, String> {
if paths.len() > MAX_SOURCES {
return Err(format!(
"a fact may name at most {MAX_SOURCES} source files; this names {}",
paths.len()
));
}
let mut out: Vec<String> = Vec::new();
for raw in paths {
let unified = raw.trim().replace('\\', "/");
if unified.is_empty() || unified.len() > MAX_PATH_BYTES {
return Err(format!("`{raw}` is not a usable source path"));
}
let absolute = unified.starts_with('/')
|| unified.as_bytes().get(1) == Some(&b':')
|| unified.starts_with("//");
let mut parts = Vec::new();
for part in unified.split('/') {
match part {
"" | "." => {}
".." => {
return Err(format!(
"`{raw}` climbs out of the project with `..`; name files inside it"
))
}
other => parts.push(other),
}
}
if absolute || parts.is_empty() {
return Err(format!(
"`{raw}` must be a path relative to the project, such as `src/auth/login.rs`"
));
}
let path = parts.join("/");
if !out.contains(&path) {
out.push(path);
}
}
Ok(out)
}
pub fn sources_of(meta: &BTreeMap<String, String>) -> Option<Vec<String>> {
meta.get(SOURCES_META)
.and_then(|raw| serde_json::from_str::<Vec<String>>(raw).ok())
}
pub fn sources_meta(paths: &[String]) -> String {
Json::from(paths.to_vec()).to_string()
}
pub fn record_id(key: &str, value: &[u8], sources: &[String]) -> String {
let mut hasher = blake3::Hasher::new();
hasher.update(&(key.len() as u64).to_le_bytes());
hasher.update(key.as_bytes());
hasher.update(&(value.len() as u64).to_le_bytes());
hasher.update(value);
for s in sources {
hasher.update(&(s.len() as u64).to_le_bytes());
hasher.update(s.as_bytes());
}
hasher.finalize().to_hex().to_string()
}
pub type Hashes = BTreeMap<String, Option<String>>;
#[derive(Debug)]
struct Seen {
size: u64,
modified: Option<SystemTime>,
hash: String,
}
#[derive(Debug, Default)]
pub struct Hasher {
cache: Mutex<BTreeMap<PathBuf, Seen>>,
}
impl Hasher {
pub fn hash(&self, root: &Path, relative: &str, spent: &mut u64) -> Option<String> {
let path = relative
.split('/')
.fold(root.to_path_buf(), |p, part| p.join(part));
let meta = std::fs::metadata(&path).ok().filter(|m| m.is_file())?;
let size = meta.len();
let modified = meta.modified().ok();
if let Some(seen) = self
.cache
.lock()
.unwrap_or_else(|e| e.into_inner())
.get(&path)
{
if seen.size == size && seen.modified == modified && modified.is_some() {
return Some(seen.hash.clone());
}
}
let mut file = std::fs::File::open(&path).ok()?;
let mut hasher = blake3::Hasher::new();
let mut buffer = vec![0u8; 64 * 1024];
let mut read_total = 0u64;
while read_total < MAX_HASHED_BYTES {
let want = buffer
.len()
.min(usize::try_from(MAX_HASHED_BYTES - read_total).unwrap_or(usize::MAX));
let n = file.read(&mut buffer[..want]).ok()?;
if n == 0 {
break;
}
hasher.update(&buffer[..n]);
read_total += n as u64;
}
hasher.update(&size.to_le_bytes());
*spent += read_total;
let hash = hasher.finalize().to_hex().to_string();
self.cache.lock().unwrap_or_else(|e| e.into_inner()).insert(
path,
Seen {
size,
modified,
hash: hash.clone(),
},
);
Some(hash)
}
pub fn record(&self, root: &Path, sources: &[String]) -> Hashes {
let mut spent = 0u64;
sources
.iter()
.map(|s| (s.clone(), self.hash(root, s, &mut spent)))
.collect()
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Checked {
pub fresh: u64,
pub stale: u64,
pub unverified: u64,
pub facts: Vec<(String, String)>,
}
impl Checked {
pub fn is_empty(&self) -> bool {
self.facts.is_empty()
}
}
pub fn check(answer: &mut Json, root: &Path, hasher: &Hasher) -> Checked {
let mut checked = Checked::default();
let mut spent = 0u64;
walk(answer, root, hasher, &mut spent, &mut checked);
checked
}
fn walk(value: &mut Json, root: &Path, hasher: &Hasher, spent: &mut u64, out: &mut Checked) {
match value {
Json::Object(map) => {
if map.contains_key("recorded") && map.get("sources").is_some_and(Json::is_array) {
let recorded = map.remove("recorded").unwrap_or(Json::Null);
let key = map
.get("key")
.and_then(Json::as_str)
.unwrap_or("")
.to_owned();
let (state, stale) = judge(&recorded, root, hasher, spent);
map.insert("fact".to_owned(), json!(state));
if !stale.is_empty() {
map.insert("stale_sources".to_owned(), json!(stale));
}
match state {
"fresh" => out.fresh += 1,
"stale" => out.stale += 1,
_ => out.unverified += 1,
}
out.facts.push((key, state.to_owned()));
}
for child in map.values_mut() {
walk(child, root, hasher, spent, out);
}
}
Json::Array(items) => {
for item in items {
walk(item, root, hasher, spent, out);
}
}
_ => {}
}
if !out.facts.is_empty() {
crate::tools::handoff::restate_size(value);
}
}
fn judge(
recorded: &Json,
root: &Path,
hasher: &Hasher,
spent: &mut u64,
) -> (&'static str, Vec<String>) {
let Some(recorded) = recorded.as_object() else {
return ("unverified", Vec::new());
};
let mut stale = Vec::new();
for (path, then) in recorded {
if *spent > MAX_BYTES_PER_CHECK {
return ("unverified", Vec::new());
}
let now = hasher.hash(root, path, spent);
if now.is_none() || now.as_deref() != then.as_str() {
stale.push(path.clone());
}
}
if stale.is_empty() {
("fresh", stale)
} else {
("stale", stale)
}
}
pub fn project_root(cwd: &Path) -> PathBuf {
crate::namespace::repository_root(cwd).unwrap_or_else(|| cwd.to_path_buf())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn source_paths_are_made_canonical_and_kept_inside_the_project() {
let ok = normalise(&[
"src\\auth\\login.rs".to_owned(),
"./src/auth/".to_owned(),
"src/auth/login.rs".to_owned(),
])
.unwrap();
assert_eq!(ok, ["src/auth/login.rs", "src/auth"]);
for bad in [
"/etc/passwd",
"C:/Windows",
"../other/x",
"src/../../x",
"",
".",
] {
assert!(normalise(&[bad.to_owned()]).is_err(), "{bad} was accepted");
}
let many: Vec<String> = (0..=MAX_SOURCES).map(|i| format!("f{i}")).collect();
assert!(normalise(&many).is_err());
}
#[test]
fn a_fact_is_fresh_then_stale_when_its_file_changes_and_stale_when_it_goes() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join("src")).unwrap();
std::fs::write(tmp.path().join("src/a.rs"), "fn a() {}").unwrap();
let hasher = Hasher::default();
let recorded = hasher.record(tmp.path(), &["src/a.rs".to_owned()]);
let answer = |recorded: &Hashes| json!({"key": "p:fact:a", "sources": ["src/a.rs"], "recorded": recorded});
let mut fresh = answer(&recorded);
let checked = check(&mut fresh, tmp.path(), &hasher);
assert_eq!(fresh["fact"], "fresh");
assert!(fresh.get("recorded").is_none());
assert_eq!(checked.fresh, 1);
std::fs::write(tmp.path().join("src/a.rs"), "fn a() { changed() }").unwrap();
let mut stale = answer(&recorded);
check(&mut stale, tmp.path(), &hasher);
assert_eq!(stale["fact"], "stale");
assert_eq!(stale["stale_sources"], json!(["src/a.rs"]));
std::fs::remove_file(tmp.path().join("src/a.rs")).unwrap();
let mut gone = answer(&recorded);
check(&mut gone, tmp.path(), &hasher);
assert_eq!(gone["fact"], "stale");
}
#[test]
fn nothing_recorded_is_unverified_and_nested_facts_are_found() {
let tmp = tempfile::tempdir().unwrap();
let hasher = Hasher::default();
let mut answer = json!({"results": [
{"key": "a", "sources": ["x"], "recorded": null},
{"key": "b", "value": "not a fact"},
]});
let checked = check(&mut answer, tmp.path(), &hasher);
assert_eq!(answer["results"][0]["fact"], "unverified");
assert!(answer["results"][1].get("fact").is_none());
assert_eq!(
checked.facts,
vec![("a".to_owned(), "unverified".to_owned())]
);
}
#[test]
fn a_record_id_depends_on_key_words_and_sources_only() {
let a = record_id("k", b"v", &["x".to_owned()]);
assert_eq!(a, record_id("k", b"v", &["x".to_owned()]));
assert_ne!(a, record_id("k", b"w", &["x".to_owned()]));
assert_ne!(a, record_id("k", b"v", &["y".to_owned()]));
assert_ne!(record_id("ab", b"c", &[]), record_id("a", b"bc", &[]));
}
}