fix(appd): move only real app data and never replace a directory

weft-pack treated any top-level data directory in a package as app
data from the earlier layout, in every store and for every app: a
package shipping data/ lost that content to the app data directory on
uninstall and could then no longer be uninstalled. Data is now moved
only from ~/.local/share/weft/apps/<id>/data, the only place earlier
versions wrote it, and only for packages declaring an app-data
capability; packages may no longer contain a top-level data entry.

The move left an empty package directory behind, which then blocked
weft-pack install although nothing was listed; an emptied package
directory is now removed, and install accepts an empty leftover. The
rename uses RENAME_NOREPLACE, so a target created after the check is
reported as a conflict instead of being replaced, and a move across
filesystems is refused with a message naming both locations instead of
a bare OS error. Directories moved by weft-pack are made private too,
and a symbolic link at the target is refused rather than followed.

The system UI now shows launch refusals, which it only logged, and the
weft-appd unit may write under ~/.local/share/weft, which
ProtectSystem=strict made read-only. check_app_data.py also types into
Notes after the move and requires the save to reach the moved file, and
checks that no empty package directory remains.
This commit is contained in:
Marco Allegretti 2026-10-09 08:52:09 +00:00
parent 4b27d7f16c
commit a508f1ebe4
No known key found for this signature in database
9 changed files with 338 additions and 129 deletions

1
Cargo.lock generated
View file

@ -10522,6 +10522,7 @@ name = "weft-ipc-types"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"rmp-serde", "rmp-serde",
"rustix 1.1.4",
"serde", "serde",
] ]

View file

