Fail if failed to ummount the filesystems

This commit is contained in:
Roey Darwish Dror 2018-11-22 11:47:20 +02:00
parent 4efa7668c8
commit 7c8109934c
3 changed files with 24 additions and 6 deletions

View File

@ -55,6 +55,9 @@ pub enum ErrorKind {
#[fail(display = "Error caused by the interactive mode")] #[fail(display = "Error caused by the interactive mode")]
Interactive, Interactive,
#[fail(display = "Failed umounting filesystems")]
UmountFailure,
} }
impl Fail for Error { impl Fail for Error {

View File

@ -210,6 +210,10 @@ fn create(command: CreateCommand) -> Result<(), Error> {
} }
info!("Unmounting filesystems"); info!("Unmounting filesystems");
mount_stack.umount()?;
info!("Installation succeeded. It is now safe to unplug your device.");
Ok(()) Ok(())
} }

View File

@ -1,3 +1,5 @@
use super::error::{Error, ErrorKind};
use failure::Fail;
use log::{debug, warn}; use log::{debug, warn};
use nix; use nix;
use nix::mount::{mount, umount, MsFlags}; use nix::mount::{mount, umount, MsFlags};
@ -48,15 +50,24 @@ impl<'a> MountStack<'a> {
self.targets.push(target); self.targets.push(target);
Ok(()) Ok(())
} }
pub 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
}
} }
impl<'a> Drop for MountStack<'a> { impl<'a> Drop for MountStack<'a> {
fn drop(&mut self) { fn drop(&mut self) {
while let Some(target) = self.targets.pop() { self.umount().ok();
debug!("Unmounting {}", target.display());
if !umount(target).is_ok() {
warn!("Unable to umount {}", target.display());
};
}
} }
} }