From 8fe5c36904114ff8d612b5e261a61baedaa04cac Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Tue, 5 Dec 2023 11:10:36 +0200 Subject: [PATCH] scmi: use PathBuf for socket path clap can parse a PathBuf directly from the command line arguments, and paths are not always UTF-8. Use PathBuf instead of a String to allow for all valid filesystem paths. Signed-off-by: Manos Pitsidianakis --- vhost-device-scmi/src/main.rs | 13 +++++++------ vhost-device-scmi/src/vhu_scmi.rs | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/vhost-device-scmi/src/main.rs b/vhost-device-scmi/src/main.rs index 32b143d4..e4cf63d6 100644 --- a/vhost-device-scmi/src/main.rs +++ b/vhost-device-scmi/src/main.rs @@ -39,6 +39,7 @@ mod vhu_scmi; use devices::common::{print_devices_help, DeviceDescription, DeviceProperties}; use std::{ + path::PathBuf, process::exit, sync::{Arc, RwLock}, }; @@ -58,7 +59,7 @@ struct ScmiArgs { // Location of vhost-user Unix domain socket. // Required, unless one of the --help options is used. #[clap(short, long, help = "vhost-user socket to use (required)")] - socket_path: Option, + socket_path: Option, // Specification of SCMI devices to create. #[clap(short, long, help = "Devices to expose")] #[arg(num_args(1..))] @@ -68,7 +69,7 @@ struct ScmiArgs { } pub struct VuScmiConfig { - socket_path: String, + socket_path: PathBuf, devices: DeviceDescription, } @@ -76,10 +77,9 @@ impl TryFrom for VuScmiConfig { type Error = String; fn try_from(cmd_args: ScmiArgs) -> Result { - if cmd_args.socket_path.is_none() { + let Some(socket_path) = cmd_args.socket_path else { return Result::Err("Required argument socket-path was not provided".to_string()); - } - let socket_path = cmd_args.socket_path.unwrap().trim().to_string(); + }; let mut devices: DeviceDescription = vec![]; let device_iterator = cmd_args.device.iter(); for d in device_iterator { @@ -159,6 +159,7 @@ fn main() { #[cfg(test)] mod tests { use super::*; + use std::path::Path; #[test] fn test_command_line() { @@ -173,7 +174,7 @@ mod tests { let params: Vec<&str> = params_string.split_whitespace().collect(); let args: ScmiArgs = process_args(Parser::parse_from(params)).unwrap(); let config = VuScmiConfig::try_from(args).unwrap(); - assert_eq!(config.socket_path, path); + assert_eq!(&config.socket_path, Path::new(&path)); let devices = vec![ ("dummy".to_owned(), DeviceProperties::new(vec![])), ( diff --git a/vhost-device-scmi/src/vhu_scmi.rs b/vhost-device-scmi/src/vhu_scmi.rs index 683b327b..9c509481 100644 --- a/vhost-device-scmi/src/vhu_scmi.rs +++ b/vhost-device-scmi/src/vhu_scmi.rs @@ -587,7 +587,7 @@ mod tests { fn make_backend() -> VuScmiBackend { let config = VuScmiConfig { - socket_path: "/foo/scmi.sock".to_owned(), + socket_path: "/foo/scmi.sock".into(), devices: vec![], }; VuScmiBackend::new(&config).unwrap()