Skip to content

Commit 7f74da7

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

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

vhost-device-gpio/src/backend.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use log::error;
99
use std::num::ParseIntError;
10+
use std::path::PathBuf;
1011
use std::process::exit;
1112
use std::sync::{Arc, RwLock};
1213
use std::thread::{spawn, JoinHandle};
@@ -58,7 +59,7 @@ Example, \"-c 4 -l 3:s4:6:s1\"\n";
5859
struct GpioArgs {
5960
/// Location of vhost-user Unix domain socket. This is suffixed by 0,1,2..socket_count-1.
6061
#[clap(short, long)]
61-
socket_path: String,
62+
socket_path: PathBuf,
6263

6364
/// Number of guests (sockets) to connect to.
6465
#[clap(short = 'c', long, default_value_t = 1)]
@@ -143,7 +144,7 @@ impl TryFrom<&str> for DeviceConfig {
143144

144145
#[derive(PartialEq, Debug)]
145146
struct GpioConfiguration {
146-
socket_path: String,
147+
socket_path: PathBuf,
147148
socket_count: usize,
148149
devices: DeviceConfig,
149150
}
@@ -173,7 +174,7 @@ impl TryFrom<GpioArgs> for GpioConfiguration {
173174
}
174175
}
175176

176-
fn start_device_backend<D: GpioDevice>(device: D, socket: String) -> Result<()> {
177+
fn start_device_backend<D: GpioDevice>(device: D, socket: PathBuf) -> Result<()> {
177178
let controller = GpioController::new(device).map_err(Error::CouldNotCreateGpioController)?;
178179
let backend = Arc::new(RwLock::new(
179180
VhostUserGpioBackend::new(controller).map_err(Error::CouldNotCreateBackend)?,
@@ -196,7 +197,9 @@ fn start_backend(args: GpioArgs) -> Result<()> {
196197
let mut handles = Vec::new();
197198

198199
for i in 0..config.socket_count {
199-
let socket = config.socket_path.to_owned() + &i.to_string();
200+
let mut socket = config.socket_path.clone();
201+
socket.as_mut_os_string().push(i.to_string());
202+
200203
let cfg = config.devices.inner[i];
201204

202205
let handle: JoinHandle<Result<()>> = spawn(move || loop {
@@ -258,9 +261,9 @@ mod tests {
258261
}
259262
}
260263

261-
fn get_cmd_args(path: &str, devices: &str, count: usize) -> GpioArgs {
264+
fn get_cmd_args(path: PathBuf, devices: &str, count: usize) -> GpioArgs {
262265
GpioArgs {
263-
socket_path: path.to_string(),
266+
socket_path: path,
264267
socket_count: count,
265268
device_list: devices.to_string(),
266269
}
@@ -287,31 +290,31 @@ mod tests {
287290

288291
#[test]
289292
fn test_gpio_parse_failure() {
290-
let socket_name = "vgpio.sock";
293+
let socket_name = PathBuf::from("vgpio.sock");
291294

292295
// Invalid device number
293-
let cmd_args = get_cmd_args(socket_name, "1:4d:5", 3);
296+
let cmd_args = get_cmd_args(socket_name.clone(), "1:4d:5", 3);
294297
assert_matches!(
295298
GpioConfiguration::try_from(cmd_args).unwrap_err(),
296299
Error::ParseFailure(e) if e == "4d".parse::<u32>().unwrap_err()
297300
);
298301

299302
// Zero socket count
300-
let cmd_args = get_cmd_args(socket_name, "1:4", 0);
303+
let cmd_args = get_cmd_args(socket_name.clone(), "1:4", 0);
301304
assert_matches!(
302305
GpioConfiguration::try_from(cmd_args).unwrap_err(),
303306
Error::SocketCountInvalid(0)
304307
);
305308

306309
// Duplicate client address: 4
307-
let cmd_args = get_cmd_args(socket_name, "1:4:5:6:4", 5);
310+
let cmd_args = get_cmd_args(socket_name.clone(), "1:4:5:6:4", 5);
308311
assert_matches!(
309312
GpioConfiguration::try_from(cmd_args).unwrap_err(),
310313
Error::DeviceDuplicate(4)
311314
);
312315

313316
// Device count mismatch
314-
let cmd_args = get_cmd_args(socket_name, "1:4:5:6", 5);
317+
let cmd_args = get_cmd_args(socket_name.clone(), "1:4:5:6", 5);
315318
assert_matches!(
316319
GpioConfiguration::try_from(cmd_args).unwrap_err(),
317320
Error::DeviceCountMismatch(5, 4)
@@ -324,16 +327,16 @@ mod tests {
324327

325328
#[test]
326329
fn test_gpio_parse_successful() {
327-
let socket_name = "vgpio.sock";
330+
let socket_name = PathBuf::from("vgpio.sock");
328331

329332
// Match expected and actual configuration
330-
let cmd_args = get_cmd_args(socket_name, "1:4:32:21:5", 5);
333+
let cmd_args = get_cmd_args(socket_name.clone(), "1:4:32:21:5", 5);
331334
let config = GpioConfiguration::try_from(cmd_args).unwrap();
332335

333336
let expected_devices = DeviceConfig::new_with(vec![1, 4, 32, 21, 5]);
334337
let expected_config = GpioConfiguration {
335338
socket_count: 5,
336-
socket_path: String::from(socket_name),
339+
socket_path: socket_name,
337340
devices: expected_devices,
338341
};
339342

@@ -342,7 +345,7 @@ mod tests {
342345

343346
#[test]
344347
fn test_gpio_fail_listener_mock() {
345-
let socket_name = "~/path/not/present/gpio";
348+
let socket_name = PathBuf::from("~/path/not/present/gpio");
346349
let cmd_args = get_cmd_args(socket_name, "s1:s4:s3:s5", 4);
347350

348351
assert_matches!(start_backend(cmd_args).unwrap_err(), Error::ServeFailed(_));

0 commit comments

Comments
 (0)