diff --git a/crates/doc/src/refedit.rs b/crates/doc/src/refedit.rs index fcf1b60e7..8130f08e2 100644 --- a/crates/doc/src/refedit.rs +++ b/crates/doc/src/refedit.rs @@ -67,8 +67,8 @@ pub struct XrefSource { pub binary: bool, /// The block of the source drawing being edited; `None` for its model space. pub block: Option, - /// The file's size and modification time (nanoseconds since 1970) when the edit began. - pub stamp: Option<(u64, u128)>, + /// The file's size and modification time when the edit began. + pub stamp: Option<(u64, std::time::SystemTime)>, /// Edited block objects (their handles in this drawing) and the objects of the source /// drawing they were loaded from. pub sources: Vec<(Handle, Handle)>, diff --git a/crates/doc/src/tables.rs b/crates/doc/src/tables.rs index d9ea001d8..696d6a093 100644 --- a/crates/doc/src/tables.rs +++ b/crates/doc/src/tables.rs @@ -728,11 +728,11 @@ pub struct Block { /// The parameters of an associative array when this is an array block. #[serde(default, skip_serializing_if = "Option::is_none")] pub array: Option, - /// An xref's file as its loaded objects were read from it: size and modification time - /// (nanoseconds since 1970). Kept per drawing, so an edit in place (REFEDIT) can tell whether + /// An xref's file as its loaded objects were read from it: size and modification time (as the + /// system gives it, so a time before 1970 is kept too). Kept per drawing, so an edit in place (REFEDIT) can tell whether /// the file changed since this drawing loaded it (session state; not saved). #[serde(skip)] - pub xref_stamp: Option<(u64, u128)>, + pub xref_stamp: Option<(u64, std::time::SystemTime)>, } /// The load state of an external reference. diff --git a/crates/engine/src/cmd/blocks/xref/load.rs b/crates/engine/src/cmd/blocks/xref/load.rs index e58f56732..9d1def4f8 100644 --- a/crates/engine/src/cmd/blocks/xref/load.rs +++ b/crates/engine/src/cmd/blocks/xref/load.rs @@ -344,14 +344,15 @@ fn read(file: &Path, cx: &mut Ctx) -> Result<(Drawing, Option, Vec), } } -/// A file's size and modification time (nanoseconds since 1970): how an edit in place (REFEDIT) -/// tells whether an xref's file changed since it was read ([`cadcraft_doc::Block::xref_stamp`]). -pub(crate) type Stamp = (u64, u128); +/// A file's size and modification time: how an edit in place (REFEDIT) tells whether an xref's +/// file changed since it was read ([`cadcraft_doc::Block::xref_stamp`]). The time is kept as the +/// system gives it: counted from 1970, a file dated before then would have none and always look +/// changed. +pub(crate) type Stamp = (u64, std::time::SystemTime); #[cfg_attr(target_arch = "wasm32", allow(dead_code))] fn stamp_of(m: &std::fs::Metadata) -> Option { - let t = m.modified().ok()?.duration_since(std::time::UNIX_EPOCH).ok()?.as_nanos(); - Some((m.len(), t)) + Some((m.len(), m.modified().ok()?)) } /// The [`Stamp`] of the file at `file` now. diff --git a/crates/engine/tests/it/refedit_xref.rs b/crates/engine/tests/it/refedit_xref.rs index 99d60feb9..dde9dd24a 100644 --- a/crates/engine/tests/it/refedit_xref.rs +++ b/crates/engine/tests/it/refedit_xref.rs @@ -984,3 +984,22 @@ fn a_reference_with_several_columns_or_rows_is_not_edited_in_place() { let e = s.execute("refedit", &json!({ "handle": o, "path": [inner.hex()] })).unwrap_err().to_string(); assert!(e.contains("columns or rows"), "{e}"); } + +/// Review of #3352: a file dated before 1970 had no stamp (its time was counted from 1970), so it +/// always looked changed and REFEDIT refused it even after a reload. The time is kept as it is. +#[test] +fn an_xref_file_dated_before_1970_can_be_edited_in_place() { + let (mut s, dir, _, insert) = attached("pre-1970"); + let file = dir.join("site.dxf"); + let old = std::time::UNIX_EPOCH - std::time::Duration::from_secs(10 * 365 * 86_400); + let set = std::fs::OpenOptions::new().write(true).open(&file).and_then(|f| f.set_modified(old)); + if set.is_err() || std::fs::metadata(&file).and_then(|m| m.modified()).ok() != Some(old) { + eprintln!("skipped: this file system doesn't keep a time before 1970 ({set:?})"); + return; + } + s.execute("-xref", &json!({ "option": "reload", "names": "site" })).unwrap(); + s.execute("refedit", &json!({ "handle": insert })).unwrap(); + s.execute("line", &json!({ "points": [[100, 20], [110, 20]] })).unwrap(); + s.execute("refclose", &json!({ "option": "save" })).unwrap(); + assert_eq!(read(&file).model.len(), 3, "the edit is written"); +}