1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use libc::{self, c_char, c_int};
use std::ffi::{CStr, CString};
use std::io;
use std::path::Path;
extern "C" {
pub fn fuse_unmount_compat22(mountpoint: *const c_char);
}
pub fn unmount(mountpoint: &Path) -> io::Result<()> {
#[cfg(any(
target_os = "macos",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "bitrig",
target_os = "netbsd"
))]
#[inline]
fn libc_umount(mnt: &CStr) -> c_int {
unsafe { libc::unmount(mnt.as_ptr(), 0) }
}
#[cfg(not(any(
target_os = "macos",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "bitrig",
target_os = "netbsd"
)))]
#[inline]
fn libc_umount(mnt: &CStr) -> c_int {
use std::io::ErrorKind::PermissionDenied;
let rc = unsafe { libc::umount(mnt.as_ptr()) };
if rc < 0 && io::Error::last_os_error().kind() == PermissionDenied {
unsafe {
fuse_unmount_compat22(mnt.as_ptr());
}
0
} else {
rc
}
}
let mnt = CString::new(
mountpoint
.to_path_buf()
.into_os_string()
.into_string()
.unwrap(),
)?;
let rc = libc_umount(&mnt);
if rc < 0 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
}