This repository has been archived by the owner on Nov 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #551 from cgwalters/add-mountpoint-util-check
lib: Add mountutil module
- Loading branch information
Showing
3 changed files
with
56 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//! Helpers for interacting with mounts. | ||
|
||
use std::os::fd::AsFd; | ||
use std::path::Path; | ||
|
||
use anyhow::Result; | ||
use cap_std::fs::Dir; | ||
use cap_std_ext::cap_std; | ||
|
||
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}; | ||
|
||
// 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(|| r.stx_attributes & mountroot_flag > 0)) | ||
} | ||
Err(e) if e == rustix::io::Errno::NOSYS => Ok(None), | ||
Err(e) => Err(e.into()), | ||
} | ||
} | ||
|
||
/// Try to (heuristically) determine if the provided path is a mount root. | ||
pub fn is_mountpoint(root: &Dir, path: impl AsRef<Path>) -> Result<Option<bool>> { | ||
is_mountpoint_impl_statx(root, path.as_ref()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use cap_std_ext::cap_tempfile; | ||
|
||
#[test] | ||
fn test_is_mountpoint() -> Result<()> { | ||
let root = cap_std::fs::Dir::open_ambient_dir("/", cap_std::ambient_authority())?; | ||
let supported = is_mountpoint(&root, Path::new("/")).unwrap(); | ||
match supported { | ||
Some(r) => assert!(r), | ||
// If the host doesn't support statx, ignore this for now | ||
None => return Ok(()), | ||
} | ||
let tmpdir = cap_tempfile::TempDir::new(cap_std::ambient_authority())?; | ||
assert!(!is_mountpoint(&tmpdir, Path::new(".")).unwrap().unwrap()); | ||
Ok(()) | ||
} | ||
} |