Avoid copy in MountStack

This commit is contained in:
Roey Darwish Dror 2018-11-07 10:27:02 +02:00
parent f08064f2ba
commit 4b80f5c41b
2 changed files with 11 additions and 13 deletions

View File

@ -67,7 +67,6 @@ fn create(command: CreateCommand) -> Result<(), Error> {
let genfstab = Tool::find("genfstab")?;
let mkfat = Tool::find("mkfs.fat")?;
let mkbtrfs = Tool::find("mkfs.btrfs")?;
let mut mount_stack = MountStack::new();
if !(command.disk.starts_with("/dev/disk/by-id")
&& (command
@ -81,6 +80,8 @@ fn create(command: CreateCommand) -> Result<(), Error> {
}
let mount_point = tempdir().context(ErrorKind::TmpDirError)?;
let boot_point = mount_point.path().join("boot");
let mut mount_stack = MountStack::new();
info!("Partitioning the disk");
sgdisk
@ -119,7 +120,6 @@ fn create(command: CreateCommand) -> Result<(), Error> {
Some("compress=zstd"),
).context(ErrorKind::Mounting)?;
let boot_point = mount_point.path().join("boot");
fs::create_dir(&boot_point).context(ErrorKind::CreateBoot)?;
mount_stack
@ -193,8 +193,6 @@ fn create(command: CreateCommand) -> Result<(), Error> {
}
info!("Unmounting filesystems");
drop(mount_stack);
Ok(())
}

View File

@ -1,6 +1,6 @@
use nix;
use nix::mount::{mount, umount, MsFlags};
use std::path::{Path, PathBuf};
use std::path::Path;
#[derive(Debug)]
pub enum Filesystem {
@ -9,7 +9,7 @@ pub enum Filesystem {
}
impl Filesystem {
fn to_type(self) -> &'static str {
fn to_type(&self) -> &'static str {
match self {
Filesystem::Btrfs => "btrfs",
Filesystem::Vfat => "vfat",
@ -17,11 +17,11 @@ impl Filesystem {
}
}
pub struct MountStack {
targets: Vec<PathBuf>,
pub struct MountStack<'a> {
targets: Vec<&'a Path>,
}
impl MountStack {
impl<'a> MountStack<'a> {
pub fn new() -> Self {
MountStack {
targets: Vec::new(),
@ -32,7 +32,7 @@ impl MountStack {
pub fn mount(
&mut self,
source: &Path,
target: &Path,
target: &'a Path,
filesystem: Filesystem,
options: Option<&str>,
) -> nix::Result<()> {
@ -44,16 +44,16 @@ impl MountStack {
MsFlags::empty(),
options,
)?;
self.targets.push(target.to_owned());
self.targets.push(target);
Ok(())
}
}
impl Drop for MountStack {
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() {
if !umount(target).is_ok() {
warn!("Unable to mount {}", target.display());
};
}