rustix::fs

Function statx

Source
pub fn statx<P: Arg, Fd: AsFd>(
    dirfd: Fd,
    path: P,
    flags: AtFlags,
    mask: StatxFlags,
) -> Result<Statx>
Expand description

statx(dirfd, path, flags, mask, statxbuf)—Extended stat.

This function returns io::Errno::NOSYS if statx is not available on the platform, such as Linux before 4.11. This also includes older Docker versions where the actual syscall fails with different error codes; rustix handles this and translates them into NOSYS.

§References

§Examples

/// Try to determine if the provided path is a mount root. Will return
/// `Ok(None)` if the kernel is not new enough to support `statx` or
/// [`StatxAttributes::MOUNT_ROOT`].
fn is_mountpoint(root: BorrowedFd<'_>, path: &Path) -> io::Result<Option<bool>> {
    use rustix::fs::{AtFlags, StatxAttributes, StatxFlags};

    match rustix::fs::statx(
        root,
        path,
        AtFlags::NO_AUTOMOUNT | AtFlags::SYMLINK_NOFOLLOW,
        StatxFlags::empty(),
    ) {
        Ok(r) => {
            let present = r.stx_attributes_mask.contains(StatxAttributes::MOUNT_ROOT);
            Ok(present.then(|| r.stx_attributes.contains(StatxAttributes::MOUNT_ROOT)))
        }
        Err(rustix::io::Errno::NOSYS) => Ok(None),
        Err(e) => Err(e.into()),
    }
}