From c38a61574cdf0e71d1959b0ba9fd045d898966b1 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Wed, 3 Apr 2024 09:42:32 +0800 Subject: [PATCH 1/3] refactor: make with_opt_nix_path() a util --- src/lib.rs | 14 ++++++++++++++ src/mount/apple.rs | 13 +------------ src/mount/linux.rs | 17 +++-------------- src/sys/fanotify.rs | 13 +------------ 4 files changed, 19 insertions(+), 38 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index dffac29b54..2dcef93e48 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -362,3 +362,17 @@ impl NixPath for PathBuf { self.as_os_str().with_nix_path(f) } } + +/// Like `NixPath::with_nix_path()`, but allow the `path` argument to be optional. +/// +/// A NULL pointer will be provided if `path.is_none()`. +pub(crate) fn with_opt_nix_path(path: Option<&P>, f: F) -> Result +where + P: ?Sized + NixPath, + F: FnOnce(*const libc::c_char) -> T, +{ + match path { + Some(path) => path.with_nix_path(|p_str| f(p_str.as_ptr())), + None => Ok(f(ptr::null())), + } +} diff --git a/src/mount/apple.rs b/src/mount/apple.rs index 6759ed20bb..ce0ab1e9ca 100644 --- a/src/mount/apple.rs +++ b/src/mount/apple.rs @@ -82,20 +82,9 @@ pub fn mount< flags: MntFlags, data: Option<&P3>, ) -> Result<()> { - fn with_opt_nix_path(p: Option<&P>, f: F) -> Result - where - P: ?Sized + NixPath, - F: FnOnce(*const libc::c_char) -> T, - { - match p { - Some(path) => path.with_nix_path(|p_str| f(p_str.as_ptr())), - None => Ok(f(std::ptr::null())), - } - } - let res = source.with_nix_path(|s| { target.with_nix_path(|t| { - with_opt_nix_path(data, |d| unsafe { + crate::with_opt_nix_path(data, |d| unsafe { libc::mount( s.as_ptr(), t.as_ptr(), diff --git a/src/mount/linux.rs b/src/mount/linux.rs index aa166bc9d3..3c27150761 100644 --- a/src/mount/linux.rs +++ b/src/mount/linux.rs @@ -113,21 +113,10 @@ pub fn mount< flags: MsFlags, data: Option<&P4>, ) -> Result<()> { - fn with_opt_nix_path(p: Option<&P>, f: F) -> Result - where - P: ?Sized + NixPath, - F: FnOnce(*const libc::c_char) -> T, - { - match p { - Some(path) => path.with_nix_path(|p_str| f(p_str.as_ptr())), - None => Ok(f(std::ptr::null())), - } - } - - let res = with_opt_nix_path(source, |s| { + let res = crate::with_opt_nix_path(source, |s| { target.with_nix_path(|t| { - with_opt_nix_path(fstype, |ty| { - with_opt_nix_path(data, |d| unsafe { + crate::with_opt_nix_path(fstype, |ty| { + crate::with_opt_nix_path(data, |d| unsafe { libc::mount( s, t.as_ptr(), diff --git a/src/sys/fanotify.rs b/src/sys/fanotify.rs index e217406e02..9ff306017d 100644 --- a/src/sys/fanotify.rs +++ b/src/sys/fanotify.rs @@ -313,18 +313,7 @@ impl Fanotify { dirfd: Option, path: Option<&P>, ) -> Result<()> { - fn with_opt_nix_path(p: Option<&P>, f: F) -> Result - where - P: ?Sized + NixPath, - F: FnOnce(*const libc::c_char) -> T, - { - match p { - Some(path) => path.with_nix_path(|p_str| f(p_str.as_ptr())), - None => Ok(f(std::ptr::null())), - } - } - - let res = with_opt_nix_path(path, |p| unsafe { + let res = crate::with_opt_nix_path(path, |p| unsafe { libc::fanotify_mark( self.fd.as_raw_fd(), flags.bits(), From b34ac838858722146b42a0857334ac2f8bc0c82b Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Wed, 3 Apr 2024 10:08:39 +0800 Subject: [PATCH 2/3] refactor: feature/target gate it --- src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 2dcef93e48..175acef3fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -366,6 +366,10 @@ impl NixPath for PathBuf { /// Like `NixPath::with_nix_path()`, but allow the `path` argument to be optional. /// /// A NULL pointer will be provided if `path.is_none()`. +#[cfg(any( + all(apple_targets, feature = "mount"), + all(target_os = "linux", any(feature = "mount", feature = "fanotify")) +))] pub(crate) fn with_opt_nix_path(path: Option<&P>, f: F) -> Result where P: ?Sized + NixPath, From 8fe6a5b8a0e10faf48123e7daeee2208627ef2d1 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Wed, 3 Apr 2024 10:14:43 +0800 Subject: [PATCH 3/3] refactor: add Android --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 175acef3fb..c4c0fa53cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -368,7 +368,7 @@ impl NixPath for PathBuf { /// A NULL pointer will be provided if `path.is_none()`. #[cfg(any( all(apple_targets, feature = "mount"), - all(target_os = "linux", any(feature = "mount", feature = "fanotify")) + all(linux_android, any(feature = "mount", feature = "fanotify")) ))] pub(crate) fn with_opt_nix_path(path: Option<&P>, f: F) -> Result where