This rejects non-directories where a directory is expected. Signed-off-by: Demi Marie Obenour <demiobenour@gmail.com> --- tools/mount-flatpak/src/main.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tools/mount-flatpak/src/main.rs b/tools/mount-flatpak/src/main.rs index dca42e5007e52b24c9a4b6940d5667f770a64bd0..68a1f6ea308387a9471682c927d3a71439ab69e4 100644 --- a/tools/mount-flatpak/src/main.rs +++ b/tools/mount-flatpak/src/main.rs @@ -49,8 +49,8 @@ use std::os::unix::prelude::*; use std::path::{Path, PathBuf}; use std::process::exit; -use pathrs::Root; use pathrs::flags::{OpenFlags, ResolverFlags}; +use pathrs::{Root, error::Error}; use rustix::fs::{CWD, FileType, fstat}; use rustix::mount::{MoveMountFlags, OpenTreeFlags, move_mount, open_tree}; @@ -61,6 +61,11 @@ fn ex_usage() -> ! { exit(1); } +fn open_subdir(root: &Root, path: &Path) -> Result<Root, Error> { + root.open_subpath(path, OpenFlags::O_PATH | OpenFlags::O_DIRECTORY) + .map(|f| Root::from_fd(f).with_resolver_flags(ResolverFlags::NO_SYMLINKS)) +} + fn mount_commit( source_commit: &dyn AsFd, target_installation: &Root, @@ -106,11 +111,8 @@ fn run(mut args: ArgsOs) -> Result<(), String> { .map_err(|e| format!("opening user data partition: {e}"))? .with_resolver_flags(ResolverFlags::NO_SYMLINKS); - let source_installation_dir = user_data - .open_subpath(&installation_path, OpenFlags::O_PATH) - .map(Root::from_fd) - .map_err(|e| format!("opening source flatpak installation: {e}"))? - .with_resolver_flags(ResolverFlags::NO_SYMLINKS); + let source_installation_dir = open_subdir(&user_data, &installation_path) + .map_err(|e| format!("opening source flatpak installation: {e}"))?; std::fs::create_dir("flatpak") .map_err(|e| format!("creating target flatpak installation: {e}"))?; @@ -150,12 +152,11 @@ fn run(mut args: ArgsOs) -> Result<(), String> { full_app_path.pop(); full_app_path.push(&commit); - let source_app_dir = source_installation_dir - .resolve(&full_app_path) + let source_app_dir = open_subdir(&source_installation_dir, &full_app_path) .map_err(|e| format!("opening source app directory: {e}"))?; let metadata = source_installation_dir - .resolve(full_app_path.join("metadata")) + .resolve(&full_app_path.join("metadata")) .map_err(|e| format!("resolving app metadata: {e}"))?; let metadata_stat = @@ -182,8 +183,7 @@ fn run(mut args: ArgsOs) -> Result<(), String> { full_runtime_path.pop(); full_runtime_path.push(&runtime_commit); - let source_runtime_dir = source_installation_dir - .resolve(&full_runtime_path) + 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)?; -- 2.52.0