use core::ffi::c_char;
use crate::fs::linux::{
FsConfig, FsPickFlags, ListMountFlags, MountAttributes, MountFlags, MountSetAttr,
MountSetAttrFlags, MoveMountFlags, OpenTreeFlags, Propagation, StatMountFields, UnmountFlags,
};
use crate::path::with_cstr;
use crate::sys::syscall;
use crate::{BorrowedHandle, ErrorKind, OwnedHandle, Result};
const MS_RDONLY: usize = 1;
const MS_NOSUID: usize = 2;
const MS_NODEV: usize = 4;
const MS_NOEXEC: usize = 8;
const MS_SYNCHRONOUS: usize = 16;
const MS_REMOUNT: usize = 32;
const MS_DIRSYNC: usize = 128;
const MS_NOSYMFOLLOW: usize = 256;
const MS_NOATIME: usize = 1024;
const MS_NODIRATIME: usize = 2048;
const MS_BIND: usize = 4096;
const MS_MOVE: usize = 8192;
const MS_REC: usize = 16384;
const MS_SILENT: usize = 32768;
const MS_UNBINDABLE: usize = 1 << 17;
const MS_PRIVATE: usize = 1 << 18;
const MS_SLAVE: usize = 1 << 19;
const MS_SHARED: usize = 1 << 20;
const MS_RELATIME: usize = 1 << 21;
const MS_STRICTATIME: usize = 1 << 24;
const MS_LAZYTIME: usize = 1 << 25;
const MNT_FORCE: usize = 1;
const MNT_DETACH: usize = 2;
const MNT_EXPIRE: usize = 4;
const UMOUNT_NOFOLLOW: usize = 8;
fn mount_flags(flags: MountFlags) -> usize {
let table = [
(MountFlags::READ_ONLY, MS_RDONLY),
(MountFlags::NO_SUID, MS_NOSUID),
(MountFlags::NO_DEV, MS_NODEV),
(MountFlags::NO_EXEC, MS_NOEXEC),
(MountFlags::SYNCHRONOUS, MS_SYNCHRONOUS),
(MountFlags::DIR_SYNC, MS_DIRSYNC),
(MountFlags::NO_SYMLINK_FOLLOW, MS_NOSYMFOLLOW),
(MountFlags::NO_ATIME, MS_NOATIME),
(MountFlags::NO_DIR_ATIME, MS_NODIRATIME),
(MountFlags::REL_ATIME, MS_RELATIME),
(MountFlags::STRICT_ATIME, MS_STRICTATIME),
(MountFlags::LAZY_TIME, MS_LAZYTIME),
(MountFlags::SILENT, MS_SILENT),
];
let mut raw = 0;
for (flag, value) in table {
if flags.contains(flag) {
raw |= value;
}
}
raw
}
fn with_optional<R>(text: &[u8], f: impl FnOnce(*const c_char) -> Result<R>) -> Result<R> {
if text.is_empty() {
f(core::ptr::null())
} else {
with_cstr(text, |text| f(text.as_ptr()))
}
}
fn mount_raw(source: &[u8], target: &[u8], fstype: &[u8], flags: usize, data: &[u8]) -> Result<()> {
with_cstr(target, |target| {
with_optional(source, |source| {
with_optional(fstype, |fstype| {
with_optional(data, |data| {
unsafe { syscall!(MOUNT, source, target.as_ptr(), fstype, flags, data) }
.map(drop)
})
})
})
})
}
pub(crate) fn mount(
source: &[u8],
target: &[u8],
fstype: &[u8],
flags: MountFlags,
data: &[u8],
) -> Result<()> {
if fstype.is_empty() {
return Err(ErrorKind::InvalidInput.into());
}
mount_raw(source, target, fstype, mount_flags(flags), data)
}
pub(crate) fn bind(source: &[u8], target: &[u8], recursive: bool) -> Result<()> {
let flags = MS_BIND | if recursive { MS_REC } else { 0 };
mount_raw(source, target, b"", flags, b"")
}
pub(crate) fn remount(target: &[u8], flags: MountFlags, data: &[u8]) -> Result<()> {
mount_raw(b"", target, b"", MS_REMOUNT | mount_flags(flags), data)
}
pub(crate) fn remount_bind(target: &[u8], flags: MountFlags) -> Result<()> {
let flags = MS_REMOUNT | MS_BIND | mount_flags(flags);
mount_raw(b"", target, b"", flags, b"")
}
fn propagation_flag(propagation: Propagation) -> usize {
match propagation {
Propagation::Private => MS_PRIVATE,
Propagation::Slave => MS_SLAVE,
Propagation::Shared => MS_SHARED,
Propagation::Unbindable => MS_UNBINDABLE,
}
}
pub(crate) fn set_propagation(
target: &[u8],
propagation: Propagation,
recursive: bool,
) -> Result<()> {
let kind = propagation_flag(propagation);
let flags = kind | if recursive { MS_REC } else { 0 };
mount_raw(b"", target, b"", flags, b"")
}
pub(crate) fn move_mount(from: &[u8], to: &[u8]) -> Result<()> {
mount_raw(from, to, b"", MS_MOVE, b"")
}
pub(crate) fn unmount(target: &[u8], flags: UnmountFlags) -> Result<()> {
let table = [
(UnmountFlags::FORCE, MNT_FORCE),
(UnmountFlags::DETACH, MNT_DETACH),
(UnmountFlags::EXPIRE, MNT_EXPIRE),
(UnmountFlags::NO_FOLLOW, UMOUNT_NOFOLLOW),
];
let mut raw = 0;
for (flag, value) in table {
if flags.contains(flag) {
raw |= value;
}
}
with_cstr(target, |target| {
unsafe { syscall!(UMOUNT2, target.as_ptr(), raw) }.map(drop)
})
}
pub(crate) fn pivot_root(new_root: &[u8], put_old: &[u8]) -> Result<()> {
with_cstr(new_root, |new_root| {
with_cstr(put_old, |put_old| {
unsafe { syscall!(PIVOT_ROOT, new_root.as_ptr(), put_old.as_ptr()) }.map(drop)
})
})
}
pub(crate) fn chroot(path: &[u8]) -> Result<()> {
with_cstr(path, |path| {
unsafe { syscall!(CHROOT, path.as_ptr()) }.map(drop)
})
}
const AT_FDCWD: i32 = -100;
const AT_SYMLINK_NOFOLLOW: usize = 0x100;
const AT_NO_AUTOMOUNT: usize = 0x800;
const AT_EMPTY_PATH: usize = 0x1000;
const AT_RECURSIVE: usize = 0x8000;
const O_CLOEXEC: usize = 0o2000000;
const FSOPEN_CLOEXEC: usize = 1;
const FSMOUNT_CLOEXEC: usize = 1;
const FSPICK_CLOEXEC: usize = 1;
const FSPICK_SYMLINK_NOFOLLOW: usize = 2;
const FSPICK_NO_AUTOMOUNT: usize = 4;
const FSPICK_EMPTY_PATH: usize = 8;
const FSCONFIG_SET_FLAG: usize = 0;
const FSCONFIG_SET_STRING: usize = 1;
const FSCONFIG_SET_BINARY: usize = 2;
const FSCONFIG_SET_PATH: usize = 3;
const FSCONFIG_SET_PATH_EMPTY: usize = 4;
const FSCONFIG_SET_FD: usize = 5;
const FSCONFIG_CMD_CREATE: usize = 6;
const FSCONFIG_CMD_RECONFIGURE: usize = 7;
const FSCONFIG_CMD_CREATE_EXCL: usize = 8;
const OPEN_TREE_CLONE: usize = 1;
const OPEN_TREE_CLOEXEC: usize = O_CLOEXEC;
const MOVE_MOUNT_F_SYMLINKS: usize = 0x1;
const MOVE_MOUNT_F_AUTOMOUNTS: usize = 0x2;
const MOVE_MOUNT_F_EMPTY_PATH: usize = 0x4;
const MOVE_MOUNT_T_SYMLINKS: usize = 0x10;
const MOVE_MOUNT_T_AUTOMOUNTS: usize = 0x20;
const MOVE_MOUNT_T_EMPTY_PATH: usize = 0x40;
const MOVE_MOUNT_SET_GROUP: usize = 0x100;
const MOVE_MOUNT_BENEATH: usize = 0x200;
const MOUNT_ATTR_RDONLY: u64 = 0x1;
const MOUNT_ATTR_NOSUID: u64 = 0x2;
const MOUNT_ATTR_NODEV: u64 = 0x4;
const MOUNT_ATTR_NOEXEC: u64 = 0x8;
const MOUNT_ATTR_ATIME: u64 = 0x70;
const MOUNT_ATTR_NOATIME: u64 = 0x10;
const MOUNT_ATTR_STRICTATIME: u64 = 0x20;
const MOUNT_ATTR_NODIRATIME: u64 = 0x80;
const MOUNT_ATTR_IDMAP: u64 = 0x10_0000;
const MOUNT_ATTR_NOSYMFOLLOW: u64 = 0x20_0000;
#[repr(C)]
struct MountAttr {
attr_set: u64,
attr_clr: u64,
propagation: u64,
userns_fd: u64,
}
const _: () = assert!(size_of::<MountAttr>() == 32);
fn dir_fd(dir: Option<BorrowedHandle<'_>>) -> i32 {
dir.map_or(AT_FDCWD, |dir| dir.as_raw())
}
fn translate<F: Copy, R: Copy + core::ops::BitOr<Output = R>>(
zero: R,
table: &[(F, R)],
has: impl Fn(F) -> bool,
) -> R {
let mut raw = zero;
for &(flag, value) in table {
if has(flag) {
raw = raw | value;
}
}
raw
}
fn attribute_bits(attributes: MountAttributes) -> (u64, bool) {
let table = [
(MountAttributes::READ_ONLY, MOUNT_ATTR_RDONLY),
(MountAttributes::NO_SUID, MOUNT_ATTR_NOSUID),
(MountAttributes::NO_DEV, MOUNT_ATTR_NODEV),
(MountAttributes::NO_EXEC, MOUNT_ATTR_NOEXEC),
(MountAttributes::NO_ATIME, MOUNT_ATTR_NOATIME),
(MountAttributes::STRICT_ATIME, MOUNT_ATTR_STRICTATIME),
(MountAttributes::NO_DIR_ATIME, MOUNT_ATTR_NODIRATIME),
(MountAttributes::NO_SYMLINK_FOLLOW, MOUNT_ATTR_NOSYMFOLLOW),
];
let bits = translate(0, &table, |flag| attributes.contains(flag));
let times =
MountAttributes::NO_ATIME | MountAttributes::STRICT_ATIME | MountAttributes::REL_ATIME;
(bits, attributes.intersects(times))
}
fn new_handle(fd: usize) -> OwnedHandle {
unsafe { OwnedHandle::from_raw(fd as i32) }
}
pub(crate) fn fsopen(fstype: &[u8]) -> Result<OwnedHandle> {
with_cstr(fstype, |fstype| {
unsafe { syscall!(FSOPEN, fstype.as_ptr(), FSOPEN_CLOEXEC) }.map(new_handle)
})
}
pub(crate) fn fsconfig(fs: BorrowedHandle<'_>, command: &FsConfig<'_>) -> Result<()> {
let fd = fs.as_raw();
let null = core::ptr::null::<u8>();
match *command {
FsConfig::SetFlag(key) => with_cstr(key.as_bytes(), |key| {
unsafe { syscall!(FSCONFIG, fd, FSCONFIG_SET_FLAG, key.as_ptr(), null, 0) }.map(drop)
}),
FsConfig::SetString(key, value) => with_cstr(key.as_bytes(), |key| {
with_cstr(value.as_bytes(), |value| {
let (key, value) = (key.as_ptr(), value.as_ptr());
unsafe { syscall!(FSCONFIG, fd, FSCONFIG_SET_STRING, key, value, 0) }.map(drop)
})
}),
FsConfig::SetBinary(key, value) => with_cstr(key.as_bytes(), |key| {
let (key, data, len) = (key.as_ptr(), value.as_ptr(), value.len());
unsafe { syscall!(FSCONFIG, fd, FSCONFIG_SET_BINARY, key, data, len) }.map(drop)
}),
FsConfig::SetHandle(key, handle) => with_cstr(key.as_bytes(), |key| {
let (key, handle) = (key.as_ptr(), handle.as_raw());
unsafe { syscall!(FSCONFIG, fd, FSCONFIG_SET_FD, key, null, handle) }.map(drop)
}),
FsConfig::Create => {
unsafe { syscall!(FSCONFIG, fd, FSCONFIG_CMD_CREATE, null, null, 0) }.map(drop)
}
FsConfig::Reconfigure => {
unsafe { syscall!(FSCONFIG, fd, FSCONFIG_CMD_RECONFIGURE, null, null, 0) }.map(drop)
}
FsConfig::SetPath(key, dir, path) => with_cstr(key.as_bytes(), |key| {
with_cstr(path, |path| {
let (key, path, dir) = (key.as_ptr(), path.as_ptr(), dir_fd(dir));
unsafe { syscall!(FSCONFIG, fd, FSCONFIG_SET_PATH, key, path, dir) }.map(drop)
})
}),
FsConfig::SetPathEmpty(key, handle) => with_cstr(key.as_bytes(), |key| {
let (key, empty, handle) = (key.as_ptr(), c"".as_ptr(), handle.as_raw());
unsafe { syscall!(FSCONFIG, fd, FSCONFIG_SET_PATH_EMPTY, key, empty, handle) }.map(drop)
}),
FsConfig::CreateExclusive => {
unsafe { syscall!(FSCONFIG, fd, FSCONFIG_CMD_CREATE_EXCL, null, null, 0) }.map(drop)
}
}
}
pub(crate) fn fsmount(fs: BorrowedHandle<'_>, attributes: MountAttributes) -> Result<OwnedHandle> {
let (attributes, _) = attribute_bits(attributes);
unsafe { syscall!(FSMOUNT, fs.as_raw(), FSMOUNT_CLOEXEC, attributes as usize) }.map(new_handle)
}
pub(crate) fn fspick(
dir: Option<BorrowedHandle<'_>>,
path: &[u8],
flags: FsPickFlags,
) -> Result<OwnedHandle> {
let table = [
(FsPickFlags::NO_FOLLOW, FSPICK_SYMLINK_NOFOLLOW),
(FsPickFlags::NO_AUTOMOUNT, FSPICK_NO_AUTOMOUNT),
(FsPickFlags::EMPTY_PATH, FSPICK_EMPTY_PATH),
];
let flags = translate(FSPICK_CLOEXEC, &table, |flag| flags.contains(flag));
with_cstr(path, |path| {
unsafe { syscall!(FSPICK, dir_fd(dir), path.as_ptr(), flags) }.map(new_handle)
})
}
fn open_tree_flags(flags: OpenTreeFlags) -> usize {
let table = [
(OpenTreeFlags::CLONE, OPEN_TREE_CLONE),
(OpenTreeFlags::RECURSIVE, AT_RECURSIVE),
(OpenTreeFlags::EMPTY_PATH, AT_EMPTY_PATH),
(OpenTreeFlags::NO_FOLLOW, AT_SYMLINK_NOFOLLOW),
(OpenTreeFlags::NO_AUTOMOUNT, AT_NO_AUTOMOUNT),
];
translate(OPEN_TREE_CLOEXEC, &table, |flag| flags.contains(flag))
}
pub(crate) fn open_tree(
dir: Option<BorrowedHandle<'_>>,
path: &[u8],
flags: OpenTreeFlags,
) -> Result<OwnedHandle> {
let flags = open_tree_flags(flags);
with_cstr(path, |path| {
unsafe { syscall!(OPEN_TREE, dir_fd(dir), path.as_ptr(), flags) }.map(new_handle)
})
}
pub(crate) fn move_mount_at(
from_dir: Option<BorrowedHandle<'_>>,
from: &[u8],
to_dir: Option<BorrowedHandle<'_>>,
to: &[u8],
flags: MoveMountFlags,
) -> Result<()> {
let table = [
(MoveMountFlags::FROM_SYMLINKS, MOVE_MOUNT_F_SYMLINKS),
(MoveMountFlags::FROM_AUTOMOUNTS, MOVE_MOUNT_F_AUTOMOUNTS),
(MoveMountFlags::FROM_EMPTY_PATH, MOVE_MOUNT_F_EMPTY_PATH),
(MoveMountFlags::TO_SYMLINKS, MOVE_MOUNT_T_SYMLINKS),
(MoveMountFlags::TO_AUTOMOUNTS, MOVE_MOUNT_T_AUTOMOUNTS),
(MoveMountFlags::TO_EMPTY_PATH, MOVE_MOUNT_T_EMPTY_PATH),
(MoveMountFlags::SET_GROUP, MOVE_MOUNT_SET_GROUP),
(MoveMountFlags::BENEATH, MOVE_MOUNT_BENEATH),
];
let flags = translate(0, &table, |flag| flags.contains(flag));
with_cstr(from, |from| {
with_cstr(to, |to| {
let (from_dir, to_dir) = (dir_fd(from_dir), dir_fd(to_dir));
unsafe {
syscall!(
MOVE_MOUNT,
from_dir,
from.as_ptr(),
to_dir,
to.as_ptr(),
flags
)
}
.map(drop)
})
})
}
pub(crate) fn mount_setattr(
dir: Option<BorrowedHandle<'_>>,
path: &[u8],
flags: MountSetAttrFlags,
change: &MountSetAttr<'_>,
) -> Result<()> {
let table = [
(MountSetAttrFlags::RECURSIVE, AT_RECURSIVE),
(MountSetAttrFlags::EMPTY_PATH, AT_EMPTY_PATH),
(MountSetAttrFlags::NO_FOLLOW, AT_SYMLINK_NOFOLLOW),
(MountSetAttrFlags::NO_AUTOMOUNT, AT_NO_AUTOMOUNT),
];
let flags = translate(0, &table, |flag| flags.contains(flag));
let attr = mount_attr(change)?;
with_cstr(path, |path| {
let (path, attr, size) = (path.as_ptr(), &raw const attr, size_of::<MountAttr>());
unsafe { syscall!(MOUNT_SETATTR, dir_fd(dir), path, flags, attr, size) }.map(drop)
})
}
fn mount_attr(change: &MountSetAttr<'_>) -> Result<MountAttr> {
let (set, set_times) = attribute_bits(change.set);
let (clear, clear_times) = attribute_bits(change.clear);
let mut attr = MountAttr {
attr_set: set,
attr_clr: clear & !MOUNT_ATTR_ATIME
| if set_times || clear_times {
MOUNT_ATTR_ATIME
} else {
0
},
propagation: change
.propagation
.map_or(0, |kind| propagation_flag(kind) as u64),
userns_fd: 0,
};
if let Some(namespace) = change.id_map {
attr.userns_fd = u64::try_from(namespace.as_raw()).map_err(|_| ErrorKind::BadHandle)?;
attr.attr_set |= MOUNT_ATTR_IDMAP;
}
Ok(attr)
}
pub(crate) fn open_tree_attr(
dir: Option<BorrowedHandle<'_>>,
path: &[u8],
flags: OpenTreeFlags,
change: &MountSetAttr<'_>,
) -> Result<OwnedHandle> {
let flags = open_tree_flags(flags);
let attr = mount_attr(change)?;
with_cstr(path, |path| {
let (path, attr, size) = (path.as_ptr(), &raw const attr, size_of::<MountAttr>());
unsafe { syscall!(OPEN_TREE_ATTR, dir_fd(dir), path, flags, attr, size) }.map(new_handle)
})
}
pub(crate) const LSMT_ROOT: u64 = u64::MAX;
const LISTMOUNT_REVERSE: usize = 1;
const MNT_ID_REQ_SIZE_VER0: u32 = 24;
const STATX_MNT_ID_UNIQUE: u32 = 0x4000;
#[repr(C)]
struct MntIdReq {
size: u32,
mnt_ns_fd: u32,
mnt_id: u64,
param: u64,
mnt_ns_id: u64,
}
#[repr(C)]
#[derive(Clone, Copy)]
pub(crate) struct StatMount {
pub(crate) size: u32,
pub(crate) mnt_opts: u32,
pub(crate) mask: u64,
pub(crate) sb_dev_major: u32,
pub(crate) sb_dev_minor: u32,
pub(crate) sb_magic: u64,
pub(crate) sb_flags: u32,
pub(crate) fs_type: u32,
pub(crate) mnt_id: u64,
pub(crate) mnt_parent_id: u64,
pub(crate) mnt_id_old: u32,
pub(crate) mnt_parent_id_old: u32,
pub(crate) mnt_attr: u64,
pub(crate) mnt_propagation: u64,
pub(crate) mnt_peer_group: u64,
pub(crate) mnt_master: u64,
pub(crate) propagate_from: u64,
pub(crate) mnt_root: u32,
pub(crate) mnt_point: u32,
pub(crate) mnt_ns_id: u64,
pub(crate) fs_subtype: u32,
pub(crate) sb_source: u32,
pub(crate) opt_num: u32,
pub(crate) opt_array: u32,
pub(crate) opt_sec_num: u32,
pub(crate) opt_sec_array: u32,
pub(crate) supported_mask: u64,
pub(crate) mnt_uidmap_num: u32,
pub(crate) mnt_uidmap: u32,
pub(crate) mnt_gidmap_num: u32,
pub(crate) mnt_gidmap: u32,
spare: [u64; 43],
}
const _: () = assert!(
size_of::<MntIdReq>() == 32
&& core::mem::offset_of!(MntIdReq, mnt_ns_id) == MNT_ID_REQ_SIZE_VER0 as usize
&& size_of::<StatMount>() == 512
&& core::mem::offset_of!(StatMount, mnt_id) == 40
&& core::mem::offset_of!(StatMount, mnt_ns_id) == 112
&& core::mem::offset_of!(StatMount, supported_mask) == 144
&& core::mem::offset_of!(StatMount, mnt_gidmap) == 164
);
pub(crate) const STATMOUNT_SIZE: usize = size_of::<StatMount>();
pub(crate) fn statmount(
id: u64,
fields: StatMountFields,
buf: &mut [u8],
) -> Result<(StatMount, &[u8])> {
if buf.len() < STATMOUNT_SIZE {
return Err(ErrorKind::BufferTooSmall.into());
}
let request = MntIdReq {
size: MNT_ID_REQ_SIZE_VER0,
mnt_ns_fd: 0,
mnt_id: id,
param: fields.bits(),
mnt_ns_id: 0,
};
let (request, out, len) = (&raw const request, buf.as_mut_ptr(), buf.len());
unsafe { syscall!(STATMOUNT, request, out, len, 0) }.map_err(|error| {
if error.raw_os_error() == crate::sys::errno::EOVERFLOW {
ErrorKind::BufferTooSmall.into()
} else {
error
}
})?;
let stat = unsafe { buf.as_ptr().cast::<StatMount>().read_unaligned() };
let end = (stat.size as usize).clamp(STATMOUNT_SIZE, buf.len());
Ok((stat, &buf[STATMOUNT_SIZE..end]))
}
pub(crate) fn listmount(
parent: u64,
after: u64,
flags: ListMountFlags,
ids: &mut [u64],
) -> Result<usize> {
let request = MntIdReq {
size: MNT_ID_REQ_SIZE_VER0,
mnt_ns_fd: 0,
mnt_id: parent,
param: after,
mnt_ns_id: 0,
};
let flags = if flags.contains(ListMountFlags::REVERSE) {
LISTMOUNT_REVERSE
} else {
0
};
let (request, out, len) = (&raw const request, ids.as_mut_ptr(), ids.len());
unsafe { syscall!(LISTMOUNT, request, out, len, flags) }
}
pub(crate) fn mount_id(dir: Option<BorrowedHandle<'_>>, path: &[u8], follow: bool) -> Result<u64> {
let mut stat = [0u64; 32];
let mut flags = AT_NO_AUTOMOUNT;
if !follow {
flags |= AT_SYMLINK_NOFOLLOW;
}
if path.is_empty() {
flags |= AT_EMPTY_PATH;
}
with_cstr(path, |path| {
let (path, mask) = (path.as_ptr(), STATX_MNT_ID_UNIQUE);
unsafe { syscall!(STATX, dir_fd(dir), path, flags, mask, &raw mut stat) }.map(drop)
})?;
if stat[0] as u32 & STATX_MNT_ID_UNIQUE == 0 {
return Err(ErrorKind::Unsupported.into());
}
Ok(stat[18])
}
pub(crate) fn attributes_from(bits: u64) -> (MountAttributes, bool) {
let table = [
(MountAttributes::READ_ONLY, MOUNT_ATTR_RDONLY),
(MountAttributes::NO_SUID, MOUNT_ATTR_NOSUID),
(MountAttributes::NO_DEV, MOUNT_ATTR_NODEV),
(MountAttributes::NO_EXEC, MOUNT_ATTR_NOEXEC),
(MountAttributes::NO_ATIME, MOUNT_ATTR_NOATIME),
(MountAttributes::STRICT_ATIME, MOUNT_ATTR_STRICTATIME),
(MountAttributes::NO_DIR_ATIME, MOUNT_ATTR_NODIRATIME),
(MountAttributes::NO_SYMLINK_FOLLOW, MOUNT_ATTR_NOSYMFOLLOW),
];
let mut attributes = MountAttributes::empty();
for (flag, value) in table {
if bits & value != 0 {
attributes |= flag;
}
}
if bits & MOUNT_ATTR_ATIME == 0 {
attributes |= MountAttributes::REL_ATIME;
}
(attributes, bits & MOUNT_ATTR_IDMAP != 0)
}
pub(crate) fn filesystem_flags_from(bits: u32) -> MountFlags {
let table = [
(MountFlags::READ_ONLY, MS_RDONLY),
(MountFlags::SYNCHRONOUS, MS_SYNCHRONOUS),
(MountFlags::DIR_SYNC, MS_DIRSYNC),
(MountFlags::LAZY_TIME, MS_LAZYTIME),
];
let mut flags = MountFlags::empty();
for (flag, value) in table {
if bits as usize & value != 0 {
flags |= flag;
}
}
flags
}
pub(crate) fn propagation_from(bits: u64) -> Propagation {
let bits = bits as usize;
if bits & MS_SHARED != 0 {
Propagation::Shared
} else if bits & MS_SLAVE != 0 {
Propagation::Slave
} else if bits & MS_UNBINDABLE != 0 {
Propagation::Unbindable
} else {
Propagation::Private
}
}