diff --git a/src/mount/mount_unmount.rs b/src/mount/mount_unmount.rs index ebb517332..ac7c816bc 100644 --- a/src/mount/mount_unmount.rs +++ b/src/mount/mount_unmount.rs @@ -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)` /// @@ -36,6 +41,35 @@ pub fn mount( + source: Option, + target: Target, + file_system_type: Option, + 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 diff --git a/src/path/arg.rs b/src/path/arg.rs index f71125d88..fc2910291 100644 --- a/src/path/arg.rs +++ b/src/path/arg.rs @@ -89,6 +89,19 @@ pub trait Arg { F: FnOnce(&CStr) -> io::Result; } +/// Runs a closure on `arg` where `A` is mapped to a `&CStr` +pub fn option_into_with_c_str(arg: Option, f: F) -> io::Result +where + A: Sized, + F: FnOnce(Option<&CStr>) -> io::Result, +{ + 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> { diff --git a/src/path/mod.rs b/src/path/mod.rs index 19bf2c7f0..ae1cbc14b 100644 --- a/src/path/mod.rs +++ b/src/path/mod.rs @@ -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;