@ -37,8 +37,8 @@ pub(crate) struct SessionGrants {
/// Why a launch is refused, with the error code reported to the client: /// Why a launch is refused, with the error code reported to the client:
/// 400 for a malformed app ID, 404 for a package that is not installed, 403 /// 400 for a malformed app ID, 404 for a package that is not installed, 403
/// for a package this host will not run as declared and 500 for a host /// for a package this host will not run as declared, 409 for app data the
/// fault. /// user must reconcile and 500 for a host fault.
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub(crate) struct Refusal { pub(crate) struct Refusal {
pub code: u32, pub code: u32,
@ -76,8 +76,9 @@ impl HostDirs {
.map(PathBuf::from) .map(PathBuf::from)
.filter(|dir| dir.is_absolute()) .filter(|dir| dir.is_absolute())
.unwrap_or_else(|| home.join(".config")); .unwrap_or_else(|| home.join(".config"));
let data_home = weft_ipc_types::package::data_home() let data_home = weft_ipc_types::package::data_home().ok_or_else(|| {
.ok_or_else(|| Refusal::new(500, "no data home: HOME is not set"))?; Refusal::new(500, "no data home: XDG_DATA_HOME and HOME are unusable")
})?;
Ok(Self { Ok(Self {
data_home, data_home,
documents: documents_dir(&home, &config), documents: documents_dir(&home, &config),
@ -171,51 +172,28 @@ pub(crate) fn derive(
} }
/// Creates the app's data directory, first moving data an earlier version /// Creates the app's data directory, first moving data an earlier version
/// kept inside the package store. A conflict between the two locations /// kept inside the user package store, and makes it private. A conflict
/// refuses the launch (409) and leaves both untouched. /// between the two locations refuses the launch (409) and leaves both
/// untouched.
fn prepare_app_data(app_id: &str, dir: &Path, host: &HostDirs) -> Result<(), Refusal> { fn prepare_app_data(app_id: &str, dir: &Path, host: &HostDirs) -> Result<(), Refusal> {
use weft_ipc_types::package::{Migration, legacy_app_data_dir, migrate_app_data}; use weft_ipc_types::package::{
Migration, MigrationError, legacy_app_data_dir, migrate_app_data,
};
let legacy = legacy_app_data_dir(&host.home, app_id); let legacy = legacy_app_data_dir(&host.home, app_id);
match migrate_app_data(&legacy, dir) { match migrate_app_data(&legacy, dir) {
Ok(Migration::Moved) => { Ok(Migration::Moved) => {
tracing::info!(%app_id, from = %legacy.display(), to = %dir.display(), "app data moved"); tracing::info!(%app_id, from = %legacy.display(), to = %dir.display(), "app data moved");
} }
Ok(Migration::NotNeeded) => {} Ok(Migration::NotNeeded) => {}
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { Err(e @ MigrationError::Conflict { .. }) => return Err(Refusal::new(409, e.to_string())),
return Err(Refusal::new(409, e.to_string())); Err(e) => return Err(Refusal::new(500, e.to_string())),
}
Err(e) => {
return Err(Refusal::new(
500,
format!("cannot move app data from {}: {e}", legacy.display()),
));
}
}
create_private_dir(dir)
.map_err(|e| Refusal::new(500, format!("cannot create {}: {e}", dir.display())))
}
/// Creates `dir` and missing parents, and makes the directory itself
/// accessible only to the user, including when it already existed or was
/// moved from the earlier layout.
fn create_private_dir(dir: &Path) -> std::io::Result<()> {
if let Some(parent) = dir.parent() {
std::fs::create_dir_all(parent)?;
} }
let mut builder = std::fs::DirBuilder::new(); let mut builder = std::fs::DirBuilder::new();
#[cfg(unix)] builder.recursive(true);
std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700); builder
match builder.create(dir) { .create(dir)
Ok(()) => {} .and_then(|()| weft_ipc_types::package::make_private(dir))
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists && dir.is_dir() => {} .map_err(|e| Refusal::new(500, format!("cannot prepare {}: {e}", dir.display())))
Err(e) => return Err(e),
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700))?;
}
Ok(())
} }
/// Adds a directory grant; declaring both modes for one directory grants the /// Adds a directory grant; declaring both modes for one directory grants the

View file

@ -14,3 +14,6 @@ rmp-serde = "1"
[lints] [lints]
workspace = true workspace = true
[target.'cfg(target_os = "linux")'.dependencies]
rustix = { version = "1", features = ["fs"] }

View file

@ -54,40 +54,129 @@ pub enum Migration {
Moved, Moved,
} }
/// Moves `legacy` to `target` if `legacy` is a directory, in one rename so /// Why data in the earlier layout could not be moved. Nothing was changed.
/// the data is never partly in both places. Refuses, leaving both untouched, #[derive(Debug)]
/// when `target` already exists, since either copy may hold the user's pub enum MigrationError {
/// latest data, and when `legacy` is a symbolic link. /// Data exists in both locations; either may hold the user's latest
pub fn migrate_app_data(legacy: &Path, target: &Path) -> std::io::Result<Migration> { /// data, so the user has to choose.
use std::io::{Error, ErrorKind}; Conflict {
let metadata = match std::fs::symlink_metadata(legacy) { legacy: PathBuf,
Ok(metadata) => metadata, target: PathBuf,
Err(e) if e.kind() == ErrorKind::NotFound => return Ok(Migration::NotNeeded), },
Err(e) => return Err(e), /// The two locations are on different filesystems, so the data cannot
}; /// be moved in one step.
if !metadata.is_dir() { CrossDevice {
return Err(Error::other(format!( legacy: PathBuf,
"{} is not a directory; move it aside to continue", target: PathBuf,
legacy.display() },
))); /// The earlier location is not a plain directory.
NotADirectory(PathBuf),
Io(std::io::Error),
} }
if std::fs::symlink_metadata(target).is_ok() {
return Err(Error::new( impl std::fmt::Display for MigrationError {
ErrorKind::AlreadyExists, fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
format!( match self {
"app data exists both in {} and in {}; keep one of them", Self::Conflict { legacy, target } => write!(
f,
"app data exists both in {} and in {}; keep one of them and remove the other",
legacy.display(), legacy.display(),
target.display() target.display()
), ),
)); Self::CrossDevice { legacy, target } => write!(
f,
"app data in {} cannot be moved to {} on another filesystem; move it there by hand",
legacy.display(),
target.display()
),
Self::NotADirectory(path) => write!(
f,
"{} is not a plain directory; move it aside to continue",
path.display()
),
Self::Io(e) => e.fmt(f),
}
}
}
impl std::error::Error for MigrationError {}
/// Moves `legacy` to `target` if `legacy` is a directory, in one rename so
/// the data is never partly in both places, and then removes the earlier
/// package directory if that leaves it empty. Refuses, changing nothing,
/// when `target` already exists (even empty), when `legacy` is a symbolic
/// link or not a directory, and when the rename would cross filesystems.
pub fn migrate_app_data(legacy: &Path, target: &Path) -> Result<Migration, MigrationError> {
let conflict = || MigrationError::Conflict {
legacy: legacy.to_path_buf(),
target: target.to_path_buf(),
};
let metadata = match std::fs::symlink_metadata(legacy) {
Ok(metadata) => metadata,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Migration::NotNeeded),
Err(e) => return Err(MigrationError::Io(e)),
};
if !metadata.is_dir() {
return Err(MigrationError::NotADirectory(legacy.to_path_buf()));
}
if std::fs::symlink_metadata(target).is_ok() {
return Err(conflict());
} }
if let Some(parent) = target.parent() { if let Some(parent) = target.parent() {
std::fs::create_dir_all(parent)?; std::fs::create_dir_all(parent).map_err(MigrationError::Io)?;
}
match rename_no_replace(legacy, target) {
Ok(()) => {}
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => return Err(conflict()),
Err(e) if e.kind() == std::io::ErrorKind::CrossesDevices => {
return Err(MigrationError::CrossDevice {
legacy: legacy.to_path_buf(),
target: target.to_path_buf(),
});
}
Err(e) => return Err(MigrationError::Io(e)),
}
if let Some(package_dir) = legacy.parent() {
// Fails, harmlessly, unless the directory is now empty.
let _ = std::fs::remove_dir(package_dir);
} }
std::fs::rename(legacy, target)?;
Ok(Migration::Moved) Ok(Migration::Moved)
} }
/// Renames `from` to `to`, failing with `AlreadyExists` instead of replacing
/// an existing `to`, even one created after the caller checked for it.
#[cfg(target_os = "linux")]
fn rename_no_replace(from: &Path, to: &Path) -> std::io::Result<()> {
use rustix::fs::{CWD, RenameFlags, renameat_with};
renameat_with(CWD, from, CWD, to, RenameFlags::NOREPLACE).map_err(std::io::Error::from)
}
#[cfg(not(target_os = "linux"))]
fn rename_no_replace(from: &Path, to: &Path) -> std::io::Result<()> {
if std::fs::symlink_metadata(to).is_ok() {
return Err(std::io::ErrorKind::AlreadyExists.into());
}
std::fs::rename(from, to)
}
/// Makes `dir` accessible only to its owner. A symbolic link is refused
/// rather than followed.
pub fn make_private(dir: &Path) -> std::io::Result<()> {
let metadata = std::fs::symlink_metadata(dir)?;
if !metadata.is_dir() {
return Err(std::io::Error::other(format!(
"{} is not a plain directory",
dir.display()
)));
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700))?;
}
Ok(())
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -131,6 +220,16 @@ mod tests {
); );
} }
#[test]
fn migration_keeps_a_package_directory_with_other_content() {
let root = temp("keep-package");
let legacy = root.join("apps/org.weft.app/data");
std::fs::create_dir_all(&legacy).unwrap();
std::fs::write(root.join("apps/org.weft.app/wapp.toml"), "").unwrap();
migrate_app_data(&legacy, &root.join("new")).unwrap();
assert!(root.join("apps/org.weft.app/wapp.toml").exists());
}
#[test] #[test]
fn migration_moves_data_intact() { fn migration_moves_data_intact() {
let root = temp("move"); let root = temp("move");
@ -166,7 +265,14 @@ mod tests {
std::fs::write(legacy.join("a"), "old").unwrap(); std::fs::write(legacy.join("a"), "old").unwrap();
std::fs::write(target.join("a"), "new").unwrap(); std::fs::write(target.join("a"), "new").unwrap();
let err = migrate_app_data(&legacy, &target).unwrap_err(); let err = migrate_app_data(&legacy, &target).unwrap_err();
assert_eq!(err.kind(), std::io::ErrorKind::AlreadyExists); assert!(matches!(err, MigrationError::Conflict { .. }));
// An empty target is a conflict too: it may be in use.
std::fs::remove_file(target.join("a")).unwrap();
assert!(matches!(
migrate_app_data(&legacy, &target),
Err(MigrationError::Conflict { .. })
));
std::fs::write(target.join("a"), "new").unwrap();
assert_eq!(std::fs::read_to_string(legacy.join("a")).unwrap(), "old"); assert_eq!(std::fs::read_to_string(legacy.join("a")).unwrap(), "old");
assert_eq!(std::fs::read_to_string(target.join("a")).unwrap(), "new"); assert_eq!(std::fs::read_to_string(target.join("a")).unwrap(), "new");
} }
@ -177,7 +283,10 @@ mod tests {
let root = temp("link"); let root = temp("link");
std::fs::create_dir_all(root.join("elsewhere")).unwrap(); std::fs::create_dir_all(root.join("elsewhere")).unwrap();
std::os::unix::fs::symlink(root.join("elsewhere"), root.join("old")).unwrap(); std::os::unix::fs::symlink(root.join("elsewhere"), root.join("old")).unwrap();
assert!(migrate_app_data(&root.join("old"), &root.join("new")).is_err()); assert!(matches!(
migrate_app_data(&root.join("old"), &root.join("new")),
Err(MigrationError::NotADirectory(_))
));
assert!(!root.join("new").exists()); assert!(!root.join("new").exists());
} }
} }

