From 665c39c05cc505718fadef72664a7dc3241e3307 Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 9 May 2025 14:09:59 -0400 Subject: [PATCH] fix clippy pedantry warnings --- src/cli.rs | 2 +- src/main.rs | 23 +++++++++++++++-------- src/vm.rs | 38 ++++++++++++++++++++++---------------- 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index c4a0e32..ab08771 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,7 +1,7 @@ use clap::{Parser, Subcommand}; use std::{path::PathBuf, str::FromStr}; -/// VM controller for AirgapOS +/// VM controller for `AirgapOS` #[derive(Parser, Clone, Debug)] pub struct App { // global options go here diff --git a/src/main.rs b/src/main.rs index 07a162b..613fbe1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +#![allow(clippy::redundant_else)] + use clap::Parser; use eyre::WrapErr; use std::io::Write; @@ -60,13 +62,16 @@ fn main() -> eyre::Result<()> { let spawn_arguments = SpawnArguments::default(); let mut vm = VirtualMachine::load(spawn_arguments, None)?; vm.execute_host("qmp_capabilities", serde_json::json!({}))?; - vm.execute_host("device_add", serde_json::json!({ - "driver": "usb-host", - "bus": "usb.0", - "vendorid": device.vendor_id, - "productid": device.device_id, - }))?; - }, + vm.execute_host( + "device_add", + serde_json::json!({ + "driver": "usb-host", + "bus": "usb.0", + "vendorid": device.vendor_id, + "productid": device.device_id, + }), + )?; + } cli::Commands::Push { local_path, remote_path, @@ -88,7 +93,9 @@ fn main() -> eyre::Result<()> { let mut vm = VirtualMachine::load(spawn_arguments, None)?; let (response, exit_code) = vm.run_command(&command, args)?; std::io::stdout().write_all(&response)?; - std::process::exit(exit_code as i32); + std::process::exit( + i32::try_from(exit_code).context(eyre::eyre!("bad PID: pid < i32::MAX << 1"))?, + ); } } diff --git a/src/vm.rs b/src/vm.rs index 5f649e9..d060d10 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -66,6 +66,7 @@ fn to_lowercase_hexlike(s: impl AsRef) -> String { s.to_ascii_lowercase() } +#[allow(clippy::struct_field_names)] #[derive(Clone, Debug)] struct Device { vendor_id: u16, @@ -100,7 +101,7 @@ fn find_pci_device_by_class(class: u16) -> Result> { let bus_id = bus_address .into_string() .map_err(|bad| eyre::eyre!("non-utf8 bus address: {bad:?}"))? - .split_once(":") + .split_once(':') .ok_or(eyre::eyre!("bad path ID"))? .1 .to_string(); @@ -169,8 +170,9 @@ impl VirtualMachine { if std::fs::exists(&args.lockfile_path)? { // Check if VM is running use nix::unistd::{getpgid, Pid}; - let pid = get_pid(&args.lockfile_path)?; - if getpgid(Some(Pid::from_raw(pid as i32))).is_ok() { + let pid = i32::try_from(get_pid(&args.lockfile_path)?) + .context(eyre::eyre!("bad PID: pid < i32::MAX << 1"))?; + if getpgid(Some(Pid::from_raw(pid))).is_ok() { // process exists, exit return Err(eyre::eyre!( "VM with this configuration exists as PID {pid}" @@ -217,7 +219,7 @@ impl VirtualMachine { ), )?; net_args.push("-device".to_string()); - net_args.push(format!("vfio-pci,host={bus_id}")) + net_args.push(format!("vfio-pci,host={bus_id}")); } let mut child = Command::new("qemu-system-x86_64") @@ -274,13 +276,12 @@ impl VirtualMachine { pub fn load(args: SpawnArguments, pid: Option) -> Result { let bar = spinner("Connecting to VM"); - let pid = match pid { - Some(pid) => pid, - None => { - let pid_str = std::fs::read_to_string(&args.lockfile_path) - .context("error reading PID from lockfile")?; - pid_str.parse().context("could not parse PID")? - } + let pid = if let Some(pid) = pid { + pid + } else { + let pid_str = std::fs::read_to_string(&args.lockfile_path) + .context("error reading PID from lockfile")?; + pid_str.parse().context("could not parse PID")? }; let guest = UnixStream::connect(&args.guest_agent_socket_path) @@ -319,7 +320,8 @@ impl VirtualMachine { ); let mut server_hello = String::new(); - host_reader.read_line(&mut server_hello) + host_reader + .read_line(&mut server_hello) .context("can't read line from socket (pre-load)")?; let mut vm = Self { @@ -339,7 +341,7 @@ impl VirtualMachine { // crashing if those circumstances happen to be met. let time = SystemTime::now().duration_since(UNIX_EPOCH)?; - let identifier = time.as_secs() % (u32::MAX as u64); + let identifier = time.as_secs() % u64::from(u32::MAX); let ping_response = vm .execute_internal("guest-sync", serde_json::json!({"id": identifier})) @@ -441,7 +443,8 @@ impl VirtualMachine { .ok_or(eyre::eyre!("not given 'count' of bytes written"))? .as_u64() .ok_or(eyre::eyre!("'count' not u64"))?; - written += response_written as usize; + written += + usize::try_from(response_written).expect("wrote more than u46::MAX bytes"); if written == size { break; } @@ -672,7 +675,8 @@ impl VirtualMachine { .read_line(&mut line) .context("can't read line from socket")?; - let response: serde_json::Value = serde_json::from_str(&line).context("response from qemu is not json")?; + let response: serde_json::Value = + serde_json::from_str(&line).context("response from qemu is not json")?; bar.finish_and_clear(); @@ -694,7 +698,9 @@ impl VirtualMachine { sys::signal::{kill, SIGKILL}, unistd::{getpgid, Pid}, }; - let pid = Pid::from_raw(self.pid as i32); + let pid = Pid::from_raw( + i32::try_from(self.pid).context(eyre::eyre!("bad PID: pid < i32::MAX << 1"))?, + ); if getpgid(Some(pid)).is_err() { eprintln!("Process not found"); return Ok(());