Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
Fail if failed to ummount the filesystems
Browse files Browse the repository at this point in the history
  • Loading branch information
r-darwish committed Nov 22, 2018
1 parent 4efa766 commit 7c81099
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
21 changes: 16 additions & 5 deletions src/mountstack.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::error::{Error, ErrorKind};
use failure::Fail;
use log::{debug, warn};
use nix;
use nix::mount::{mount, umount, MsFlags};
Expand Down Expand Up @@ -48,15 +50,24 @@ impl<'a> MountStack<'a> {
self.targets.push(target);
Ok(())
}
}

impl<'a> Drop for MountStack<'a> {
fn drop(&mut self) {
pub fn umount(&mut self) -> Result<(), Error> {
let mut result = Ok(());

while let Some(target) = self.targets.pop() {
debug!("Unmounting {}", target.display());
if !umount(target).is_ok() {
warn!("Unable to umount {}", 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) {
self.umount().ok();
}
}

0 comments on commit 7c81099

Please sign in to comment.