View file

@ -154,6 +154,12 @@ fn check_package(dir: &Path) -> anyhow::Result<String> {
} }
}; };
// Earlier versions kept app data in `<package>/data`; the name stays
// reserved so package content is never mistaken for user data.
if std::fs::symlink_metadata(dir.join("data")).is_ok() {
errors.push("a top-level 'data' entry is reserved and cannot be part of a package".into());
}
if let Some(ref m) = manifest { if let Some(ref m) = manifest {
if !weft_ipc_types::package::is_valid_app_id(&m.package.id) { if !weft_ipc_types::package::is_valid_app_id(&m.package.id) {
errors.push(format!( errors.push(format!(
@ -292,11 +298,14 @@ fn install_package_to(path: &Path, store_root: &Path) -> anyhow::Result<()> {
let manifest = load_manifest(dir)?; let manifest = load_manifest(dir)?;
let app_id = &manifest.package.id; let app_id = &manifest.package.id;
let dest = store_root.join(app_id); let dest = store_root.join(app_id);
// An earlier version could leave only app data in the package directory; // An earlier weft-appd could leave only app data, or an empty directory,
// move it to its own location so it neither blocks nor joins the package. // 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 dest.is_dir() && only_holds_app_data(&dest)? {
if declares_app_data(&manifest) {
move_app_data_out(app_id, &dest)?; move_app_data_out(app_id, &dest)?;
std::fs::remove_dir(&dest).with_context(|| format!("remove {}", dest.display()))?; }
let _ = std::fs::remove_dir(&dest);
} }
if dest.exists() { if dest.exists() {
anyhow::bail!( anyhow::bail!(
@ -328,38 +337,58 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()>
target.display() target.display()
); );
} }
if load_manifest(&target).is_ok_and(|m| declares_app_data(&m)) {
move_app_data_out(app_id, &target)?; move_app_data_out(app_id, &target)?;
}
std::fs::remove_dir_all(&target).with_context(|| format!("remove {}", target.display()))?; std::fs::remove_dir_all(&target).with_context(|| format!("remove {}", target.display()))?;
println!("uninstalled {}", app_id); println!("uninstalled {}", app_id);
Ok(()) Ok(())
} }
/// Whether a package directory contains nothing but a `data` directory. /// Whether a package directory is empty or contains nothing but `data`.
fn only_holds_app_data(package_dir: &Path) -> anyhow::Result<bool> { fn only_holds_app_data(package_dir: &Path) -> anyhow::Result<bool> {
let mut entries = std::fs::read_dir(package_dir) let entries = std::fs::read_dir(package_dir)
.with_context(|| format!("read {}", package_dir.display()))? .with_context(|| format!("read {}", package_dir.display()))?
.map(|entry| entry.map(|e| e.file_name())) .map(|entry| entry.map(|e| e.file_name()))
.collect::<Result<Vec<_>, _>>()?; .collect::<Result<Vec<_>, _>>()?;
entries.sort(); Ok(entries.is_empty() || entries == ["data"])
Ok(entries == ["data"])
} }
/// Moves app data that an earlier version kept in `<package_dir>/data` to /// Whether the manifest declares an app-data capability; only such apps
/// the app's data directory, which package operations never remove. Fails, /// had data kept in the package directory.
/// changing nothing, if the data cannot be moved. fn declares_app_data(manifest: &Manifest) -> bool {
use weft_ipc_types::capability::Capability;
manifest
.package
.capabilities
.iter()
.flatten()
.any(|c| matches!(c.parse(), Ok(Capability::AppData(_))))
}
/// Moves app data that an earlier weft-appd kept in `<package_dir>/data` to
/// the app's data directory, which package operations never remove. Earlier
/// versions only did so in the user package store (`~/.local/share/weft/apps`),
/// so other stores are left alone. Fails, changing nothing, if the data
/// cannot be moved.
fn move_app_data_out(app_id: &str, package_dir: &Path) -> anyhow::Result<()> { fn move_app_data_out(app_id: &str, package_dir: &Path) -> anyhow::Result<()> {
use weft_ipc_types::package::{Migration, app_data_dir, data_home, migrate_app_data}; use weft_ipc_types::package::{
Migration, app_data_dir, data_home, legacy_app_data_dir, make_private, migrate_app_data,
};
let Some(home) = std::env::var_os("HOME").map(PathBuf::from) else {
return Ok(());
};
let legacy = package_dir.join("data"); let legacy = package_dir.join("data");
if std::fs::symlink_metadata(&legacy).is_err() { if legacy != legacy_app_data_dir(&home, app_id) {
return Ok(()); return Ok(());
} }
let home = data_home().context("cannot locate the data home to keep app data")?; let data_home = data_home().context("cannot locate the data home to keep app data")?;
let target = app_data_dir(&home, app_id); let target = app_data_dir(&data_home, app_id);
match migrate_app_data(&legacy, &target) if migrate_app_data(&legacy, &target).with_context(|| format!("keep app data of {app_id}"))?
.with_context(|| format!("keep app data from {}", legacy.display()))? == Migration::Moved
{ {
Migration::Moved => println!("app data kept at {}", target.display()), make_private(&target).with_context(|| format!("restrict {}", target.display()))?;
Migration::NotNeeded => {} println!("app data kept at {}", target.display());
} }
Ok(()) Ok(())
} }
@ -638,83 +667,142 @@ mod tests {
ENV_LOCK.lock().unwrap_or_else(|p| p.into_inner()) ENV_LOCK.lock().unwrap_or_else(|p| p.into_inner())
} }
/// A minimal valid package for `app_id` in `src`. /// A minimal valid package for `app_id` in `src` declaring `capabilities`.
fn write_package(src: &Path, app_id: &str) { fn write_package(src: &Path, app_id: &str, capabilities: &str) {
std::fs::create_dir_all(src.join("ui")).unwrap(); std::fs::create_dir_all(src.join("ui")).unwrap();
std::fs::write(src.join("app.wasm"), b"\0asm").unwrap(); std::fs::write(src.join("app.wasm"), b"\0asm").unwrap();
std::fs::write(src.join("ui/index.html"), b"").unwrap(); std::fs::write(src.join("ui/index.html"), b"").unwrap();
std::fs::write( std::fs::write(
src.join("wapp.toml"), src.join("wapp.toml"),
format!( format!(
"[package]\nid = \"{app_id}\"\nname = \"D\"\nversion = \"1.0.0\"\n\n\ "[package]\nid = \"{app_id}\"\nname = \"D\"\nversion = \"1.0.0\"\n\
capabilities = [{capabilities}]\n\n\
[runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n" [runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n"
), ),
) )
.unwrap(); .unwrap();
} }
/// Runs `f` with XDG_DATA_HOME set to `data_home`. /// Runs `f` with HOME set to `home` and XDG_DATA_HOME to `home/share`.
fn with_data_home<T>(data_home: &Path, f: impl FnOnce() -> T) -> T { fn with_home<T>(home: &Path, f: impl FnOnce() -> T) -> T {
let _env = env_lock(); let _env = env_lock();
let prior = std::env::var_os("XDG_DATA_HOME"); let prior: Vec<_> = ["HOME", "XDG_DATA_HOME"]
.iter()
.map(|k| (*k, std::env::var_os(k)))
.collect();
// SAFETY: env_lock serialises every test that touches the environment. // SAFETY: env_lock serialises every test that touches the environment.
unsafe { std::env::set_var("XDG_DATA_HOME", data_home) };
let result = f();
unsafe { unsafe {
match prior { std::env::set_var("HOME", home);
Some(v) => std::env::set_var("XDG_DATA_HOME", v), std::env::set_var("XDG_DATA_HOME", home.join("share"));
None => std::env::remove_var("XDG_DATA_HOME"), }
let result = f();
for (key, value) in prior {
unsafe {
match value {
Some(v) => std::env::set_var(key, v),
None => std::env::remove_var(key),
}
} }
} }
result result
} }
#[test] fn temp_root(name: &str) -> PathBuf {
fn uninstall_keeps_app_data_from_the_package_directory() { let root = std::env::temp_dir().join(format!("weft_pack_{name}_{}", std::process::id()));
let root = std::env::temp_dir().join(format!("weft_pack_keep_{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root); let _ = std::fs::remove_dir_all(&root);
root
}
const APP_DATA: &str = "\"fs:rw:app-data\"";
#[test]
fn uninstall_keeps_app_data_from_the_user_store() {
let home = temp_root("keep");
let app_id = "com.example.keep"; let app_id = "com.example.keep";
write_package(&root.join("src"), app_id); write_package(&home.join("src"), app_id, APP_DATA);
let store = root.join("store"); let store = home.join(".local/share/weft/apps");
install_package_to(&root.join("src"), &store).unwrap(); install_package_to(&home.join("src"), &store).unwrap();
let legacy = store.join(app_id).join("data"); let legacy = store.join(app_id).join("data");
std::fs::create_dir_all(&legacy).unwrap(); std::fs::create_dir_all(&legacy).unwrap();
std::fs::write(legacy.join("notes.txt"), "first\nsecond").unwrap(); std::fs::write(legacy.join("notes.txt"), "first\nsecond").unwrap();
let data_home = root.join("share"); with_home(&home, || uninstall_package_from(app_id, &store)).unwrap();
with_data_home(&data_home, || uninstall_package_from(app_id, &store)).unwrap();
assert!(!store.join(app_id).exists()); assert!(!store.join(app_id).exists());
let kept = weft_ipc_types::package::app_data_dir(&data_home, app_id); let kept = weft_ipc_types::package::app_data_dir(&home.join("share"), app_id);
assert_eq!( assert_eq!(
std::fs::read_to_string(kept.join("notes.txt")).unwrap(), std::fs::read_to_string(kept.join("notes.txt")).unwrap(),
"first\nsecond" "first\nsecond"
); );
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mode = std::fs::metadata(&kept).unwrap().permissions().mode();
assert_eq!(mode & 0o777, 0o700);
}
// A second copy cannot be merged: uninstall refuses and removes nothing. // A second copy cannot be merged: uninstall refuses and removes nothing.
install_package_to(&root.join("src"), &store).unwrap(); install_package_to(&home.join("src"), &store).unwrap();
std::fs::create_dir_all(&legacy).unwrap(); std::fs::create_dir_all(&legacy).unwrap();
assert!(with_data_home(&data_home, || uninstall_package_from(app_id, &store)).is_err()); assert!(with_home(&home, || uninstall_package_from(app_id, &store)).is_err());
assert!(legacy.is_dir() && kept.is_dir()); assert!(legacy.is_dir() && kept.is_dir());
let _ = std::fs::remove_dir_all(&home);
}
#[test]
fn other_stores_and_apps_without_data_capability_are_left_alone() {
let home = temp_root("alone");
let app_id = "com.example.alone";
// No app-data capability: a data directory is not app data.
write_package(&home.join("src"), app_id, "");
let store = home.join(".local/share/weft/apps");
install_package_to(&home.join("src"), &store).unwrap();
std::fs::create_dir_all(store.join(app_id).join("data")).unwrap();
with_home(&home, || uninstall_package_from(app_id, &store)).unwrap();
assert!(!home.join("share").exists());
// Another store: earlier versions never kept data there.
write_package(&home.join("src2"), app_id, APP_DATA);
let other = home.join("other-store");
install_package_to(&home.join("src2"), &other).unwrap();
std::fs::create_dir_all(other.join(app_id).join("data")).unwrap();
with_home(&home, || uninstall_package_from(app_id, &other)).unwrap();
assert!(!home.join("share").exists());
let _ = std::fs::remove_dir_all(&home);
}
#[test]
fn packages_cannot_ship_a_top_level_data_entry() {
let root = temp_root("ships-data");
let src = root.join("src");
write_package(&src, "com.example.ships", "");
std::fs::create_dir_all(src.join("data")).unwrap();
std::fs::write(src.join("data/asset.json"), "{}").unwrap();
let err = check_package(&src).unwrap_err().to_string();
assert!(err.contains("'data'"), "{err}");
assert!(install_package_to(&src, &root.join("store")).is_err());
let _ = std::fs::remove_dir_all(&root); let _ = std::fs::remove_dir_all(&root);
} }
#[test] #[test]
fn install_moves_leftover_app_data_out_of_the_way() { fn install_moves_leftover_app_data_out_of_the_way() {
let root = std::env::temp_dir().join(format!("weft_pack_leftover_{}", std::process::id())); let home = temp_root("leftover");
let _ = std::fs::remove_dir_all(&root);
let app_id = "com.example.leftover"; let app_id = "com.example.leftover";
write_package(&root.join("src"), app_id); write_package(&home.join("src"), app_id, APP_DATA);
let store = root.join("store"); let store = home.join(".local/share/weft/apps");
std::fs::create_dir_all(store.join(app_id).join("data")).unwrap(); std::fs::create_dir_all(store.join(app_id).join("data")).unwrap();
std::fs::write(store.join(app_id).join("data/x"), "x").unwrap(); std::fs::write(store.join(app_id).join("data/x"), "x").unwrap();
let data_home = root.join("share"); with_home(&home, || install_package_to(&home.join("src"), &store)).unwrap();
with_data_home(&data_home, || install_package_to(&root.join("src"), &store)).unwrap();
assert!(store.join(app_id).join("wapp.toml").exists()); assert!(store.join(app_id).join("wapp.toml").exists());
assert!(!store.join(app_id).join("data").exists()); assert!(!store.join(app_id).join("data").exists());
let kept = weft_ipc_types::package::app_data_dir(&data_home, app_id); let kept = weft_ipc_types::package::app_data_dir(&home.join("share"), app_id);
assert_eq!(std::fs::read_to_string(kept.join("x")).unwrap(), "x"); assert_eq!(std::fs::read_to_string(kept.join("x")).unwrap(), "x");
let _ = std::fs::remove_dir_all(&root);
// An empty directory left behind does not block installation either.
with_home(&home, || uninstall_package_from(app_id, &store)).unwrap();
std::fs::create_dir_all(store.join(app_id)).unwrap();
with_home(&home, || install_package_to(&home.join("src"), &store)).unwrap();
let _ = std::fs::remove_dir_all(&home);
} }
#[test] #[test]

View file

@ -112,9 +112,14 @@ Package store roots (in priority order):
## App data ## App data
Each app's private data lives in `$XDG_DATA_HOME/weft/app-data/<id>` (`~/.local/share/weft/app-data/<id>` by default), apart from installed packages. Installing, updating or uninstalling a package never removes it. `weft-appd` creates the directory when a session with an app-data capability starts and makes it accessible only to the user (mode 0700). Each app's private data lives in `$XDG_DATA_HOME/weft/app-data/<id>` (`~/.local/share/weft/app-data/<id>` by default), apart from installed packages. Installing, updating or uninstalling a package never removes it. `weft-appd` creates the directory when a session with an app-data capability starts and makes it accessible only to the user (mode 0700). `weft-appd` and `weft-pack` must run with the same `HOME` and `XDG_DATA_HOME` to agree on this location; the systemd unit allows `weft-appd` to write under `~/.local/share/weft`.
Earlier versions kept app data inside the user package store, in `~/.local/share/weft/apps/<id>/data`. The first launch with an app-data capability moves that directory to the new location in a single rename. `weft-pack uninstall` and `weft-pack install` move it the same way before touching the package directory. If data exists in both locations, nothing is moved: the launch is refused with error 409 and `weft-pack` stops, so the user can choose which copy to keep. `tests/frame/check_app_data.py` checks that existing Notes data arrives byte for byte and that a conflict is refused. Earlier versions kept app data inside the user package store, in `~/.local/share/weft/apps/<id>/data`, for apps declaring an app-data capability. That directory is moved to the new location in a single rename, which never replaces an existing directory:
- by `weft-appd`, on the first launch with an app-data capability;
- by `weft-pack uninstall` and `weft-pack install`, before they touch the package directory, for packages in the user store that declare an app-data capability.
A package directory left empty by the move is removed. Nothing is moved, and `weft-pack` stops or the launch is refused, when the new location already exists, even empty (error 409), when the old one is not a plain directory, or when the two are on different filesystems (error 500); the message names both locations so the user can keep one copy and remove the other. The system UI shows such refusals. Packages cannot contain a top-level `data` entry, so package content is never taken for app data. `tests/frame/check_app_data.py` checks that existing Notes data arrives byte for byte, that the Notes session then reads and saves it, and that a conflict is refused.
## Environment Variables ## Environment Variables

View file

@ -336,7 +336,10 @@
}, },
})); }));
} else if (msg.type === 'ERROR') { } else if (msg.type === 'ERROR') {
// Launch refusals (missing package, capability or data conflict)
// must reach the user; the launcher otherwise looks unresponsive.
console.warn('appd error', msg.code, msg.message); console.warn('appd error', msg.code, msg.message);
showNotification('Cannot open: ' + msg.message);
} }
} }

