diff --git a/README.md b/README.md index 95d9671..194eada 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ WEFT OS is a Wayland compositor and application runtime where every app is a Web **Runtime** — `weft-runtime` runs WASI Component Model binaries under Wasmtime 30 (`--features wasmtime-runtime`). Provides `weft:app/notify`, `weft:app/ipc`, `weft:app/fetch`, `weft:app/notifications`, and `weft:app/clipboard` host imports. Preopens filesystem paths according to declared capabilities. -**Package management** — `weft-pack` handles check, sign, verify, install, uninstall, list, build-image (EROFS dm-verity), and info. Validates capability strings at check time. +**Package management** — `weft-pack` handles check, sign, verify, install, uninstall, list, build-image (EROFS dm-verity), and info. Validates capability strings at check time. App data is stored apart from packages and survives uninstall. **File portal** — `weft-file-portal` is a per-session file proxy with a path allowlist and `..` blocking. diff --git a/crates/weft-appd/src/grants.rs b/crates/weft-appd/src/grants.rs index c9cdcb3..4d311fc 100644 --- a/crates/weft-appd/src/grants.rs +++ b/crates/weft-appd/src/grants.rs @@ -56,8 +56,11 @@ impl Refusal { /// Where the host resources behind filesystem capabilities live. pub(crate) struct HostDirs { - /// Root under which each app's private data directory is created. - pub app_data_root: PathBuf, + /// The user's data home (`$XDG_DATA_HOME`); app data lives under it in + /// `weft/app-data/`, apart from installed packages. + pub data_home: PathBuf, + /// The home directory, under which earlier versions kept app data. + pub home: PathBuf, /// The user's documents directory, if the platform configures one. pub documents: Option, } @@ -73,9 +76,12 @@ impl HostDirs { .map(PathBuf::from) .filter(|dir| dir.is_absolute()) .unwrap_or_else(|| home.join(".config")); + let data_home = weft_ipc_types::package::data_home() + .ok_or_else(|| Refusal::new(500, "no data home: HOME is not set"))?; Ok(Self { - app_data_root: home.join(".local/share/weft/apps"), + data_home, documents: documents_dir(&home, &config), + home, }) } } @@ -139,7 +145,7 @@ pub(crate) fn derive( for capability in capabilities { match (&capability, &host) { (Capability::AppData(access), Some(host)) => { - let dir = host.app_data_root.join(app_id).join("data"); + let dir = weft_ipc_types::package::app_data_dir(&host.data_home, app_id); grant_dir(&mut grants, dir, "/data", *access)?; } (Capability::Documents(access), Some(host)) => { @@ -158,16 +164,60 @@ pub(crate) fn derive( } } } - for dir in &grants.dirs { - if dir.guest == "/data" { - std::fs::create_dir_all(&dir.host).map_err(|e| { - Refusal::new(500, format!("cannot create {}: {e}", dir.host.display())) - })?; - } + if let (Some(dir), Some(host)) = (grants.dirs.iter().find(|d| d.guest == "/data"), &host) { + prepare_app_data(app_id, &dir.host, host)?; } Ok(grants) } +/// Creates the app's data directory, first moving data an earlier version +/// kept inside the package store. A conflict between the two locations +/// refuses the launch (409) and leaves both untouched. +fn prepare_app_data(app_id: &str, dir: &Path, host: &HostDirs) -> Result<(), Refusal> { + use weft_ipc_types::package::{Migration, legacy_app_data_dir, migrate_app_data}; + let legacy = legacy_app_data_dir(&host.home, app_id); + match migrate_app_data(&legacy, dir) { + Ok(Migration::Moved) => { + tracing::info!(%app_id, from = %legacy.display(), to = %dir.display(), "app data moved"); + } + Ok(Migration::NotNeeded) => {} + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { + return Err(Refusal::new(409, e.to_string())); + } + Err(e) => { + return Err(Refusal::new( + 500, + format!("cannot move app data from {}: {e}", legacy.display()), + )); + } + } + create_private_dir(dir) + .map_err(|e| Refusal::new(500, format!("cannot create {}: {e}", dir.display()))) +} + +/// Creates `dir` and missing parents, and makes the directory itself +/// accessible only to the user, including when it already existed or was +/// moved from the earlier layout. +fn create_private_dir(dir: &Path) -> std::io::Result<()> { + if let Some(parent) = dir.parent() { + std::fs::create_dir_all(parent)?; + } + let mut builder = std::fs::DirBuilder::new(); + #[cfg(unix)] + std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700); + match builder.create(dir) { + Ok(()) => {} + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists && dir.is_dir() => {} + Err(e) => return Err(e), + } + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700))?; + } + Ok(()) +} + /// Adds a directory grant; declaring both modes for one directory grants the /// wider one. The host path must survive the `--preopen HOST::GUEST::MODE` /// encoding unchanged: it must be UTF-8 and must not contain `::`. @@ -237,7 +287,8 @@ mod tests { let root = root.to_path_buf(); move || { Ok(HostDirs { - app_data_root: root.join("apps"), + data_home: root.join("share"), + home: root.join("home"), documents, }) } @@ -276,7 +327,7 @@ mod tests { .unwrap(); assert_eq!(read.dirs[0].access, Access::Read); assert!(read.dirs[0].preopen_arg().ends_with("::/data::ro")); - assert!(root.join("apps/org.example.app/data").is_dir()); + assert!(root.join("share/weft/app-data/org.example.app").is_dir()); let both = derive( "org.example.app", @@ -320,7 +371,7 @@ mod tests { derive("org.example.app", &caps(declared), host(&root, None)).unwrap_err(); assert_eq!(refusal.code, 403, "{declared:?}"); } - assert!(!root.join("apps").exists()); + assert!(!root.join("share").exists()); } #[test] @@ -378,4 +429,71 @@ mod tests { assert_eq!(grants.dirs[0].guest, "/xdg/documents"); assert_eq!(grants.dirs[0].access, Access::ReadWrite); } + + #[test] + fn earlier_app_data_is_moved_on_launch() { + let root = temp("migrate"); + let legacy = root.join("home/.local/share/weft/apps/org.example.app/data"); + std::fs::create_dir_all(&legacy).unwrap(); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(&legacy, std::fs::Permissions::from_mode(0o755)).unwrap(); + } + std::fs::write(legacy.join("notes.txt"), "kept").unwrap(); + let grants = derive( + "org.example.app", + &caps(&["fs:rw:app-data"]), + host(&root, None), + ) + .unwrap(); + let data = root.join("share/weft/app-data/org.example.app"); + assert_eq!(grants.dirs[0].host, data); + assert_eq!( + std::fs::read_to_string(data.join("notes.txt")).unwrap(), + "kept" + ); + assert!(!legacy.exists()); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mode = std::fs::metadata(&data).unwrap().permissions().mode(); + assert_eq!(mode & 0o777, 0o700); + } + } + + #[test] + fn conflicting_app_data_refuses_the_launch() { + let root = temp("conflict"); + let legacy = root.join("home/.local/share/weft/apps/org.example.app/data"); + let data = root.join("share/weft/app-data/org.example.app"); + std::fs::create_dir_all(&legacy).unwrap(); + std::fs::create_dir_all(&data).unwrap(); + let refusal = derive( + "org.example.app", + &caps(&["fs:read:app-data"]), + host(&root, None), + ) + .unwrap_err(); + assert_eq!(refusal.code, 409); + assert!(legacy.is_dir() && data.is_dir()); + } + + #[cfg(unix)] + #[test] + fn app_data_is_private() { + use std::os::unix::fs::PermissionsExt; + let root = temp("private"); + derive( + "org.example.app", + &caps(&["fs:rw:app-data"]), + host(&root, None), + ) + .unwrap(); + let mode = std::fs::metadata(root.join("share/weft/app-data/org.example.app")) + .unwrap() + .permissions() + .mode(); + assert_eq!(mode & 0o777, 0o700); + } } diff --git a/crates/weft-ipc-types/src/package.rs b/crates/weft-ipc-types/src/package.rs index 037eb64..6b39fe0 100644 --- a/crates/weft-ipc-types/src/package.rs +++ b/crates/weft-ipc-types/src/package.rs @@ -1,4 +1,7 @@ -//! Package identity rules shared by the packaging tool and the supervisor. +//! Package identity and storage rules shared by the packaging tool and the +//! supervisor. + +use std::path::{Path, PathBuf}; /// Whether `id` is a valid application ID: at least three dot-separated /// segments, each starting with a lowercase ASCII letter and containing only @@ -14,6 +17,77 @@ pub fn is_valid_app_id(id: &str) -> bool { }) } +/// The user's data home: `$XDG_DATA_HOME` when it is an absolute path, +/// otherwise `$HOME/.local/share`. +pub fn data_home() -> Option { + std::env::var_os("XDG_DATA_HOME") + .map(PathBuf::from) + .filter(|dir| dir.is_absolute()) + .or_else(|| { + std::env::var_os("HOME") + .map(PathBuf::from) + .filter(|home| home.is_absolute()) + .map(|home| home.join(".local/share")) + }) +} + +/// The private data directory of an application, independent of where or +/// whether its package is installed. +pub fn app_data_dir(data_home: &Path, app_id: &str) -> PathBuf { + data_home.join("weft/app-data").join(app_id) +} + +/// Where earlier versions kept application data: inside the user package +/// store, under the installed package directory. +pub fn legacy_app_data_dir(home: &Path, app_id: &str) -> PathBuf { + home.join(".local/share/weft/apps") + .join(app_id) + .join("data") +} + +/// The result of moving data from the earlier layout. +#[derive(Debug, PartialEq, Eq)] +pub enum Migration { + /// No data in the earlier layout. + NotNeeded, + /// The directory was moved to the new location. + Moved, +} + +/// Moves `legacy` to `target` if `legacy` is a directory, in one rename so +/// the data is never partly in both places. Refuses, leaving both untouched, +/// when `target` already exists, since either copy may hold the user's +/// latest data, and when `legacy` is a symbolic link. +pub fn migrate_app_data(legacy: &Path, target: &Path) -> std::io::Result { + use std::io::{Error, ErrorKind}; + let metadata = match std::fs::symlink_metadata(legacy) { + Ok(metadata) => metadata, + Err(e) if e.kind() == ErrorKind::NotFound => return Ok(Migration::NotNeeded), + Err(e) => return Err(e), + }; + if !metadata.is_dir() { + return Err(Error::other(format!( + "{} is not a directory; move it aside to continue", + legacy.display() + ))); + } + if std::fs::symlink_metadata(target).is_ok() { + return Err(Error::new( + ErrorKind::AlreadyExists, + format!( + "app data exists both in {} and in {}; keep one of them", + legacy.display(), + target.display() + ), + )); + } + if let Some(parent) = target.parent() { + std::fs::create_dir_all(parent)?; + } + std::fs::rename(legacy, target)?; + Ok(Migration::Moved) +} + #[cfg(test)] mod tests { use super::*; @@ -34,4 +108,76 @@ mod tests { assert!(!is_valid_app_id(bad), "{bad:?}"); } } + + fn temp(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("weft_migrate_{name}_{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir + } + + #[test] + fn data_lives_outside_the_package_store() { + let home = Path::new("/home/u"); + let data = app_data_dir(&home.join(".local/share"), "org.weft.demo"); + assert_eq!( + data, + Path::new("/home/u/.local/share/weft/app-data/org.weft.demo") + ); + assert!(!data.starts_with("/home/u/.local/share/weft/apps")); + assert_eq!( + legacy_app_data_dir(home, "org.weft.demo"), + Path::new("/home/u/.local/share/weft/apps/org.weft.demo/data") + ); + } + + #[test] + fn migration_moves_data_intact() { + let root = temp("move"); + let legacy = root.join("apps/org.weft.notes/data"); + std::fs::create_dir_all(legacy.join("sub")).unwrap(); + std::fs::write(legacy.join("notes.txt"), "line one\nline \\two").unwrap(); + std::fs::write(legacy.join("sub/x"), [0u8, 255, 10]).unwrap(); + let target = root.join("app-data/org.weft.notes"); + + assert_eq!( + migrate_app_data(&legacy, &target).unwrap(), + Migration::Moved + ); + assert!(!legacy.exists()); + assert_eq!( + std::fs::read_to_string(target.join("notes.txt")).unwrap(), + "line one\nline \\two" + ); + assert_eq!(std::fs::read(target.join("sub/x")).unwrap(), [0u8, 255, 10]); + assert_eq!( + migrate_app_data(&legacy, &target).unwrap(), + Migration::NotNeeded + ); + } + + #[test] + fn migration_refuses_conflicts_and_leaves_both() { + let root = temp("conflict"); + let legacy = root.join("old"); + let target = root.join("new"); + std::fs::create_dir_all(&legacy).unwrap(); + std::fs::create_dir_all(&target).unwrap(); + std::fs::write(legacy.join("a"), "old").unwrap(); + std::fs::write(target.join("a"), "new").unwrap(); + let err = migrate_app_data(&legacy, &target).unwrap_err(); + assert_eq!(err.kind(), std::io::ErrorKind::AlreadyExists); + assert_eq!(std::fs::read_to_string(legacy.join("a")).unwrap(), "old"); + assert_eq!(std::fs::read_to_string(target.join("a")).unwrap(), "new"); + } + + #[cfg(unix)] + #[test] + fn migration_refuses_a_linked_legacy_directory() { + let root = temp("link"); + std::fs::create_dir_all(root.join("elsewhere")).unwrap(); + std::os::unix::fs::symlink(root.join("elsewhere"), root.join("old")).unwrap(); + assert!(migrate_app_data(&root.join("old"), &root.join("new")).is_err()); + assert!(!root.join("new").exists()); + } } diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 83637e7..e715f64 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -292,6 +292,12 @@ fn install_package_to(path: &Path, store_root: &Path) -> anyhow::Result<()> { let manifest = load_manifest(dir)?; let app_id = &manifest.package.id; let dest = store_root.join(app_id); + // An earlier version could leave only app data in the package directory; + // move it to its own location so it neither blocks nor joins the package. + if dest.is_dir() && only_holds_app_data(&dest)? { + move_app_data_out(app_id, &dest)?; + std::fs::remove_dir(&dest).with_context(|| format!("remove {}", dest.display()))?; + } if dest.exists() { anyhow::bail!( "package '{}' is already installed at {}; remove it first", @@ -322,11 +328,42 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> target.display() ); } + move_app_data_out(app_id, &target)?; std::fs::remove_dir_all(&target).with_context(|| format!("remove {}", target.display()))?; println!("uninstalled {}", app_id); Ok(()) } +/// Whether a package directory contains nothing but a `data` directory. +fn only_holds_app_data(package_dir: &Path) -> anyhow::Result { + let mut entries = std::fs::read_dir(package_dir) + .with_context(|| format!("read {}", package_dir.display()))? + .map(|entry| entry.map(|e| e.file_name())) + .collect::, _>>()?; + entries.sort(); + Ok(entries == ["data"]) +} + +/// Moves app data that an earlier version kept in `/data` to +/// the app's data directory, which package operations never remove. Fails, +/// changing nothing, if the data cannot be moved. +fn move_app_data_out(app_id: &str, package_dir: &Path) -> anyhow::Result<()> { + use weft_ipc_types::package::{Migration, app_data_dir, data_home, migrate_app_data}; + let legacy = package_dir.join("data"); + if std::fs::symlink_metadata(&legacy).is_err() { + return Ok(()); + } + let home = data_home().context("cannot locate the data home to keep app data")?; + let target = app_data_dir(&home, app_id); + match migrate_app_data(&legacy, &target) + .with_context(|| format!("keep app data from {}", legacy.display()))? + { + Migration::Moved => println!("app data kept at {}", target.display()), + Migration::NotNeeded => {} + } + Ok(()) +} + fn list_installed_roots() -> Vec { if let Ok(explicit) = std::env::var("WEFT_APP_STORE") { return vec![PathBuf::from(explicit)]; @@ -593,6 +630,93 @@ fn verify_package(dir: &Path, pub_key_file: &Path) -> anyhow::Result { mod tests { use super::*; + /// Serialises the tests that change the process environment; the test + /// harness runs tests on parallel threads that share it. + static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + + fn env_lock() -> std::sync::MutexGuard<'static, ()> { + ENV_LOCK.lock().unwrap_or_else(|p| p.into_inner()) + } + + /// A minimal valid package for `app_id` in `src`. + fn write_package(src: &Path, app_id: &str) { + std::fs::create_dir_all(src.join("ui")).unwrap(); + std::fs::write(src.join("app.wasm"), b"\0asm").unwrap(); + std::fs::write(src.join("ui/index.html"), b"").unwrap(); + std::fs::write( + src.join("wapp.toml"), + format!( + "[package]\nid = \"{app_id}\"\nname = \"D\"\nversion = \"1.0.0\"\n\n\ + [runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n" + ), + ) + .unwrap(); + } + + /// Runs `f` with XDG_DATA_HOME set to `data_home`. + fn with_data_home(data_home: &Path, f: impl FnOnce() -> T) -> T { + let _env = env_lock(); + let prior = std::env::var_os("XDG_DATA_HOME"); + // SAFETY: env_lock serialises every test that touches the environment. + unsafe { std::env::set_var("XDG_DATA_HOME", data_home) }; + let result = f(); + unsafe { + match prior { + Some(v) => std::env::set_var("XDG_DATA_HOME", v), + None => std::env::remove_var("XDG_DATA_HOME"), + } + } + result + } + + #[test] + fn uninstall_keeps_app_data_from_the_package_directory() { + let root = std::env::temp_dir().join(format!("weft_pack_keep_{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + let app_id = "com.example.keep"; + write_package(&root.join("src"), app_id); + let store = root.join("store"); + install_package_to(&root.join("src"), &store).unwrap(); + let legacy = store.join(app_id).join("data"); + std::fs::create_dir_all(&legacy).unwrap(); + std::fs::write(legacy.join("notes.txt"), "first\nsecond").unwrap(); + + let data_home = root.join("share"); + with_data_home(&data_home, || uninstall_package_from(app_id, &store)).unwrap(); + assert!(!store.join(app_id).exists()); + let kept = weft_ipc_types::package::app_data_dir(&data_home, app_id); + assert_eq!( + std::fs::read_to_string(kept.join("notes.txt")).unwrap(), + "first\nsecond" + ); + + // A second copy cannot be merged: uninstall refuses and removes nothing. + install_package_to(&root.join("src"), &store).unwrap(); + std::fs::create_dir_all(&legacy).unwrap(); + assert!(with_data_home(&data_home, || uninstall_package_from(app_id, &store)).is_err()); + assert!(legacy.is_dir() && kept.is_dir()); + let _ = std::fs::remove_dir_all(&root); + } + + #[test] + fn install_moves_leftover_app_data_out_of_the_way() { + let root = std::env::temp_dir().join(format!("weft_pack_leftover_{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + let app_id = "com.example.leftover"; + write_package(&root.join("src"), app_id); + let store = root.join("store"); + std::fs::create_dir_all(store.join(app_id).join("data")).unwrap(); + std::fs::write(store.join(app_id).join("data/x"), "x").unwrap(); + + let data_home = root.join("share"); + with_data_home(&data_home, || install_package_to(&root.join("src"), &store)).unwrap(); + assert!(store.join(app_id).join("wapp.toml").exists()); + assert!(!store.join(app_id).join("data").exists()); + let kept = weft_ipc_types::package::app_data_dir(&data_home, app_id); + assert_eq!(std::fs::read_to_string(kept.join("x")).unwrap(), "x"); + let _ = std::fs::remove_dir_all(&root); + } + #[test] fn app_id_valid() { assert!(weft_ipc_types::package::is_valid_app_id( @@ -971,6 +1095,7 @@ entry = "ui/index.html" #[test] fn list_installed_roots_uses_weft_app_store_when_set() { + let _env = env_lock(); let prior = std::env::var("WEFT_APP_STORE").ok(); unsafe { std::env::set_var("WEFT_APP_STORE", "/custom/store") }; let roots = list_installed_roots(); @@ -985,6 +1110,7 @@ entry = "ui/index.html" #[test] fn list_installed_roots_includes_system_path() { + let _env = env_lock(); let prior = std::env::var("WEFT_APP_STORE").ok(); unsafe { std::env::remove_var("WEFT_APP_STORE") }; let roots = list_installed_roots(); diff --git a/docs/architecture.md b/docs/architecture.md index 8c386d0..45a1800 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -71,7 +71,7 @@ Every WebSocket connection must complete the upgrade and send `HELLO` as its fir Capabilities are declared in `wapp.toml` under `[package] capabilities`. One vocabulary (`weft_ipc_types::capability`) is used by `weft-pack check`, which rejects unknown strings, by `weft-appd`, which derives the session's effective grants, and by `weft-runtime`, which enforces them. -`weft-appd` derives grants once, before a session exists. `LAUNCH_APP` is answered with an error and nothing starts when the app ID is malformed (code 400), the package is not installed (404), a declared capability is unknown, unsupported on this host or cannot be satisfied (403), or the host cannot provide the resources (500, for example without `HOME`). All capabilities are checked before the app's data directory is created. Without `WEFT_RUNTIME_BIN`, appd starts no processes and derives no grants. The runtime does not read the manifest; it receives the grants as arguments: +`weft-appd` derives grants once, before a session exists. `LAUNCH_APP` is answered with an error and nothing starts when the app ID is malformed (code 400), the package is not installed (404), a declared capability is unknown, unsupported on this host or cannot be satisfied (403), app data exists in both the current and the earlier location (409), or the host cannot provide the resources (500, for example without `HOME`). All capabilities are checked before the app's data directory is created. Without `WEFT_RUNTIME_BIN`, appd starts no processes and derives no grants. The runtime does not read the manifest; it receives the grants as arguments: - `--preopen HOST::GUEST::ro|rw` for each directory, with the access mode the capability names; - `--grant ` for each host-import capability. @@ -80,7 +80,7 @@ Each capability-controlled host import (fetch, notifications, clipboard) checks | Capability | Effect | |---|---| -| `fs:read:app-data` / `fs:rw:app-data` | Preopen `~/.local/share/weft/apps//data` as `/data`, read-only or read-write | +| `fs:read:app-data` / `fs:rw:app-data` | Preopen the app's data directory (see *App data*) as `/data`, read-only or read-write | | `fs:read:xdg-documents` / `fs:rw:xdg-documents` | Preopen the documents directory from the XDG user-dirs configuration (`XDG_DOCUMENTS_DIR`) as `/xdg/documents`; the launch fails when none is configured | | `net:fetch:` | `weft:app/fetch` to that exact host (a lowercase DNS name or dotted IPv4 address) over HTTP or HTTPS, on any port | | `net:fetch:*` | `weft:app/fetch` to any host | @@ -110,6 +110,12 @@ Package store roots (in priority order): 2. `~/.local/share/weft/apps/` 3. `/usr/share/weft/apps/` +## App data + +Each app's private data lives in `$XDG_DATA_HOME/weft/app-data/` (`~/.local/share/weft/app-data/` by default), apart from installed packages. Installing, updating or uninstalling a package never removes it. `weft-appd` creates the directory when a session with an app-data capability starts and makes it accessible only to the user (mode 0700). + +Earlier versions kept app data inside the user package store, in `~/.local/share/weft/apps//data`. The first launch with an app-data capability moves that directory to the new location in a single rename. `weft-pack uninstall` and `weft-pack install` move it the same way before touching the package directory. If data exists in both locations, nothing is moved: the launch is refused with error 409 and `weft-pack` stops, so the user can choose which copy to keep. `tests/frame/check_app_data.py` checks that existing Notes data arrives byte for byte and that a conflict is refused. + ## Environment Variables | Variable | Default | Description | diff --git a/tests/frame/check_app_data.py b/tests/frame/check_app_data.py new file mode 100644 index 0000000..645cc29 --- /dev/null +++ b/tests/frame/check_app_data.py @@ -0,0 +1,107 @@ +"""Check that existing Notes data survives the move to the app data store. + +Earlier versions kept app data inside the user package store, at +`~/.local/share/weft/apps//data`. The harness places a notes file there +(with a backslash sequence, a newline and non-ASCII text, which must come +through byte for byte), runs weft-appd on a nested desktop and launches +`org.weft.demo.notes` with the real runtime and app shell. The session must +reach APP_READY, the file must then be at +`$XDG_DATA_HOME/weft/app-data/org.weft.demo.notes/notes.txt` with identical +bytes, the old directory must be gone and the new one private (0700). + +A second launch after placing a different file at the old location must be +refused with error 409, leaving both copies untouched. + +Requires Xvfb, xwd, ImageMagick's convert and xdotool. +""" + +import argparse +import shutil +import sys +import tempfile +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" +NOTES = "first line\nliteral \\n stays\ncafé ☃\n".encode() + + +def run(args, desktop, home): + legacy = home / ".local/share/weft/apps" / APP_ID / "data" + legacy.mkdir(parents=True) + (legacy / "notes.txt").write_bytes(NOTES) + data_home = home / "data-home" + target = data_home / "weft/app-data" / APP_ID + + 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}") + if (target / "notes.txt").read_bytes() != NOTES: + raise AssertionError("notes changed while moving") + if legacy.exists(): + raise AssertionError(f"{legacy} still exists") + if target.stat().st_mode & 0o777 != 0o700: + raise AssertionError(f"{target} is not private: {oct(target.stat().st_mode)}") + + appd.send({"type": "TERMINATE_APP", "session_id": reply["session_id"]}) + legacy.mkdir(parents=True) + (legacy / "notes.txt").write_bytes(b"other copy") + 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"), 60) + if reply.get("type") != "ERROR" or reply.get("code") != 409: + raise AssertionError(f"conflicting data was not refused: {reply}") + if (legacy / "notes.txt").read_bytes() != b"other copy" or \ + (target / "notes.txt").read_bytes() != NOTES: + raise AssertionError("a refused launch changed app data") + + +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/app-data-check") + args = parser.parse_args(argv) + + missing = missing_tools() + [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"]) as desktop: + run(args, desktop, home) + except (AssertionError, SessionError, TimeoutError) as failure: + print(f"app data check failed: {failure}", file=sys.stderr) + print(f"logs in {args.output}", file=sys.stderr) + return 1 + finally: + shutil.rmtree(home, ignore_errors=True) + print("app data check passed") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main())