From a57cdbc8bef6b7dfac7e0aec6e8ba70305431577 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 17:33:57 +0000 Subject: [PATCH 1/5] feat(pack): install and update packages as immutable revisions activated atomically An installed package could not be updated: install refused it, and uninstalling and reinstalling it was neither atomic nor safe for running sessions, which read files from the package directory. A package is now staged, checked, verified, admitted and flushed. It is then renamed to an immutable revision named by its content digest, /.revisions//, and activated by one rename of the link /. A failure or interruption at any point leaves the previously active revision active, and a failed first installation leaves nothing installed. The revision active before an update is kept, and weft-pack rollback makes it active again after checking it against the owner record. Other revisions are collected by later operations on the app, unless a session still runs from them. Leftovers of interrupted operations are collected too. weft-appd follows the store link only to one of the app's own revisions. It runs a session from the revision directory and holds a shared lock on it for the session's lifetime, so an update, rollback or uninstall never changes or removes the bytes of a running session. A package directory from before revisions is still read. An update replaces it in one exchange, and only while no session runs from it. --- README.md | 2 +- crates/weft-appd/src/launch.rs | 78 ++- crates/weft-ipc-types/src/lib.rs | 1 + crates/weft-ipc-types/src/store.rs | 227 +++++++++ crates/weft-pack/src/main.rs | 744 ++++++++++++++++++++++++++--- docs/architecture.md | 12 +- 6 files changed, 971 insertions(+), 93 deletions(-) create mode 100644 crates/weft-ipc-types/src/store.rs diff --git a/README.md b/README.md index f6a5c18..2ef170b 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. Installs only packages signed by a trusted key (or explicitly as development content) and binds each app ID to its first publisher. App data is stored apart from packages and survives uninstall. +**Package management** — `weft-pack` handles check, sign, verify, install and update, rollback, uninstall, list, build-image (EROFS dm-verity), and info. Validates capability strings at check time. Installs only packages signed by a trusted key (or explicitly as development content) and binds each app ID to its first publisher. 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/launch.rs b/crates/weft-appd/src/launch.rs index 89efc67..5641798 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -35,6 +35,9 @@ pub(crate) struct LaunchPackage { /// The image the package root is mounted from, held for as long as the /// session runs. pub(crate) image: Option, + /// The lock that keeps an installed revision from being removed while + /// the session runs from it. + pub(crate) pin: Option, } /// Resolves `app_id` in the package stores. @@ -69,10 +72,29 @@ fn resolve_in(app_id: &str, stores: &[PathBuf]) -> Result Refusal::new(403, e.to_string()), + weft_ipc_types::store::StoreError::Io(..) => Refusal::new(500, e.to_string()), + })?; + let Some(active) = active else { continue }; + let dir = active.dir().to_path_buf(); + // A directory left with only app data by an earlier layout is not a + // package. + if std::fs::symlink_metadata(dir.join(MANIFEST_FILE)).is_err() { + continue; } + // The session runs from the revision itself, not the link, so an + // update or rollback that switches the link while it runs leaves it + // on the bytes it started with. + let pin = weft_ipc_types::store::pin(&dir).map_err(|e| { + Refusal::new( + 500, + format!("cannot hold {} while it runs: {e}", dir.display()), + ) + })?; + let mut package = from_root(app_id, dir, None)?; + package.pin = Some(pin); + return Ok(package); } Err(Refusal::new( 404, @@ -129,6 +151,7 @@ fn from_root(app_id: &str, root: PathBuf, image: Option) -> Result/.revisions//`. The active +//! revision is chosen by `/`, a symbolic link to it that one +//! rename replaces, so an update or rollback switches every new launch at +//! once and an interrupted one leaves the previous choice in place. Stores +//! written before revisions existed hold the package directly in +//! `/`; that layout is still read. +//! +//! A session pins the revision it was launched from with a shared lock on +//! the revision directory, held for the session's lifetime. Package +//! operations remove a revision only after taking that lock exclusively, so +//! a live session keeps running on the bytes it started with. + +use std::fs::File; +use std::path::{Path, PathBuf}; + +/// The directory of a store that holds package revisions. +pub const REVISIONS_DIR: &str = ".revisions"; + +/// The directory holding the revisions of `app_id`. +pub fn revisions_of(store_root: &Path, app_id: &str) -> PathBuf { + store_root.join(REVISIONS_DIR).join(app_id) +} + +/// The link target that makes `revision` the active revision of `app_id`, +/// relative to the store root. +pub fn link_target(app_id: &str, revision: &str) -> PathBuf { + Path::new(REVISIONS_DIR).join(app_id).join(revision) +} + +/// Whether `name` is a revision name: the hex content digest of a package. +pub fn is_revision_name(name: &str) -> bool { + name.len() == 64 && name.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) +} + +/// Where the active package of `app_id` is in a store. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Active { + /// A revision, by name, in `.revisions//`. + Revision { name: String, dir: PathBuf }, + /// A package directory installed before revisions existed. + Directory(PathBuf), +} + +impl Active { + /// The package root. + pub fn dir(&self) -> &Path { + match self { + Self::Revision { dir, .. } | Self::Directory(dir) => dir, + } + } +} + +/// Why the active package of a store cannot be determined. +#[derive(Debug)] +pub enum StoreError { + Io(PathBuf, std::io::Error), + /// `/` is neither a package directory nor a link to one + /// of its revisions. + NotAPackage(PathBuf), +} + +impl std::fmt::Display for StoreError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Io(path, e) => write!(f, "cannot read {}: {e}", path.display()), + Self::NotAPackage(path) => write!( + f, + "{} is neither a package directory nor a link to one of its revisions", + path.display() + ), + } + } +} + +impl std::error::Error for StoreError {} + +/// The active package of `app_id` in `store_root`, if any. A link is +/// followed only when it names one of the app's own revisions, exactly as +/// package operations write it, and that revision is a directory. +pub fn active(store_root: &Path, app_id: &str) -> Result, StoreError> { + let entry = store_root.join(app_id); + let metadata = match std::fs::symlink_metadata(&entry) { + Ok(m) => m, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(e) => return Err(StoreError::Io(entry, e)), + }; + if metadata.is_dir() { + return Ok(Some(Active::Directory(entry))); + } + if !metadata.file_type().is_symlink() { + return Err(StoreError::NotAPackage(entry)); + } + let target = std::fs::read_link(&entry).map_err(|e| StoreError::Io(entry.clone(), e))?; + let name = target + .file_name() + .and_then(|n| n.to_str()) + .filter(|n| is_revision_name(n)) + .map(str::to_owned); + let Some(name) = name.filter(|n| target == link_target(app_id, n)) else { + return Err(StoreError::NotAPackage(entry)); + }; + let dir = revisions_of(store_root, app_id).join(&name); + match std::fs::symlink_metadata(&dir) { + Ok(m) if m.is_dir() => Ok(Some(Active::Revision { name, dir })), + Ok(_) => Err(StoreError::NotAPackage(entry)), + Err(e) => Err(StoreError::Io(dir, e)), + } +} + +/// A shared lock on a package directory, held while a session runs from it. +#[derive(Debug)] +pub struct Pin { + _dir: File, +} + +/// Pins the package directory `dir`. The lock is taken on the directory +/// that is opened, and the path must still name that directory afterwards: +/// a revision removed or replaced in between is not pinned. +pub fn pin(dir: &Path) -> std::io::Result { + let file = File::open(dir)?; + file.lock_shared()?; + if !same_file(&file, dir)? { + return Err(std::io::Error::other(format!( + "{} changed while it was being pinned", + dir.display() + ))); + } + Ok(Pin { _dir: file }) +} + +/// Takes the exclusive lock on a package directory that no session pins, +/// or returns `None` while one does. +pub fn try_claim(dir: &Path) -> std::io::Result> { + let file = File::open(dir)?; + match file.try_lock() { + Ok(()) => Ok(Some(file)), + Err(std::fs::TryLockError::WouldBlock) => Ok(None), + Err(std::fs::TryLockError::Error(e)) => Err(e), + } +} + +#[cfg(unix)] +fn same_file(file: &File, path: &Path) -> std::io::Result { + use std::os::unix::fs::MetadataExt; + let opened = file.metadata()?; + let named = std::fs::symlink_metadata(path)?; + Ok(opened.dev() == named.dev() && opened.ino() == named.ino()) +} + +#[cfg(not(unix))] +fn same_file(_file: &File, _path: &Path) -> std::io::Result { + Ok(true) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn store(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("weft_store_{name}_{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir + } + + const REV: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; + + #[cfg(unix)] + #[test] + fn only_links_to_the_apps_own_revisions_are_followed() { + use std::os::unix::fs::symlink; + let root = store("links"); + std::fs::create_dir_all(revisions_of(&root, "org.a").join(REV)).unwrap(); + std::fs::create_dir_all(revisions_of(&root, "org.b").join(REV)).unwrap(); + assert!(active(&root, "org.a").unwrap().is_none()); + + symlink(link_target("org.a", REV), root.join("org.a")).unwrap(); + assert_eq!( + active(&root, "org.a").unwrap(), + Some(Active::Revision { + name: REV.to_owned(), + dir: revisions_of(&root, "org.a").join(REV), + }) + ); + // Another app's revision, an absolute target, a non-revision name. + for target in [ + link_target("org.b", REV), + revisions_of(&root, "org.c").join(REV), + PathBuf::from(".revisions/org.c/latest"), + ] { + let _ = std::fs::remove_file(root.join("org.c")); + std::fs::create_dir_all(revisions_of(&root, "org.c").join(REV)).unwrap(); + symlink(&target, root.join("org.c")).unwrap(); + assert!( + matches!(active(&root, "org.c"), Err(StoreError::NotAPackage(_))), + "{}", + target.display() + ); + } + // A package installed before revisions existed. + std::fs::create_dir(root.join("org.d")).unwrap(); + assert_eq!( + active(&root, "org.d").unwrap(), + Some(Active::Directory(root.join("org.d"))) + ); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn a_pinned_directory_cannot_be_claimed() { + let root = store("pin"); + let dir = root.join("rev"); + std::fs::create_dir(&dir).unwrap(); + let pinned = pin(&dir).unwrap(); + assert!(try_claim(&dir).unwrap().is_none()); + // Other sessions can pin it too. + let again = pin(&dir).unwrap(); + drop((pinned, again)); + let claimed = try_claim(&dir).unwrap(); + assert!(claimed.is_some()); + drop(claimed); + std::fs::remove_dir_all(&root).unwrap(); + } +} diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index c8f56c1..40d5199 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -37,6 +37,10 @@ fn main() -> anyhow::Result<()> { let app_id = args.get(2).context("usage: weft-pack uninstall ")?; uninstall_package(app_id)?; } + Some("rollback") => { + let app_id = args.get(2).context("usage: weft-pack rollback ")?; + rollback_package(&resolve_install_root()?, app_id)?; + } Some("list") => { list_installed(); } @@ -120,6 +124,7 @@ fn main() -> anyhow::Result<()> { " --claim-data: adopt app data with no owner" ); eprintln!(" weft-pack uninstall remove installed package"); + eprintln!(" weft-pack rollback activate the previous revision"); eprintln!(" weft-pack list list installed packages"); eprintln!( " weft-pack build-image [--out ] create EROFS image with mkfs.erofs" @@ -418,20 +423,48 @@ fn install_staged( } let _ = std::fs::remove_dir(&dest); } - if dest.exists() { - anyhow::bail!( - "package '{}' is already installed at {}; remove it first", - app_id, - dest.display() - ); - } + // A package installed before revisions existed is replaced by the new + // revision only while no session runs from it, since it is not kept. + let legacy = match active(store_root, app_id)? { + Some(Active::Directory(dir)) => Some( + weft_ipc_types::store::try_claim(&dir) + .with_context(|| format!("open {}", dir.display()))? + .with_context(|| { + format!("{app_id} is running from {}; close it first", dir.display()) + })?, + ), + _ => None, + }; let admission = admit(staging, app_id, mode, &data_home, claim_data)?; let owner = admission.owner; - place(staging, &dest, admission)?; - println!("installed {} -> {} ({owner})", app_id, dest.display()); + let revision = place(staging, store_root, app_id, admission)?; + drop(legacy); + collect(store_root, app_id); + println!( + "installed {app_id} -> {} (revision {}, {owner})", + dest.display(), + &revision[..12] + ); Ok(()) } +/// The active package of `app_id` in `store_root`. +fn active( + store_root: &Path, + app_id: &str, +) -> anyhow::Result> { + Ok(weft_ipc_types::store::active(store_root, app_id)?) +} + +use weft_ipc_types::store::Active; + +/// How long a leftover staging, unpacking or removal directory must be +/// unchanged before it counts as abandoned by an interrupted operation. +const ABANDONED_AFTER: std::time::Duration = std::time::Duration::from_secs(3600); + +/// The revision a store keeps for rollback, a link next to the revisions. +const PREVIOUS_LINK: &str = "previous"; + /// The owner a staged package was admitted under, and the owner record this /// installation created, if any. struct Admission { @@ -439,17 +472,301 @@ struct Admission { created_record: Option, } -/// Makes the staged package available at `dest`. If that fails, the staging -/// copy is removed, and so is an owner record this installation created, so +/// Makes the staged package the active revision of `app_id` and returns +/// the revision's name. The package becomes an immutable revision named by +/// its content digest (identical content is installed once), and one rename +/// of the store's link activates it, so an interruption at any point leaves +/// the previously active revision, or none on a first installation, in +/// place. The revision active before is kept for rollback and for the +/// sessions running from it. If anything fails, the staging copy, a new +/// revision and an owner record this installation created are removed, so /// a failed first installation leaves no claim on the app ID. -fn place(staging: &Path, dest: &Path, admission: Admission) -> anyhow::Result<()> { - if let Err(e) = weft_ipc_types::package::rename_no_replace(staging, dest) { +fn place( + staging: &Path, + store_root: &Path, + app_id: &str, + admission: Admission, +) -> anyhow::Result { + let mut created_revision = None; + let result = (|| { + let revision = hex::encode( + weft_ipc_types::trust::content_digest(staging) + .with_context(|| format!("read {}", staging.display()))?, + ); + let revisions = weft_ipc_types::store::revisions_of(store_root, app_id); + create_store(&revisions)?; + let dir = revisions.join(&revision); + sync_tree(staging)?; + match weft_ipc_types::package::rename_no_replace(staging, &dir) { + Ok(()) => created_revision = Some(dir.clone()), + // The same content is already installed; it is activated as is. + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { + let _ = std::fs::remove_dir_all(staging); + } + Err(e) => { + return Err(e) + .with_context(|| format!("move {} -> {}", staging.display(), dir.display())); + } + } + sync_dir(&revisions)?; + activate(store_root, app_id, &revision)?; + Ok(revision) + })(); + if result.is_err() { let _ = std::fs::remove_dir_all(staging); + if let Some(dir) = &created_revision { + let _ = std::fs::remove_dir_all(dir); + } if let Some(record) = &admission.created_record { let _ = std::fs::remove_file(record); } - return Err(e).with_context(|| format!("move {} -> {}", staging.display(), dest.display())); } + result +} + +/// Makes `revision` the active revision of `app_id`. The revision active +/// before becomes the one kept for rollback. Its link is written first, so +/// an interruption between the two steps keeps both revisions. A package +/// directory from before revisions existed is exchanged with the new link in +/// one step and then removed; the caller holds it so no session runs from it. +fn activate(store_root: &Path, app_id: &str, revision: &str) -> anyhow::Result<()> { + use weft_ipc_types::store::{link_target, revisions_of}; + let revisions = revisions_of(store_root, app_id); + let current = active(store_root, app_id)?; + if let Some(Active::Revision { name, .. }) = ¤t + && name != revision + { + replace_link(&revisions, PREVIOUS_LINK, Path::new(name))?; + } + let dest = store_root.join(app_id); + let link = temporary_link(store_root, app_id, &link_target(app_id, revision))?; + let switched = match ¤t { + Some(Active::Directory(_)) => exchange(&link, &dest), + _ => std::fs::rename(&link, &dest), + }; + if let Err(e) = switched { + let _ = std::fs::remove_file(&link); + return Err(e).with_context(|| format!("activate {}", dest.display())); + } + sync_dir(store_root)?; + if let Some(Active::Directory(_)) = current { + // The earlier package directory now has the temporary name. + std::fs::remove_dir_all(&link) + .with_context(|| format!("remove the replaced package at {}", link.display()))?; + } + Ok(()) +} + +/// Creates a symbolic link to `target` under a fresh temporary name in +/// `store_root`, named so the collection of `app_id` can recognise it. +fn temporary_link(store_root: &Path, app_id: &str, target: &Path) -> anyhow::Result { + let link = store_root.join(format!( + ".link-{app_id}-{}", + hex::encode(rand::random::<[u8; 8]>()) + )); + #[cfg(unix)] + std::os::unix::fs::symlink(target, &link) + .with_context(|| format!("create {}", link.display()))?; + #[cfg(not(unix))] + anyhow::bail!("installing packages requires a Unix host"); + Ok(link) +} + +/// Points the link `name` in `dir` at `target`, replacing it in one rename. +fn replace_link(dir: &Path, name: &str, target: &Path) -> anyhow::Result<()> { + let temporary = dir.join(format!( + ".{name}-{}", + hex::encode(rand::random::<[u8; 8]>()) + )); + #[cfg(unix)] + std::os::unix::fs::symlink(target, &temporary) + .with_context(|| format!("create {}", temporary.display()))?; + if let Err(e) = std::fs::rename(&temporary, dir.join(name)) { + let _ = std::fs::remove_file(&temporary); + return Err(e).with_context(|| format!("update {}", dir.join(name).display())); + } + sync_dir(dir) +} + +#[cfg(target_os = "linux")] +fn exchange(a: &Path, b: &Path) -> std::io::Result<()> { + use rustix::fs::{CWD, RenameFlags, renameat_with}; + renameat_with(CWD, a, CWD, b, RenameFlags::EXCHANGE).map_err(std::io::Error::from) +} + +#[cfg(not(target_os = "linux"))] +fn exchange(_a: &Path, _b: &Path) -> std::io::Result<()> { + Err(std::io::Error::other( + "replacing a package directory requires Linux", + )) +} + +/// Flushes a directory's entries to disk. +fn sync_dir(dir: &Path) -> anyhow::Result<()> { + std::fs::File::open(dir) + .and_then(|d| d.sync_all()) + .with_context(|| format!("sync {}", dir.display())) +} + +/// Flushes every file and directory of a staged package to disk, so a +/// revision that is activated is complete even after a crash. +fn sync_tree(dir: &Path) -> anyhow::Result<()> { + for entry in std::fs::read_dir(dir).with_context(|| format!("read {}", dir.display()))? { + let entry = entry?; + let path = entry.path(); + if entry.file_type()?.is_dir() { + sync_tree(&path)?; + } else { + std::fs::File::open(&path) + .and_then(|f| f.sync_all()) + .with_context(|| format!("sync {}", path.display()))?; + } + } + sync_dir(dir) +} + +/// The revision of `app_id` kept for rollback, if any. +fn previous_revision(store_root: &Path, app_id: &str) -> Option { + let revisions = weft_ipc_types::store::revisions_of(store_root, app_id); + std::fs::read_link(revisions.join(PREVIOUS_LINK)) + .ok()? + .to_str() + .filter(|name| weft_ipc_types::store::is_revision_name(name)) + .filter(|name| std::fs::symlink_metadata(revisions.join(name)).is_ok_and(|m| m.is_dir())) + .map(str::to_owned) +} + +/// Removes what package operations on `app_id` no longer need, keeping the +/// active revision, the one kept for rollback and every revision a session +/// is running from: other revisions, temporary links, and staging, +/// unpacking and removal directories that interrupted operations left +/// behind. The caller holds the owner lock of `app_id`. Failures are +/// reported and leave the entry for the next operation. +fn collect(store_root: &Path, app_id: &str) { + use weft_ipc_types::store::{is_revision_name, revisions_of, try_claim}; + let revisions = revisions_of(store_root, app_id); + let mut keep = Vec::new(); + if let Ok(Some(Active::Revision { name, .. })) = active(store_root, app_id) { + keep.push(name); + } + keep.extend(previous_revision(store_root, app_id)); + if let Ok(entries) = std::fs::read_dir(&revisions) { + for entry in entries.flatten() { + let name = entry.file_name(); + let Some(name) = name.to_str() else { continue }; + let path = entry.path(); + if name == PREVIOUS_LINK { + // A link to a revision that is gone keeps nothing. + if previous_revision(store_root, app_id).is_none() { + let _ = std::fs::remove_file(&path); + } + continue; + } + if name.starts_with('.') { + // A link replacement that was interrupted. + if std::fs::symlink_metadata(&path).is_ok_and(|m| m.file_type().is_symlink()) { + let _ = std::fs::remove_file(&path); + } + continue; + } + if !is_revision_name(name) || keep.iter().any(|k| k == name) { + continue; + } + match try_claim(&path) { + Ok(Some(claim)) => { + remove_claimed(store_root, &path); + drop(claim); + } + Ok(None) => println!("kept revision {} for a running session", &name[..12]), + Err(e) => eprintln!("cannot inspect {}: {e}", path.display()), + } + } + } + let _ = std::fs::remove_dir(&revisions); + let link_prefix = format!(".link-{app_id}-"); + if let Ok(entries) = std::fs::read_dir(store_root) { + for entry in entries.flatten() { + let name = entry.file_name(); + let Some(name) = name.to_str() else { continue }; + let path = entry.path(); + if name.starts_with(&link_prefix) { + // A temporary link of an interrupted activation. + if std::fs::symlink_metadata(&path).is_ok_and(|m| m.file_type().is_symlink()) { + let _ = std::fs::remove_file(&path); + } else if let Ok(Some(claim)) = try_claim(&path) { + // A replaced package directory whose removal was cut short. + remove_claimed(store_root, &path); + drop(claim); + } + } else if [".staging-", ".unpack-", ".trash-"] + .iter() + .any(|p| name.starts_with(p)) + && abandoned(&path) + { + let _ = std::fs::remove_dir_all(&path); + } + } + } +} + +/// Whether a temporary directory has been left unchanged long enough to be +/// abandoned. +fn abandoned(path: &Path) -> bool { + std::fs::symlink_metadata(path) + .and_then(|m| m.modified()) + .ok() + .and_then(|t| t.elapsed().ok()) + .is_some_and(|age| age >= ABANDONED_AFTER) +} + +/// Removes a claimed package directory: it is first renamed out of the way, +/// so no launch can find it half removed. +fn remove_claimed(store_root: &Path, dir: &Path) { + let trash = store_root.join(format!(".trash-{}", hex::encode(rand::random::<[u8; 8]>()))); + match std::fs::rename(dir, &trash) { + Ok(()) => { + if let Err(e) = std::fs::remove_dir_all(&trash) { + eprintln!("cannot remove {}: {e}", trash.display()); + } + } + Err(e) => eprintln!("cannot remove {}: {e}", dir.display()), + } +} + +/// Makes the revision kept for rollback the active revision of `app_id` +/// again; the revision it replaces is kept in turn. The kept revision must +/// still satisfy the app's owner record: a revision by a verified publisher +/// is checked against the trust store again, so a revoked key cannot be +/// rolled back to. +fn rollback_package(store_root: &Path, app_id: &str) -> anyhow::Result<()> { + anyhow::ensure!( + weft_ipc_types::package::is_valid_app_id(app_id), + "'{app_id}' is not a valid app ID" + ); + let data_home = weft_ipc_types::package::data_home() + .context("cannot locate the data home to check who owns the app ID")?; + let _lock = weft_ipc_types::trust::lock_owner(&data_home, app_id)?; + let Some(Active::Revision { .. }) = active(store_root, app_id)? else { + anyhow::bail!("{app_id} has no installed revision to roll back from"); + }; + let previous = previous_revision(store_root, app_id) + .with_context(|| format!("{app_id} has no earlier revision to roll back to"))?; + let dir = weft_ipc_types::store::revisions_of(store_root, app_id).join(&previous); + let record = weft_ipc_types::trust::owner_record_path(&data_home, app_id); + match weft_ipc_types::trust::read_owner(&record)? { + Some(Owner::Development) => {} + Some(Owner::Verified(key)) => { + let signer = TrustStore::load(&TrustStore::directories())?.signer(&dir)?; + anyhow::ensure!( + signer.as_ref() == Some(&key), + "the earlier revision of {app_id} is no longer signed by a trusted key of its owner" + ); + } + None => anyhow::bail!("{app_id} has no recorded owner"), + } + activate(store_root, app_id, &previous)?; + collect(store_root, app_id); + println!("rolled {app_id} back to revision {}", &previous[..12]); Ok(()) } @@ -534,45 +851,40 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> None => None, }; let target = store_root.join(app_id); - if !target.exists() { - // An installation interrupted between recording its owner and placing - // the package leaves a record with nothing installed; with no data - // either, it holds the ID for nothing and is released. - if let Some(home) = &data_home - && !installed_in_another_store(app_id, store_root) - && release_owner_without_data(home, app_id) - { - println!("released {app_id}, which was not installed"); - return Ok(()); - } - anyhow::bail!( - "package '{}' is not installed at {}", - app_id, - target.display() - ); - } - // A `data` directory is never removed with the package: packages cannot - // ship one, so it holds app data from an earlier version. It is moved to - // the app data directory when this is the user store and the package - // declares app data (or its manifest cannot be read); otherwise uninstall - // stops and leaves everything in place. - if std::fs::symlink_metadata(target.join("data")).is_ok() { - let may_hold_app_data = load_manifest(&target).map_or(true, |m| declares_app_data(&m)); - if !(may_hold_app_data && is_user_store(store_root)) { + match active(store_root, app_id)? { + None => { + // Revisions left by sessions that ran past an earlier uninstall + // are removed now that nothing uses them. + collect(store_root, app_id); + // An installation interrupted between recording its owner and + // placing the package leaves a record with nothing installed; + // with no data either, it holds the ID for nothing and is + // released. + if let Some(home) = &data_home + && !installed_in_another_store(app_id, store_root) + && release_owner_without_data(home, app_id) + { + println!("released {app_id}, which was not installed"); + return Ok(()); + } anyhow::bail!( - "{} contains a 'data' directory that may hold app data; move it aside before \ - uninstalling", + "package '{}' is not installed at {}", + app_id, target.display() ); } - move_app_data_out(app_id, &target)?; - } - // Moving the data removes a package directory it leaves empty. - match std::fs::remove_dir_all(&target) { - Err(e) if e.kind() != std::io::ErrorKind::NotFound => { - return Err(e).with_context(|| format!("remove {}", target.display())); + // New launches no longer find the app; sessions running from one of + // its revisions keep it until they end and a later operation on the + // app removes it. + Some(Active::Revision { .. }) => { + std::fs::remove_file(&target) + .with_context(|| format!("remove {}", target.display()))?; + sync_dir(store_root)?; + let revisions = weft_ipc_types::store::revisions_of(store_root, app_id); + let _ = std::fs::remove_file(revisions.join(PREVIOUS_LINK)); + collect(store_root, app_id); } - _ => {} + Some(Active::Directory(_)) => uninstall_directory(app_id, store_root, &target)?, } // The owner record keeps the app ID, and the data with it, for its owner // while the data remains; with no data left, the ID is free again. @@ -585,6 +897,43 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> Ok(()) } +/// Removes a package directory installed before revisions existed, which +/// only happens while no session runs from it. +fn uninstall_directory(app_id: &str, store_root: &Path, target: &Path) -> anyhow::Result<()> { + let _claim = weft_ipc_types::store::try_claim(target) + .with_context(|| format!("open {}", target.display()))? + .with_context(|| { + format!( + "{app_id} is running from {}; close it first", + target.display() + ) + })?; + // A `data` directory is never removed with the package: packages cannot + // ship one, so it holds app data from an earlier version. It is moved to + // the app data directory when this is the user store and the package + // declares app data (or its manifest cannot be read); otherwise uninstall + // stops and leaves everything in place. + if std::fs::symlink_metadata(target.join("data")).is_ok() { + let may_hold_app_data = load_manifest(target).map_or(true, |m| declares_app_data(&m)); + if !(may_hold_app_data && is_user_store(store_root)) { + anyhow::bail!( + "{} contains a 'data' directory that may hold app data; move it aside before \ + uninstalling", + target.display() + ); + } + move_app_data_out(app_id, target)?; + } + // Moving the data removes a package directory it leaves empty. + match std::fs::remove_dir_all(target) { + Err(e) if e.kind() != std::io::ErrorKind::NotFound => { + return Err(e).with_context(|| format!("remove {}", target.display())); + } + _ => {} + } + Ok(()) +} + /// Whether `app_id` is installed in a known store other than `store_root`. /// Owner records belong to the data home, not to a store, so a record stays /// while any store this host knows holds the package. @@ -1099,6 +1448,246 @@ mod tests { }) } + /// Writes a package whose UI holds `marker`, so each marker is a + /// different revision. + fn write_revision(src: &Path, app_id: &str, marker: &str) { + let _ = std::fs::remove_dir_all(src); + write_package(src, app_id, ""); + std::fs::write(src.join("ui/index.html"), marker).unwrap(); + } + + fn active_ui(store: &Path, app_id: &str) -> String { + std::fs::read_to_string(store.join(app_id).join("ui/index.html")).unwrap() + } + + fn revision_count(store: &Path, app_id: &str) -> usize { + std::fs::read_dir(weft_ipc_types::store::revisions_of(store, app_id)).map_or(0, |d| { + d.flatten() + .filter(|e| { + weft_ipc_types::store::is_revision_name(&e.file_name().to_string_lossy()) + }) + .count() + }) + } + + fn active_dir(store: &Path, app_id: &str) -> PathBuf { + active(store, app_id).unwrap().unwrap().dir().to_path_buf() + } + + #[test] + fn an_update_keeps_the_previous_revision_and_collects_older_ones() { + let home = temp_root("update"); + let store = home.join("store"); + let app_id = "org.weft.test.update"; + let src = home.join("src"); + let install = || { + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + }; + write_revision(&src, app_id, "one"); + install().unwrap(); + let one = active_dir(&store, app_id); + write_revision(&src, app_id, "two"); + install().unwrap(); + assert_eq!(active_ui(&store, app_id), "two"); + assert!(one.is_dir(), "the previous revision is kept for rollback"); + write_revision(&src, app_id, "three"); + install().unwrap(); + assert_eq!(active_ui(&store, app_id), "three"); + assert!( + !one.exists(), + "a revision neither active nor previous is removed" + ); + assert_eq!(revision_count(&store, app_id), 2); + assert!(!staging_left(&store)); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn a_revision_a_session_runs_from_is_kept_until_it_ends() { + let home = temp_root("pinned"); + let store = home.join("store"); + let app_id = "org.weft.test.pinned"; + let src = home.join("src"); + let install = || { + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + }; + write_revision(&src, app_id, "one"); + install().unwrap(); + let one = active_dir(&store, app_id); + let session = weft_ipc_types::store::pin(&one).unwrap(); + for marker in ["two", "three", "four"] { + write_revision(&src, app_id, marker); + install().unwrap(); + } + assert_eq!( + std::fs::read_to_string(one.join("ui/index.html")).unwrap(), + "one", + "the running session keeps its bytes" + ); + // Uninstalling stops new launches but not the session. + with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); + assert!(active(&store, app_id).unwrap().is_none()); + assert!(one.is_dir()); + drop(session); + // Once it has ended, the next operation on the app removes it. + let _ = with_home(&home, || uninstall_package_from(app_id, &store)); + assert!(!one.exists()); + assert_eq!(revision_count(&store, app_id), 0); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn a_failed_update_keeps_the_active_revision() { + let home = temp_root("failed_update"); + let store = home.join("store"); + let app_id = "org.weft.test.failed"; + let src = home.join("src"); + write_revision(&src, app_id, "good"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + // A broken component, then the same ID under another owner. + write_revision(&src, app_id, "broken"); + std::fs::write(src.join("app.wasm"), b"not wasm").unwrap(); + assert!( + with_home(&home, || install_package_to( + &src, + &store, + InstallMode::Development + )) + .is_err() + ); + // Signed by a trusted publisher, which does not own the ID. + write_revision(&src, app_id, "other owner"); + sign_package(&src, &key(&home, "other", 7, true)).unwrap(); + let refused = with_trust(&home, || { + install_package_to(&src, &store, InstallMode::Verified) + }); + assert!( + refused + .as_ref() + .is_err_and(|e| e.to_string().contains("belongs to")), + "{refused:?}" + ); + assert_eq!(active_ui(&store, app_id), "good"); + assert_eq!(revision_count(&store, app_id), 1); + assert!(!staging_left(&store)); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn rollback_switches_to_the_kept_revision_and_back() { + let home = temp_root("rollback"); + let store = home.join("store"); + let app_id = "org.weft.test.rollback"; + let src = home.join("src"); + let rollback = || with_home(&home, || rollback_package(&store, app_id)); + write_revision(&src, app_id, "one"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + assert!( + rollback().is_err(), + "a first installation has nothing to roll back to" + ); + write_revision(&src, app_id, "two"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + rollback().unwrap(); + assert_eq!(active_ui(&store, app_id), "one"); + rollback().unwrap(); + assert_eq!(active_ui(&store, app_id), "two"); + assert_eq!(revision_count(&store, app_id), 2); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn a_package_directory_from_before_revisions_is_replaced_unless_in_use() { + let home = temp_root("legacy_update"); + let store = home.join("store"); + let app_id = "org.weft.test.legacy"; + write_revision(&store.join(app_id), app_id, "old"); + let src = home.join("src"); + write_revision(&src, app_id, "new"); + let install = || { + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + }; + let session = weft_ipc_types::store::pin(&store.join(app_id)).unwrap(); + assert!( + install().is_err(), + "a running package directory is not replaced" + ); + assert!(matches!( + active(&store, app_id).unwrap(), + Some(Active::Directory(_)) + )); + assert_eq!(active_ui(&store, app_id), "old"); + drop(session); + install().unwrap(); + assert!(matches!( + active(&store, app_id).unwrap(), + Some(Active::Revision { .. }) + )); + assert_eq!(active_ui(&store, app_id), "new"); + let leftovers: Vec<_> = std::fs::read_dir(&store) + .unwrap() + .flatten() + .map(|e| e.file_name()) + .filter(|n| n != ".revisions" && n.to_str() != Some(app_id)) + .collect(); + assert!(leftovers.is_empty(), "{leftovers:?}"); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn leftovers_of_interrupted_operations_are_removed() { + let home = temp_root("leftovers"); + let store = home.join("store"); + let app_id = "org.weft.test.leftovers"; + let src = home.join("src"); + write_revision(&src, app_id, "one"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + let revisions = weft_ipc_types::store::revisions_of(&store, app_id); + // An activation and a link update cut short, an old staging copy and + // a recent one that may belong to an installation in progress. + #[cfg(unix)] + { + std::os::unix::fs::symlink("x", store.join(format!(".link-{app_id}-0"))).unwrap(); + std::os::unix::fs::symlink("x", revisions.join(".previous-0")).unwrap(); + } + let old = store.join(".staging-old"); + std::fs::create_dir(&old).unwrap(); + std::fs::File::open(&old) + .unwrap() + .set_modified(std::time::SystemTime::now() - 2 * ABANDONED_AFTER) + .unwrap(); + let recent = store.join(".staging-recent"); + std::fs::create_dir(&recent).unwrap(); + write_revision(&src, app_id, "two"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + assert!(std::fs::symlink_metadata(store.join(format!(".link-{app_id}-0"))).is_err()); + assert!(std::fs::symlink_metadata(revisions.join(".previous-0")).is_err()); + assert!(!old.exists()); + assert!(recent.exists()); + let _ = std::fs::remove_dir_all(&home); + } + #[test] fn only_packages_signed_by_a_trusted_key_install_as_verified() { let home = temp_root("verified"); @@ -1248,33 +1837,41 @@ mod tests { #[test] fn a_failed_placement_leaves_no_claim_on_the_id() { let home = temp_root("placement"); - let staging = home.join(".staging-x"); - let dest = home.join("org.weft.test.placed"); - std::fs::create_dir_all(&staging).unwrap(); - std::fs::create_dir_all(dest.join("ui")).unwrap(); - let record = home.join("owners/org.weft.test.placed"); + let store = home.join("store"); + let app_id = "org.weft.test.placed"; + let staging = store.join(".staging-x"); + write_package(&staging, app_id, ""); + // Something that is not a package where the app's link would go. + std::fs::write(store.join(app_id), "not a package").unwrap(); + let record = home.join("owners").join(app_id); weft_ipc_types::trust::write_owner(&record, Owner::Development).unwrap(); let admission = Admission { owner: Owner::Development, created_record: Some(record.clone()), }; - assert!(place(&staging, &dest, admission).is_err()); + assert!(place(&staging, &store, app_id, admission).is_err()); assert!(!staging.exists()); assert!(!record.exists()); + assert_eq!( + std::fs::read_to_string(store.join(app_id)).unwrap(), + "not a package", + "what was there is untouched" + ); + let revisions = weft_ipc_types::store::revisions_of(&store, app_id); assert!( - dest.join("ui").is_dir(), - "the existing package is untouched" + std::fs::read_dir(&revisions).map_or(true, |mut d| d.next().is_none()), + "the new revision is removed" ); // A record that existed before this installation is kept. - std::fs::create_dir_all(&staging).unwrap(); + write_package(&staging, app_id, ""); weft_ipc_types::trust::write_owner(&record, Owner::Development).unwrap(); let admission = Admission { owner: Owner::Development, created_record: None, }; - assert!(place(&staging, &dest, admission).is_err()); + assert!(place(&staging, &store, app_id, admission).is_err()); assert!(record.exists()); let _ = std::fs::remove_dir_all(&home); } @@ -1537,12 +2134,10 @@ mod tests { fn uninstall_keeps_app_data_from_the_user_store() { let home = temp_root("keep"); let app_id = "com.example.keep"; - write_package(&home.join("src"), app_id, APP_DATA); let store = home.join(".local/share/weft/apps"); - with_home(&home, || { - install_package_to(&home.join("src"), &store, InstallMode::Development) - }) - .unwrap(); + // A package directory from before revisions existed, with the data an + // earlier weft-appd kept inside it. + write_package(&store.join(app_id), app_id, APP_DATA); 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(); @@ -1562,10 +2157,7 @@ mod tests { } // A second copy cannot be merged: uninstall refuses and removes nothing. - with_home(&home, || { - install_package_to(&home.join("src"), &store, InstallMode::Development) - }) - .unwrap(); + write_package(&store.join(app_id), app_id, APP_DATA); std::fs::create_dir_all(&legacy).unwrap(); assert!(with_home(&home, || uninstall_package_from(app_id, &store)).is_err()); assert!(legacy.is_dir() && kept.is_dir()); @@ -1577,12 +2169,8 @@ mod tests { let home = temp_root("refuse"); let app_id = "com.example.refuse"; // No app-data capability: the data directory is left and uninstall stops. - write_package(&home.join("src"), app_id, ""); let store = home.join(".local/share/weft/apps"); - with_home(&home, || { - install_package_to(&home.join("src"), &store, InstallMode::Development) - }) - .unwrap(); + write_package(&store.join(app_id), app_id, ""); std::fs::create_dir_all(store.join(app_id).join("data")).unwrap(); std::fs::write(store.join(app_id).join("data/x"), "x").unwrap(); assert!(with_home(&home, || uninstall_package_from(app_id, &store)).is_err()); @@ -1593,12 +2181,8 @@ mod tests { std::fs::remove_dir_all(store.join(app_id)).unwrap(); // Another store: earlier versions never kept data there, so it stops too. - write_package(&home.join("src2"), app_id, APP_DATA); let other = home.join("other-store"); - with_home(&home, || { - install_package_to(&home.join("src2"), &other, InstallMode::Development) - }) - .unwrap(); + write_package(&other.join(app_id), app_id, APP_DATA); std::fs::create_dir_all(other.join(app_id).join("data")).unwrap(); assert!(with_home(&home, || uninstall_package_from(app_id, &other)).is_err()); assert!(other.join(app_id).join("data").is_dir()); @@ -1855,7 +2439,11 @@ entry = "ui/index.html" assert!(store.join(&app_id).join("app.wasm").exists()); assert!(store.join(&app_id).join("wapp.toml").exists()); assert!(store.join(&app_id).join("ui").join("index.html").exists()); - assert!(install_package_to(&src, &store, InstallMode::Development).is_err()); + // The store links the app to an immutable revision; installing + // the same content again keeps that revision. + let revision = std::fs::read_link(store.join(&app_id)).unwrap(); + assert!(install_package_to(&src, &store, InstallMode::Development).is_ok()); + assert_eq!(std::fs::read_link(store.join(&app_id)).unwrap(), revision); let _ = fs::remove_dir_all(&src); let _ = fs::remove_dir_all(&store); }); diff --git a/docs/architecture.md b/docs/architecture.md index 4a94562..c11ee41 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -28,7 +28,7 @@ WASI Preview 2 + Component Model execution host (Wasmtime 30). Runs the componen ### weft-pack -Package management CLI. Subcommands: `check` (validate wapp.toml, entries and package content), `sign` (Ed25519 signature over every file), `verify` (verify signature), `generate-key`, `install` (requires a signature by a trusted key unless `--dev`; records the app ID's owner), `uninstall`, `list`, `build-image` (EROFS dm-verity), `info`. +Package management CLI. Subcommands: `check` (validate wapp.toml, entries and package content), `sign` (Ed25519 signature over every file), `verify` (verify signature), `generate-key`, `install` (requires a signature by a trusted key unless `--dev`; records the app ID's owner; installs or updates, see [Installing and updating](#installing-and-updating)), `uninstall`, `rollback`, `list`, `build-image` (EROFS dm-verity), `info`. ### weft-file-portal @@ -115,11 +115,17 @@ Package store roots (in priority order): `weft-appd` resolves a launch once. A verified image in any store root takes precedence over a directory install in any root, so a user's directory cannot shadow a system image; otherwise the first root with a directory install is used: - **Verified image:** `/.app.img` with its dm-verity hash tree `.app.hash` and root hash `.app.roothash`, the names `weft-pack build-image` and `build-verity` produce. When any of the three exists, the image must mount through `weft-mount-helper`; a missing companion, a malformed root hash, a missing helper or a failed mount refuses the launch, and appd never falls back to a directory install. The root hash is read from the store next to the image and is not yet authenticated, so dm-verity detects corruption but not a store whose image, hash tree and root hash were replaced together. The mount belongs to the session and is released when the session stops. -- **Directory install:** `//wapp.toml`. +- **Directory install:** `/`, a link to the active revision `.revisions//` written by `weft-pack`, or, for a store written before revisions, a package directory. A link is followed only in exactly that form, to one of the app's own revisions; any other link is refused (403). The session runs from the revision directory itself and holds a shared lock on it until it stops, so it keeps its bytes while an update or rollback switches the link. The package root must be a directory and its manifest a regular file, neither a symbolic link, and the manifest must declare the requested ID. `[runtime].module` and `[ui].entry` must be relative paths of plain components, without symbolic links, naming regular files inside that root. The component, the UI document and the capabilities all come from that one manifest. A refused launch reports 400 (invalid ID), 404 (not installed), 403 (invalid package, image or image metadata) or 500 (the host cannot read the package or mount the image). -A directory install can still be changed by its owner while a session runs, and nothing verifies its content at launch. +A directory install can still be changed by its owner while a session runs; for a verified owner, appd checks its content against the signature at launch (see security.md). + +### Installing and updating + +`weft-pack install` copies the package into a staging directory in the store and checks, verifies and admits that copy, so the bytes that are checked are the bytes that are installed. The copy is flushed to disk and renamed to an immutable revision named by its content digest, `/.revisions//`; installing the same content again reuses it. One rename of the link `/` then makes it active. An installation that fails, or is interrupted at any point, leaves the previously active revision active, or, for a first installation, nothing installed and no owner record created by it. A package directory from before revisions is replaced in one exchange with the new link, only while no session runs from it. + +The revision active before an update is kept for `weft-pack rollback `, which makes it active again, keeping the revision it replaces in turn; it is checked against the owner record again, so a revision whose publisher key was withdrawn is not rolled back to. Other revisions are removed by each install, rollback or uninstall of the app, except those a session still runs from; those stay until a later operation on the app finds them unused. Temporary links of interrupted activations are removed then too, and staging, unpacking and removal directories once they are an hour old. `weft-pack uninstall` removes the link, so new launches no longer find the app, and the kept revision; running sessions continue on their revisions. App data is never touched by any of these operations. Package revisions and app data are independent: rolling back a package does not roll back its data. ## App data From 9206088d105e89d1bc823af5cd9b853b59df7f93 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 17:38:10 +0000 Subject: [PATCH 2/5] test(frame): check package updates against running sessions check_update.py installs a test app with weft-pack, runs it with the real runtime and app shell, and checks the following: - an update leaves a running session on the bytes and the revision it started from; - a failed update keeps the active revision; - a later launch gets the update; - the old revision is collected only after its session ends; - rollback reactivates the kept revision; - uninstall refuses new launches without disturbing a running session, and a later uninstall removes the revisions. --- docs/building.md | 1 + tests/frame/check_update.py | 219 ++++++++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100755 tests/frame/check_update.py diff --git a/docs/building.md b/docs/building.md index 5afe1d9..0622109 100644 --- a/docs/building.md +++ b/docs/building.md @@ -88,6 +88,7 @@ python3 tests/frame/check_counter.py # Counter round trip through the session python3 tests/frame/check_notes.py # Notes stores exactly what is typed, refuses stale saves python3 tests/frame/check_app_data.py # Notes data from the earlier layout is moved and kept python3 tests/frame/check_confinement.py # an app page reaches only its UI files and its session bridge +python3 tests/frame/check_update.py # updates leave running sessions on their revision; failed updates, rollback and uninstall ``` ## Demo apps (wasm32-wasip2) diff --git a/tests/frame/check_update.py b/tests/frame/check_update.py new file mode 100755 index 0000000..92e8c7b --- /dev/null +++ b/tests/frame/check_update.py @@ -0,0 +1,219 @@ +#!/usr/bin/env python3 +"""Check that package updates are transactional and leave running sessions alone. + +The harness installs a test application with weft-pack into a private store +and runs weft-appd with the real runtime and app shell on a nested desktop +(see session.py). Each revision of the application's page shows its own +colour. In the presented pixels and the store: + +1. A session started from revision one keeps showing it after the package is + updated to revision two, and its app shell runs from revision one's own + directory, which the update keeps while the session runs. +2. An update that fails (an invalid component) changes nothing: revision two + stays active. +3. A session launched after the update shows revision two. +4. Once the first session has ended, the next update removes revision one, + and keeps revision two for rollback; `weft-pack rollback` makes revision + two active again. +5. Uninstalling while a session runs stops new launches (404) but not the + session; the next uninstall, after it has ended, removes the revisions. + +The application is unsigned development content using the Counter demo's +component. +""" + +import argparse +import os +import shutil +import subprocess +import sys +import tempfile +import time +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from check_counter import AppdClient, free_port, read_endpoint # noqa: E402 +from session import ROOT, Desktop, SessionError, bounding_box, missing_tools, parse_ppm # noqa: E402 + +APP_ID = "org.weft.test.update" +COLOURS = {"one": (200, 0, 0), "two": (0, 0, 200), "three": (0, 160, 0)} +VERSIONS = {"one": 1, "two": 2, "bad": 3, "three": 4} +STARTUP_TIMEOUT = 120.0 + + +def write_revision(src, name, wasm=None): + shutil.rmtree(src, ignore_errors=True) + (src / "ui").mkdir(parents=True) + (src / "wapp.toml").write_text( + f'[package]\nid = "{APP_ID}"\nname = "Update"\nversion = "0.0.{VERSIONS[name]}"\n' + '[runtime]\nmodule = "app.wasm"\n[ui]\nentry = "ui/index.html"\n') + if wasm is None: + shutil.copy(ROOT / "examples/org.weft.demo.counter/app.wasm", src / "app.wasm") + else: + (src / "app.wasm").write_bytes(wasm) + colour = COLOURS.get(name, (0, 0, 0)) + (src / "ui/index.html").write_text( + '') + + +def shows(desktop, colour, timeout=20): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + width, height, pixels = parse_ppm(desktop.capture()) + if bounding_box(width, height, pixels, colour) is not None: + return True + time.sleep(0.3) + return False + + +def run(args, desktop, store, home): + t = Path(args.target) + env = dict(os.environ, WEFT_APP_STORE=str(store), HOME=str(home), + XDG_DATA_HOME=str(home / "share")) + + def pack(*arguments, ok=True): + done = subprocess.run([t / "weft-pack", *arguments], env=env, + capture_output=True, text=True, timeout=60) + if ok and done.returncode != 0: + raise AssertionError(f"weft-pack {' '.join(arguments)} failed: {done.stderr}") + if not ok and done.returncode == 0: + raise AssertionError(f"weft-pack {' '.join(arguments)} succeeded: {done.stdout}") + return done.stdout + done.stderr + + revisions = store / ".revisions" / APP_ID + + def active(): + return os.readlink(store / APP_ID) + + src = home / "src" + write_revision(src, "one") + pack("install", str(src), "--dev") + one = store / active() + + 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(store), + "WEFT_DISABLE_CGROUP": "1", + "WEFT_APPD_WS_PORT": str(free_port()), + "HOME": str(home), + "XDG_DATA_HOME": str(home / "share"), + }) + port, token = read_endpoint(desktop.runtime, 30) + appd = AppdClient(port, token) + + def launch(): + 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"), STARTUP_TIMEOUT) + if reply.get("type") != "APP_READY": + raise AssertionError(f"the app did not start: {reply}") + return reply["session_id"] + + def stop(session_id): + appd.send({"type": "TERMINATE_APP", "session_id": session_id, "force": True}) + appd.wait_for(lambda m: m.get("type") == "APP_STATE" and m.get("state") == "stopped" + and m.get("session_id") == session_id, 20) + + first = launch() + if not shows(desktop, COLOURS["one"]): + raise AssertionError("revision one is not shown") + shells = subprocess.run(["pgrep", "-af", "weft-app-shell"], capture_output=True, + text=True).stdout + if str(one / "ui/index.html") not in shells: + raise AssertionError(f"the app shell does not run from revision one: {shells}") + + # 1. An update leaves the running session on its revision. + write_revision(src, "two") + pack("install", str(src), "--dev") + two = store / active() + if two == one: + raise AssertionError("the update did not activate a new revision") + time.sleep(2) + if not shows(desktop, COLOURS["one"], 5) or shows(desktop, COLOURS["two"], 1): + raise AssertionError("the update changed what the running session shows") + if (one / "ui/index.html").read_text().find("200, 0, 0") < 0: + raise AssertionError("the running session's revision changed") + + # 2. A failed update changes nothing. + write_revision(src, "bad", wasm=b"not a component") + pack("install", str(src), "--dev", ok=False) + if store / active() != two: + raise AssertionError("a failed update changed the active revision") + + # 3. A new launch gets the update. + second = launch() + if not shows(desktop, COLOURS["two"]): + raise AssertionError("a launch after the update does not show revision two") + stop(second) + + # 4. With the first session ended, the next update collects revision one. + if not one.is_dir(): + raise AssertionError("revision one was removed while its session ran") + stop(first) + write_revision(src, "three") + pack("install", str(src), "--dev") + if one.exists(): + raise AssertionError("revision one was kept after its session ended") + if not two.is_dir(): + raise AssertionError("the previous revision was not kept for rollback") + pack("rollback", APP_ID) + if store / active() != two: + raise AssertionError("rollback did not make revision two active") + third = launch() + if not shows(desktop, COLOURS["two"]): + raise AssertionError("a launch after rollback does not show revision two") + + # 5. Uninstall stops new launches, not the running session. + pack("uninstall", APP_ID) + appd.send({"type": "LAUNCH_APP", "app_id": APP_ID, "surface_id": 0}) + refused = appd.wait_for(lambda m: m.get("type") in ("APP_READY", "ERROR"), 30) + if refused.get("code") != 404: + raise AssertionError(f"a launch after uninstall was not refused: {refused}") + if not two.is_dir() or not shows(desktop, COLOURS["two"], 5): + raise AssertionError("uninstall disturbed the running session") + stop(third) + pack("uninstall", APP_ID, ok=False) + if revisions.exists(): + raise AssertionError(f"revisions left after uninstall: {list(revisions.iterdir())}") + + +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-pack, weft-runtime and weft-app-shell") + parser.add_argument("--output", type=Path, default=ROOT / "target/update-check") + args = parser.parse_args(argv) + + missing = missing_tools() + [str(p) for p in (args.compositor, args.target / "weft-appd", + args.target / "weft-pack", + 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 + store = Path(tempfile.mkdtemp(prefix="weft-store-")) + home = Path(tempfile.mkdtemp(prefix="weft-home-")) + try: + with Desktop(args.compositor, args.output, outputs=["appd.log", "update.ppm"]) as desktop: + try: + run(args, desktop, store, home) + except AssertionError: + (args.output / "update.ppm").write_bytes(desktop.capture()) + raise + except (AssertionError, SessionError, TimeoutError) as failure: + print(f"update check failed: {failure}", file=sys.stderr) + print(f"logs and capture in {args.output}", file=sys.stderr) + return 1 + finally: + shutil.rmtree(store, ignore_errors=True) + shutil.rmtree(home, ignore_errors=True) + print("update check passed") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) From 194fb9f0fa75e5fd9bdc84a77d4fadf4dc392432 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 17:44:50 +0000 Subject: [PATCH 3/5] fix(pack): keep earlier app data and committed activations through updates An update of a package directory from before revisions removed it together with the app data an earlier weft-appd kept in its data directory. That data is now moved out under the uninstall rules first, or the update stops. A failure after the store link was switched removed the new revision while the link pointed to it. The switch is now the commit point; cleanup after it is left to collection. Other fixes: - A revision is reused only when its content and signature file match. - The revisions directory is flushed before activation. - A rollback link is not inherited by a new installation, and rollback refuses a link to the active revision. - Staging and unpacking directories are held while in use, so another operation never collects them. - weft-appd refuses a launch, instead of waiting, while a package operation holds the revision. - Listings read only an app's active package. --- crates/weft-appd/src/main.rs | 18 +- crates/weft-ipc-types/src/store.rs | 15 +- crates/weft-pack/src/main.rs | 342 +++++++++++++++++++++++------ docs/architecture.md | 8 +- 4 files changed, 305 insertions(+), 78 deletions(-) diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index a681762..bf588fd 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -631,12 +631,22 @@ fn scan_installed_apps() -> Vec { continue; }; for entry in entries.flatten() { - let Ok(m) = weft_ipc_types::manifest::Manifest::read(&entry.path()) else { + // Only an app's active package, named after the ID its manifest + // declares, is installed; staging copies, revisions and stray + // directories or links are not. + let Some(name) = entry.file_name().to_str().map(str::to_owned) else { continue; }; - // Only a directory named after the ID its manifest declares is an - // installed package; staging copies and stray directories are not. - if entry.file_name().to_str() != Some(m.package.id.as_str()) { + if !weft_ipc_types::package::is_valid_app_id(&name) { + continue; + } + let Ok(Some(found)) = weft_ipc_types::store::active(&root, &name) else { + continue; + }; + let Ok(m) = weft_ipc_types::manifest::Manifest::read(found.dir()) else { + continue; + }; + if m.package.id != name { continue; } if seen.insert(m.package.id.clone()) { diff --git a/crates/weft-ipc-types/src/store.rs b/crates/weft-ipc-types/src/store.rs index ff08eb3..49c117b 100644 --- a/crates/weft-ipc-types/src/store.rs +++ b/crates/weft-ipc-types/src/store.rs @@ -118,10 +118,21 @@ pub struct Pin { /// Pins the package directory `dir`. The lock is taken on the directory /// that is opened, and the path must still name that directory afterwards: -/// a revision removed or replaced in between is not pinned. +/// a revision removed or replaced in between is not pinned. A directory a +/// package operation holds is not waited for, so nobody able to lock a +/// package directory can stall launches; the launch is refused instead. pub fn pin(dir: &Path) -> std::io::Result { let file = File::open(dir)?; - file.lock_shared()?; + match file.try_lock_shared() { + Ok(()) => {} + Err(std::fs::TryLockError::WouldBlock) => { + return Err(std::io::Error::other(format!( + "{} is being changed; try again", + dir.display() + ))); + } + Err(std::fs::TryLockError::Error(e)) => return Err(e), + } if !same_file(&file, dir)? { return Err(std::io::Error::other(format!( "{} changed while it was being pinned", diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 40d5199..e7d0857 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -305,6 +305,8 @@ fn install_into( // A fresh private directory in the store, removed whatever happens, so // nothing can be planted in it and nothing is left behind. let unpacked = private_scratch_dir(store_root, ".unpack")?; + let _in_use = weft_ipc_types::store::try_claim(&unpacked) + .with_context(|| format!("open {}", unpacked.display()))?; let result = unbundle_package(path, &unpacked) .and_then(|()| install_dir(&unpacked.join(name), store_root, mode, claim_data)); let _ = std::fs::remove_dir_all(&unpacked); @@ -392,7 +394,13 @@ fn install_dir( .with_context(|| format!("resolve {}", dir.display())) .and_then(|source| copy_dir(&source, &staging)) .with_context(|| format!("copy {} -> {}", dir.display(), staging.display())) - .and_then(|()| install_staged(&staging, store_root, mode, claim_data)); + .and_then(|()| { + // Held while the copy is in use, so the collection of another + // operation never takes it for abandoned. + let _in_use = weft_ipc_types::store::try_claim(&staging) + .with_context(|| format!("open {}", staging.display()))?; + install_staged(&staging, store_root, mode, claim_data) + }); if result.is_err() { let _ = std::fs::remove_dir_all(&staging); } @@ -414,25 +422,24 @@ fn install_staged( // Installations and uninstallations of one ID run one at a time, so the // owner record always describes the package that is placed. let _lock = weft_ipc_types::trust::lock_owner(&data_home, app_id)?; - // 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)? { - if std::fs::symlink_metadata(dest.join("data")).is_ok() && is_user_store(store_root) { - move_app_data_out(app_id, &dest)?; - } - let _ = std::fs::remove_dir(&dest); - } - // A package installed before revisions existed is replaced by the new - // revision only while no session runs from it, since it is not kept. + // A package directory from before revisions existed is replaced by the + // new revision only while no session runs from it, since it is not kept, + // and only once the app data an earlier weft-appd kept inside it has + // been moved to its own location: the update stops rather than replace + // data with the package. A directory that held only data, or nothing, + // is no package to replace. let legacy = match active(store_root, app_id)? { - Some(Active::Directory(dir)) => Some( - weft_ipc_types::store::try_claim(&dir) + Some(Active::Directory(dir)) => { + let claim = weft_ipc_types::store::try_claim(&dir) .with_context(|| format!("open {}", dir.display()))? .with_context(|| { format!("{app_id} is running from {}; close it first", dir.display()) - })?, - ), + })?; + keep_legacy_data(app_id, store_root, &dir)?; + let emptied = + std::fs::symlink_metadata(&dir).is_err() || std::fs::remove_dir(&dir).is_ok(); + (!emptied).then_some(claim) + } _ => None, }; let admission = admit(staging, app_id, mode, &data_home, claim_data)?; @@ -497,21 +504,33 @@ fn place( create_store(&revisions)?; let dir = revisions.join(&revision); sync_tree(staging)?; - match weft_ipc_types::package::rename_no_replace(staging, &dir) { - Ok(()) => created_revision = Some(dir.clone()), - // The same content is already installed; it is activated as is. - Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { + // A revision of the same name is reused only when it still holds this + // content and the same signature file; anything else under the name, + // such as the remains of an interrupted removal, is replaced. + if std::fs::symlink_metadata(&dir).is_ok() { + if same_revision(&dir, staging, &revision) { let _ = std::fs::remove_dir_all(staging); + return Ok(revision); } - Err(e) => { - return Err(e) - .with_context(|| format!("move {} -> {}", staging.display(), dir.display())); - } + let claim = weft_ipc_types::store::try_claim(&dir) + .with_context(|| format!("open {}", dir.display()))? + .with_context(|| { + format!("{} is in use and differs; try again later", dir.display()) + })?; + remove_claimed(store_root, &dir); + drop(claim); } + weft_ipc_types::package::rename_no_replace(staging, &dir) + .with_context(|| format!("move {} -> {}", staging.display(), dir.display()))?; + created_revision = Some(dir.clone()); sync_dir(&revisions)?; + sync_dir(&store_root.join(weft_ipc_types::store::REVISIONS_DIR))?; + Ok(revision) + })() + .and_then(|revision| { activate(store_root, app_id, &revision)?; Ok(revision) - })(); + }); if result.is_err() { let _ = std::fs::remove_dir_all(staging); if let Some(dir) = &created_revision { @@ -529,14 +548,25 @@ fn place( /// an interruption between the two steps keeps both revisions. A package /// directory from before revisions existed is exchanged with the new link in /// one step and then removed; the caller holds it so no session runs from it. +/// An error means the switch did not happen: once the link is in place, the +/// activation stands, and what remains to tidy is left to `collect`. fn activate(store_root: &Path, app_id: &str, revision: &str) -> anyhow::Result<()> { use weft_ipc_types::store::{link_target, revisions_of}; let revisions = revisions_of(store_root, app_id); let current = active(store_root, app_id)?; - if let Some(Active::Revision { name, .. }) = ¤t - && name != revision - { - replace_link(&revisions, PREVIOUS_LINK, Path::new(name))?; + match ¤t { + Some(Active::Revision { name, .. }) if name != revision => { + replace_link(&revisions, PREVIOUS_LINK, Path::new(name))?; + } + Some(Active::Revision { .. }) => {} + // Nothing earlier of this installation to roll back to; a link left + // by an earlier, interrupted uninstall is dropped. + _ => match std::fs::remove_file(revisions.join(PREVIOUS_LINK)) { + Err(e) if e.kind() != std::io::ErrorKind::NotFound => { + return Err(e).context("remove the stale rollback link"); + } + _ => {} + }, } let dest = store_root.join(app_id); let link = temporary_link(store_root, app_id, &link_target(app_id, revision))?; @@ -548,15 +578,31 @@ fn activate(store_root: &Path, app_id: &str, revision: &str) -> anyhow::Result<( let _ = std::fs::remove_file(&link); return Err(e).with_context(|| format!("activate {}", dest.display())); } - sync_dir(store_root)?; + if let Err(e) = sync_dir(store_root) { + eprintln!("{e:#}"); + } if let Some(Active::Directory(_)) = current { - // The earlier package directory now has the temporary name. - std::fs::remove_dir_all(&link) - .with_context(|| format!("remove the replaced package at {}", link.display()))?; + // The earlier package directory now has the temporary name, which + // `collect` also recognises. + remove_claimed(store_root, &link); } Ok(()) } +/// Whether the revision directory `dir` holds the content named `revision` +/// and the same signature file as `staging`. +fn same_revision(dir: &Path, staging: &Path, revision: &str) -> bool { + let signature = + |root: &Path| match std::fs::read(root.join(weft_ipc_types::trust::SIGNATURE_FILE)) { + Ok(bytes) => Some(Some(bytes)), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Some(None), + Err(_) => None, + }; + weft_ipc_types::trust::content_digest(dir).is_ok_and(|d| hex::encode(d) == revision) + && signature(dir).is_some() + && signature(dir) == signature(staging) +} + /// Creates a symbolic link to `target` under a fresh temporary name in /// `store_root`, named so the collection of `app_id` can recognise it. fn temporary_link(store_root: &Path, app_id: &str, target: &Path) -> anyhow::Result { @@ -702,8 +748,11 @@ fn collect(store_root: &Path, app_id: &str) { .iter() .any(|p| name.starts_with(p)) && abandoned(&path) + && let Ok(Some(claim)) = try_claim(&path) { + // An operation in progress holds its directory. let _ = std::fs::remove_dir_all(&path); + drop(claim); } } } @@ -746,10 +795,11 @@ fn rollback_package(store_root: &Path, app_id: &str) -> anyhow::Result<()> { let data_home = weft_ipc_types::package::data_home() .context("cannot locate the data home to check who owns the app ID")?; let _lock = weft_ipc_types::trust::lock_owner(&data_home, app_id)?; - let Some(Active::Revision { .. }) = active(store_root, app_id)? else { + let Some(Active::Revision { name: current, .. }) = active(store_root, app_id)? else { anyhow::bail!("{app_id} has no installed revision to roll back from"); }; let previous = previous_revision(store_root, app_id) + .filter(|previous| *previous != current) .with_context(|| format!("{app_id} has no earlier revision to roll back to"))?; let dir = weft_ipc_types::store::revisions_of(store_root, app_id).join(&previous); let record = weft_ipc_types::trust::owner_record_path(&data_home, app_id); @@ -854,8 +904,11 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> match active(store_root, app_id)? { None => { // Revisions left by sessions that ran past an earlier uninstall - // are removed now that nothing uses them. - collect(store_root, app_id); + // are removed now that nothing uses them; collecting needs the + // owner lock. + if data_home.is_some() { + collect(store_root, app_id); + } // An installation interrupted between recording its owner and // placing the package leaves a record with nothing installed; // with no data either, it holds the ID for nothing and is @@ -882,7 +935,10 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> sync_dir(store_root)?; let revisions = weft_ipc_types::store::revisions_of(store_root, app_id); let _ = std::fs::remove_file(revisions.join(PREVIOUS_LINK)); - collect(store_root, app_id); + let _ = sync_dir(&revisions); + if data_home.is_some() { + collect(store_root, app_id); + } } Some(Active::Directory(_)) => uninstall_directory(app_id, store_root, &target)?, } @@ -897,6 +953,28 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> Ok(()) } +/// Moves the app data an earlier weft-appd kept in `/data` to the +/// app's data directory, before the package directory `dir` is removed or +/// replaced. A `data` directory is never removed with a package: packages +/// cannot ship one, so it holds app data from an earlier version. It is +/// moved when this is the user store and the package declares app data (or +/// its manifest cannot be read); otherwise this stops and leaves everything +/// in place. +fn keep_legacy_data(app_id: &str, store_root: &Path, target: &Path) -> anyhow::Result<()> { + if std::fs::symlink_metadata(target.join("data")).is_ok() { + let may_hold_app_data = load_manifest(target).map_or(true, |m| declares_app_data(&m)); + if !(may_hold_app_data && is_user_store(store_root)) { + anyhow::bail!( + "{} contains a 'data' directory that may hold app data; move it aside before \ + uninstalling or updating", + target.display() + ); + } + move_app_data_out(app_id, target)?; + } + Ok(()) +} + /// Removes a package directory installed before revisions existed, which /// only happens while no session runs from it. fn uninstall_directory(app_id: &str, store_root: &Path, target: &Path) -> anyhow::Result<()> { @@ -908,22 +986,7 @@ fn uninstall_directory(app_id: &str, store_root: &Path, target: &Path) -> anyhow target.display() ) })?; - // A `data` directory is never removed with the package: packages cannot - // ship one, so it holds app data from an earlier version. It is moved to - // the app data directory when this is the user store and the package - // declares app data (or its manifest cannot be read); otherwise uninstall - // stops and leaves everything in place. - if std::fs::symlink_metadata(target.join("data")).is_ok() { - let may_hold_app_data = load_manifest(target).map_or(true, |m| declares_app_data(&m)); - if !(may_hold_app_data && is_user_store(store_root)) { - anyhow::bail!( - "{} contains a 'data' directory that may hold app data; move it aside before \ - uninstalling", - target.display() - ); - } - move_app_data_out(app_id, target)?; - } + keep_legacy_data(app_id, store_root, target)?; // Moving the data removes a package directory it leaves empty. match std::fs::remove_dir_all(target) { Err(e) if e.kind() != std::io::ErrorKind::NotFound => { @@ -948,7 +1011,10 @@ fn installed_in_another_store(app_id: &str, store_root: &Path) -> bool { roots .iter() .filter(|root| root.as_path() != store_root) - .any(|root| Manifest::read(&root.join(app_id)).is_ok_and(|m| m.package.id == app_id)) + .any(|root| { + matches!(active(root, app_id), Ok(Some(found)) + if Manifest::read(found.dir()).is_ok_and(|m| m.package.id == app_id)) + }) } /// Removes the owner record of `app_id` when the app has no data, and @@ -965,15 +1031,6 @@ fn release_owner_without_data(data_home: &Path, app_id: &str) -> bool { && std::fs::remove_file(weft_ipc_types::trust::owner_record_path(data_home, app_id)).is_ok() } -/// Whether a package directory is empty or contains nothing but `data`. -fn only_holds_app_data(package_dir: &Path) -> anyhow::Result { - let entries = std::fs::read_dir(package_dir) - .with_context(|| format!("read {}", package_dir.display()))? - .map(|entry| entry.map(|e| e.file_name())) - .collect::, _>>()?; - Ok(entries.is_empty() || entries == ["data"]) -} - /// 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 { @@ -1048,14 +1105,22 @@ fn list_installed() { }; let mut pkgs: Vec<(String, String, String)> = Vec::new(); for entry in entries.flatten() { - let Ok(m) = Manifest::read(&entry.path()) else { + // Only an app's active package, named after the ID its manifest + // declares, is installed; staging copies, revisions and stray + // directories or links are not. + let Some(name) = entry.file_name().to_str().map(str::to_owned) else { continue; }; - // Only a directory named after the ID its manifest declares is an - // installed package; staging copies and stray directories are not. - if entry.file_name().to_str() != Some(m.package.id.as_str()) - || !weft_ipc_types::package::is_valid_app_id(&m.package.id) - { + if !weft_ipc_types::package::is_valid_app_id(&name) { + continue; + } + let Ok(Some(found)) = active(&root, &name) else { + continue; + }; + let Ok(m) = Manifest::read(found.dir()) else { + continue; + }; + if m.package.id != name { continue; } if seen.insert(m.package.id.clone()) { @@ -1649,6 +1714,145 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } + #[test] + fn an_update_keeps_app_data_from_a_package_directory() { + let home = temp_root("legacy_data"); + let app_id = "org.weft.test.legacydata"; + let src = home.join("src"); + write_revision(&src, app_id, "new"); + // In the user store, the data an earlier weft-appd kept in the + // package directory moves to the app data directory. + let store = home.join(".local/share/weft/apps"); + write_package(&store.join(app_id), app_id, APP_DATA); + std::fs::create_dir_all(store.join(app_id).join("data")).unwrap(); + std::fs::write(store.join(app_id).join("data/notes.txt"), "kept").unwrap(); + weft_ipc_types::trust::write_owner( + &weft_ipc_types::trust::owner_record_path(&home.join("share"), app_id), + Owner::Development, + ) + .unwrap(); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + 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(), + "kept" + ); + assert_eq!(active_ui(&store, app_id), "new"); + + // Elsewhere nothing claims to know what it is: the update stops. + let other = home.join("other-store"); + write_package(&other.join(app_id), app_id, APP_DATA); + std::fs::create_dir_all(other.join(app_id).join("data")).unwrap(); + std::fs::write(other.join(app_id).join("data/notes.txt"), "here").unwrap(); + assert!( + with_home(&home, || install_package_to( + &src, + &other, + InstallMode::Development + )) + .is_err() + ); + assert_eq!( + std::fs::read_to_string(other.join(app_id).join("data/notes.txt")).unwrap(), + "here" + ); + assert!(matches!( + active(&other, app_id).unwrap(), + Some(Active::Directory(_)) + )); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn rollback_refuses_a_revision_whose_key_was_withdrawn() { + let home = temp_root("rollback_revoked"); + let store = home.join("store"); + let app_id = "org.weft.test.revoked"; + let src = home.join("src"); + let signing = key(&home, "publisher", 9, true); + for marker in ["one", "two"] { + write_revision(&src, app_id, marker); + sign_package(&src, &signing).unwrap(); + with_trust(&home, || { + install_package_to(&src, &store, InstallMode::Verified) + }) + .unwrap(); + } + std::fs::remove_file(home.join("keys/publisher.pub")).unwrap(); + let refused = with_trust(&home, || rollback_package(&store, app_id)); + assert!( + refused + .as_ref() + .is_err_and(|e| e.to_string().contains("no longer signed")), + "{refused:?}" + ); + assert_eq!(active_ui(&store, app_id), "two"); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn a_damaged_revision_is_not_reused() { + let home = temp_root("damaged"); + let store = home.join("store"); + let app_id = "org.weft.test.damaged"; + let src = home.join("src"); + let install = || { + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + }; + write_revision(&src, app_id, "one"); + install().unwrap(); + let one = active_dir(&store, app_id); + write_revision(&src, app_id, "two"); + install().unwrap(); + // The kept revision is damaged, as by an interrupted removal. + std::fs::remove_file(one.join("app.wasm")).unwrap(); + write_revision(&src, app_id, "one"); + install().unwrap(); + assert_eq!(active_dir(&store, app_id), one); + assert!( + one.join("app.wasm").is_file(), + "the damaged revision was reused" + ); + let _ = std::fs::remove_dir_all(&home); + } + + #[cfg(unix)] + #[test] + fn a_new_installation_does_not_inherit_a_rollback_link() { + let home = temp_root("stale_previous"); + let store = home.join("store"); + let app_id = "org.weft.test.stale"; + let src = home.join("src"); + write_revision(&src, app_id, "one"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + let one = active(&store, app_id).unwrap().unwrap(); + let Active::Revision { name, .. } = one else { + panic!() + }; + with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); + // An uninstall interrupted before it removed the rollback link, with + // the revision still held by a session. + let revisions = weft_ipc_types::store::revisions_of(&store, app_id); + std::fs::create_dir_all(revisions.join(&name)).unwrap(); + std::os::unix::fs::symlink(&name, revisions.join(PREVIOUS_LINK)).unwrap(); + write_revision(&src, app_id, "two"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + assert!(with_home(&home, || rollback_package(&store, app_id)).is_err()); + assert_eq!(active_ui(&store, app_id), "two"); + let _ = std::fs::remove_dir_all(&home); + } + #[test] fn leftovers_of_interrupted_operations_are_removed() { let home = temp_root("leftovers"); diff --git a/docs/architecture.md b/docs/architecture.md index c11ee41..d4220d3 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -119,13 +119,15 @@ Package store roots (in priority order): The package root must be a directory and its manifest a regular file, neither a symbolic link, and the manifest must declare the requested ID. `[runtime].module` and `[ui].entry` must be relative paths of plain components, without symbolic links, naming regular files inside that root. The component, the UI document and the capabilities all come from that one manifest. A refused launch reports 400 (invalid ID), 404 (not installed), 403 (invalid package, image or image metadata) or 500 (the host cannot read the package or mount the image). -A directory install can still be changed by its owner while a session runs; for a verified owner, appd checks its content against the signature at launch (see security.md). +A directory install can still be changed by its owner while a session runs; for a verified owner, appd checks its content against the signature at launch, with the limits security.md describes. ### Installing and updating -`weft-pack install` copies the package into a staging directory in the store and checks, verifies and admits that copy, so the bytes that are checked are the bytes that are installed. The copy is flushed to disk and renamed to an immutable revision named by its content digest, `/.revisions//`; installing the same content again reuses it. One rename of the link `/` then makes it active. An installation that fails, or is interrupted at any point, leaves the previously active revision active, or, for a first installation, nothing installed and no owner record created by it. A package directory from before revisions is replaced in one exchange with the new link, only while no session runs from it. +`weft-pack install` copies the package into a staging directory in the store and checks, verifies and admits that copy, so the bytes that are checked are the bytes that are installed. The copy is flushed to disk and renamed to an immutable revision named by its content digest, `/.revisions//`; installing the same content again reuses it. One rename of the link `/` then makes it active. The rename of the link is the commit point. An installation that fails before it leaves the previously active revision active, or, for a first installation, nothing installed; a failed first installation also removes the owner record it created, while one interrupted by a crash leaves that record until `weft-pack uninstall ` releases it. After the commit point the update stands, and what an interruption leaves to tidy is collected later. A revision is reused only when it still holds the same content and signature file. A package directory from before revisions is replaced in one exchange with the new link, only while no session runs from it and only after the app data an earlier weft-appd kept in its `data` directory has been moved to the app data directory, under the same rules as for uninstall; otherwise the update stops and changes nothing. -The revision active before an update is kept for `weft-pack rollback `, which makes it active again, keeping the revision it replaces in turn; it is checked against the owner record again, so a revision whose publisher key was withdrawn is not rolled back to. Other revisions are removed by each install, rollback or uninstall of the app, except those a session still runs from; those stay until a later operation on the app finds them unused. Temporary links of interrupted activations are removed then too, and staging, unpacking and removal directories once they are an hour old. `weft-pack uninstall` removes the link, so new launches no longer find the app, and the kept revision; running sessions continue on their revisions. App data is never touched by any of these operations. Package revisions and app data are independent: rolling back a package does not roll back its data. +The revision active before an update is kept for `weft-pack rollback `, which makes it active again, keeping the revision it replaces in turn; it is checked against the owner record again, so a revision whose publisher key was withdrawn is not rolled back to. Other revisions are removed by each install, rollback or uninstall of the app, except those a session still runs from; those stay until a later operation on the app finds them unused. Temporary links of interrupted activations are removed then too, and staging, unpacking and removal directories once they are an hour old. `weft-pack uninstall` removes the link, so new launches no longer find the app, and the kept revision; running sessions continue on their revisions. Apart from that move of data from the earlier layout, app data is never touched by these operations. Package revisions and app data are independent: rolling back a package does not roll back its data. + +A launch is refused (500, try again) while a package operation holds the revision it would run from, rather than waiting for it. Only sessions started by this weft-appd hold their revisions: a session left from an earlier version, or processes that outlive weft-appd, do not, and a package directory from before revisions such sessions run from can be replaced under them. Package operations on one app are serialised by the owner lock in the data home of the user running them; the store itself is not locked against users with other data homes. ## App data From a65fb89662bbdcdb769e7ffecbd3865fd0eef9ab Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 17:52:38 +0000 Subject: [PATCH 4/5] fix(pack): replace a differing revision of the same name in one exchange Installing the same content with another signature file gives the same revision name. The existing revision, possibly the active one, was removed before the new copy was renamed into place, so a failure in between left the store link pointing at nothing. The new copy now replaces it in one exchange. If an activation fails before its switch, the kept rollback link is restored, and dropping a stale rollback link is flushed. The staging claim is released once the copy is placed, so launches are not refused while the operation finishes. A launch now passes over a store whose entry for the app is unusable, as the listing does. --- crates/weft-appd/src/launch.rs | 28 +++++--- crates/weft-pack/src/main.rs | 116 +++++++++++++++++++++++++++------ docs/architecture.md | 2 +- 3 files changed, 117 insertions(+), 29 deletions(-) diff --git a/crates/weft-appd/src/launch.rs b/crates/weft-appd/src/launch.rs index 5641798..1303055 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -71,12 +71,25 @@ fn resolve_in(app_id: &str, stores: &[PathBuf]) -> Result Refusal::new(403, e.to_string()), - weft_ipc_types::store::StoreError::Io(..) => Refusal::new(500, e.to_string()), - })?; - let Some(active) = active else { continue }; + let active = match weft_ipc_types::store::active(store, app_id) { + Ok(Some(active)) => active, + Ok(None) => continue, + Err(e) => { + tracing::warn!(app_id, error = %e, "package entry skipped"); + unusable.get_or_insert(match e { + weft_ipc_types::store::StoreError::NotAPackage(_) => { + Refusal::new(403, e.to_string()) + } + weft_ipc_types::store::StoreError::Io(..) => Refusal::new(500, e.to_string()), + }); + continue; + } + }; let dir = active.dir().to_path_buf(); // A directory left with only app data by an earlier layout is not a // package. @@ -96,10 +109,7 @@ fn resolve_in(app_id: &str, stores: &[PathBuf]) -> Result) -> Result { diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index e7d0857..aa4eaaf 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -397,9 +397,9 @@ fn install_dir( .and_then(|()| { // Held while the copy is in use, so the collection of another // operation never takes it for abandoned. - let _in_use = weft_ipc_types::store::try_claim(&staging) + let in_use = weft_ipc_types::store::try_claim(&staging) .with_context(|| format!("open {}", staging.display()))?; - install_staged(&staging, store_root, mode, claim_data) + install_staged(&staging, store_root, mode, claim_data, in_use) }); if result.is_err() { let _ = std::fs::remove_dir_all(&staging); @@ -407,11 +407,15 @@ fn install_dir( result } +/// Installs the staged copy. `in_use` holds the copy against collection by +/// other operations until it is placed; it is released then, since the copy +/// has become the revision sessions launch from. fn install_staged( staging: &Path, store_root: &Path, mode: InstallMode, claim_data: bool, + in_use: Option, ) -> anyhow::Result<()> { check_package(staging)?; let manifest = load_manifest(staging)?; @@ -445,6 +449,7 @@ fn install_staged( let admission = admit(staging, app_id, mode, &data_home, claim_data)?; let owner = admission.owner; let revision = place(staging, store_root, app_id, admission)?; + drop(in_use); drop(legacy); collect(store_root, app_id); println!( @@ -512,17 +517,28 @@ fn place( let _ = std::fs::remove_dir_all(staging); return Ok(revision); } + // The revision may be the active one, as when the same content is + // installed again with another signature file, so it is replaced + // in one exchange: the name always holds a complete revision. let claim = weft_ipc_types::store::try_claim(&dir) .with_context(|| format!("open {}", dir.display()))? .with_context(|| { - format!("{} is in use and differs; try again later", dir.display()) + format!( + "{} differs from this package but a running session uses it; close \ + the app and install again", + dir.display() + ) })?; - remove_claimed(store_root, &dir); + exchange(staging, &dir) + .with_context(|| format!("replace {} with {}", dir.display(), staging.display()))?; + // The earlier copy now has the staging name. + remove_claimed(store_root, staging); drop(claim); + } else { + weft_ipc_types::package::rename_no_replace(staging, &dir) + .with_context(|| format!("move {} -> {}", staging.display(), dir.display()))?; + created_revision = Some(dir.clone()); } - weft_ipc_types::package::rename_no_replace(staging, &dir) - .with_context(|| format!("move {} -> {}", staging.display(), dir.display()))?; - created_revision = Some(dir.clone()); sync_dir(&revisions)?; sync_dir(&store_root.join(weft_ipc_types::store::REVISIONS_DIR))?; Ok(revision) @@ -554,29 +570,44 @@ fn activate(store_root: &Path, app_id: &str, revision: &str) -> anyhow::Result<( use weft_ipc_types::store::{link_target, revisions_of}; let revisions = revisions_of(store_root, app_id); let current = active(store_root, app_id)?; - match ¤t { + let dest = store_root.join(app_id); + let link = temporary_link(store_root, app_id, &link_target(app_id, revision))?; + let kept_before = std::fs::read_link(revisions.join(PREVIOUS_LINK)).ok(); + let kept = match ¤t { Some(Active::Revision { name, .. }) if name != revision => { - replace_link(&revisions, PREVIOUS_LINK, Path::new(name))?; + replace_link(&revisions, PREVIOUS_LINK, Path::new(name)) } - Some(Active::Revision { .. }) => {} + Some(Active::Revision { .. }) => Ok(()), // Nothing earlier of this installation to roll back to; a link left // by an earlier, interrupted uninstall is dropped. _ => match std::fs::remove_file(revisions.join(PREVIOUS_LINK)) { Err(e) if e.kind() != std::io::ErrorKind::NotFound => { - return Err(e).context("remove the stale rollback link"); + Err(e).context("remove the stale rollback link") } - _ => {} + _ => sync_dir(&revisions), }, - } - let dest = store_root.join(app_id); - let link = temporary_link(store_root, app_id, &link_target(app_id, revision))?; - let switched = match ¤t { - Some(Active::Directory(_)) => exchange(&link, &dest), - _ => std::fs::rename(&link, &dest), }; + let switched = kept.and_then(|()| { + match ¤t { + Some(Active::Directory(_)) => exchange(&link, &dest), + _ => std::fs::rename(&link, &dest), + } + .with_context(|| format!("activate {}", dest.display())) + }); if let Err(e) = switched { let _ = std::fs::remove_file(&link); - return Err(e).with_context(|| format!("activate {}", dest.display())); + // The revision kept for rollback stays what it was. + if let Some(Active::Revision { name, .. }) = ¤t + && name != revision + { + let _ = match &kept_before { + Some(target) => replace_link(&revisions, PREVIOUS_LINK, target), + None => { + std::fs::remove_file(revisions.join(PREVIOUS_LINK)).map_err(anyhow::Error::from) + } + }; + } + return Err(e); } if let Err(e) = sync_dir(store_root) { eprintln!("{e:#}"); @@ -1821,6 +1852,53 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } + #[test] + fn the_same_content_with_another_signature_replaces_the_active_revision_in_place() { + let home = temp_root("resigned"); + let store = home.join("store"); + let app_id = "org.weft.test.resigned"; + let src = home.join("src"); + let install = || { + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + }; + write_revision(&src, app_id, "one"); + install().unwrap(); + let revision = active_dir(&store, app_id); + sign_package(&src, &key(&home, "dev", 3, false)).unwrap(); + // A session runs from the unsigned copy: it is not replaced under it. + let session = weft_ipc_types::store::pin(&revision).unwrap(); + assert!(install().is_err()); + assert!( + !revision + .join(weft_ipc_types::trust::SIGNATURE_FILE) + .exists() + ); + drop(session); + install().unwrap(); + assert_eq!( + active_dir(&store, app_id), + revision, + "same content, same revision" + ); + assert!( + revision + .join(weft_ipc_types::trust::SIGNATURE_FILE) + .is_file() + ); + assert_eq!(active_ui(&store, app_id), "one"); + assert!(!staging_left(&store)); + let leftovers: Vec<_> = std::fs::read_dir(&store) + .unwrap() + .flatten() + .map(|e| e.file_name()) + .filter(|n| n.to_string_lossy().starts_with(".trash-")) + .collect(); + assert!(leftovers.is_empty(), "{leftovers:?}"); + let _ = std::fs::remove_dir_all(&home); + } + #[cfg(unix)] #[test] fn a_new_installation_does_not_inherit_a_rollback_link() { diff --git a/docs/architecture.md b/docs/architecture.md index d4220d3..02b277c 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -123,7 +123,7 @@ A directory install can still be changed by its owner while a session runs; for ### Installing and updating -`weft-pack install` copies the package into a staging directory in the store and checks, verifies and admits that copy, so the bytes that are checked are the bytes that are installed. The copy is flushed to disk and renamed to an immutable revision named by its content digest, `/.revisions//`; installing the same content again reuses it. One rename of the link `/` then makes it active. The rename of the link is the commit point. An installation that fails before it leaves the previously active revision active, or, for a first installation, nothing installed; a failed first installation also removes the owner record it created, while one interrupted by a crash leaves that record until `weft-pack uninstall ` releases it. After the commit point the update stands, and what an interruption leaves to tidy is collected later. A revision is reused only when it still holds the same content and signature file. A package directory from before revisions is replaced in one exchange with the new link, only while no session runs from it and only after the app data an earlier weft-appd kept in its `data` directory has been moved to the app data directory, under the same rules as for uninstall; otherwise the update stops and changes nothing. +`weft-pack install` copies the package into a staging directory in the store and checks, verifies and admits that copy, so the bytes that are checked are the bytes that are installed. The copy is flushed to disk and renamed to an immutable revision named by its content digest, `/.revisions//`; installing the same content again reuses it. One rename of the link `/` then makes it active. The rename of the link is the commit point. An installation that fails before it leaves the previously active revision active, or, for a first installation, nothing installed; a failed first installation also removes the owner record it created, while one interrupted by a crash leaves that record until `weft-pack uninstall ` releases it. After the commit point the update stands, and what an interruption leaves to tidy is collected later. A revision is reused only when it still holds the same content and signature file; otherwise the new copy replaces it in one exchange, unless a running session uses it. A package directory from before revisions is replaced in one exchange with the new link, only while no session runs from it and only after the app data an earlier weft-appd kept in its `data` directory has been moved to the app data directory, under the same rules as for uninstall; otherwise the update stops before replacing anything. Data moved this way stays in the app data directory even if the update is then refused. The revision active before an update is kept for `weft-pack rollback `, which makes it active again, keeping the revision it replaces in turn; it is checked against the owner record again, so a revision whose publisher key was withdrawn is not rolled back to. Other revisions are removed by each install, rollback or uninstall of the app, except those a session still runs from; those stay until a later operation on the app finds them unused. Temporary links of interrupted activations are removed then too, and staging, unpacking and removal directories once they are an hour old. `weft-pack uninstall` removes the link, so new launches no longer find the app, and the kept revision; running sessions continue on their revisions. Apart from that move of data from the earlier layout, app data is never touched by these operations. Package revisions and app data are independent: rolling back a package does not roll back its data. From d76d4c726d93fb7d02ece6b6295f18a26520c993 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 17:56:27 +0000 Subject: [PATCH 5/5] test(appd): check that an unusable store entry does not hide a later store The test also covers the refusal reported when no other store has the app. The new comment states that a package found and then refused is not replaced by another store's copy. The architecture doc says that a re-signed copy of the same content is visible at once. --- crates/weft-appd/src/launch.rs | 27 ++++++++++++++++++++++++++- docs/architecture.md | 2 +- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/crates/weft-appd/src/launch.rs b/crates/weft-appd/src/launch.rs index 1303055..0d929d5 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -98,7 +98,9 @@ fn resolve_in(app_id: &str, stores: &[PathBuf]) -> Result/.revisions//`; installing the same content again reuses it. One rename of the link `/` then makes it active. The rename of the link is the commit point. An installation that fails before it leaves the previously active revision active, or, for a first installation, nothing installed; a failed first installation also removes the owner record it created, while one interrupted by a crash leaves that record until `weft-pack uninstall ` releases it. After the commit point the update stands, and what an interruption leaves to tidy is collected later. A revision is reused only when it still holds the same content and signature file; otherwise the new copy replaces it in one exchange, unless a running session uses it. A package directory from before revisions is replaced in one exchange with the new link, only while no session runs from it and only after the app data an earlier weft-appd kept in its `data` directory has been moved to the app data directory, under the same rules as for uninstall; otherwise the update stops before replacing anything. Data moved this way stays in the app data directory even if the update is then refused. +`weft-pack install` copies the package into a staging directory in the store and checks, verifies and admits that copy, so the bytes that are checked are the bytes that are installed. The copy is flushed to disk and renamed to an immutable revision named by its content digest, `/.revisions//`; installing the same content again reuses it. One rename of the link `/` then makes it active. The rename of the link is the commit point. An installation that fails before it leaves the previously active revision active, or, for a first installation, nothing installed; a failed first installation also removes the owner record it created, while one interrupted by a crash leaves that record until `weft-pack uninstall ` releases it. After the commit point the update stands, and what an interruption leaves to tidy is collected later. A revision is reused only when it still holds the same content and signature file; otherwise the new copy replaces it in one exchange, unless a running session uses it. Such a replacement, for the same content with another signature file, is visible under the revision's name at once, even if the installation then fails. A package directory from before revisions is replaced in one exchange with the new link, only while no session runs from it and only after the app data an earlier weft-appd kept in its `data` directory has been moved to the app data directory, under the same rules as for uninstall; otherwise the update stops before replacing anything. Data moved this way stays in the app data directory even if the update is then refused. The revision active before an update is kept for `weft-pack rollback `, which makes it active again, keeping the revision it replaces in turn; it is checked against the owner record again, so a revision whose publisher key was withdrawn is not rolled back to. Other revisions are removed by each install, rollback or uninstall of the app, except those a session still runs from; those stay until a later operation on the app finds them unused. Temporary links of interrupted activations are removed then too, and staging, unpacking and removal directories once they are an hour old. `weft-pack uninstall` removes the link, so new launches no longer find the app, and the kept revision; running sessions continue on their revisions. Apart from that move of data from the earlier layout, app data is never touched by these operations. Package revisions and app data are independent: rolling back a package does not roll back its data.