View file

@ -16,7 +16,8 @@ Restart=on-failure
RestartSec=1s RestartSec=1s
ProtectSystem=strict ProtectSystem=strict
PrivateTmp=true PrivateTmp=true
ReadWritePaths=%t # App data and the user package store, where earlier versions kept app data.
ReadWritePaths=%t -%h/.local/share/weft
[Install] [Install]
WantedBy=graphical.target WantedBy=graphical.target

View file

@ -7,7 +7,10 @@ through byte for byte), runs weft-appd on a nested desktop and launches
`org.weft.demo.notes` with the real runtime and app shell. The session must `org.weft.demo.notes` with the real runtime and app shell. The session must
reach APP_READY, the file must then be at reach APP_READY, the file must then be at
`$XDG_DATA_HOME/weft/app-data/org.weft.demo.notes/notes.txt` with identical `$XDG_DATA_HOME/weft/app-data/org.weft.demo.notes/notes.txt` with identical
bytes, the old directory must be gone and the new one private (0700). bytes, the old directory and the emptied package directory must be gone,
and the new one private (0700). Typing at the end of the note and pressing
Ctrl+S must then store the moved text plus the typed text, which shows that
the session reads and writes the moved directory.
A second launch after placing a different file at the old location must be A second launch after placing a different file at the old location must be
refused with error 409, leaving both copies untouched. refused with error 409, leaving both copies untouched.
@ -17,19 +20,22 @@ Requires Xvfb, xwd, ImageMagick's convert and xdotool.
import argparse import argparse
import shutil import shutil
import subprocess
import sys import sys
import tempfile import tempfile
import time
from pathlib import Path from pathlib import Path
sys.dont_write_bytecode = True sys.dont_write_bytecode = True
from check_counter import AppdClient, free_port, read_endpoint # noqa: E402 from check_counter import AppdClient, free_port, read_endpoint # noqa: E402
from check_notes import wait_for_file # noqa: E402
from session import ROOT, Desktop, SessionError, missing_tools # noqa: E402 from session import ROOT, Desktop, SessionError, missing_tools # noqa: E402
APP_ID = "org.weft.demo.notes" APP_ID = "org.weft.demo.notes"
NOTES = "first line\nliteral \\n stays\ncafé ☃\n".encode() NOTES = "first line\nliteral \\n stays\ncafé ☃\n".encode()
def run(args, desktop, home): def run(args, desktop, home, xdotool):
legacy = home / ".local/share/weft/apps" / APP_ID / "data" legacy = home / ".local/share/weft/apps" / APP_ID / "data"
legacy.mkdir(parents=True) legacy.mkdir(parents=True)
(legacy / "notes.txt").write_bytes(NOTES) (legacy / "notes.txt").write_bytes(NOTES)
@ -52,13 +58,26 @@ def run(args, desktop, home):
reply = appd.wait_for(lambda m: m.get("type") in ("APP_READY", "ERROR"), 120) reply = appd.wait_for(lambda m: m.get("type") in ("APP_READY", "ERROR"), 120)
if reply.get("type") != "APP_READY": if reply.get("type") != "APP_READY":
raise AssertionError(f"Notes did not start: {reply}") raise AssertionError(f"Notes did not start: {reply}")
if (target / "notes.txt").read_bytes() != NOTES: if not (target / "notes.txt").is_file() or (target / "notes.txt").read_bytes() != NOTES:
raise AssertionError("notes changed while moving") raise AssertionError("notes did not arrive unchanged")
if legacy.exists(): if legacy.parent.exists():
raise AssertionError(f"{legacy} still exists") raise AssertionError(f"{legacy.parent} still exists")
if target.stat().st_mode & 0o777 != 0o700: if target.stat().st_mode & 0o777 != 0o700:
raise AssertionError(f"{target} is not private: {oct(target.stat().st_mode)}") raise AssertionError(f"{target} is not private: {oct(target.stat().st_mode)}")
def act(*arguments):
subprocess.run([xdotool, *arguments], env={"DISPLAY": desktop.display},
check=True, timeout=30)
time.sleep(2)
act("search", "--class", "weft-compositor", "windowfocus", "--sync")
act("mousemove", "--sync", "100", "300")
act("click", "1")
act("key", "ctrl+End")
act("type", "--delay", "40", "ok")
act("key", "ctrl+s")
wait_for_file(target / "notes.txt", NOTES.decode() + "ok")
appd.send({"type": "TERMINATE_APP", "session_id": reply["session_id"]}) appd.send({"type": "TERMINATE_APP", "session_id": reply["session_id"]})
legacy.mkdir(parents=True) legacy.mkdir(parents=True)
(legacy / "notes.txt").write_bytes(b"other copy") (legacy / "notes.txt").write_bytes(b"other copy")
@ -67,7 +86,7 @@ def run(args, desktop, home):
if reply.get("type") != "ERROR" or reply.get("code") != 409: if reply.get("type") != "ERROR" or reply.get("code") != 409:
raise AssertionError(f"conflicting data was not refused: {reply}") raise AssertionError(f"conflicting data was not refused: {reply}")
if (legacy / "notes.txt").read_bytes() != b"other copy" or \ if (legacy / "notes.txt").read_bytes() != b"other copy" or \
(target / "notes.txt").read_bytes() != NOTES: (target / "notes.txt").read_bytes() != NOTES + b"ok":
raise AssertionError("a refused launch changed app data") raise AssertionError("a refused launch changed app data")
@ -82,7 +101,9 @@ def main(argv=None):
parser.add_argument("--output", type=Path, default=ROOT / "target/app-data-check") parser.add_argument("--output", type=Path, default=ROOT / "target/app-data-check")
args = parser.parse_args(argv) args = parser.parse_args(argv)
missing = missing_tools() + [str(p) for p in (args.compositor, args.target / "weft-appd", xdotool = shutil.which("xdotool")
missing = missing_tools() + ([] if xdotool else ["xdotool"])
missing += [str(p) for p in (args.compositor, args.target / "weft-appd",
args.target / "weft-runtime", args.target / "weft-runtime",
args.target / "weft-app-shell") args.target / "weft-app-shell")
if not p.is_file()] if not p.is_file()]
@ -92,7 +113,7 @@ def main(argv=None):
home = Path(tempfile.mkdtemp(prefix="weft-home-")) home = Path(tempfile.mkdtemp(prefix="weft-home-"))
try: try:
with Desktop(args.compositor, args.output, outputs=["appd.log"]) as desktop: with Desktop(args.compositor, args.output, outputs=["appd.log"]) as desktop:
run(args, desktop, home) run(args, desktop, home, xdotool)
except (AssertionError, SessionError, TimeoutError) as failure: except (AssertionError, SessionError, TimeoutError) as failure:
print(f"app data check failed: {failure}", file=sys.stderr) print(f"app data check failed: {failure}", file=sys.stderr)
print(f"logs in {args.output}", file=sys.stderr) print(f"logs in {args.output}", file=sys.stderr)