extern crate libc;
extern crate loopdev;
#[macro_use]
extern crate bitflags;
mod fstype;
mod mount;
mod supported;
mod umount;
pub use self::{fstype::*, mount::*, supported::*, umount::*};
use libc::swapoff as c_swapoff;
use std::{
ffi::CString,
io::{self, Error, ErrorKind},
os::unix::ffi::OsStrExt,
path::Path,
ptr,
};
pub fn swapoff<P: AsRef<Path>>(dest: P) -> io::Result<()> {
unsafe {
let swap = CString::new(dest.as_ref().as_os_str().as_bytes().to_owned());
let swap_ptr = swap.as_ref().ok().map_or(ptr::null(), |cstr| cstr.as_ptr());
match c_swapoff(swap_ptr) {
0 => Ok(()),
_err => Err(Error::new(
ErrorKind::Other,
format!(
"failed to swapoff {}: {}",
dest.as_ref().display(),
Error::last_os_error()
),
)),
}
}
}
fn to_cstring(data: &[u8]) -> io::Result<CString> {
CString::new(data).map_err(|why| {
io::Error::new(io::ErrorKind::InvalidData, format!("failed to create `CString`: {}", why))
})
}