diff --git a/Cargo.lock b/Cargo.lock index 2420198..c9199fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10522,6 +10522,7 @@ name = "weft-ipc-types" version = "0.1.0" dependencies = [ "rmp-serde", + "rustix 1.1.4", "serde", ] diff --git a/crates/weft-appd/src/grants.rs b/crates/weft-appd/src/grants.rs index 4d311fc..3126794 100644 --- a/crates/weft-appd/src/grants.rs +++ b/crates/weft-appd/src/grants.rs @@ -37,8 +37,8 @@ pub(crate) struct SessionGrants { /// Why a launch is refused, with the error code reported to the client: /// 400 for a malformed app ID, 404 for a package that is not installed, 403 -/// for a package this host will not run as declared and 500 for a host -/// fault. +/// for a package this host will not run as declared, 409 for app data the +/// user must reconcile and 500 for a host fault. #[derive(Debug, PartialEq, Eq)] pub(crate) struct Refusal { pub code: u32, @@ -76,8 +76,9 @@ 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"))?; + let data_home = weft_ipc_types::package::data_home().ok_or_else(|| { + Refusal::new(500, "no data home: XDG_DATA_HOME and HOME are unusable") + })?; Ok(Self { data_home, documents: documents_dir(&home, &config), @@ -171,51 +172,28 @@ pub(crate) fn derive( } /// 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. +/// kept inside the user package store, and makes it private. 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}; + use weft_ipc_types::package::{ + Migration, MigrationError, 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)?; + Err(e @ MigrationError::Conflict { .. }) => return Err(Refusal::new(409, e.to_string())), + Err(e) => return Err(Refusal::new(500, e.to_string())), } 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(()) + builder.recursive(true); + builder + .create(dir) + .and_then(|()| weft_ipc_types::package::make_private(dir)) + .map_err(|e| Refusal::new(500, format!("cannot prepare {}: {e}", dir.display()))) } /// Adds a directory grant; declaring both modes for one directory grants the diff --git a/crates/weft-ipc-types/Cargo.toml b/crates/weft-ipc-types/Cargo.toml index 64b262a..419fe4d 100644 --- a/crates/weft-ipc-types/Cargo.toml +++ b/crates/weft-ipc-types/Cargo.toml @@ -14,3 +14,6 @@ rmp-serde = "1" [lints] workspace = true + +[target.'cfg(target_os = "linux")'.dependencies] +rustix = { version = "1", features = ["fs"] } diff --git a/crates/weft-ipc-types/src/package.rs b/crates/weft-ipc-types/src/package.rs index 6b39fe0..95c232d 100644 --- a/crates/weft-ipc-types/src/package.rs +++ b/crates/weft-ipc-types/src/package.rs @@ -54,40 +54,129 @@ pub enum Migration { 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", +/// Why data in the earlier layout could not be moved. Nothing was changed. +#[derive(Debug)] +pub enum MigrationError { + /// Data exists in both locations; either may hold the user's latest + /// data, so the user has to choose. + Conflict { + legacy: PathBuf, + target: PathBuf, + }, + /// The two locations are on different filesystems, so the data cannot + /// be moved in one step. + CrossDevice { + legacy: PathBuf, + target: PathBuf, + }, + /// The earlier location is not a plain directory. + NotADirectory(PathBuf), + Io(std::io::Error), +} + +impl std::fmt::Display for MigrationError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Conflict { legacy, target } => write!( + f, + "app data exists both in {} and in {}; keep one of them and remove the other", legacy.display(), target.display() ), - )); + Self::CrossDevice { legacy, target } => write!( + f, + "app data in {} cannot be moved to {} on another filesystem; move it there by hand", + legacy.display(), + target.display() + ), + Self::NotADirectory(path) => write!( + f, + "{} is not a plain directory; move it aside to continue", + path.display() + ), + Self::Io(e) => e.fmt(f), + } + } +} + +impl std::error::Error for MigrationError {} + +/// Moves `legacy` to `target` if `legacy` is a directory, in one rename so +/// the data is never partly in both places, and then removes the earlier +/// package directory if that leaves it empty. Refuses, changing nothing, +/// when `target` already exists (even empty), when `legacy` is a symbolic +/// link or not a directory, and when the rename would cross filesystems. +pub fn migrate_app_data(legacy: &Path, target: &Path) -> Result { + let conflict = || MigrationError::Conflict { + legacy: legacy.to_path_buf(), + target: target.to_path_buf(), + }; + let metadata = match std::fs::symlink_metadata(legacy) { + Ok(metadata) => metadata, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Migration::NotNeeded), + Err(e) => return Err(MigrationError::Io(e)), + }; + if !metadata.is_dir() { + return Err(MigrationError::NotADirectory(legacy.to_path_buf())); + } + if std::fs::symlink_metadata(target).is_ok() { + return Err(conflict()); } if let Some(parent) = target.parent() { - std::fs::create_dir_all(parent)?; + std::fs::create_dir_all(parent).map_err(MigrationError::Io)?; + } + match rename_no_replace(legacy, target) { + Ok(()) => {} + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => return Err(conflict()), + Err(e) if e.kind() == std::io::ErrorKind::CrossesDevices => { + return Err(MigrationError::CrossDevice { + legacy: legacy.to_path_buf(), + target: target.to_path_buf(), + }); + } + Err(e) => return Err(MigrationError::Io(e)), + } + if let Some(package_dir) = legacy.parent() { + // Fails, harmlessly, unless the directory is now empty. + let _ = std::fs::remove_dir(package_dir); } - std::fs::rename(legacy, target)?; Ok(Migration::Moved) } +/// Renames `from` to `to`, failing with `AlreadyExists` instead of replacing +/// an existing `to`, even one created after the caller checked for it. +#[cfg(target_os = "linux")] +fn rename_no_replace(from: &Path, to: &Path) -> std::io::Result<()> { + use rustix::fs::{CWD, RenameFlags, renameat_with}; + renameat_with(CWD, from, CWD, to, RenameFlags::NOREPLACE).map_err(std::io::Error::from) +} + +#[cfg(not(target_os = "linux"))] +fn rename_no_replace(from: &Path, to: &Path) -> std::io::Result<()> { + if std::fs::symlink_metadata(to).is_ok() { + return Err(std::io::ErrorKind::AlreadyExists.into()); + } + std::fs::rename(from, to) +} + +/// Makes `dir` accessible only to its owner. A symbolic link is refused +/// rather than followed. +pub fn make_private(dir: &Path) -> std::io::Result<()> { + let metadata = std::fs::symlink_metadata(dir)?; + if !metadata.is_dir() { + return Err(std::io::Error::other(format!( + "{} is not a plain directory", + dir.display() + ))); + } + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700))?; + } + Ok(()) +} + #[cfg(test)] mod tests { use super::*; @@ -131,6 +220,16 @@ mod tests { ); } + #[test] + fn migration_keeps_a_package_directory_with_other_content() { + let root = temp("keep-package"); + let legacy = root.join("apps/org.weft.app/data"); + std::fs::create_dir_all(&legacy).unwrap(); + std::fs::write(root.join("apps/org.weft.app/wapp.toml"), "").unwrap(); + migrate_app_data(&legacy, &root.join("new")).unwrap(); + assert!(root.join("apps/org.weft.app/wapp.toml").exists()); + } + #[test] fn migration_moves_data_intact() { let root = temp("move"); @@ -166,7 +265,14 @@ mod tests { 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!(matches!(err, MigrationError::Conflict { .. })); + // An empty target is a conflict too: it may be in use. + std::fs::remove_file(target.join("a")).unwrap(); + assert!(matches!( + migrate_app_data(&legacy, &target), + Err(MigrationError::Conflict { .. }) + )); + std::fs::write(target.join("a"), "new").unwrap(); assert_eq!(std::fs::read_to_string(legacy.join("a")).unwrap(), "old"); assert_eq!(std::fs::read_to_string(target.join("a")).unwrap(), "new"); } @@ -177,7 +283,10 @@ mod tests { 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!(matches!( + migrate_app_data(&root.join("old"), &root.join("new")), + Err(MigrationError::NotADirectory(_)) + )); assert!(!root.join("new").exists()); } } diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index e715f64..978fac3 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -154,6 +154,12 @@ fn check_package(dir: &Path) -> anyhow::Result { } }; + // Earlier versions kept app data in `/data`; the name stays + // reserved so package content is never mistaken for user data. + if std::fs::symlink_metadata(dir.join("data")).is_ok() { + errors.push("a top-level 'data' entry is reserved and cannot be part of a package".into()); + } + if let Some(ref m) = manifest { if !weft_ipc_types::package::is_valid_app_id(&m.package.id) { errors.push(format!( @@ -292,11 +298,14 @@ 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. + // An earlier weft-appd could leave only app data, or an empty directory, + // where the package now goes; move the data 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 declares_app_data(&manifest) { + move_app_data_out(app_id, &dest)?; + } + let _ = std::fs::remove_dir(&dest); } if dest.exists() { anyhow::bail!( @@ -328,38 +337,58 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> target.display() ); } - move_app_data_out(app_id, &target)?; + if load_manifest(&target).is_ok_and(|m| declares_app_data(&m)) { + 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. +/// Whether a package directory is empty or contains nothing but `data`. fn only_holds_app_data(package_dir: &Path) -> anyhow::Result { - let mut entries = std::fs::read_dir(package_dir) + let 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"]) + Ok(entries.is_empty() || 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. +/// Whether the manifest declares an app-data capability; only such apps +/// had data kept in the package directory. +fn declares_app_data(manifest: &Manifest) -> bool { + use weft_ipc_types::capability::Capability; + manifest + .package + .capabilities + .iter() + .flatten() + .any(|c| matches!(c.parse(), Ok(Capability::AppData(_)))) +} + +/// Moves app data that an earlier weft-appd kept in `/data` to +/// the app's data directory, which package operations never remove. Earlier +/// versions only did so in the user package store (`~/.local/share/weft/apps`), +/// so other stores are left alone. 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}; + use weft_ipc_types::package::{ + Migration, app_data_dir, data_home, legacy_app_data_dir, make_private, migrate_app_data, + }; + let Some(home) = std::env::var_os("HOME").map(PathBuf::from) else { + return Ok(()); + }; let legacy = package_dir.join("data"); - if std::fs::symlink_metadata(&legacy).is_err() { + if legacy != legacy_app_data_dir(&home, app_id) { 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()))? + let data_home = data_home().context("cannot locate the data home to keep app data")?; + let target = app_data_dir(&data_home, app_id); + if migrate_app_data(&legacy, &target).with_context(|| format!("keep app data of {app_id}"))? + == Migration::Moved { - Migration::Moved => println!("app data kept at {}", target.display()), - Migration::NotNeeded => {} + make_private(&target).with_context(|| format!("restrict {}", target.display()))?; + println!("app data kept at {}", target.display()); } Ok(()) } @@ -638,83 +667,142 @@ mod tests { 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) { + /// A minimal valid package for `app_id` in `src` declaring `capabilities`. + fn write_package(src: &Path, app_id: &str, capabilities: &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\ + "[package]\nid = \"{app_id}\"\nname = \"D\"\nversion = \"1.0.0\"\n\ + capabilities = [{capabilities}]\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 { + /// Runs `f` with HOME set to `home` and XDG_DATA_HOME to `home/share`. + fn with_home(home: &Path, f: impl FnOnce() -> T) -> T { let _env = env_lock(); - let prior = std::env::var_os("XDG_DATA_HOME"); + let prior: Vec<_> = ["HOME", "XDG_DATA_HOME"] + .iter() + .map(|k| (*k, std::env::var_os(k))) + .collect(); // 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"), + std::env::set_var("HOME", home); + std::env::set_var("XDG_DATA_HOME", home.join("share")); + } + let result = f(); + for (key, value) in prior { + unsafe { + match value { + Some(v) => std::env::set_var(key, v), + None => std::env::remove_var(key), + } } } 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())); + fn temp_root(name: &str) -> PathBuf { + let root = std::env::temp_dir().join(format!("weft_pack_{name}_{}", std::process::id())); let _ = std::fs::remove_dir_all(&root); + root + } + + const APP_DATA: &str = "\"fs:rw:app-data\""; + + #[test] + fn uninstall_keeps_app_data_from_the_user_store() { + let home = temp_root("keep"); 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(); + write_package(&home.join("src"), app_id, APP_DATA); + let store = home.join(".local/share/weft/apps"); + install_package_to(&home.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(); + with_home(&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); + let kept = weft_ipc_types::package::app_data_dir(&home.join("share"), app_id); assert_eq!( std::fs::read_to_string(kept.join("notes.txt")).unwrap(), "first\nsecond" ); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mode = std::fs::metadata(&kept).unwrap().permissions().mode(); + assert_eq!(mode & 0o777, 0o700); + } // A second copy cannot be merged: uninstall refuses and removes nothing. - install_package_to(&root.join("src"), &store).unwrap(); + install_package_to(&home.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!(with_home(&home, || uninstall_package_from(app_id, &store)).is_err()); assert!(legacy.is_dir() && kept.is_dir()); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn other_stores_and_apps_without_data_capability_are_left_alone() { + let home = temp_root("alone"); + let app_id = "com.example.alone"; + // No app-data capability: a data directory is not app data. + write_package(&home.join("src"), app_id, ""); + let store = home.join(".local/share/weft/apps"); + install_package_to(&home.join("src"), &store).unwrap(); + std::fs::create_dir_all(store.join(app_id).join("data")).unwrap(); + with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); + assert!(!home.join("share").exists()); + + // Another store: earlier versions never kept data there. + write_package(&home.join("src2"), app_id, APP_DATA); + let other = home.join("other-store"); + install_package_to(&home.join("src2"), &other).unwrap(); + std::fs::create_dir_all(other.join(app_id).join("data")).unwrap(); + with_home(&home, || uninstall_package_from(app_id, &other)).unwrap(); + assert!(!home.join("share").exists()); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn packages_cannot_ship_a_top_level_data_entry() { + let root = temp_root("ships-data"); + let src = root.join("src"); + write_package(&src, "com.example.ships", ""); + std::fs::create_dir_all(src.join("data")).unwrap(); + std::fs::write(src.join("data/asset.json"), "{}").unwrap(); + let err = check_package(&src).unwrap_err().to_string(); + assert!(err.contains("'data'"), "{err}"); + assert!(install_package_to(&src, &root.join("store")).is_err()); 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 home = temp_root("leftover"); let app_id = "com.example.leftover"; - write_package(&root.join("src"), app_id); - let store = root.join("store"); + write_package(&home.join("src"), app_id, APP_DATA); + let store = home.join(".local/share/weft/apps"); 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(); + with_home(&home, || install_package_to(&home.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); + let kept = weft_ipc_types::package::app_data_dir(&home.join("share"), app_id); assert_eq!(std::fs::read_to_string(kept.join("x")).unwrap(), "x"); - let _ = std::fs::remove_dir_all(&root); + + // An empty directory left behind does not block installation either. + with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); + std::fs::create_dir_all(store.join(app_id)).unwrap(); + with_home(&home, || install_package_to(&home.join("src"), &store)).unwrap(); + let _ = std::fs::remove_dir_all(&home); } #[test] diff --git a/docs/architecture.md b/docs/architecture.md index 45a1800..fe70d0b 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -112,9 +112,14 @@ Package store roots (in priority order): ## 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). +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). `weft-appd` and `weft-pack` must run with the same `HOME` and `XDG_DATA_HOME` to agree on this location; the systemd unit allows `weft-appd` to write under `~/.local/share/weft`. -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. +Earlier versions kept app data inside the user package store, in `~/.local/share/weft/apps//data`, for apps declaring an app-data capability. That directory is moved to the new location in a single rename, which never replaces an existing directory: + +- by `weft-appd`, on the first launch with an app-data capability; +- by `weft-pack uninstall` and `weft-pack install`, before they touch the package directory, for packages in the user store that declare an app-data capability. + +A package directory left empty by the move is removed. Nothing is moved, and `weft-pack` stops or the launch is refused, when the new location already exists, even empty (error 409), when the old one is not a plain directory, or when the two are on different filesystems (error 500); the message names both locations so the user can keep one copy and remove the other. The system UI shows such refusals. Packages cannot contain a top-level `data` entry, so package content is never taken for app data. `tests/frame/check_app_data.py` checks that existing Notes data arrives byte for byte, that the Notes session then reads and saves it, and that a conflict is refused. ## Environment Variables diff --git a/infra/shell/system-ui.html b/infra/shell/system-ui.html index 1f8aeef..9653a41 100644 --- a/infra/shell/system-ui.html +++ b/infra/shell/system-ui.html @@ -336,7 +336,10 @@ }, })); } else if (msg.type === 'ERROR') { + // Launch refusals (missing package, capability or data conflict) + // must reach the user; the launcher otherwise looks unresponsive. console.warn('appd error', msg.code, msg.message); + showNotification('Cannot open: ' + msg.message); } } diff --git a/infra/systemd/weft-appd.service b/infra/systemd/weft-appd.service index 268af47..ebff272 100644 --- a/infra/systemd/weft-appd.service +++ b/infra/systemd/weft-appd.service @@ -16,7 +16,8 @@ Restart=on-failure RestartSec=1s ProtectSystem=strict PrivateTmp=true -ReadWritePaths=%t +# App data and the user package store, where earlier versions kept app data. +ReadWritePaths=%t -%h/.local/share/weft [Install] WantedBy=graphical.target diff --git a/tests/frame/check_app_data.py b/tests/frame/check_app_data.py index 645cc29..2f43cb8 100644 --- a/tests/frame/check_app_data.py +++ b/tests/frame/check_app_data.py @@ -7,7 +7,10 @@ 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). +bytes, the old directory and the emptied package directory must be gone, +and the new one private (0700). Typing at the end of the note and pressing +Ctrl+S must then store the moved text plus the typed text, which shows that +the session reads and writes the moved directory. A second launch after placing a different file at the old location must be refused with error 409, leaving both copies untouched. @@ -17,19 +20,22 @@ 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 check_notes import wait_for_file # 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): +def run(args, desktop, home, xdotool): legacy = home / ".local/share/weft/apps" / APP_ID / "data" legacy.mkdir(parents=True) (legacy / "notes.txt").write_bytes(NOTES) @@ -52,13 +58,26 @@ def run(args, desktop, home): 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 not (target / "notes.txt").is_file() or (target / "notes.txt").read_bytes() != NOTES: + raise AssertionError("notes did not arrive unchanged") + if legacy.parent.exists(): + raise AssertionError(f"{legacy.parent} still exists") if target.stat().st_mode & 0o777 != 0o700: raise AssertionError(f"{target} is not private: {oct(target.stat().st_mode)}") + def act(*arguments): + subprocess.run([xdotool, *arguments], env={"DISPLAY": desktop.display}, + check=True, timeout=30) + + time.sleep(2) + act("search", "--class", "weft-compositor", "windowfocus", "--sync") + act("mousemove", "--sync", "100", "300") + act("click", "1") + act("key", "ctrl+End") + act("type", "--delay", "40", "ok") + act("key", "ctrl+s") + wait_for_file(target / "notes.txt", NOTES.decode() + "ok") + appd.send({"type": "TERMINATE_APP", "session_id": reply["session_id"]}) legacy.mkdir(parents=True) (legacy / "notes.txt").write_bytes(b"other copy") @@ -67,7 +86,7 @@ def run(args, desktop, home): 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: + (target / "notes.txt").read_bytes() != NOTES + b"ok": raise AssertionError("a refused launch changed app data") @@ -82,7 +101,9 @@ def main(argv=None): 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", + 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()] @@ -92,7 +113,7 @@ def main(argv=None): home = Path(tempfile.mkdtemp(prefix="weft-home-")) try: with Desktop(args.compositor, args.output, outputs=["appd.log"]) as desktop: - run(args, desktop, home) + run(args, desktop, home, xdotool) 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)