fix clippy pedantry warnings

This commit is contained in:
Ryan Heywood 2025-05-09 14:09:59 -04:00
parent 6c5c130e77
commit 665c39c05c
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
3 changed files with 38 additions and 25 deletions

View File

@ -1,7 +1,7 @@
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use std::{path::PathBuf, str::FromStr}; use std::{path::PathBuf, str::FromStr};
/// VM controller for AirgapOS /// VM controller for `AirgapOS`
#[derive(Parser, Clone, Debug)] #[derive(Parser, Clone, Debug)]
pub struct App { pub struct App {
// global options go here // global options go here

View File

@ -1,3 +1,5 @@
#![allow(clippy::redundant_else)]
use clap::Parser; use clap::Parser;
use eyre::WrapErr; use eyre::WrapErr;
use std::io::Write; use std::io::Write;
@ -60,13 +62,16 @@ fn main() -> eyre::Result<()> {
let spawn_arguments = SpawnArguments::default(); let spawn_arguments = SpawnArguments::default();
let mut vm = VirtualMachine::load(spawn_arguments, None)?; let mut vm = VirtualMachine::load(spawn_arguments, None)?;
vm.execute_host("qmp_capabilities", serde_json::json!({}))?; vm.execute_host("qmp_capabilities", serde_json::json!({}))?;
vm.execute_host("device_add", serde_json::json!({ vm.execute_host(
"driver": "usb-host", "device_add",
"bus": "usb.0", serde_json::json!({
"vendorid": device.vendor_id, "driver": "usb-host",
"productid": device.device_id, "bus": "usb.0",
}))?; "vendorid": device.vendor_id,
}, "productid": device.device_id,
}),
)?;
}
cli::Commands::Push { cli::Commands::Push {
local_path, local_path,
remote_path, remote_path,
@ -88,7 +93,9 @@ fn main() -> eyre::Result<()> {
let mut vm = VirtualMachine::load(spawn_arguments, None)?; let mut vm = VirtualMachine::load(spawn_arguments, None)?;
let (response, exit_code) = vm.run_command(&command, args)?; let (response, exit_code) = vm.run_command(&command, args)?;
std::io::stdout().write_all(&response)?; 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"))?,
);
} }
} }

View File

@ -66,6 +66,7 @@ fn to_lowercase_hexlike(s: impl AsRef<str>) -> String {
s.to_ascii_lowercase() s.to_ascii_lowercase()
} }
#[allow(clippy::struct_field_names)]
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct Device { struct Device {
vendor_id: u16, vendor_id: u16,
@ -100,7 +101,7 @@ fn find_pci_device_by_class(class: u16) -> Result<Vec<Device>> {
let bus_id = bus_address let bus_id = bus_address
.into_string() .into_string()
.map_err(|bad| eyre::eyre!("non-utf8 bus address: {bad:?}"))? .map_err(|bad| eyre::eyre!("non-utf8 bus address: {bad:?}"))?
.split_once(":") .split_once(':')
.ok_or(eyre::eyre!("bad path ID"))? .ok_or(eyre::eyre!("bad path ID"))?
.1 .1
.to_string(); .to_string();
@ -169,8 +170,9 @@ impl VirtualMachine {
if std::fs::exists(&args.lockfile_path)? { if std::fs::exists(&args.lockfile_path)? {
// Check if VM is running // Check if VM is running
use nix::unistd::{getpgid, Pid}; use nix::unistd::{getpgid, Pid};
let pid = get_pid(&args.lockfile_path)?; let pid = i32::try_from(get_pid(&args.lockfile_path)?)
if getpgid(Some(Pid::from_raw(pid as i32))).is_ok() { .context(eyre::eyre!("bad PID: pid < i32::MAX << 1"))?;
if getpgid(Some(Pid::from_raw(pid))).is_ok() {
// process exists, exit // process exists, exit
return Err(eyre::eyre!( return Err(eyre::eyre!(
"VM with this configuration exists as PID {pid}" "VM with this configuration exists as PID {pid}"
@ -217,7 +219,7 @@ impl VirtualMachine {
), ),
)?; )?;
net_args.push("-device".to_string()); 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") let mut child = Command::new("qemu-system-x86_64")
@ -274,13 +276,12 @@ impl VirtualMachine {
pub fn load(args: SpawnArguments, pid: Option<u32>) -> Result<Self> { pub fn load(args: SpawnArguments, pid: Option<u32>) -> Result<Self> {
let bar = spinner("Connecting to VM"); let bar = spinner("Connecting to VM");
let pid = match pid { let pid = if let Some(pid) = pid {
Some(pid) => pid, pid
None => { } else {
let pid_str = std::fs::read_to_string(&args.lockfile_path) let pid_str = std::fs::read_to_string(&args.lockfile_path)
.context("error reading PID from lockfile")?; .context("error reading PID from lockfile")?;
pid_str.parse().context("could not parse PID")? pid_str.parse().context("could not parse PID")?
}
}; };
let guest = UnixStream::connect(&args.guest_agent_socket_path) let guest = UnixStream::connect(&args.guest_agent_socket_path)
@ -319,7 +320,8 @@ impl VirtualMachine {
); );
let mut server_hello = String::new(); 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)")?; .context("can't read line from socket (pre-load)")?;
let mut vm = Self { let mut vm = Self {
@ -339,7 +341,7 @@ impl VirtualMachine {
// crashing if those circumstances happen to be met. // crashing if those circumstances happen to be met.
let time = SystemTime::now().duration_since(UNIX_EPOCH)?; 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 let ping_response = vm
.execute_internal("guest-sync", serde_json::json!({"id": identifier})) .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"))? .ok_or(eyre::eyre!("not given 'count' of bytes written"))?
.as_u64() .as_u64()
.ok_or(eyre::eyre!("'count' not 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 { if written == size {
break; break;
} }
@ -672,7 +675,8 @@ impl VirtualMachine {
.read_line(&mut line) .read_line(&mut line)
.context("can't read line from socket")?; .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(); bar.finish_and_clear();
@ -694,7 +698,9 @@ impl VirtualMachine {
sys::signal::{kill, SIGKILL}, sys::signal::{kill, SIGKILL},
unistd::{getpgid, Pid}, 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() { if getpgid(Some(pid)).is_err() {
eprintln!("Process not found"); eprintln!("Process not found");
return Ok(()); return Ok(());