From 144006fade46a98770727bb4df9d6680a6845c93 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 10 Jun 2020 07:16:47 +0200 Subject: [PATCH] src/tools.rs: add new run_command helper --- src/tools.rs | 15 ++++++++++++- src/tools/disks.rs | 55 ++++++++++++++++------------------------------ 2 files changed, 33 insertions(+), 37 deletions(-) diff --git a/src/tools.rs b/src/tools.rs index cd268f65..cff171ea 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -480,7 +480,7 @@ pub fn normalize_uri_path(path: &str) -> Result<(String, Vec<&str>), Error> { /// is considered successful. pub fn command_output( output: std::process::Output, - exit_code_check: Option bool> + exit_code_check: Option bool>, ) -> Result { if !output.status.success() { @@ -507,6 +507,19 @@ pub fn command_output( Ok(output) } +pub fn run_command( + mut command: std::process::Command, + exit_code_check: Option bool>, +) -> Result { + + let output = command.output() + .map_err(|err| format_err!("failed to execute {:?} - {}", command, err))?; + + let output = crate::tools::command_output(output, exit_code_check) + .map_err(|err| format_err!("command {:?} failed - {}", command, err))?; + + Ok(output) +} pub fn fd_change_cloexec(fd: RawFd, on: bool) -> Result<(), Error> { use nix::fcntl::{fcntl, FdFlag, F_GETFD, F_SETFD}; diff --git a/src/tools/disks.rs b/src/tools/disks.rs index 70dc05c4..0e1364dd 100644 --- a/src/tools/disks.rs +++ b/src/tools/disks.rs @@ -563,11 +563,7 @@ pub fn get_partition_type_info() -> Result>, Error> let mut command = std::process::Command::new(LSBLK_BIN_PATH); command.args(&["--json", "-o", "path,parttype"]); - let output = command.output() - .map_err(|err| format_err!("failed to execute '{}' - {}", LSBLK_BIN_PATH, err))?; - - let output = crate::tools::command_output(output, None) - .map_err(|err| format_err!("lsblk command failed: {}", err))?; + let output = crate::tools::run_command(command, None)?; let mut res: HashMap> = HashMap::new(); @@ -865,11 +861,7 @@ pub fn reread_partition_table(disk: &Disk) -> Result<(), Error> { command.arg("--rereadpt"); command.arg(disk_path); - let output = command.output() - .map_err(|err| format_err!("failed to execute '{}' - {}", BLOCKDEV_BIN_PATH, err))?; - - crate::tools::command_output(output, None) - .map_err(|err| format_err!("re-read partition table failed: {}", err))?; + crate::tools::run_command(command, None)?; Ok(()) } @@ -888,11 +880,7 @@ pub fn inititialize_gpt_disk(disk: &Disk, uuid: Option<&str>) -> Result<(), Erro command.arg(disk_path); command.args(&["-U", uuid]); - let output = command.output() - .map_err(|err| format_err!("failed to execute '{}' - {}", SGDISK_BIN_PATH, err))?; - - crate::tools::command_output(output, None) - .map_err(|err| format_err!("sgdisk command failed: {}", err))?; + crate::tools::run_command(command, None)?; Ok(()) } @@ -909,11 +897,7 @@ pub fn create_single_linux_partition(disk: &Disk) -> Result { command.args(&["-n1", "-t1:8300"]); command.arg(disk_path); - let output = command.output() - .map_err(|err| format_err!("failed to execute '{}' - {}", SGDISK_BIN_PATH, err))?; - - crate::tools::command_output(output, None) - .map_err(|err| format_err!("sgdisk command failed: {}", err))?; + crate::tools::run_command(command, None)?; let mut partitions = disk.partitions()?; @@ -924,7 +908,7 @@ pub fn create_single_linux_partition(disk: &Disk) -> Result { } #[api()] -#[derive(Debug, Serialize, Deserialize, PartialEq)] +#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq)] #[serde(rename_all="lowercase")] pub enum FileSystemType { /// Linux Ext4 @@ -933,6 +917,16 @@ pub enum FileSystemType { Xfs, } +impl std::fmt::Display for FileSystemType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let text = match self { + FileSystemType::Ext4 => "ext4", + FileSystemType::Xfs => "xfs", + }; + write!(f, "{}", text) + } +} + /// Create a file system on a disk or disk partition pub fn create_file_system(disk: &Disk, fs_type: FileSystemType) -> Result<(), Error> { @@ -941,20 +935,13 @@ pub fn create_file_system(disk: &Disk, fs_type: FileSystemType) -> Result<(), Er None => bail!("disk {:?} has no node in /dev", disk.syspath()), }; - let fs_type = match fs_type { - FileSystemType::Ext4 => "ext4", - FileSystemType::Xfs => "xfs", - }; + let fs_type = fs_type.to_string(); let mut command = std::process::Command::new(MKFS_BIN_PATH); - command.args(&["-t", fs_type]); + command.args(&["-t", &fs_type]); command.arg(disk_path); - let output = command.output() - .map_err(|err| format_err!("failed to execute '{}' - {}", MKFS_BIN_PATH, err))?; - - crate::tools::command_output(output, None) - .map_err(|err| format_err!("mkfs {} failed: {}", fs_type, err))?; + crate::tools::run_command(command, None)?; Ok(()) } @@ -992,11 +979,7 @@ pub fn get_fs_uuid(disk: &Disk) -> Result { command.args(&["-o", "export"]); command.arg(disk_path); - let output = command.output() - .map_err(|err| format_err!("failed to execute '{}' - {}", BLKID_BIN_PATH, err))?; - - let output = crate::tools::command_output(output, None) - .map_err(|err| format_err!("blkid command failed: {}", err))?; + let output = crate::tools::run_command(command, None)?; for line in output.lines() { if line.starts_with("UUID=") {