2023-02-05 23:50:59 -05:00
|
|
|
use sc::*;
|
2023-02-06 18:57:28 -05:00
|
|
|
use std::{ffi::CString, io};
|
2023-02-05 23:50:59 -05:00
|
|
|
|
|
|
|
mod mntent;
|
|
|
|
mod mntentries;
|
|
|
|
mod mntflags;
|
|
|
|
|
|
|
|
pub use self::{
|
|
|
|
mntent::MntEntry,
|
|
|
|
mntentries::MntEntries,
|
|
|
|
mntflags::{Flags as MntFlags, ParseFlagsError},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Wraps the Linux SYS_mount syscall in a nicer interface
|
|
|
|
pub fn mount(
|
|
|
|
dev: &str,
|
|
|
|
mountpoint: &str,
|
|
|
|
fstype: &str,
|
|
|
|
flags: u32,
|
|
|
|
opts: Option<&str>,
|
|
|
|
) -> io::Result<()> {
|
|
|
|
let ret = unsafe {
|
|
|
|
if let Some(opts) = opts {
|
|
|
|
syscall!(
|
|
|
|
MOUNT,
|
|
|
|
CString::new(dev)?.as_ptr(),
|
|
|
|
CString::new(mountpoint)?.as_ptr(),
|
|
|
|
CString::new(fstype)?.as_ptr(),
|
|
|
|
flags as usize,
|
|
|
|
CString::new(opts)?.as_ptr()
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
syscall!(
|
|
|
|
MOUNT,
|
|
|
|
CString::new(dev)?.as_ptr(),
|
|
|
|
CString::new(mountpoint)?.as_ptr(),
|
|
|
|
CString::new(fstype)?.as_ptr(),
|
|
|
|
flags as usize
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if ret == 0 {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(io::Error::last_os_error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn swapon(dev: &str, flags: usize) -> io::Result<()> {
|
|
|
|
let ret = unsafe { syscall!(SWAPON, CString::new(dev)?.as_ptr(), flags) };
|
|
|
|
if ret == 0 {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(io::Error::last_os_error())
|
|
|
|
}
|
|
|
|
}
|
2023-02-06 18:57:28 -05:00
|
|
|
|
|
|
|
pub fn umount(special: &str, flags: u32) -> io::Result<()> {
|
|
|
|
let ret = unsafe { syscall!(UMOUNT2, CString::new(special)?.as_ptr(), flags) };
|
|
|
|
if ret == 0 {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(io::Error::last_os_error())
|
|
|
|
}
|
|
|
|
}
|