From 5ff83fad08d9b6452351b10098c3337c30570003 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Mon, 4 May 2020 21:28:55 +1000 Subject: [PATCH] catch attempts to overwrite notetype with older version --- rslib/src/notetype/mod.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/rslib/src/notetype/mod.rs b/rslib/src/notetype/mod.rs index db1aaff35..83a4bb905 100644 --- a/rslib/src/notetype/mod.rs +++ b/rslib/src/notetype/mod.rs @@ -322,12 +322,11 @@ impl Collection { pub fn update_notetype(&mut self, nt: &mut NoteType, preserve_usn: bool) -> Result<()> { let existing = self.get_notetype(nt.id)?; nt.prepare_for_update(existing.as_ref().map(AsRef::as_ref))?; - if !preserve_usn { - nt.mtime_secs = TimestampSecs::now(); - nt.usn = self.usn()?; - } self.transact(None, |col| { if let Some(existing_notetype) = existing { + if existing_notetype.mtime_secs > nt.mtime_secs { + return Err(AnkiError::invalid_input("attempt to save stale notetype")); + } col.update_notes_for_changed_fields( nt, existing_notetype.fields.len(), @@ -336,6 +335,11 @@ impl Collection { col.update_cards_for_changed_templates(nt, existing_notetype.templates.len())?; } + if !preserve_usn { + nt.mtime_secs = TimestampSecs::now(); + nt.usn = col.usn()?; + } + col.storage.update_notetype_config(&nt)?; col.storage.update_notetype_fields(nt.id, &nt.fields)?; col.storage