Disk cache: file stamps keep the whole modification time

A file's modification time was nanoseconds since 1970 as u64: a time before 1970
read as "no time" (two such files of one size looked alike) and times after 2554
were truncated. It is now i128 nanoseconds, negative before the epoch; None only
means the file system has no time. The content-key hash writes a presence tag and
the full value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vangel76
2026-11-07 01:58:15 +01:00
co-authored by Claude Opus 5.5
parent 2de2406bcc
commit d2372b7e2d
2 changed files with 52 additions and 6 deletions
+6 -2
View File
@@ -1064,8 +1064,12 @@ fn file_stamp(h: &mut Hash128, path: &str, virtual_id: Option<VirtualFileId<'_>>
}
if let Some(crate::reads::FileStamp::Native { len, modified }) = crate::reads::FileStamp::native(path) {
h.write_u64(len);
if let Some(t) = modified {
h.write_u64(t);
match modified {
Some(t) => {
h.write(&[1]);
h.write(&t.to_le_bytes());
}
None => h.write(&[0]),
}
return;
}
+46 -4
View File
@@ -17,8 +17,8 @@ use std::sync::{Arc, Mutex, PoisonError};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum FileStamp {
/// A file on disk: its size in bytes and its modification time (nanoseconds since the Unix
/// epoch; `None` where the file system has none).
Native { len: u64, modified: Option<u64> },
/// epoch, negative before it; `None` only where the file system has none).
Native { len: u64, modified: Option<i128> },
/// Bytes given from memory instead of the file system (the browser's file table): their
/// identity is the size and content hash a
/// [`VirtualFileId`](crate::disk_cache::VirtualFileId) gives for the path.
@@ -31,8 +31,7 @@ pub enum FileStamp {
impl FileStamp {
/// The identity of a file from metadata the caller already has (of an open file).
pub fn of_metadata(md: &std::fs::Metadata) -> FileStamp {
let modified = md.modified().ok().and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok()).map(|d| d.as_nanos() as u64);
FileStamp::Native { len: md.len(), modified }
FileStamp::Native { len: md.len(), modified: md.modified().ok().map(epoch_nanos) }
}
/// The identity of the file at `path` now: one `metadata` call. `None` when the file system
@@ -42,6 +41,16 @@ impl FileStamp {
}
}
/// `t` in nanoseconds since the Unix epoch, negative before it: every time a file system can
/// store fits (`i128` nanoseconds span far more than any `SystemTime`), saturating otherwise.
pub fn epoch_nanos(t: std::time::SystemTime) -> i128 {
let nanos = |d: std::time::Duration| i128::try_from(d.as_nanos()).unwrap_or(i128::MAX);
match t.duration_since(std::time::UNIX_EPOCH) {
Ok(after) => nanos(after),
Err(before) => nanos(before.duration()).saturating_neg(),
}
}
/// One file a render read.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ReadFile {
@@ -209,6 +218,39 @@ mod tests {
assert!(!files.is_complete());
}
/// Modification times keep their full value: before 1970 (not "no time") and past 2554
/// (where `u64` nanoseconds end), so two such files never look alike.
#[test]
fn modification_times_are_kept_whole() {
use std::time::{Duration, UNIX_EPOCH};
let day = Duration::from_secs(86_400);
assert_eq!(epoch_nanos(UNIX_EPOCH), 0);
assert_eq!(epoch_nanos(UNIX_EPOCH - day), -86_400_000_000_000);
let far = 600 * 365 * day;
if let Some(t) = UNIX_EPOCH.checked_add(far) {
assert_eq!(epoch_nanos(t), i128::try_from(far.as_nanos()).unwrap());
}
let dir = std::env::temp_dir().join(format!("effectcraft-stamp-1969-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let (a, b) = (dir.join("a.bin"), dir.join("b.bin"));
let mut stamps = vec![];
for (p, back) in [(&a, day), (&b, 2 * day)] {
std::fs::write(p, b"abc").unwrap();
let f = std::fs::File::options().write(true).open(p).unwrap();
if f.set_modified(UNIX_EPOCH - back).is_err() {
// (a file system that can't store times before 1970)
let _ = std::fs::remove_dir_all(&dir);
return;
}
drop(f);
stamps.push(FileStamp::native(&p.to_string_lossy()).unwrap());
}
assert!(matches!(stamps[0], FileStamp::Native { modified: Some(t), .. } if t < 0), "{:?}", stamps[0]);
assert_ne!(stamps[0], stamps[1], "two files from before 1970 with the same size");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn the_list_is_bounded() {
let files: ReadFiles = (0..MAX_READ_FILES + 3).map(|i| file(&format!("/f{i:06}"), 1)).collect();