Refactor main.rs and fix clippy lints

Status: WIP

Completed:
* Fixed flagged clippy lints
* Moved qemu(), main.rs::mount() and chroot() to the tools module.
* Moved constants in main.rs to constants.rs (including base packages
  array)
* Renamed Presets struct to PresetsCollection to avoid confusion with
  Preset struct
* Moved main() to the top of main.rs to highlight general logic path
* Added comments and docstrings to some functions
* Removed some uses of `use foo::*` to make the source of imported functions
  and structs clearer

TODO:
* Move remaining code in main.rs to modules (except main())
* Break up create() function in to separate steps
* Log every command run (with arguments) to debug! when verbose flag is used
* Add docstrings for remaining functions and document constants (e.g.
  why noatime is used)
* Remove remaining uses of `use foo::*`
* Consider renaming/moving tools module to address tool:: vs. Tool::
  confusion
This commit is contained in:
James McMurray
2020-03-06 23:11:56 +01:00
parent 19eef3a0e1
commit 490ab30f4c
14 changed files with 252 additions and 195 deletions

View File

@@ -9,8 +9,8 @@ use std::io::Read;
use std::marker::PhantomData;
use std::path::{Path, PathBuf};
static LUKS_MAGIC_1: &'static [u8] = &[0x4c, 0x55, 0x4b, 0x53, 0xba, 0xbe];
static LUKS_MAGIC_2: &'static [u8] = &[0x53, 0x4b, 0x55, 0x4c, 0xba, 0xbe];
static LUKS_MAGIC_1: &[u8] = &[0x4c, 0x55, 0x4b, 0x53, 0xba, 0xbe];
static LUKS_MAGIC_2: &[u8] = &[0x53, 0x4b, 0x55, 0x4c, 0xba, 0xbe];
#[derive(Debug)]
pub struct EncryptedDevice<'t, 'o> {

View File

@@ -21,9 +21,7 @@ impl LoopDevice {
.context(ErrorKind::Image)?;
if !output.status.success() {
Err(ErrorKind::Losetup(
String::from_utf8(output.stderr).unwrap(),
))?
return Err(ErrorKind::Losetup(String::from_utf8(output.stderr).unwrap()).into());
}
let path = PathBuf::from(String::from_utf8(output.stdout).unwrap().trim());

View File

@@ -1,5 +1,5 @@
use std::path::Path;
// Marker traits
pub trait BlockDevice: std::fmt::Debug {
fn path(&self) -> &Path;
}

View File

@@ -2,7 +2,6 @@ use super::Filesystem;
use crate::error::{Error, ErrorKind};
use failure::Fail;
use log::{debug, warn};
use nix;
use nix::mount::{mount, umount, MsFlags};
use std::marker::PhantomData;
use std::path::PathBuf;
@@ -20,7 +19,6 @@ impl<'a> MountStack<'a> {
}
}
#[must_use]
pub fn mount(
&mut self,
filesystem: &'a Filesystem,

View File

@@ -31,8 +31,11 @@ impl<'a> StorageDevice<'a> {
path,
origin: PhantomData,
};
if !allow_non_removable && (!(_self.is_removable_device()? || _self.is_loop_device())) {
return Err(ErrorKind::DangerousDevice)?;
// If we only allow removable/loop devices, and the device is neither removable or a loop
// device then throw a DangerousDevice error
if !(allow_non_removable || _self.is_removable_device()? || _self.is_loop_device()) {
return Err(ErrorKind::DangerousDevice.into());
}
Ok(_self)