Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dir: Add an is_mountpoint API #56

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cap-primitives = "3"

[target.'cfg(not(windows))'.dependencies]
rustix = { version = "0.38", features = ["fs", "procfs", "process", "pipe"] }
libc = "0.2"

[dev-dependencies]
anyhow = "1.0"
Expand Down
34 changes: 34 additions & 0 deletions src/dirext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ pub trait CapStdExtDirExt {
contents: impl AsRef<[u8]>,
perms: cap_std::fs::Permissions,
) -> Result<()>;

#[cfg(any(target_os = "android", target_os = "linux"))]
/// Returns `Some(true)` if the target is known to be a mountpoint, or
/// `Some(false)` if the target is definitively known not to be a mountpoint.
///
/// In some scenarios (such as an older kernel) this currently may not be possible
/// to determine, and `None` will be returned in those cases.
fn is_mountpoint(&self, path: impl AsRef<Path>) -> Result<Option<bool>>;
}

#[cfg(feature = "fs_utf8")]
Expand Down Expand Up @@ -300,6 +308,28 @@ fn subdir_of<'d, 'p>(d: &'d Dir, p: &'p Path) -> io::Result<(DirOwnedOrBorrowed<
Ok((r, name))
}

fn is_mountpoint_impl_statx(root: &Dir, path: &Path) -> Result<Option<bool>> {
// https://github.com/systemd/systemd/blob/8fbf0a214e2fe474655b17a4b663122943b55db0/src/basic/mountpoint-util.c#L176
use rustix::fs::{AtFlags, StatxFlags};
use std::os::fd::AsFd;

// SAFETY(unwrap): We can infallibly convert an i32 into a u64.
let mountroot_flag: u64 = libc::STATX_ATTR_MOUNT_ROOT.try_into().unwrap();
match rustix::fs::statx(
root.as_fd(),
path,
AtFlags::NO_AUTOMOUNT | AtFlags::SYMLINK_NOFOLLOW,
StatxFlags::empty(),
) {
Ok(r) => {
let present = (r.stx_attributes_mask & mountroot_flag) > 0;
Ok(present.then_some(r.stx_attributes & mountroot_flag > 0))
}
Err(e) if e == rustix::io::Errno::NOSYS => Ok(None),
Err(e) => Err(e.into()),
}
}

impl CapStdExtDirExt for Dir {
fn open_optional(&self, path: impl AsRef<Path>) -> Result<Option<File>> {
map_optional(self.open(path.as_ref()))
Expand Down Expand Up @@ -452,6 +482,10 @@ impl CapStdExtDirExt for Dir {
Ok(())
})
}

fn is_mountpoint(&self, path: impl AsRef<Path>) -> Result<Option<bool>> {
is_mountpoint_impl_statx(self, path.as_ref()).map_err(Into::into)
}
}

// Implementation for the Utf8 variant of Dir. You shouldn't need to add
Expand Down
9 changes: 9 additions & 0 deletions tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,12 @@ fn test_rootdir_entries() -> Result<()> {
assert_eq!(ents.len(), 2);
Ok(())
}

#[test]
fn test_mountpoint() -> Result<()> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

let root = &Dir::open_ambient_dir("/", cap_std::ambient_authority())?;
assert_eq!(root.is_mountpoint(".").unwrap(), Some(true));
let td = &cap_tempfile::TempDir::new(cap_std::ambient_authority())?;
assert_eq!(td.is_mountpoint(".").unwrap(), Some(false));
Ok(())
}
Loading