Skip to content

Commit 4c3335c

Browse files
authored
Add mount2 syscall (#1024)
* 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
1 parent a6a5cc8 commit 4c3335c

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/mount/mount_unmount.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
use crate::backend::mount::types::{
44
InternalMountFlags, MountFlags, MountFlagsArg, MountPropagationFlags, UnmountFlags,
55
};
6-
use crate::{backend, io, path};
6+
use crate::{
7+
backend,
8+
ffi::CStr,
9+
io,
10+
path::{self, option_into_with_c_str},
11+
};
712

813
/// `mount(source, target, filesystemtype, mountflags, data)`
914
///
@@ -36,6 +41,35 @@ pub fn mount<Source: path::Arg, Target: path::Arg, Fs: path::Arg, Data: path::Ar
3641
})
3742
}
3843

44+
/// `mount2(source, target, filesystemtype, mountflags, data)`
45+
///
46+
/// # References
47+
/// - [Linux]
48+
///
49+
/// [Linux]: https://man7.org/linux/man-pages/man2/mount.2.html
50+
#[inline]
51+
pub fn mount2<Source: path::Arg, Target: path::Arg, Fs: path::Arg>(
52+
source: Option<Source>,
53+
target: Target,
54+
file_system_type: Option<Fs>,
55+
flags: MountFlags,
56+
data: Option<&CStr>,
57+
) -> io::Result<()> {
58+
option_into_with_c_str(source, |source| {
59+
target.into_with_c_str(|target| {
60+
option_into_with_c_str(file_system_type, |file_system_type| {
61+
backend::mount::syscalls::mount(
62+
source,
63+
target,
64+
file_system_type,
65+
MountFlagsArg(flags.bits()),
66+
data,
67+
)
68+
})
69+
})
70+
})
71+
}
72+
3973
/// `mount(NULL, target, NULL, MS_REMOUNT | mountflags, data)`
4074
///
4175
/// # References

src/path/arg.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ pub trait Arg {
8989
F: FnOnce(&CStr) -> io::Result<T>;
9090
}
9191

92+
/// Runs a closure on `arg` where `A` is mapped to a `&CStr`
93+
pub fn option_into_with_c_str<T, F, A: Arg>(arg: Option<A>, f: F) -> io::Result<T>
94+
where
95+
A: Sized,
96+
F: FnOnce(Option<&CStr>) -> io::Result<T>,
97+
{
98+
if let Some(arg) = arg {
99+
arg.into_with_c_str(|p| f(Some(p)))
100+
} else {
101+
f(None)
102+
}
103+
}
104+
92105
impl Arg for &str {
93106
#[inline]
94107
fn as_str(&self) -> io::Result<&str> {

src/path/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod arg;
44
#[cfg(feature = "itoa")]
55
mod dec_int;
66

7-
pub use arg::Arg;
7+
pub use arg::{option_into_with_c_str, Arg};
88
#[cfg(feature = "itoa")]
99
#[cfg_attr(doc_cfg, doc(cfg(feature = "itoa")))]
1010
pub use dec_int::DecInt;

0 commit comments

Comments
 (0)