Skip to content

Commit 0517893

Browse files
authored
refactor: make with_opt_nix_path() a util (#2353)
* refactor: make with_opt_nix_path() a util * refactor: feature/target gate it * refactor: add Android
1 parent bc374af commit 0517893

File tree

4 files changed

+23
-38
lines changed

4 files changed

+23
-38
lines changed

src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,21 @@ impl NixPath for PathBuf {
362362
self.as_os_str().with_nix_path(f)
363363
}
364364
}
365+
366+
/// Like `NixPath::with_nix_path()`, but allow the `path` argument to be optional.
367+
///
368+
/// A NULL pointer will be provided if `path.is_none()`.
369+
#[cfg(any(
370+
all(apple_targets, feature = "mount"),
371+
all(linux_android, any(feature = "mount", feature = "fanotify"))
372+
))]
373+
pub(crate) fn with_opt_nix_path<P, T, F>(path: Option<&P>, f: F) -> Result<T>
374+
where
375+
P: ?Sized + NixPath,
376+
F: FnOnce(*const libc::c_char) -> T,
377+
{
378+
match path {
379+
Some(path) => path.with_nix_path(|p_str| f(p_str.as_ptr())),
380+
None => Ok(f(ptr::null())),
381+
}
382+
}

src/mount/apple.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,9 @@ pub fn mount<
8282
flags: MntFlags,
8383
data: Option<&P3>,
8484
) -> Result<()> {
85-
fn with_opt_nix_path<P, T, F>(p: Option<&P>, f: F) -> Result<T>
86-
where
87-
P: ?Sized + NixPath,
88-
F: FnOnce(*const libc::c_char) -> T,
89-
{
90-
match p {
91-
Some(path) => path.with_nix_path(|p_str| f(p_str.as_ptr())),
92-
None => Ok(f(std::ptr::null())),
93-
}
94-
}
95-
9685
let res = source.with_nix_path(|s| {
9786
target.with_nix_path(|t| {
98-
with_opt_nix_path(data, |d| unsafe {
87+
crate::with_opt_nix_path(data, |d| unsafe {
9988
libc::mount(
10089
s.as_ptr(),
10190
t.as_ptr(),

src/mount/linux.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,10 @@ pub fn mount<
113113
flags: MsFlags,
114114
data: Option<&P4>,
115115
) -> Result<()> {
116-
fn with_opt_nix_path<P, T, F>(p: Option<&P>, f: F) -> Result<T>
117-
where
118-
P: ?Sized + NixPath,
119-
F: FnOnce(*const libc::c_char) -> T,
120-
{
121-
match p {
122-
Some(path) => path.with_nix_path(|p_str| f(p_str.as_ptr())),
123-
None => Ok(f(std::ptr::null())),
124-
}
125-
}
126-
127-
let res = with_opt_nix_path(source, |s| {
116+
let res = crate::with_opt_nix_path(source, |s| {
128117
target.with_nix_path(|t| {
129-
with_opt_nix_path(fstype, |ty| {
130-
with_opt_nix_path(data, |d| unsafe {
118+
crate::with_opt_nix_path(fstype, |ty| {
119+
crate::with_opt_nix_path(data, |d| unsafe {
131120
libc::mount(
132121
s,
133122
t.as_ptr(),

src/sys/fanotify.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -313,18 +313,7 @@ impl Fanotify {
313313
dirfd: Option<RawFd>,
314314
path: Option<&P>,
315315
) -> Result<()> {
316-
fn with_opt_nix_path<P, T, F>(p: Option<&P>, f: F) -> Result<T>
317-
where
318-
P: ?Sized + NixPath,
319-
F: FnOnce(*const libc::c_char) -> T,
320-
{
321-
match p {
322-
Some(path) => path.with_nix_path(|p_str| f(p_str.as_ptr())),
323-
None => Ok(f(std::ptr::null())),
324-
}
325-
}
326-
327-
let res = with_opt_nix_path(path, |p| unsafe {
316+
let res = crate::with_opt_nix_path(path, |p| unsafe {
328317
libc::fanotify_mark(
329318
self.fd.as_raw_fd(),
330319
flags.bits(),

0 commit comments

Comments
 (0)