From 7f96eb2b672c50ab0845badf4b9cef9aa0bd147f Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Fri, 12 Mar 2021 13:24:03 +0000 Subject: [PATCH] vmm: migration: Simplify url socket handling in migration code Extract URL handling to a common function and simplify to remove url crate dependency. Signed-off-by: Rob Bradford --- vmm/src/lib.rs | 66 ++++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 43 deletions(-) diff --git a/vmm/src/lib.rs b/vmm/src/lib.rs index f76e58b9f..75d9fa2b1 100644 --- a/vmm/src/lib.rs +++ b/vmm/src/lib.rs @@ -43,6 +43,7 @@ use std::io::{Read, Write}; use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; use std::os::unix::net::UnixListener; use std::os::unix::net::UnixStream; +use std::path::PathBuf; use std::sync::mpsc::{Receiver, RecvError, SendError, Sender}; use std::sync::{Arc, Mutex}; use std::{result, thread}; @@ -811,6 +812,14 @@ impl Vmm { Ok(()) } + fn socket_url_to_path(url: &str) -> result::Result { + url.strip_prefix("unix:") + .ok_or_else(|| { + MigratableError::MigrateSend(anyhow!("Could not extract path from URL: {}", url)) + }) + .map(|s| s.into()) + } + fn vm_receive_migration( &mut self, receive_data_migration: VmReceiveMigrationData, @@ -820,34 +829,16 @@ impl Vmm { receive_data_migration.receiver_url ); - let url = url::Url::parse(&receive_data_migration.receiver_url) - .map_err(|e| MigratableError::MigrateReceive(anyhow!("Error parsing URL: {}", e)))?; - - let mut socket = match url.scheme() { - "unix" => { - let path = url.to_file_path().map_err(|_| { - MigratableError::MigrateReceive(anyhow!("Error extracting path from URL")) - })?; - let listener = UnixListener::bind(&path).map_err(|e| { - MigratableError::MigrateReceive(anyhow!("Error binding to UNIX socket: {}", e)) - })?; - let (socket, _addr) = listener.accept().map_err(|e| { - MigratableError::MigrateReceive(anyhow!( - "Error accepting on UNIX socket: {}", - e - )) - })?; - std::fs::remove_file(&path).map_err(|e| { - MigratableError::MigrateReceive(anyhow!("Error unlinking UNIX socket: {}", e)) - })?; - socket - } - _ => { - return Err(MigratableError::MigrateReceive(anyhow!( - "Unsupported URL scheme" - ))) - } - }; + let path = Self::socket_url_to_path(&receive_data_migration.receiver_url)?; + let listener = UnixListener::bind(&path).map_err(|e| { + MigratableError::MigrateReceive(anyhow!("Error binding to UNIX socket: {}", e)) + })?; + let (mut socket, _addr) = listener.accept().map_err(|e| { + MigratableError::MigrateReceive(anyhow!("Error accepting on UNIX socket: {}", e)) + })?; + std::fs::remove_file(&path).map_err(|e| { + MigratableError::MigrateReceive(anyhow!("Error unlinking UNIX socket: {}", e)) + })?; let mut started = false; let mut vm: Option = None; @@ -968,21 +959,10 @@ impl Vmm { send_data_migration.destination_url ); if let Some(ref mut vm) = self.vm { - let url = url::Url::parse(&send_data_migration.destination_url) - .map_err(|e| MigratableError::MigrateSend(anyhow!("Error parsing URL: {}", e)))?; - let mut socket = match url.scheme() { - "unix" => UnixStream::connect(url.to_file_path().map_err(|_| { - MigratableError::MigrateSend(anyhow!("Error extracting path from URL")) - })?) - .map_err(|e| { - MigratableError::MigrateSend(anyhow!("Error connecting to UNIX socket: {}", e)) - })?, - _ => { - return Err(MigratableError::MigrateReceive(anyhow!( - "Unsupported URL scheme" - ))) - } - }; + let path = Self::socket_url_to_path(&send_data_migration.destination_url)?; + let mut socket = UnixStream::connect(&path).map_err(|e| { + MigratableError::MigrateSend(anyhow!("Error connecting to UNIX socket: {}", e)) + })?; // Start the migration Request::start().write_to(&mut socket)?;