From b56539896e7175b38a7bf1706f1dea29d95bf90f Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Tue, 23 Jul 2024 12:45:55 +0200 Subject: [PATCH] sound: Use PathBuf for socket paths instead of Strings Signed-off-by: Bilal Elmoussaoui --- vhost-device-sound/src/device.rs | 8 ++------ vhost-device-sound/src/lib.rs | 11 ++++++----- vhost-device-sound/src/main.rs | 8 +++----- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/vhost-device-sound/src/device.rs b/vhost-device-sound/src/device.rs index 5fea9ea65..33acbb2f3 100644 --- a/vhost-device-sound/src/device.rs +++ b/vhost-device-sound/src/device.rs @@ -902,7 +902,7 @@ mod tests { fn test_sound_backend() { crate::init_logger(); let test_dir = tempdir().expect("Could not create a temp test directory."); - let socket_path = test_dir.path().join(SOCKET_PATH).display().to_string(); + let socket_path = test_dir.path().join(SOCKET_PATH); let config = SoundConfig::new(socket_path, false, BackendType::Null); let backend = VhostUserSoundBackend::new(config).expect("Could not create backend."); @@ -980,11 +980,7 @@ mod tests { crate::init_logger(); let test_dir = tempdir().expect("Could not create a temp test directory."); - let socket_path = test_dir - .path() - .join("sound_failures.socket") - .display() - .to_string(); + let socket_path = test_dir.path().join("sound_failures.socket"); let config = SoundConfig::new(socket_path, false, BackendType::Null); let backend = VhostUserSoundBackend::new(config); diff --git a/vhost-device-sound/src/lib.rs b/vhost-device-sound/src/lib.rs index 5cf788ffe..b84b48366 100644 --- a/vhost-device-sound/src/lib.rs +++ b/vhost-device-sound/src/lib.rs @@ -46,6 +46,7 @@ use std::{ convert::TryFrom, io::{Error as IoError, ErrorKind}, mem::size_of, + path::{Path, PathBuf}, sync::Arc, }; @@ -255,7 +256,7 @@ impl TryFrom for ControlMessageKind { /// is allowed to configure the backend. pub struct SoundConfig { /// vhost-user Unix domain socket - socket: String, + socket: PathBuf, /// use multiple threads to hanlde the virtqueues multi_thread: bool, /// audio backend variant @@ -265,7 +266,7 @@ pub struct SoundConfig { impl SoundConfig { /// Create a new instance of the SoundConfig struct, containing the /// parameters to be fed into the sound-backend server. - pub const fn new(socket: String, multi_thread: bool, audio_backend: BackendType) -> Self { + pub const fn new(socket: PathBuf, multi_thread: bool, audio_backend: BackendType) -> Self { Self { socket, multi_thread, @@ -275,8 +276,8 @@ impl SoundConfig { /// Return the path of the unix domain socket which is listening to /// requests from the guest. - pub fn get_socket_path(&self) -> String { - String::from(&self.socket) + pub fn get_socket_path(&self) -> &Path { + self.socket.as_path() } pub const fn get_audio_backend(&self) -> BackendType { @@ -344,7 +345,7 @@ impl Drop for IOMessage { /// vhost-device-sound backend server. pub fn start_backend_server(config: SoundConfig) { log::trace!("Using config {:?}.", &config); - let socket = config.get_socket_path(); + let socket = config.get_socket_path().to_path_buf(); let backend = Arc::new(VhostUserSoundBackend::new(config).unwrap()); let mut daemon = VhostUserDaemon::new( diff --git a/vhost-device-sound/src/main.rs b/vhost-device-sound/src/main.rs index a6f0047ec..dffd53d47 100644 --- a/vhost-device-sound/src/main.rs +++ b/vhost-device-sound/src/main.rs @@ -1,7 +1,7 @@ // Manos Pitsidianakis // Stefano Garzarella // SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause -use std::convert::TryFrom; +use std::{convert::TryFrom, path::PathBuf}; use clap::Parser; use vhost_device_sound::{start_backend_server, BackendType, Error, Result, SoundConfig}; @@ -11,7 +11,7 @@ use vhost_device_sound::{start_backend_server, BackendType, Error, Result, Sound struct SoundArgs { /// vhost-user Unix domain socket path. #[clap(long)] - socket: String, + socket: PathBuf, /// audio backend to be used #[clap(long)] #[clap(value_enum)] @@ -22,9 +22,7 @@ impl TryFrom for SoundConfig { type Error = Error; fn try_from(cmd_args: SoundArgs) -> Result { - let socket = cmd_args.socket.trim().to_string(); - - Ok(SoundConfig::new(socket, false, cmd_args.backend)) + Ok(SoundConfig::new(cmd_args.socket, false, cmd_args.backend)) } }