diff --git a/examples/org.weft.demo.notes/app.wasm b/examples/org.weft.demo.notes/app.wasm index 19879b8..35818e7 100644 Binary files a/examples/org.weft.demo.notes/app.wasm and b/examples/org.weft.demo.notes/app.wasm differ diff --git a/examples/org.weft.demo.notes/signature.sig b/examples/org.weft.demo.notes/signature.sig index b5f9c3a..fe2ce52 100644 --- a/examples/org.weft.demo.notes/signature.sig +++ b/examples/org.weft.demo.notes/signature.sig @@ -1 +1 @@ -f84d1c550b851837fd1ef7b90cd15cee29c9967facc871a117c1ea2b650a171136c1335f4ee24920a740867969a6f017e130e68e9767122d0f23e2320fcf2f0e \ No newline at end of file +555f2d3d0183d28f99f66efef3f2dc8bb8aa3e6a4d038dc78b1414ccf54d23dd52a1668314d377d10603580908d7d9de7109a6ddcf0d7aa175bbf4bbd835c80c \ No newline at end of file diff --git a/examples/org.weft.demo.notes/src/main.rs b/examples/org.weft.demo.notes/src/main.rs index 7d49a44..e5162d8 100644 --- a/examples/org.weft.demo.notes/src/main.rs +++ b/examples/org.weft.demo.notes/src/main.rs @@ -50,22 +50,49 @@ fn load() -> Value { } } -/// Exclusive access to the notes file for one save. -struct SaveLock; +/// A value unique to this process and moment, for lock ownership and +/// staging file names. +fn unique() -> String { + static COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + let nanos = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_nanos()) + .unwrap_or_default(); + let n = COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + format!("{nanos}-{n}") +} + +/// Exclusive access to the notes file for one save. The lock file holds an +/// owner token, so a save only ever removes its own lock. +struct SaveLock { + token: String, +} impl SaveLock { fn acquire() -> Result { + use std::io::Write; + let token = unique(); for _ in 0..50 { - match std::fs::OpenOptions::new().write(true).create_new(true).open(LOCK_PATH) { - Ok(_) => return Ok(SaveLock), + match std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(LOCK_PATH) + { + Ok(mut file) => { + let _ = file.write_all(token.as_bytes()); + return Ok(SaveLock { token }); + } Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { + // A lock older than STALE_LOCK (or of unknown age) was left + // by an ended session. Renaming it away is atomic, so only + // one save can take it over. let stale = std::fs::metadata(LOCK_PATH) .and_then(|m| m.modified()) - .ok() - .and_then(|t| t.elapsed().ok()) - .is_some_and(|age| age > STALE_LOCK); - if stale { - let _ = std::fs::remove_file(LOCK_PATH); + .map(|t| t.elapsed().is_ok_and(|age| age > STALE_LOCK)) + .unwrap_or(true); + let taken = format!("/data/.notes.lock.{token}.stale"); + if stale && std::fs::rename(LOCK_PATH, &taken).is_ok() { + let _ = std::fs::remove_file(&taken); } else { std::thread::sleep(std::time::Duration::from_millis(20)); } @@ -79,7 +106,24 @@ impl SaveLock { impl Drop for SaveLock { fn drop(&mut self) { - let _ = std::fs::remove_file(LOCK_PATH); + if std::fs::read_to_string(LOCK_PATH).is_ok_and(|t| t == self.token) { + let _ = std::fs::remove_file(LOCK_PATH); + } + } +} + +/// Removes staging files left by saves that ended before their rename. Only +/// called with the lock held, when no other save is in progress. +fn remove_abandoned_staging() { + let Ok(entries) = std::fs::read_dir("/data") else { + return; + }; + for entry in entries.flatten() { + let name = entry.file_name(); + let name = name.to_string_lossy(); + if name.starts_with(".notes.txt.") && name.ends_with(".saving") { + let _ = std::fs::remove_file(entry.path()); + } } } @@ -88,19 +132,14 @@ impl Drop for SaveLock { /// the new text in full. fn replace_notes(text: &str) -> std::io::Result<()> { use std::io::Write; - let nanos = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .map(|d| d.as_nanos()) - .unwrap_or_default(); - let staging = format!("/data/.notes.txt.{nanos}.saving"); - let written = std::fs::OpenOptions::new() + let staging = format!("/data/.notes.txt.{}.saving", unique()); + let mut file = std::fs::OpenOptions::new() .write(true) .create_new(true) - .open(&staging) - .and_then(|mut file| { - file.write_all(text.as_bytes())?; - file.sync_all() - }) + .open(&staging)?; + let written = file + .write_all(text.as_bytes()) + .and_then(|()| file.sync_all()) .and_then(|()| std::fs::rename(&staging, NOTES_PATH)); if written.is_err() { let _ = std::fs::remove_file(&staging); @@ -109,9 +148,7 @@ fn replace_notes(text: &str) -> std::io::Result<()> { } fn save(seq: &Value, base: &str, text: &str) -> Value { - let error = |conflict: bool, message: String| { - json!({ "op": "error", "seq": seq, "conflict": conflict, "message": message }) - }; + let error = |conflict: bool, message: String| json!({ "op": "error", "seq": seq, "conflict": conflict, "message": message }); if text.len() > MAX_NOTES { return error(false, format!("notes are limited to {MAX_NOTES} bytes")); } @@ -119,6 +156,7 @@ fn save(seq: &Value, base: &str, text: &str) -> Value { Ok(lock) => lock, Err(message) => return error(false, message), }; + remove_abandoned_staging(); let current = match stored() { Ok(bytes) => revision(&bytes), Err(message) => return error(false, message), diff --git a/examples/org.weft.demo.notes/ui/index.html b/examples/org.weft.demo.notes/ui/index.html index 40976d5..7442945 100644 --- a/examples/org.weft.demo.notes/ui/index.html +++ b/examples/org.weft.demo.notes/ui/index.html @@ -118,6 +118,7 @@ var rev = null; var seq = 0; var pending = null; + var lastSent = null; var encoder = new TextEncoder(); function setStatus(msg) { statusEl.textContent = msg; } @@ -143,11 +144,18 @@ return; } var request = { op: 'save', seq: seq + 1, base: rev, text: editor.value }; + try { + encodeURIComponent(request.text); + } catch (_) { + setStatus('not saved: the text contains characters that cannot be stored'); + return; + } if (encoder.encode(JSON.stringify(request)).length > MAX_MESSAGE) { setStatus('not saved: the notes are too long'); return; } seq += 1; + lastSent = { seq: seq, text: request.text }; pending = { seq: seq, text: request.text, @@ -163,7 +171,7 @@ // Discard reloads the stored notes, replacing unsaved edits. document.getElementById('discard').addEventListener('click', function () { - if (!window.weftIpc || pending) return; + if (!window.weftIpc || pending || rev === null) return; editor.value = saved; countChars(); rev = null; @@ -183,14 +191,18 @@ var msg; try { msg = JSON.parse(raw); } catch (_) { return; } if (msg.op === 'loaded') { + // Only the reply to the outstanding load applies; the editor is + // read-only until it arrives, so it never replaces newer text. + if (rev !== null) return; editor.value = msg.text; countChars(); saved = msg.text; rev = msg.rev; setLoaded(true); setStatus(''); - } else if (msg.op === 'saved' && pending && msg.seq === pending.seq) { - saved = pending.text; + } else if (msg.op === 'saved' && lastSent && msg.seq === lastSent.seq) { + // Also accepts an acknowledgement that arrives after the timeout. + saved = lastSent.text; rev = msg.rev; finishSave(); setStatus(editor.value === saved ? 'saved' : 'saved; newer changes are not saved yet');