Skip to content

Commit eb23dc6

Browse files
committed
mount: Add cwd parameter to run_findmnt and inspect_filesystem_of_dir helper
Add a `cwd: Option<&Dir>` parameter to `run_findmnt()` to support running findmnt with a working directory context. This allows inspecting filesystems using a Dir fd rather than requiring absolute paths. Also add `inspect_filesystem_of_dir()` as a convenience wrapper that inspects the filesystem of a Dir by calling findmnt on "." with the directory as cwd. Assisted-by: Claude Code (Sonnet 4.5) Signed-off-by: Colin Walters <[email protected]>
1 parent 30b2627 commit eb23dc6

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

crates/mount/src/mount.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::{
1010
use anyhow::{anyhow, Context, Result};
1111
use bootc_utils::CommandRunExt;
1212
use camino::Utf8Path;
13+
use cap_std_ext::{cap_std::fs::Dir, cmdext::CapStdExtCommandExt};
1314
use fn_error_context::context;
1415
use rustix::{
1516
mount::{MoveMountFlags, OpenTreeFlags},
@@ -52,8 +53,11 @@ pub struct Findmnt {
5253
pub filesystems: Vec<Filesystem>,
5354
}
5455

55-
pub fn run_findmnt(args: &[&str], path: Option<&str>) -> Result<Findmnt> {
56+
pub fn run_findmnt(args: &[&str], cwd: Option<&Dir>, path: Option<&str>) -> Result<Findmnt> {
5657
let mut cmd = Command::new("findmnt");
58+
if let Some(cwd) = cwd {
59+
cmd.cwd_dir(cwd.try_clone()?);
60+
}
5761
cmd.args([
5862
"-J",
5963
"-v",
@@ -67,8 +71,8 @@ pub fn run_findmnt(args: &[&str], path: Option<&str>) -> Result<Findmnt> {
6771
}
6872

6973
// Retrieve a mounted filesystem from a device given a matching path
70-
fn findmnt_filesystem(args: &[&str], path: &str) -> Result<Filesystem> {
71-
let o = run_findmnt(args, Some(path))?;
74+
fn findmnt_filesystem(args: &[&str], cwd: Option<&Dir>, path: &str) -> Result<Filesystem> {
75+
let o = run_findmnt(args, cwd, Some(path))?;
7276
o.filesystems
7377
.into_iter()
7478
.next()
@@ -79,19 +83,26 @@ fn findmnt_filesystem(args: &[&str], path: &str) -> Result<Filesystem> {
7983
/// Inspect a target which must be a mountpoint root - it is an error
8084
/// if the target is not the mount root.
8185
pub fn inspect_filesystem(path: &Utf8Path) -> Result<Filesystem> {
82-
findmnt_filesystem(&["--mountpoint"], path.as_str())
86+
findmnt_filesystem(&["--mountpoint"], None, path.as_str())
87+
}
88+
89+
#[context("Inspecting filesystem")]
90+
/// Inspect a target which must be a mountpoint root - it is an error
91+
/// if the target is not the mount root.
92+
pub fn inspect_filesystem_of_dir(d: &Dir) -> Result<Filesystem> {
93+
findmnt_filesystem(&["--mountpoint"], Some(d), ".")
8394
}
8495

8596
#[context("Inspecting filesystem by UUID {uuid}")]
8697
/// Inspect a filesystem by partition UUID
8798
pub fn inspect_filesystem_by_uuid(uuid: &str) -> Result<Filesystem> {
88-
findmnt_filesystem(&["--source"], &(format!("UUID={uuid}")))
99+
findmnt_filesystem(&["--source"], None, &(format!("UUID={uuid}")))
89100
}
90101

91102
// Check if a specified device contains an already mounted filesystem
92103
// in the root mount namespace
93104
pub fn is_mounted_in_pid1_mountns(path: &str) -> Result<bool> {
94-
let o = run_findmnt(&["-N"], Some("1"))?;
105+
let o = run_findmnt(&["-N"], None, Some("1"))?;
95106

96107
let mounted = o.filesystems.iter().any(|fs| is_source_mounted(path, fs));
97108

0 commit comments

Comments
 (0)