Allow using Path/PathBuf for initialization
Instead of taking a `&str` for the path of the sockets, take `AsRef<Path>`. This way users can pass `PathBuf`, `Path`, `String`, or `&str`. Signed-off-by: Dylan Reid <dgreid@chromium.org>
This commit is contained in:
parent
62fd4ec5a4
commit
7784304860
3 changed files with 32 additions and 26 deletions
|
|
@ -9,6 +9,7 @@ use std::io::ErrorKind;
|
|||
use std::marker::PhantomData;
|
||||
use std::os::unix::io::{AsRawFd, RawFd};
|
||||
use std::os::unix::net::{UnixListener, UnixStream};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::{mem, slice};
|
||||
|
||||
use libc::{c_void, iovec};
|
||||
|
|
@ -20,7 +21,7 @@ use super::{Error, Result};
|
|||
/// Unix domain socket listener for accepting incoming connections.
|
||||
pub struct Listener {
|
||||
fd: UnixListener,
|
||||
path: String,
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
impl Listener {
|
||||
|
|
@ -29,14 +30,14 @@ impl Listener {
|
|||
/// # Return:
|
||||
/// * - the new Listener object on success.
|
||||
/// * - SocketError: failed to create listener socket.
|
||||
pub fn new(path: &str, unlink: bool) -> Result<Self> {
|
||||
pub fn new<P: AsRef<Path>>(path: P, unlink: bool) -> Result<Self> {
|
||||
if unlink {
|
||||
let _ = std::fs::remove_file(path);
|
||||
let _ = std::fs::remove_file(&path);
|
||||
}
|
||||
let fd = UnixListener::bind(path).map_err(Error::SocketError)?;
|
||||
let fd = UnixListener::bind(&path).map_err(Error::SocketError)?;
|
||||
Ok(Listener {
|
||||
fd,
|
||||
path: path.to_string(),
|
||||
path: path.as_ref().to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -83,7 +84,7 @@ impl AsRawFd for Listener {
|
|||
|
||||
impl Drop for Listener {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_file(self.path.clone());
|
||||
let _ = std::fs::remove_file(&self.path);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -99,7 +100,7 @@ impl<R: Req> Endpoint<R> {
|
|||
/// # Return:
|
||||
/// * - the new Endpoint object on success.
|
||||
/// * - SocketConnect: failed to connect to peer.
|
||||
pub fn connect(path: &str) -> Result<Self> {
|
||||
pub fn connect<P: AsRef<Path>>(path: P) -> Result<Self> {
|
||||
let sock = UnixStream::connect(path).map_err(Error::SocketConnect)?;
|
||||
Ok(Self::from_stream(sock))
|
||||
}
|
||||
|
|
@ -613,11 +614,11 @@ mod tests {
|
|||
use vmm_sys_util::rand::rand_alphanumerics;
|
||||
use vmm_sys_util::tempfile::TempFile;
|
||||
|
||||
fn temp_path() -> String {
|
||||
format!(
|
||||
fn temp_path() -> PathBuf {
|
||||
PathBuf::from(format!(
|
||||
"/tmp/vhost_test_{}",
|
||||
rand_alphanumerics(8).to_str().unwrap()
|
||||
)
|
||||
))
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
use std::mem;
|
||||
use std::os::unix::io::{AsRawFd, RawFd};
|
||||
use std::os::unix::net::UnixStream;
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex, MutexGuard};
|
||||
|
||||
use vmm_sys_util::eventfd::EventFd;
|
||||
|
|
@ -93,10 +94,10 @@ impl Master {
|
|||
///
|
||||
/// # Arguments
|
||||
/// * `path` - path of Unix domain socket listener to connect to
|
||||
pub fn connect(path: &str, max_queue_num: u64) -> Result<Self> {
|
||||
pub fn connect<P: AsRef<Path>>(path: P, max_queue_num: u64) -> Result<Self> {
|
||||
let mut retry_count = 5;
|
||||
let endpoint = loop {
|
||||
match Endpoint::<MasterReq>::connect(path) {
|
||||
match Endpoint::<MasterReq>::connect(&path) {
|
||||
Ok(endpoint) => break Ok(endpoint),
|
||||
Err(e) => match &e {
|
||||
VhostUserError::SocketConnect(why) => {
|
||||
|
|
@ -646,15 +647,17 @@ mod tests {
|
|||
use super::*;
|
||||
use vmm_sys_util::rand::rand_alphanumerics;
|
||||
|
||||
fn temp_path() -> String {
|
||||
format!(
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn temp_path() -> PathBuf {
|
||||
PathBuf::from(format!(
|
||||
"/tmp/vhost_test_{}",
|
||||
rand_alphanumerics(8).to_str().unwrap()
|
||||
)
|
||||
))
|
||||
}
|
||||
|
||||
fn create_pair(path: &str) -> (Master, Endpoint<MasterReq>) {
|
||||
let listener = Listener::new(path, true).unwrap();
|
||||
fn create_pair<P: AsRef<Path>>(path: P) -> (Master, Endpoint<MasterReq>) {
|
||||
let listener = Listener::new(&path, true).unwrap();
|
||||
listener.set_nonblocking(true).unwrap();
|
||||
let master = Master::connect(path, 2).unwrap();
|
||||
let slave = listener.accept().unwrap().unwrap();
|
||||
|
|
|
|||
|
|
@ -181,6 +181,7 @@ mod dummy_slave;
|
|||
#[cfg(all(test, feature = "vhost-user-master", feature = "vhost-user-slave"))]
|
||||
mod tests {
|
||||
use std::os::unix::io::AsRawFd;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Barrier, Mutex};
|
||||
use std::thread;
|
||||
use vmm_sys_util::rand::rand_alphanumerics;
|
||||
|
|
@ -191,20 +192,21 @@ mod tests {
|
|||
use crate::backend::VhostBackend;
|
||||
use crate::{VhostUserMemoryRegionInfo, VringConfigData};
|
||||
|
||||
fn temp_path() -> String {
|
||||
format!(
|
||||
fn temp_path() -> PathBuf {
|
||||
PathBuf::from(format!(
|
||||
"/tmp/vhost_test_{}",
|
||||
rand_alphanumerics(8).to_str().unwrap()
|
||||
)
|
||||
))
|
||||
}
|
||||
|
||||
fn create_slave<S: VhostUserSlaveReqHandler>(
|
||||
path: &str,
|
||||
backend: Arc<S>,
|
||||
) -> (Master, SlaveReqHandler<S>) {
|
||||
let listener = Listener::new(path, true).unwrap();
|
||||
fn create_slave<P, S>(path: P, backend: Arc<S>) -> (Master, SlaveReqHandler<S>)
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
S: VhostUserSlaveReqHandler,
|
||||
{
|
||||
let listener = Listener::new(&path, true).unwrap();
|
||||
let mut slave_listener = SlaveListener::new(listener, backend).unwrap();
|
||||
let master = Master::connect(path, 1).unwrap();
|
||||
let master = Master::connect(&path, 1).unwrap();
|
||||
(master, slave_listener.accept().unwrap().unwrap())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue