mirror of
https://github.com/marcoallegretti/WEFT_OS.git
synced 2026-11-22 08:13:59 +00:00
fix(notes): store notes exactly and never overwrite newer text
The Notes demo exchanged hand-escaped strings: the page turned every literal backslash-n into a line break on load and save, the component escaped only some control characters, so other text produced invalid replies, and every reply replaced the editor contents, discarding text typed while a save was in flight. Saves wrote the file in place and replaced whatever another Notes window had stored. Requests and replies are now JSON objects. The component reports a revision (a hash of the stored bytes) with each load; a save names the revision it was edited from and is refused when the stored notes have changed since, so a second window's text is never replaced silently. The file is written beside notes.txt and renamed over it. A save acknowledgement updates only the saved state, never the editor, and Discard reloads the stored notes. tests/frame/check_notes.py edits Notes with real keyboard input on the nested desktop: text with a literal backslash sequence, quotes, a tab and non-ASCII characters is stored byte for byte, text typed during a save survives, and a save after another window changed the file is refused. It fails against the previous package, which turned the seeded "\n" into a line break. app.wasm is rebuilt and the package re-signed with the demo key.
This commit is contained in:
parent
cc69c048c0
commit
4b27d7f16c
7 changed files with 268 additions and 58 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -7,3 +7,4 @@
|
|||
|
||||
Thumbs.db
|
||||
/tests/components/*/target/
|
||||
/examples/*/target/
|
||||
|
|
|
|||
|
|
@ -86,8 +86,12 @@ rustup target add wasm32-wasip2
|
|||
cd examples/org.weft.demo.counter
|
||||
cargo build --release
|
||||
# output: target/wasm32-wasip2/release/app.wasm
|
||||
cp target/wasm32-wasip2/release/app.wasm app.wasm
|
||||
rm -rf target
|
||||
```
|
||||
|
||||
The signature covers every file in the package directory, so remove the crate's `target` directory before signing it again (see *Signing packages*). The demo keys in `examples/keys` are test fixtures, not a signing authority.
|
||||
|
||||
## weft-runtime with Wasmtime
|
||||
|
||||
```sh
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -1 +1 @@
|
|||
e781544104eee4538e17f953ea3223903e2b2a46a78705cd1ffa8d6946a62d0385c76eab2cc1c2533eecbeff7600e76a4205abc66915be8851fe2e71316ebc0d
|
||||
619aae11afd3f9cd6c6c094dff51e22922f185b8b49b15f77d136fcc74ea3354e6d908e576eae55c124c9d937725cd947593264e4c8e84409a927154522f120f
|
||||
|
|
@ -7,31 +7,82 @@ wit_bindgen::generate!({
|
|||
},
|
||||
});
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use weft::app::{ipc, notify};
|
||||
|
||||
const NOTES_PATH: &str = "/data/notes.txt";
|
||||
const STAGING_PATH: &str = "/data/.notes.txt.saving";
|
||||
/// Largest note accepted, in bytes; app messages are limited to 64 KiB.
|
||||
const MAX_NOTES: usize = 60 * 1024;
|
||||
|
||||
fn load_notes() -> String {
|
||||
std::fs::read_to_string(NOTES_PATH).unwrap_or_default()
|
||||
/// A revision names the exact stored bytes. A save must name the revision
|
||||
/// it was edited from, so a save based on stale content is refused instead
|
||||
/// of silently replacing changes made in another window.
|
||||
fn revision(bytes: &[u8]) -> String {
|
||||
// FNV-1a, 64-bit.
|
||||
let hash = bytes.iter().fold(0xcbf2_9ce4_8422_2325u64, |h, b| {
|
||||
(h ^ u64::from(*b)).wrapping_mul(0x0000_0100_0000_01b3)
|
||||
});
|
||||
format!("{hash:016x}")
|
||||
}
|
||||
|
||||
fn save_notes(text: &str) -> Result<(), String> {
|
||||
std::fs::write(NOTES_PATH, text).map_err(|e| e.to_string())
|
||||
fn stored() -> Result<Vec<u8>, String> {
|
||||
match std::fs::read(NOTES_PATH) {
|
||||
Ok(bytes) => Ok(bytes),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Vec::new()),
|
||||
Err(e) => Err(format!("cannot read notes: {e}")),
|
||||
}
|
||||
}
|
||||
|
||||
fn json_text(text: &str) -> String {
|
||||
let escaped = text
|
||||
.replace('\\', "\\\\")
|
||||
.replace('"', "\\\"")
|
||||
.replace('\n', "\\n")
|
||||
.replace('\r', "\\r")
|
||||
.replace('\t', "\\t");
|
||||
format!("{{\"text\":\"{escaped}\"}}")
|
||||
fn load() -> Value {
|
||||
let bytes = match stored() {
|
||||
Ok(bytes) => bytes,
|
||||
Err(message) => return json!({ "op": "error", "message": message }),
|
||||
};
|
||||
match String::from_utf8(bytes) {
|
||||
Ok(text) => json!({ "op": "loaded", "rev": revision(text.as_bytes()), "text": text }),
|
||||
Err(_) => json!({ "op": "error", "message": "stored notes are not valid UTF-8" }),
|
||||
}
|
||||
}
|
||||
|
||||
fn json_error(msg: &str) -> String {
|
||||
let escaped = msg.replace('"', "\\\"");
|
||||
format!("{{\"error\":\"{escaped}\"}}")
|
||||
fn save(seq: &Value, base: &str, text: &str) -> Value {
|
||||
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"));
|
||||
}
|
||||
let current = match stored() {
|
||||
Ok(bytes) => revision(&bytes),
|
||||
Err(message) => return error(false, message),
|
||||
};
|
||||
if current != base {
|
||||
return error(true, "the notes were changed elsewhere".to_owned());
|
||||
}
|
||||
// Written beside the file and renamed over it, so the stored notes are
|
||||
// always either the previous or the new text in full.
|
||||
let written = std::fs::write(STAGING_PATH, text.as_bytes())
|
||||
.and_then(|()| std::fs::rename(STAGING_PATH, NOTES_PATH));
|
||||
match written {
|
||||
Ok(()) => json!({ "op": "saved", "seq": seq, "rev": revision(text.as_bytes()) }),
|
||||
Err(e) => {
|
||||
let _ = std::fs::remove_file(STAGING_PATH);
|
||||
error(false, format!("cannot save notes: {e}"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle(raw: &str) -> Option<Value> {
|
||||
let request: Value = serde_json::from_str(raw).ok()?;
|
||||
match request.get("op")?.as_str()? {
|
||||
"load" => Some(load()),
|
||||
"save" => {
|
||||
let base = request.get("base")?.as_str()?;
|
||||
let text = request.get("text")?.as_str()?;
|
||||
Some(save(request.get("seq").unwrap_or(&Value::Null), base, text))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
@ -39,19 +90,9 @@ fn main() {
|
|||
|
||||
loop {
|
||||
if let Some(raw) = ipc::recv() {
|
||||
let raw = raw.trim().to_owned();
|
||||
let reply = if raw == "load" {
|
||||
json_text(&load_notes())
|
||||
} else if let Some(rest) = raw.strip_prefix("save:") {
|
||||
let text = rest.replace("\\n", "\n");
|
||||
match save_notes(&text) {
|
||||
Ok(()) => json_text(&text),
|
||||
Err(e) => json_error(&e),
|
||||
}
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
let _ = ipc::send(&reply);
|
||||
if let Some(reply) = handle(&raw) {
|
||||
let _ = ipc::send(&reply.to_string());
|
||||
}
|
||||
} else {
|
||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -103,58 +103,84 @@
|
|||
|
||||
<script>
|
||||
(function () {
|
||||
// The component stores the text exactly as typed. Messages are JSON; each
|
||||
// save names the revision it was edited from and is acknowledged by its
|
||||
// sequence number, so a reply never replaces text typed after it was sent.
|
||||
var editor = document.getElementById('editor');
|
||||
var statusEl = document.getElementById('status');
|
||||
var charEl = document.getElementById('charcount');
|
||||
var saveButton = document.getElementById('save');
|
||||
var saved = '';
|
||||
var rev = null;
|
||||
var seq = 0;
|
||||
var pending = null;
|
||||
|
||||
function setStatus(msg) { statusEl.textContent = msg; }
|
||||
function countChars() { charEl.textContent = editor.value.length + ' chars'; }
|
||||
function send(message) { window.weftIpc.send(JSON.stringify(message)); }
|
||||
|
||||
editor.addEventListener('input', function () {
|
||||
charEl.textContent = editor.value.length + ' chars';
|
||||
});
|
||||
editor.addEventListener('input', countChars);
|
||||
|
||||
document.getElementById('save').addEventListener('click', function () {
|
||||
if (!window.weftIpc) return;
|
||||
var text = editor.value.replace(/\n/g, '\\n');
|
||||
window.weftIpc.send('save:' + text);
|
||||
saveButton.addEventListener('click', function () {
|
||||
if (!window.weftIpc || rev === null || pending) return;
|
||||
seq += 1;
|
||||
pending = { seq: seq, text: editor.value };
|
||||
send({ op: 'save', seq: seq, base: rev, text: pending.text });
|
||||
saveButton.disabled = true;
|
||||
setStatus('saving…');
|
||||
});
|
||||
|
||||
// Discard reloads the stored notes, replacing unsaved edits.
|
||||
document.getElementById('discard').addEventListener('click', function () {
|
||||
if (!window.weftIpc || pending) return;
|
||||
editor.value = saved;
|
||||
charEl.textContent = saved.length + ' chars';
|
||||
setStatus('discarded');
|
||||
setTimeout(function () { setStatus(''); }, 1500);
|
||||
countChars();
|
||||
rev = null;
|
||||
send({ op: 'load' });
|
||||
setStatus('reloading…');
|
||||
});
|
||||
|
||||
document.addEventListener('keydown', function (e) {
|
||||
if ((e.ctrlKey || e.metaKey) && e.key === 's') {
|
||||
e.preventDefault();
|
||||
document.getElementById('save').click();
|
||||
saveButton.click();
|
||||
}
|
||||
});
|
||||
|
||||
function onMessage(raw) {
|
||||
var msg;
|
||||
try { msg = JSON.parse(raw); } catch (_) { return; }
|
||||
if (msg.op === 'loaded') {
|
||||
// Edits made while the load was in flight are kept.
|
||||
if (editor.value === saved) {
|
||||
editor.value = msg.text;
|
||||
countChars();
|
||||
}
|
||||
saved = msg.text;
|
||||
rev = msg.rev;
|
||||
setStatus('');
|
||||
} else if (msg.op === 'saved' && pending && msg.seq === pending.seq) {
|
||||
saved = pending.text;
|
||||
rev = msg.rev;
|
||||
pending = null;
|
||||
saveButton.disabled = false;
|
||||
setStatus('saved');
|
||||
setTimeout(function () { if (!pending) setStatus(''); }, 1500);
|
||||
} else if (msg.op === 'error') {
|
||||
if (pending && msg.seq === pending.seq) {
|
||||
pending = null;
|
||||
saveButton.disabled = false;
|
||||
}
|
||||
setStatus(msg.conflict
|
||||
? 'not saved: changed in another window. Copy your text, then Discard to reload.'
|
||||
: 'error: ' + msg.message);
|
||||
}
|
||||
}
|
||||
|
||||
function setupIpc() {
|
||||
if (!window.weftIpc) { setTimeout(setupIpc, 50); return; }
|
||||
window.weftIpc.onmessage = function (raw) {
|
||||
try {
|
||||
var msg = typeof raw === 'string' ? JSON.parse(raw) : raw;
|
||||
if (typeof msg.error === 'string') {
|
||||
setStatus('error: ' + msg.error);
|
||||
return;
|
||||
}
|
||||
if (typeof msg.text === 'string') {
|
||||
var text = msg.text.replace(/\\n/g, '\n');
|
||||
editor.value = text;
|
||||
saved = text;
|
||||
charEl.textContent = text.length + ' chars';
|
||||
setStatus('');
|
||||
}
|
||||
} catch (_) {}
|
||||
};
|
||||
setStatus('');
|
||||
window.weftIpc.send('load');
|
||||
window.weftIpc.onmessage = onMessage;
|
||||
send({ op: 'load' });
|
||||
}
|
||||
|
||||
setupIpc();
|
||||
|
|
|
|||
138
tests/frame/check_notes.py
Normal file
138
tests/frame/check_notes.py
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
"""Check that the Notes demo stores exactly what the user types.
|
||||
|
||||
The harness runs weft-appd on a nested desktop with the real runtime and app
|
||||
shell, seeds the Notes data with text containing a literal backslash
|
||||
sequence, quotes, a tab and non-ASCII characters, launches
|
||||
`org.weft.demo.notes` and edits it with real keyboard input:
|
||||
|
||||
1. Typing at the end and pressing Ctrl+S must store the seeded text plus
|
||||
the typed text, byte for byte; a load or save that rewrote `\\n`, quotes
|
||||
or tabs fails.
|
||||
2. Text typed right after Ctrl+S, while the save is in flight, must still be
|
||||
in the editor afterwards: a second Ctrl+S stores it too.
|
||||
3. After the file is changed on disk (as by another Notes window), a save
|
||||
must be refused and the other window's text kept.
|
||||
|
||||
Requires Xvfb, xwd, ImageMagick's convert and xdotool.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
sys.dont_write_bytecode = True
|
||||
from check_counter import AppdClient, free_port, read_endpoint # noqa: E402
|
||||
from session import ROOT, Desktop, SessionError, missing_tools # noqa: E402
|
||||
|
||||
APP_ID = "org.weft.demo.notes"
|
||||
SEED = 'path C:\\new\\table "quoted"\tcaf\u00e9 \u2603'
|
||||
|
||||
|
||||
def wait_for_file(path, expected, timeout=15):
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
if path.exists() and path.read_text(encoding="utf-8") == expected:
|
||||
return
|
||||
time.sleep(0.2)
|
||||
actual = path.read_text(encoding="utf-8") if path.exists() else None
|
||||
raise AssertionError(f"stored notes {actual!r}, expected {expected!r}")
|
||||
|
||||
|
||||
def run(args, desktop, home, xdotool):
|
||||
data_home = home / "share"
|
||||
notes = data_home / "weft/app-data" / APP_ID / "notes.txt"
|
||||
notes.parent.mkdir(parents=True)
|
||||
notes.write_text(SEED, encoding="utf-8")
|
||||
|
||||
t = Path(args.target)
|
||||
desktop.launch_client("appd", [t / "weft-appd"], {
|
||||
"WEFT_RUNTIME_BIN": str(t / "weft-runtime"),
|
||||
"WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"),
|
||||
"WEFT_APP_STORE": str(args.store),
|
||||
"WEFT_DISABLE_CGROUP": "1",
|
||||
"WEFT_APPD_WS_PORT": str(free_port()),
|
||||
"HOME": str(home),
|
||||
"XDG_DATA_HOME": str(data_home),
|
||||
})
|
||||
port, token = read_endpoint(desktop.runtime, 30)
|
||||
appd = AppdClient(port, token)
|
||||
appd.send({"type": "LAUNCH_APP", "app_id": APP_ID, "surface_id": 0})
|
||||
reply = appd.wait_for(lambda m: m.get("type") in ("APP_READY", "ERROR"), 120)
|
||||
if reply.get("type") != "APP_READY":
|
||||
raise AssertionError(f"Notes did not start: {reply}")
|
||||
time.sleep(2)
|
||||
|
||||
def act(*arguments):
|
||||
subprocess.run([xdotool, *arguments], env={"DISPLAY": desktop.display},
|
||||
check=True, timeout=30)
|
||||
|
||||
act("search", "--class", "weft-compositor", "windowfocus", "--sync")
|
||||
# The editor fills the window below the header.
|
||||
act("mousemove", "--sync", "100", "300")
|
||||
act("click", "1")
|
||||
act("key", "ctrl+End")
|
||||
|
||||
act("type", "--delay", "40", ' +\\t "x"')
|
||||
act("key", "ctrl+s")
|
||||
expected = SEED + ' +\\t "x"'
|
||||
act("type", "--delay", "40", " more")
|
||||
wait_for_file(notes, expected)
|
||||
time.sleep(1)
|
||||
act("key", "ctrl+s")
|
||||
expected += " more"
|
||||
wait_for_file(notes, expected)
|
||||
|
||||
other = "written by another window"
|
||||
notes.write_text(other, encoding="utf-8")
|
||||
act("type", "--delay", "40", " lost?")
|
||||
act("key", "ctrl+s")
|
||||
time.sleep(3)
|
||||
if notes.read_text(encoding="utf-8") != other:
|
||||
raise AssertionError("a save replaced notes changed elsewhere")
|
||||
if list(notes.parent.glob(".*saving")):
|
||||
raise AssertionError("a staging file was left behind")
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(description=__doc__,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
parser.add_argument("--compositor", type=Path, default=ROOT / "target/debug/weft-compositor")
|
||||
parser.add_argument("--target", type=Path, default=ROOT / "target/debug",
|
||||
help="directory with weft-appd, weft-runtime and weft-app-shell")
|
||||
parser.add_argument("--store", type=Path, default=ROOT / "examples",
|
||||
help="app store containing org.weft.demo.notes")
|
||||
parser.add_argument("--output", type=Path, default=ROOT / "target/notes-check")
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
xdotool = shutil.which("xdotool")
|
||||
missing = missing_tools() + ([] if xdotool else ["xdotool"])
|
||||
missing += [str(p) for p in (args.compositor, args.target / "weft-appd",
|
||||
args.target / "weft-runtime", args.target / "weft-app-shell")
|
||||
if not p.is_file()]
|
||||
if missing:
|
||||
print("missing: " + ", ".join(missing), file=sys.stderr)
|
||||
return 2
|
||||
home = Path(tempfile.mkdtemp(prefix="weft-home-"))
|
||||
try:
|
||||
with Desktop(args.compositor, args.output, outputs=["appd.log", "notes.ppm"]) as desktop:
|
||||
try:
|
||||
run(args, desktop, home, xdotool)
|
||||
except AssertionError:
|
||||
(args.output / "notes.ppm").write_bytes(desktop.capture())
|
||||
raise
|
||||
except (AssertionError, SessionError, TimeoutError) as failure:
|
||||
print(f"notes check failed: {failure}", file=sys.stderr)
|
||||
print(f"logs and capture in {args.output}", file=sys.stderr)
|
||||
return 1
|
||||
finally:
|
||||
shutil.rmtree(home, ignore_errors=True)
|
||||
print("notes check passed")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Loading…
Reference in a new issue