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")]
Interactive,
#[fail(display = "Failed umounting filesystems")]
UmountFailure,
}
impl Fail for Error {

View File

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

View File

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