Skip to content

Commit

Permalink
sound: Use PathBuf for socket paths instead of Strings
Browse files Browse the repository at this point in the history
Signed-off-by: Bilal Elmoussaoui <[email protected]>
  • Loading branch information
bilelmoussaoui committed Jul 23, 2024
1 parent e3d14d4 commit 04a69be
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
12 changes: 4 additions & 8 deletions vhost-device-sound/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ mod tests {
#[test]
fn test_sound_thread_success() {
crate::init_logger();
let config = SoundConfig::new(SOCKET_PATH.to_string(), false, BackendType::Null);
let config = SoundConfig::new(SOCKET_PATH.into(), false, BackendType::Null);

let chmaps = Arc::new(RwLock::new(vec![]));
let jacks = Arc::new(RwLock::new(vec![]));
Expand Down Expand Up @@ -821,7 +821,7 @@ mod tests {
#[test]
fn test_sound_thread_failure() {
crate::init_logger();
let config = SoundConfig::new(SOCKET_PATH.to_string(), false, BackendType::Null);
let config = SoundConfig::new(SOCKET_PATH.into(), false, BackendType::Null);

let chmaps = Arc::new(RwLock::new(vec![]));
let jacks = Arc::new(RwLock::new(vec![]));
Expand Down Expand Up @@ -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.");

Expand Down Expand Up @@ -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);

Expand Down
13 changes: 7 additions & 6 deletions vhost-device-sound/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use std::{
convert::TryFrom,
io::{Error as IoError, ErrorKind},
mem::size_of,
path::{Path, PathBuf},
sync::Arc,
};

Expand Down Expand Up @@ -255,7 +256,7 @@ impl TryFrom<Le32> 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
Expand All @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -372,7 +373,7 @@ mod tests {
const SOCKET_PATH: &str = "vsound.socket";
crate::init_logger();

let config = SoundConfig::new(SOCKET_PATH.to_string(), false, BackendType::Null);
let config = SoundConfig::new(SOCKET_PATH.into(), false, BackendType::Null);

let backend = Arc::new(VhostUserSoundBackend::new(config).unwrap());
let daemon = VhostUserDaemon::new(
Expand Down
19 changes: 10 additions & 9 deletions vhost-device-sound/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Manos Pitsidianakis <[email protected]>
// Stefano Garzarella <[email protected]>
// 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};
Expand All @@ -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)]
Expand All @@ -22,9 +22,7 @@ impl TryFrom<SoundArgs> for SoundConfig {
type Error = Error;

fn try_from(cmd_args: SoundArgs) -> Result<Self> {
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))
}
}

Expand All @@ -45,9 +43,9 @@ mod tests {
use super::*;

impl SoundArgs {
fn from_args(socket: &str) -> Self {
fn from_args(socket: PathBuf) -> Self {
SoundArgs {
socket: socket.to_string(),
socket,
backend: BackendType::default(),
}
}
Expand All @@ -61,13 +59,16 @@ mod tests {
#[test]
fn test_sound_config_setup() {
init_logger();
let args = SoundArgs::from_args("/tmp/vhost-sound.socket");
let args = SoundArgs::from_args(PathBuf::from("/tmp/vhost-sound.socket"));

let config = SoundConfig::try_from(args);
assert!(config.is_ok());

let config = config.unwrap();
assert_eq!(config.get_socket_path(), "/tmp/vhost-sound.socket");
assert_eq!(
config.get_socket_path(),
PathBuf::from("/tmp/vhost-sound.socket")
);
}

#[rstest]
Expand Down

0 comments on commit 04a69be

Please sign in to comment.