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 std::{path::PathBuf, str::FromStr};
/// VM controller for AirgapOS
/// VM controller for `AirgapOS`
#[derive(Parser, Clone, Debug)]
pub struct App {
// global options go here

View File

@ -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"))?,
);
}
}

View File

@ -66,6 +66,7 @@ fn to_lowercase_hexlike(s: impl AsRef<str>) -> 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<Vec<Device>> {
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<u32>) -> Result<Self> {
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(());