#[cfg(target_os = "macos")]
use std::path::{Path, PathBuf};
#[cfg(target_os = "macos")]
use super::super::Error;
#[cfg(target_os = "macos")]
pub fn find_mountpoint_pre_canonicalized(path: &Path) -> Result<&Path, Error> {
extern crate libc;
use std;
use std::ffi::{CString, CStr, OsStr};
use std::os::unix::ffi::OsStrExt;
let cstr = CString::new(path.as_os_str().as_bytes())?;
let raw_mountpoint = unsafe {
let mut fs_stat: libc::statfs = std::mem::uninitialized();
if libc::statfs(cstr.as_ptr(), &mut fs_stat) != 0 {
return Err(Error::from(std::io::Error::last_os_error()));
} else {
CStr::from_ptr(fs_stat.f_mntonname.as_ptr())
}
};
let mountpoint = OsStr::from_bytes(raw_mountpoint.to_bytes());
assert!(path.starts_with(mountpoint));
assert!(!mountpoint.is_empty());
Ok(Path::new(mountpoint))
}
#[cfg(target_os = "macos")]
pub fn find_mountpoint(path: &Path) -> Result<PathBuf, Error> {
let canonicalized = path.canonicalize()?;
let found = find_mountpoint_pre_canonicalized(canonicalized.as_path())?;
Ok(found.to_path_buf())
}