use libc::*;
use std::{ffi::CString, io, ops::Deref, os::unix::ffi::OsStrExt, path::Path, ptr};
pub trait Unmount {
fn unmount(&self, flags: UnmountFlags) -> io::Result<()>;
fn into_unmount_drop(self, flags: UnmountFlags) -> UnmountDrop<Self>
where
Self: Sized,
{
UnmountDrop { mount: self, flags }
}
}
pub struct UnmountDrop<T: Unmount> {
pub(crate) mount: T,
pub(crate) flags: UnmountFlags,
}
impl<T: Unmount> UnmountDrop<T> {
pub fn set_unmount_flags(&mut self, flags: UnmountFlags) { self.flags = flags; }
}
impl<T: Unmount> Deref for UnmountDrop<T> {
type Target = T;
fn deref(&self) -> &T { &self.mount }
}
impl<T: Unmount> Drop for UnmountDrop<T> {
fn drop(&mut self) { let _ = self.mount.unmount(self.flags); }
}
bitflags! {
pub struct UnmountFlags: c_int {
const FORCE = MNT_FORCE;
const DETACH = MNT_DETACH;
const EXPIRE = MNT_EXPIRE;
const NOFOLLOW = O_NOFOLLOW;
}
}
pub fn unmount<P: AsRef<Path>>(path: P, flags: UnmountFlags) -> io::Result<()> {
let mount = CString::new(path.as_ref().as_os_str().as_bytes().to_owned());
let mount_ptr = mount.as_ref().ok().map_or(ptr::null(), |cstr| cstr.as_ptr());
unsafe { unmount_(mount_ptr, flags) }
}
pub(crate) unsafe fn unmount_(mount_ptr: *const c_char, flags: UnmountFlags) -> io::Result<()> {
match umount2(mount_ptr, flags.bits()) {
0 => Ok(()),
_err => Err(io::Error::last_os_error()),
}
}