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

View File

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