Skip to content

Commit 2620820

Browse files
luigix25epilys
authored andcommitted
sound: Use PathBuf for socket paths instead of Strings
Field `socket` now use PathBuf. This allows for better filesystem compatibility. Signed-off-by: Luigi Leonardi <[email protected]>
1 parent 7f74da7 commit 2620820

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

vhost-device-sound/src/device.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ impl VhostUserBackend for VhostUserSoundBackend {
675675

676676
#[cfg(test)]
677677
mod tests {
678+
use std::path::PathBuf;
678679
use tempfile::tempdir;
679680
use virtio_bindings::virtio_ring::VRING_DESC_F_WRITE;
680681
use virtio_queue::{mock::MockSplitQueue, Descriptor};
@@ -727,7 +728,7 @@ mod tests {
727728
#[test]
728729
fn test_sound_thread_success() {
729730
crate::init_logger();
730-
let config = SoundConfig::new(SOCKET_PATH.to_string(), false, BackendType::Null);
731+
let config = SoundConfig::new(PathBuf::from(SOCKET_PATH), false, BackendType::Null);
731732

732733
let chmaps = Arc::new(RwLock::new(vec![]));
733734
let jacks = Arc::new(RwLock::new(vec![]));
@@ -822,7 +823,7 @@ mod tests {
822823
#[test]
823824
fn test_sound_thread_failure() {
824825
crate::init_logger();
825-
let config = SoundConfig::new(SOCKET_PATH.to_string(), false, BackendType::Null);
826+
let config = SoundConfig::new(PathBuf::from(SOCKET_PATH), false, BackendType::Null);
826827

827828
let chmaps = Arc::new(RwLock::new(vec![]));
828829
let jacks = Arc::new(RwLock::new(vec![]));
@@ -903,7 +904,7 @@ mod tests {
903904
fn test_sound_backend() {
904905
crate::init_logger();
905906
let test_dir = tempdir().expect("Could not create a temp test directory.");
906-
let socket_path = test_dir.path().join(SOCKET_PATH).display().to_string();
907+
let socket_path = test_dir.path().join(SOCKET_PATH);
907908
let config = SoundConfig::new(socket_path, false, BackendType::Null);
908909
let backend = VhostUserSoundBackend::new(config).expect("Could not create backend.");
909910

@@ -981,11 +982,8 @@ mod tests {
981982
crate::init_logger();
982983
let test_dir = tempdir().expect("Could not create a temp test directory.");
983984

984-
let socket_path = test_dir
985-
.path()
986-
.join("sound_failures.socket")
987-
.display()
988-
.to_string();
985+
let socket_path = test_dir.path().join("sound_failures.socket");
986+
989987
let config = SoundConfig::new(socket_path, false, BackendType::Null);
990988
let backend = VhostUserSoundBackend::new(config);
991989

vhost-device-sound/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use std::{
4646
convert::TryFrom,
4747
io::{Error as IoError, ErrorKind},
4848
mem::size_of,
49+
path::PathBuf,
4950
sync::Arc,
5051
};
5152

@@ -255,7 +256,7 @@ impl TryFrom<Le32> for ControlMessageKind {
255256
/// is allowed to configure the backend.
256257
pub struct SoundConfig {
257258
/// vhost-user Unix domain socket
258-
socket: String,
259+
socket: PathBuf,
259260
/// use multiple threads to hanlde the virtqueues
260261
multi_thread: bool,
261262
/// audio backend variant
@@ -265,7 +266,7 @@ pub struct SoundConfig {
265266
impl SoundConfig {
266267
/// Create a new instance of the SoundConfig struct, containing the
267268
/// parameters to be fed into the sound-backend server.
268-
pub const fn new(socket: String, multi_thread: bool, audio_backend: BackendType) -> Self {
269+
pub const fn new(socket: PathBuf, multi_thread: bool, audio_backend: BackendType) -> Self {
269270
Self {
270271
socket,
271272
multi_thread,
@@ -275,8 +276,8 @@ impl SoundConfig {
275276

276277
/// Return the path of the unix domain socket which is listening to
277278
/// requests from the guest.
278-
pub fn get_socket_path(&self) -> String {
279-
String::from(&self.socket)
279+
pub fn get_socket_path(&self) -> PathBuf {
280+
self.socket.clone()
280281
}
281282

282283
pub const fn get_audio_backend(&self) -> BackendType {
@@ -372,7 +373,7 @@ mod tests {
372373
const SOCKET_PATH: &str = "vsound.socket";
373374
crate::init_logger();
374375

375-
let config = SoundConfig::new(SOCKET_PATH.to_string(), false, BackendType::Null);
376+
let config = SoundConfig::new(PathBuf::from(SOCKET_PATH), false, BackendType::Null);
376377

377378
let backend = Arc::new(VhostUserSoundBackend::new(config).unwrap());
378379
let daemon = VhostUserDaemon::new(

vhost-device-sound/src/main.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Manos Pitsidianakis <[email protected]>
22
// Stefano Garzarella <[email protected]>
33
// SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause
4-
use std::convert::TryFrom;
4+
use std::{convert::TryFrom, path::PathBuf};
55

66
use clap::Parser;
77
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
1111
struct SoundArgs {
1212
/// vhost-user Unix domain socket path.
1313
#[clap(long)]
14-
socket: String,
14+
socket: PathBuf,
1515
/// audio backend to be used
1616
#[clap(long)]
1717
#[clap(value_enum)]
@@ -22,9 +22,7 @@ impl TryFrom<SoundArgs> for SoundConfig {
2222
type Error = Error;
2323

2424
fn try_from(cmd_args: SoundArgs) -> Result<Self> {
25-
let socket = cmd_args.socket.trim().to_string();
26-
27-
Ok(SoundConfig::new(socket, false, cmd_args.backend))
25+
Ok(SoundConfig::new(cmd_args.socket, false, cmd_args.backend))
2826
}
2927
}
3028

@@ -45,9 +43,9 @@ mod tests {
4543
use super::*;
4644

4745
impl SoundArgs {
48-
fn from_args(socket: &str) -> Self {
46+
fn from_args(socket: PathBuf) -> Self {
4947
SoundArgs {
50-
socket: socket.to_string(),
48+
socket,
5149
backend: BackendType::default(),
5250
}
5351
}
@@ -61,13 +59,16 @@ mod tests {
6159
#[test]
6260
fn test_sound_config_setup() {
6361
init_logger();
64-
let args = SoundArgs::from_args("/tmp/vhost-sound.socket");
62+
let args = SoundArgs::from_args(PathBuf::from("/tmp/vhost-sound.socket"));
6563

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

6967
let config = config.unwrap();
70-
assert_eq!(config.get_socket_path(), "/tmp/vhost-sound.socket");
68+
assert_eq!(
69+
config.get_socket_path(),
70+
PathBuf::from("/tmp/vhost-sound.socket")
71+
);
7172
}
7273

7374
#[rstest]

0 commit comments

Comments
 (0)