7
7
8
8
use log:: error;
9
9
use std:: num:: ParseIntError ;
10
+ use std:: path:: PathBuf ;
10
11
use std:: process:: exit;
11
12
use std:: sync:: { Arc , RwLock } ;
12
13
use std:: thread:: { spawn, JoinHandle } ;
@@ -58,7 +59,7 @@ Example, \"-c 4 -l 3:s4:6:s1\"\n";
58
59
struct GpioArgs {
59
60
/// Location of vhost-user Unix domain socket. This is suffixed by 0,1,2..socket_count-1.
60
61
#[ clap( short, long) ]
61
- socket_path : String ,
62
+ socket_path : PathBuf ,
62
63
63
64
/// Number of guests (sockets) to connect to.
64
65
#[ clap( short = 'c' , long, default_value_t = 1 ) ]
@@ -143,7 +144,7 @@ impl TryFrom<&str> for DeviceConfig {
143
144
144
145
#[ derive( PartialEq , Debug ) ]
145
146
struct GpioConfiguration {
146
- socket_path : String ,
147
+ socket_path : PathBuf ,
147
148
socket_count : usize ,
148
149
devices : DeviceConfig ,
149
150
}
@@ -173,7 +174,7 @@ impl TryFrom<GpioArgs> for GpioConfiguration {
173
174
}
174
175
}
175
176
176
- fn start_device_backend < D : GpioDevice > ( device : D , socket : String ) -> Result < ( ) > {
177
+ fn start_device_backend < D : GpioDevice > ( device : D , socket : PathBuf ) -> Result < ( ) > {
177
178
let controller = GpioController :: new ( device) . map_err ( Error :: CouldNotCreateGpioController ) ?;
178
179
let backend = Arc :: new ( RwLock :: new (
179
180
VhostUserGpioBackend :: new ( controller) . map_err ( Error :: CouldNotCreateBackend ) ?,
@@ -196,7 +197,9 @@ fn start_backend(args: GpioArgs) -> Result<()> {
196
197
let mut handles = Vec :: new ( ) ;
197
198
198
199
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
+
200
203
let cfg = config. devices . inner [ i] ;
201
204
202
205
let handle: JoinHandle < Result < ( ) > > = spawn ( move || loop {
@@ -258,9 +261,9 @@ mod tests {
258
261
}
259
262
}
260
263
261
- fn get_cmd_args ( path : & str , devices : & str , count : usize ) -> GpioArgs {
264
+ fn get_cmd_args ( path : PathBuf , devices : & str , count : usize ) -> GpioArgs {
262
265
GpioArgs {
263
- socket_path : path. to_string ( ) ,
266
+ socket_path : path,
264
267
socket_count : count,
265
268
device_list : devices. to_string ( ) ,
266
269
}
@@ -287,31 +290,31 @@ mod tests {
287
290
288
291
#[ test]
289
292
fn test_gpio_parse_failure ( ) {
290
- let socket_name = "vgpio.sock" ;
293
+ let socket_name = PathBuf :: from ( "vgpio.sock" ) ;
291
294
292
295
// 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 ) ;
294
297
assert_matches ! (
295
298
GpioConfiguration :: try_from( cmd_args) . unwrap_err( ) ,
296
299
Error :: ParseFailure ( e) if e == "4d" . parse:: <u32 >( ) . unwrap_err( )
297
300
) ;
298
301
299
302
// 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 ) ;
301
304
assert_matches ! (
302
305
GpioConfiguration :: try_from( cmd_args) . unwrap_err( ) ,
303
306
Error :: SocketCountInvalid ( 0 )
304
307
) ;
305
308
306
309
// 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 ) ;
308
311
assert_matches ! (
309
312
GpioConfiguration :: try_from( cmd_args) . unwrap_err( ) ,
310
313
Error :: DeviceDuplicate ( 4 )
311
314
) ;
312
315
313
316
// 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 ) ;
315
318
assert_matches ! (
316
319
GpioConfiguration :: try_from( cmd_args) . unwrap_err( ) ,
317
320
Error :: DeviceCountMismatch ( 5 , 4 )
@@ -324,16 +327,16 @@ mod tests {
324
327
325
328
#[ test]
326
329
fn test_gpio_parse_successful ( ) {
327
- let socket_name = "vgpio.sock" ;
330
+ let socket_name = PathBuf :: from ( "vgpio.sock" ) ;
328
331
329
332
// 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 ) ;
331
334
let config = GpioConfiguration :: try_from ( cmd_args) . unwrap ( ) ;
332
335
333
336
let expected_devices = DeviceConfig :: new_with ( vec ! [ 1 , 4 , 32 , 21 , 5 ] ) ;
334
337
let expected_config = GpioConfiguration {
335
338
socket_count : 5 ,
336
- socket_path : String :: from ( socket_name) ,
339
+ socket_path : socket_name,
337
340
devices : expected_devices,
338
341
} ;
339
342
@@ -342,7 +345,7 @@ mod tests {
342
345
343
346
#[ test]
344
347
fn test_gpio_fail_listener_mock ( ) {
345
- let socket_name = "~/path/not/present/gpio" ;
348
+ let socket_name = PathBuf :: from ( "~/path/not/present/gpio" ) ;
346
349
let cmd_args = get_cmd_args ( socket_name, "s1:s4:s3:s5" , 4 ) ;
347
350
348
351
assert_matches ! ( start_backend( cmd_args) . unwrap_err( ) , Error :: ServeFailed ( _) ) ;
0 commit comments