Skip to content

Add mount2 syscall #1024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/mount/mount_unmount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
use crate::backend::mount::types::{
InternalMountFlags, MountFlags, MountFlagsArg, MountPropagationFlags, UnmountFlags,
};
use crate::{backend, io, path};
use crate::{
backend,
ffi::CStr,
io,
path::{self, option_into_with_c_str},
};

/// `mount(source, target, filesystemtype, mountflags, data)`
///
Expand Down Expand Up @@ -36,6 +41,35 @@ pub fn mount<Source: path::Arg, Target: path::Arg, Fs: path::Arg, Data: path::Ar
})
}

/// `mount2(source, target, filesystemtype, mountflags, data)`
///
/// # References
/// - [Linux]
///
/// [Linux]: https://man7.org/linux/man-pages/man2/mount.2.html
#[inline]
pub fn mount2<Source: path::Arg, Target: path::Arg, Fs: path::Arg>(
source: Option<Source>,
target: Target,
file_system_type: Option<Fs>,
flags: MountFlags,
data: Option<&CStr>,
) -> io::Result<()> {
option_into_with_c_str(source, |source| {
target.into_with_c_str(|target| {
option_into_with_c_str(file_system_type, |file_system_type| {
backend::mount::syscalls::mount(
source,
target,
file_system_type,
MountFlagsArg(flags.bits()),
data,
)
})
})
})
}

/// `mount(NULL, target, NULL, MS_REMOUNT | mountflags, data)`
///
/// # References
Expand Down
13 changes: 13 additions & 0 deletions src/path/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ pub trait Arg {
F: FnOnce(&CStr) -> io::Result<T>;
}

/// Runs a closure on `arg` where `A` is mapped to a `&CStr`
pub fn option_into_with_c_str<T, F, A: Arg>(arg: Option<A>, f: F) -> io::Result<T>
where
A: Sized,
F: FnOnce(Option<&CStr>) -> io::Result<T>,
{
if let Some(arg) = arg {
arg.into_with_c_str(|p| f(Some(p)))
} else {
f(None)
}
}

impl Arg for &str {
#[inline]
fn as_str(&self) -> io::Result<&str> {
Expand Down
2 changes: 1 addition & 1 deletion src/path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod arg;
#[cfg(feature = "itoa")]
mod dec_int;

pub use arg::Arg;
pub use arg::{option_into_with_c_str, Arg};
#[cfg(feature = "itoa")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "itoa")))]
pub use dec_int::DecInt;
Expand Down