Enable mount for nix

This commit is contained in:
topjohnwu
2025-09-09 20:17:09 -07:00
parent 8d28f10a3f
commit c8caaa98f5
11 changed files with 126 additions and 134 deletions

View File

@@ -23,4 +23,4 @@ bytemuck = { workspace = true }
num-traits = { workspace = true }
num-derive = { workspace = true }
const_format = { workspace = true }
nix = { workspace = true, features = ["fs"] }
nix = { workspace = true, features = ["fs", "mount"] }

View File

@@ -335,6 +335,11 @@ impl Utf8CStr {
unsafe { CStr::from_bytes_with_nul_unchecked(&self.0) }
}
#[inline(always)]
pub fn as_utf8_cstr(&self) -> &Utf8CStr {
self
}
#[inline(always)]
pub fn as_str(&self) -> &str {
// SAFETY: Already UTF-8 validated during construction

View File

@@ -1,91 +1,84 @@
use crate::{LibcReturn, OsResult, Utf8CStr};
use libc::c_ulong;
use std::ptr;
use nix::mount::{MntFlags, MsFlags, mount, umount2};
impl Utf8CStr {
pub fn bind_mount_to<'a>(&'a self, path: &'a Utf8CStr, rec: bool) -> OsResult<'a, ()> {
let flag = if rec { libc::MS_REC } else { 0 };
unsafe {
libc::mount(
self.as_ptr(),
path.as_ptr(),
ptr::null(),
libc::MS_BIND | flag,
ptr::null(),
)
.check_os_err("bind_mount", Some(self), Some(path))
}
let flag = if rec {
MsFlags::MS_REC
} else {
MsFlags::empty()
};
mount(
Some(self),
path,
None::<&Utf8CStr>,
flag | MsFlags::MS_BIND,
None::<&Utf8CStr>,
)
.check_os_err("bind_mount", Some(self), Some(path))
}
pub fn remount_mount_point_flags(&self, flags: c_ulong) -> OsResult<'_, ()> {
unsafe {
libc::mount(
ptr::null(),
self.as_ptr(),
ptr::null(),
libc::MS_BIND | libc::MS_REMOUNT | flags,
ptr::null(),
)
.check_os_err("remount", Some(self), None)
}
pub fn remount_mount_point_flags(&self, flags: MsFlags) -> OsResult<'_, ()> {
mount(
None::<&Utf8CStr>,
self,
None::<&Utf8CStr>,
MsFlags::MS_BIND | MsFlags::MS_REMOUNT | flags,
None::<&Utf8CStr>,
)
.check_os_err("remount", Some(self), None)
}
pub fn remount_mount_flags(&self, flags: c_ulong) -> OsResult<'_, ()> {
unsafe {
libc::mount(
ptr::null(),
self.as_ptr(),
ptr::null(),
libc::MS_REMOUNT | flags,
ptr::null(),
)
.check_os_err("remount", Some(self), None)
}
pub fn remount_mount_flags(&self, flags: MsFlags) -> OsResult<'_, ()> {
mount(
None::<&Utf8CStr>,
self,
None::<&Utf8CStr>,
MsFlags::MS_REMOUNT | flags,
None::<&Utf8CStr>,
)
.check_os_err("remount", Some(self), None)
}
pub fn remount_with_data(&self, data: &Utf8CStr) -> OsResult<'_, ()> {
unsafe {
libc::mount(
ptr::null(),
self.as_ptr(),
ptr::null(),
libc::MS_REMOUNT,
data.as_ptr().cast(),
)
.check_os_err("remount", Some(self), None)
}
mount(
None::<&Utf8CStr>,
self,
None::<&Utf8CStr>,
MsFlags::MS_REMOUNT,
Some(data),
)
.check_os_err("remount", Some(self), None)
}
pub fn move_mount_to<'a>(&'a self, path: &'a Utf8CStr) -> OsResult<'a, ()> {
unsafe {
libc::mount(
self.as_ptr(),
path.as_ptr(),
ptr::null(),
libc::MS_MOVE,
ptr::null(),
)
.check_os_err("move_mount", Some(self), Some(path))
}
mount(
Some(self),
path,
None::<&Utf8CStr>,
MsFlags::MS_MOVE,
None::<&Utf8CStr>,
)
.check_os_err("move_mount", Some(self), Some(path))
}
pub fn unmount(&self) -> OsResult<'_, ()> {
unsafe {
libc::umount2(self.as_ptr(), libc::MNT_DETACH).check_os_err("unmount", Some(self), None)
}
umount2(self, MntFlags::MNT_DETACH).check_os_err("unmount", Some(self), None)
}
pub fn set_mount_private(&self, recursive: bool) -> OsResult<'_, ()> {
let flag = if recursive { libc::MS_REC } else { 0 };
unsafe {
libc::mount(
ptr::null(),
self.as_ptr(),
ptr::null(),
libc::MS_PRIVATE | flag,
ptr::null(),
)
.check_os_err("set_mount_private", Some(self), None)
}
pub fn set_mount_private(&self, rec: bool) -> OsResult<'_, ()> {
let flag = if rec {
MsFlags::MS_REC
} else {
MsFlags::empty()
};
mount(
None::<&Utf8CStr>,
self,
None::<&Utf8CStr>,
flag | MsFlags::MS_PRIVATE,
None::<&Utf8CStr>,
)
.check_os_err("set_mount_private", Some(self), None)
}
}