Skip to content

Commit

Permalink
Add mount2 syscall (#1024)
Browse files Browse the repository at this point in the history
* Add mount2 syscall

* Fix: import CStr from core instead of std

* Remove unused generics from mount2 function

* Fix mount2 function to use Arg instead of CStr

* Change mount data to &CStr
  • Loading branch information
morr0ne committed Mar 19, 2024
1 parent a6a5cc8 commit 4c3335c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
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

0 comments on commit 4c3335c

Please sign in to comment.