From 30415b1719c25745815b37927320de660d2821d0 Mon Sep 17 00:00:00 2001 From: Federico Maria Morrone Date: Wed, 28 Feb 2024 21:50:38 +0100 Subject: [PATCH 1/5] Add mount2 syscall --- src/mount/mount_unmount.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/mount/mount_unmount.rs b/src/mount/mount_unmount.rs index ebb517332..405ae596a 100644 --- a/src/mount/mount_unmount.rs +++ b/src/mount/mount_unmount.rs @@ -1,5 +1,7 @@ //! Linux `mount`. +use std::ffi::CStr; + use crate::backend::mount::types::{ InternalMountFlags, MountFlags, MountFlagsArg, MountPropagationFlags, UnmountFlags, }; @@ -36,6 +38,31 @@ pub fn mount( + source: Option<&CStr>, + target: Target, + file_system_type: Option<&CStr>, + flags: MountFlags, + data: Option<&CStr>, +) -> io::Result<()> { + target.into_with_c_str(|target| { + backend::mount::syscalls::mount( + source, + target, + file_system_type, + MountFlagsArg(flags.bits()), + data, + ) + }) +} + /// `mount(NULL, target, NULL, MS_REMOUNT | mountflags, data)` /// /// # References From 55030437eb91e89b564bce2e8ffcf4f869b34d37 Mon Sep 17 00:00:00 2001 From: Federico Maria Morrone Date: Wed, 28 Feb 2024 22:14:41 +0100 Subject: [PATCH 2/5] Fix: import CStr from core instead of std --- src/mount/mount_unmount.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mount/mount_unmount.rs b/src/mount/mount_unmount.rs index 405ae596a..27ef5d72c 100644 --- a/src/mount/mount_unmount.rs +++ b/src/mount/mount_unmount.rs @@ -1,6 +1,6 @@ //! Linux `mount`. -use std::ffi::CStr; +use core::ffi::CStr; use crate::backend::mount::types::{ InternalMountFlags, MountFlags, MountFlagsArg, MountPropagationFlags, UnmountFlags, From 93539d2c525fbfee570dd1eb9632bbb501674afd Mon Sep 17 00:00:00 2001 From: Federico Maria Morrone Date: Wed, 28 Feb 2024 22:19:09 +0100 Subject: [PATCH 3/5] Remove unused generics from mount2 function --- src/mount/mount_unmount.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mount/mount_unmount.rs b/src/mount/mount_unmount.rs index 27ef5d72c..209b1757e 100644 --- a/src/mount/mount_unmount.rs +++ b/src/mount/mount_unmount.rs @@ -45,7 +45,7 @@ pub fn mount( +pub fn mount2( source: Option<&CStr>, target: Target, file_system_type: Option<&CStr>, From 5ba40e3f418b6aefa59804cfefd4d9b0d859a432 Mon Sep 17 00:00:00 2001 From: Federico Maria Morrone Date: Fri, 1 Mar 2024 09:22:38 +0100 Subject: [PATCH 4/5] Fix mount2 function to use Arg instead of CStr --- src/mount/mount_unmount.rs | 37 ++++++++++++++++++++++--------------- src/path/arg.rs | 13 +++++++++++++ src/path/mod.rs | 2 +- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/mount/mount_unmount.rs b/src/mount/mount_unmount.rs index 209b1757e..67c7eee2b 100644 --- a/src/mount/mount_unmount.rs +++ b/src/mount/mount_unmount.rs @@ -1,11 +1,12 @@ //! Linux `mount`. -use core::ffi::CStr; - use crate::backend::mount::types::{ InternalMountFlags, MountFlags, MountFlagsArg, MountPropagationFlags, UnmountFlags, }; -use crate::{backend, io, path}; +use crate::{ + backend, io, + path::{self, option_into_with_c_str}, +}; /// `mount(source, target, filesystemtype, mountflags, data)` /// @@ -45,21 +46,27 @@ pub fn mount( - source: Option<&CStr>, +pub fn mount2( + source: Option, target: Target, - file_system_type: Option<&CStr>, + file_system_type: Option, flags: MountFlags, - data: Option<&CStr>, + data: Option, ) -> io::Result<()> { - target.into_with_c_str(|target| { - backend::mount::syscalls::mount( - source, - target, - file_system_type, - MountFlagsArg(flags.bits()), - data, - ) + option_into_with_c_str(source, |source| { + target.into_with_c_str(|target| { + option_into_with_c_str(file_system_type, |file_system_type| { + option_into_with_c_str(data, |data| { + backend::mount::syscalls::mount( + source, + target, + file_system_type, + MountFlagsArg(flags.bits()), + data, + ) + }) + }) + }) }) } 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; From f53f2e41f02acdd21348a49a18a31b5150abc8cc Mon Sep 17 00:00:00 2001 From: Federico Maria Morrone Date: Fri, 1 Mar 2024 10:39:36 +0100 Subject: [PATCH 5/5] Change mount data to &CStr --- src/mount/mount_unmount.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/mount/mount_unmount.rs b/src/mount/mount_unmount.rs index 67c7eee2b..ac7c816bc 100644 --- a/src/mount/mount_unmount.rs +++ b/src/mount/mount_unmount.rs @@ -4,7 +4,9 @@ use crate::backend::mount::types::{ InternalMountFlags, MountFlags, MountFlagsArg, MountPropagationFlags, UnmountFlags, }; use crate::{ - backend, io, + backend, + ffi::CStr, + io, path::{self, option_into_with_c_str}, }; @@ -46,25 +48,23 @@ pub fn mount( +pub fn mount2( source: Option, target: Target, file_system_type: Option, flags: MountFlags, - data: Option, + 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| { - option_into_with_c_str(data, |data| { - backend::mount::syscalls::mount( - source, - target, - file_system_type, - MountFlagsArg(flags.bits()), - data, - ) - }) + backend::mount::syscalls::mount( + source, + target, + file_system_type, + MountFlagsArg(flags.bits()), + data, + ) }) }) })