DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Links: don't trust a linked text file's time while it is still changing
Jeffrey Thayne0541f367951d
diff --git a/crates/engine/src/cmd/links.rs b/crates/engine/src/cmd/links.rsindex 1195d1f3f..a28d49e73 100644--- a/crates/engine/src/cmd/links.rs+++ b/crates/engine/src/cmd/links.rs@@ -155,6 +155,21 @@ pub(crate) fn checkable_size(len: usize) -> bool {len as u64 <= MAX_TEXT_LINK_BYTES}+/// Can a size and time be trusted to change when the file does? Times are whole seconds, so not+/// while the file was changed in the last two: another change in that second keeps the time.+pub(crate) fn settled(f: &Fingerprint) -> bool {+ #[cfg(not(target_arch = "wasm32"))]+ {+ let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| i64::try_from(d.as_secs()).unwrap_or(i64::MAX));+ now.saturating_sub(f.mtime) >= 2+ }+ #[cfg(target_arch = "wasm32")]+ {+ let _ = f;+ false+ }+}+/// Checked linked text files: path → (size and time, hash of the contents then).pub(crate) type TextChecks = std::collections::HashMap<String, (Fingerprint, String)>;@@ -183,7 +198,9 @@ pub(crate) fn text_status(l: &TextFileLink, checks: &mut TextChecks) -> &'staticOk((b, at)) => {let hash = fnv_hex(&b);// The time from before the read: a change during it is seen next time.- checks.insert(l.path.clone(), (at, hash.clone()));+ if settled(&at) {+ checks.insert(l.path.clone(), (at, hash.clone()));+ }hash}Err(_) => return "missing",@@ -738,10 +755,12 @@ mod text_link_tests {std::fs::write(&path, "Hello").unwrap();// Unreadable: a status that reads the file would say missing.let readable = |on: bool| std::fs::set_permissions(&path, std::fs::Permissions::from_mode(if on { 0o644 } else { 0o000 })).unwrap();+ // A time `secs` ago (a file changed in the last two seconds is always read).let touch = |secs: u64| {let f = std::fs::File::options().write(true).open(&path).unwrap();- f.set_modified(std::time::SystemTime::now() + std::time::Duration::from_secs(secs)).unwrap();+ f.set_modified(std::time::SystemTime::now() - std::time::Duration::from_secs(secs)).unwrap();};+ touch(60);let mut s = Session::new();s.execute("file.new", &json!({})).unwrap();s.execute("file.place", &json!({"path": path.to_string_lossy(), "link": true})).unwrap();@@ -756,7 +775,7 @@ mod text_link_tests {assert_eq!(status(&mut s), "ok");// Saved again unchanged (a new time): read once, then not again.readable(true);- touch(10);+ touch(30);assert_eq!(status(&mut s), "ok");readable(false);assert_eq!(status(&mut s), "ok", "the second poll doesn't read it");@@ -765,6 +784,12 @@ mod text_link_tests {std::fs::write(&path, "Jello").unwrap();touch(20);assert_eq!(status(&mut s), "modified");+ // Changed again in the same second, same size: still seen (just-changed files are read).+ std::fs::write(&path, "Hello").unwrap();+ assert_eq!(status(&mut s), "ok");+ std::fs::write(&path, "Jello").unwrap();+ assert_eq!(status(&mut s), "modified");+ touch(10);// Updated: the new size and time are kept, so the next poll doesn't read it.s.execute("links.update", &json!({})).unwrap();readable(false);diff --git a/crates/engine/src/cmd/place_text.rs b/crates/engine/src/cmd/place_text.rsindex 1acff61c4..c6d682248 100644--- a/crates/engine/src/cmd/place_text.rs+++ b/crates/engine/src/cmd/place_text.rs@@ -143,7 +143,7 @@ fn fill(story: &mut Story, from: &Story) {/// The link a story placed from `path` keeps (Create Links When Placing Text Files)./// `before`: the file's size and time before `bytes` were read (its time is kept only when the-/// size matches what was read).+/// size matches what was read and it can be trusted, see [`super::links::settled`]).fn text_link(path: &str,bytes: &[u8],@@ -157,7 +157,7 @@ fn text_link(path: path.to_string(),size: bytes.len() as u64,hash: super::links::fnv_hex(bytes),- mtime: before.filter(|f| f.size == bytes.len() as u64).map_or(0, |f| f.mtime),+ mtime: before.filter(|f| f.size == bytes.len() as u64 && super::links::settled(f)).map_or(0, |f| f.mtime),style_map: map.into_iter().collect(),style_conflicts: conflict.name().into(),remove_styles: remove,