Skip to content

Commit

Permalink
refactor: make with_opt_nix_path() a util (nix-rust#2353)
Browse files Browse the repository at this point in the history
* refactor: make with_opt_nix_path() a util

* refactor: feature/target gate it

* refactor: add Android
  • Loading branch information
SteveLauC authored Apr 3, 2024
1 parent bc374af commit 0517893
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 38 deletions.
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,21 @@ 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()`.
#[cfg(any(
all(apple_targets, feature = "mount"),
all(linux_android, any(feature = "mount", feature = "fanotify"))
))]
pub(crate) fn with_opt_nix_path<P, T, F>(path: Option<&P>, f: F) -> Result<T>
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())),
}
}
13 changes: 1 addition & 12 deletions src/mount/apple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,9 @@ pub fn mount<
flags: MntFlags,
data: Option<&P3>,
) -> Result<()> {
fn with_opt_nix_path<P, T, F>(p: Option<&P>, f: F) -> Result<T>
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(),
Expand Down
17 changes: 3 additions & 14 deletions src/mount/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,10 @@ pub fn mount<
flags: MsFlags,
data: Option<&P4>,
) -> Result<()> {
fn with_opt_nix_path<P, T, F>(p: Option<&P>, f: F) -> Result<T>
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(),
Expand Down
13 changes: 1 addition & 12 deletions src/sys/fanotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,18 +313,7 @@ impl Fanotify {
dirfd: Option<RawFd>,
path: Option<&P>,
) -> Result<()> {
fn with_opt_nix_path<P, T, F>(p: Option<&P>, f: F) -> Result<T>
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(),
Expand Down

0 comments on commit 0517893

Please sign in to comment.