use std::{fs, path::PathBuf};
use crate::error::{AgentdError, AgentdResult};
const VIRTIO_PORTS_PATH: &str = "/sys/class/virtio-ports";
pub use microsandbox_protocol::AGENT_PORT_NAME;
pub fn find_serial_port(name: &str) -> AgentdResult<PathBuf> {
let ports_dir = PathBuf::from(VIRTIO_PORTS_PATH);
let entries = fs::read_dir(&ports_dir).map_err(|e| {
AgentdError::SerialPortNotFound(format!("cannot read {VIRTIO_PORTS_PATH}: {e}"))
})?;
for entry in entries {
let entry = entry?;
let name_file = entry.path().join("name");
if let Ok(port_name) = fs::read_to_string(&name_file)
&& port_name.trim() == name
{
let port_id = entry.file_name();
return Ok(PathBuf::from("/dev").join(port_id));
}
}
Err(AgentdError::SerialPortNotFound(format!(
"no virtio port with name '{name}' found"
)))
}