Xref baseline: keep the file's modification time as the system gives it, so a time before 1970 counts

This commit is contained in:
Tanish Manku
2026-11-23 13:30:21 -07:00
parent 506ad531eb
commit 39fca9e2a7
4 changed files with 30 additions and 10 deletions
+2 -2
View File
@@ -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<String>,
/// 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)>,
+3 -3
View File
@@ -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<crate::ArrayParams>,
/// 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.
+6 -5
View File
@@ -344,14 +344,15 @@ fn read(file: &Path, cx: &mut Ctx) -> Result<(Drawing, Option<Stamp>, Vec<u8>),
}
}
/// 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<Stamp> {
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.
+19
View File
@@ -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");
}