alma_philmmanjaro/src/storage/mount_stack.rs
Roey Darwish Dror bb7567e72b Uber refactoring
2019-05-29 13:59:49 +03:00

67 lines
1.6 KiB
Rust

use super::Filesystem;
use crate::error::{Error, ErrorKind};
use failure::Fail;
use log::{debug, warn};
use nix;
use nix::mount::{mount, umount, MsFlags};
use std::marker::PhantomData;
use std::path::PathBuf;
pub struct MountStack<'a> {
targets: Vec<PathBuf>,
filesystems: PhantomData<Filesystem<'a>>,
}
impl<'a> MountStack<'a> {
pub fn new() -> Self {
MountStack {
targets: Vec::new(),
filesystems: PhantomData,
}
}
#[must_use]
pub fn mount(
&mut self,
filesystem: &'a Filesystem,
target: PathBuf,
options: Option<&str>,
) -> nix::Result<()> {
let source = filesystem.block().path();
debug!("Mounting {:?} to {:?}", filesystem, target);
mount(
Some(source),
&target,
Some(filesystem.fs_type().to_mount_type()),
MsFlags::MS_NOATIME,
options,
)?;
self.targets.push(target);
Ok(())
}
fn _umount(&mut self) -> Result<(), Error> {
let mut result = Ok(());
while let Some(target) = self.targets.pop() {
debug!("Unmounting {}", target.display());
if let Err(e) = umount(&target) {
warn!("Unable to umount {}: {}", target.display(), e);
result = Err(Error::from(e.context(ErrorKind::UmountFailure)));
};
}
result
}
pub fn umount(mut self) -> Result<(), Error> {
self._umount()
}
}
impl<'a> Drop for MountStack<'a> {
fn drop(&mut self) {
self._umount().ok();
}
}