2023-02-13 00:41:58 -05:00
|
|
|
use errno::Errno;
|
2023-02-05 23:50:59 -05:00
|
|
|
use sc::*;
|
2023-02-13 00:41:58 -05:00
|
|
|
use std::{ffi::CString, error::Error};
|
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>,
|
2023-02-13 00:41:58 -05:00
|
|
|
) -> Result<(), Box<dyn Error>> {
|
2023-02-05 23:50:59 -05:00
|
|
|
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 {
|
2023-02-13 00:41:58 -05:00
|
|
|
Err(Errno::from(ret).into())
|
2023-02-05 23:50:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 00:41:58 -05:00
|
|
|
pub fn swapon(dev: &str, flags: usize) -> Result<(), Box<dyn Error>> {
|
2023-02-05 23:50:59 -05:00
|
|
|
let ret = unsafe { syscall!(SWAPON, CString::new(dev)?.as_ptr(), flags) };
|
|
|
|
if ret == 0 {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2023-02-13 00:41:58 -05:00
|
|
|
Err(Errno::from(ret).into())
|
2023-02-05 23:50:59 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-06 18:57:28 -05:00
|
|
|
|
2023-02-13 00:41:58 -05:00
|
|
|
pub fn swapoff(path: &str) -> Result<(), Box<dyn Error>> {
|
2023-02-08 16:20:25 -05:00
|
|
|
let ret = unsafe { syscall!(SWAPOFF, CString::new(path)?.as_ptr()) };
|
|
|
|
if ret == 0 {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2023-02-13 00:41:58 -05:00
|
|
|
Err(Errno::from(ret).into())
|
2023-02-08 16:20:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 00:41:58 -05:00
|
|
|
pub fn umount(special: &str, flags: u32) -> Result<(), Box<dyn Error>> {
|
2023-02-06 18:57:28 -05:00
|
|
|
let ret = unsafe { syscall!(UMOUNT2, CString::new(special)?.as_ptr(), flags) };
|
|
|
|
if ret == 0 {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2023-02-13 00:41:58 -05:00
|
|
|
Err(Errno::from(ret).into())
|
2023-02-06 18:57:28 -05:00
|
|
|
}
|
|
|
|
}
|