Make mount_commit() responsible for not only mounting the commit tree, but also for opening the source subdirectory and creating the destination subdirectory. Signed-off-by: Demi Marie Obenour <demiobenour@gmail.com> --- tools/mount-flatpak/src/main.rs | 82 ++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/tools/mount-flatpak/src/main.rs b/tools/mount-flatpak/src/main.rs index 68a1f6ea308387a9471682c927d3a71439ab69e4..b40402b1b15729bdd2228025850efe8d87a48e3b 100644 --- a/tools/mount-flatpak/src/main.rs +++ b/tools/mount-flatpak/src/main.rs @@ -43,7 +43,7 @@ mod metadata; use std::borrow::Cow; use std::env::{ArgsOs, args_os}; -use std::ffi::OsStr; +use std::ffi::{OsStr, OsString}; use std::io; use std::os::unix::prelude::*; use std::path::{Path, PathBuf}; @@ -67,12 +67,21 @@ fn open_subdir(root: &Root, path: &Path) -> Result<Root, Error> { } fn mount_commit( - source_commit: &dyn AsFd, + source_installation: &Root, + source_path: &Path, target_installation: &Root, - path: &Path, -) -> Result<(), String> { + target_path: &Path, + kind: &str, +) -> Result<(Root, OsString), String> { + let source_commit_parent_dir = open_subdir(source_installation, source_path) + .map_err(|e| format!("opening source {kind} commit parent: {e}"))?; + let commit = source_commit_parent_dir + .readlink("active") + .map_err(|e| format!("reading active {kind} commit: {e}"))?; + let source_root = open_subdir(&source_commit_parent_dir, &commit) + .map_err(|e| format!("opening source {kind} commit: {e}"))?; let source_commit_tree = open_tree( - source_commit, + &source_root, "", OpenTreeFlags::AT_EMPTY_PATH | OpenTreeFlags::OPEN_TREE_CLONE @@ -81,7 +90,7 @@ fn mount_commit( ) .map_err(|e| format!("cloning source commit tree: {e}"))?; let target_commit_dir = target_installation - .mkdir_all(path, &PermissionsExt::from_mode(0o700)) + .mkdir_all(target_path, &PermissionsExt::from_mode(0o700)) .map_err(|e| format!("creating target commit directory: {e}"))?; move_mount( source_commit_tree, @@ -90,7 +99,8 @@ fn mount_commit( "", MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH | MoveMountFlags::MOVE_MOUNT_T_EMPTY_PATH, ) - .map_err(|e| format!("mounting commit: {e}")) + .map_err(|e| format!("mounting commit: {e}"))?; + Ok((source_root, commit.as_os_str().to_owned())) } fn run(mut args: ArgsOs) -> Result<(), String> { @@ -129,11 +139,13 @@ fn run(mut args: ArgsOs) -> Result<(), String> { let target_installation_dir = Root::from_fd(target_installation_dir).with_resolver_flags(ResolverFlags::NO_SYMLINKS); - let mut full_app_path = installation_path.join("app"); - full_app_path.push(&app); - full_app_path.push("current"); - let arch_and_branch = source_installation_dir - .readlink(&full_app_path) + let mut app_path = PathBuf::new(); + app_path.push("app"); + app_path.push(&app); + let source_app_dir = open_subdir(&source_installation_dir, &app_path) + .map_err(|e| format!("opening source flatpak app: {e}"))?; + let arch_and_branch = source_app_dir + .readlink("current") .map_err(|e| format!("reading current app arch and branch: {e}"))?; let mut components = arch_and_branch.components(); let arch = components.next().unwrap().as_os_str(); @@ -142,21 +154,16 @@ fn run(mut args: ArgsOs) -> Result<(), String> { return Err("can't infer branch from \"current\" link".to_string()); } - full_app_path.pop(); - full_app_path.push(&arch_and_branch); - full_app_path.push("active"); - let commit = source_installation_dir - .readlink(&full_app_path) - .map_err(|e| format!("reading active app commit: {e}"))? - .into_os_string(); - - full_app_path.pop(); - full_app_path.push(&commit); - let source_app_dir = open_subdir(&source_installation_dir, &full_app_path) - .map_err(|e| format!("opening source app directory: {e}"))?; + let (source_commit_dir, commit) = mount_commit( + &source_app_dir, + &app_path.join(&arch_and_branch), + &target_installation_dir, + &arch_and_branch, + "app", + )?; - let metadata = source_installation_dir - .resolve(&full_app_path.join("metadata")) + let metadata = source_commit_dir + .resolve("metadata") .map_err(|e| format!("resolving app metadata: {e}"))?; let metadata_stat = @@ -172,25 +179,14 @@ fn run(mut args: ArgsOs) -> Result<(), String> { let runtime = extract_runtime(metadata).map_err(|e| format!("reading runtime from metadata: {e}"))?; + let runtime_path = Path::new("runtime").join(runtime); - let mut full_runtime_path = installation_path.join("runtime"); - full_runtime_path.push(runtime); - full_runtime_path.push("active"); - let runtime_commit = source_installation_dir - .readlink(&full_runtime_path) - .map_err(|e| format!("reading active runtime commit: {e}"))? - .into_os_string(); - - full_runtime_path.pop(); - full_runtime_path.push(&runtime_commit); - let source_runtime_dir = open_subdir(&source_installation_dir, &full_runtime_path) - .map_err(|e| format!("opening source runtime directory: {e}"))?; - - mount_commit(&source_app_dir, &target_installation_dir, &full_app_path)?; - mount_commit( - &source_runtime_dir, + let (_, runtime_commit) = mount_commit( + &source_installation_dir, + &runtime_path, &target_installation_dir, - &full_runtime_path, + &runtime_path, + "runtime", )?; target_installation_dir -- 2.52.0