Skip to content

Commit 41063ba

Browse files
authored
statx: Add an example of using this to determine mountpoint (#831)
- This use case is a specific reason to directly use the `statx` system call - It's a bit of a subtle API because there's 4 different flags involved, and it's not necessarily obvious that the relevant bitmasks aren't (yet) exposed in rustix, so one needs to use libc/linux-raw-sys directly - On the general principle of adding examples
1 parent 6556fd9 commit 41063ba

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/fs/statx.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,35 @@ use compat::statx as _statx;
2121
/// # References
2222
/// - [Linux]
2323
///
24+
/// # Examples
25+
///
26+
/// ```
27+
/// # use std::path::Path;
28+
/// # use std::io;
29+
/// # use rustix::fs::{AtFlags, StatxFlags};
30+
/// # use rustix::fd::BorrowedFd;
31+
/// /// Try to determine if the provided path is a mount root. Will return `Ok(None)` if
32+
/// /// the kernel is not new enough to support statx() or [`libc::STATX_ATTR_MOUNT_ROOT`].
33+
/// fn is_mountpoint(root: BorrowedFd<'_>, path: &Path) -> io::Result<Option<bool>> {
34+
/// use rustix::fs::{AtFlags, StatxFlags};
35+
///
36+
/// let mountroot_flag = libc::STATX_ATTR_MOUNT_ROOT as u64;
37+
/// match rustix::fs::statx(
38+
/// root,
39+
/// path,
40+
/// AtFlags::NO_AUTOMOUNT | AtFlags::SYMLINK_NOFOLLOW,
41+
/// StatxFlags::empty(),
42+
/// ) {
43+
/// Ok(r) => {
44+
/// let present = (r.stx_attributes_mask & mountroot_flag) > 0;
45+
/// Ok(present.then(|| r.stx_attributes & mountroot_flag > 0))
46+
/// }
47+
/// Err(e) if e == rustix::io::Errno::NOSYS => Ok(None),
48+
/// Err(e) => Err(e.into()),
49+
/// }
50+
/// }
51+
/// ```
52+
///
2453
/// [Linux]: https://man7.org/linux/man-pages/man2/statx.2.html
2554
#[inline]
2655
pub fn statx<P: path::Arg, Fd: AsFd>(

0 commit comments

Comments
 (0)