From afcb2b285f9ccac96faa2c6338011b2f038bcc1d Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Wed, 26 Nov 2025 14:04:47 +0100 Subject: [PATCH] tpm: remove mixture of str and Path `impl AsRef` is the most idiomatic way to consume paths in Rust. I removed the mixture. Signed-off-by: Philipp Schuster On-behalf-of: SAP philipp.schuster@sap.com --- devices/src/tpm.rs | 3 ++- tpm/src/emulator.rs | 8 +++++--- tpm/src/socket.rs | 9 ++++++--- vmm/src/device_manager.rs | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/devices/src/tpm.rs b/devices/src/tpm.rs index 86866b2e7..75a0a9e42 100644 --- a/devices/src/tpm.rs +++ b/devices/src/tpm.rs @@ -4,6 +4,7 @@ // use std::cmp; +use std::path::Path; use std::sync::{Arc, Barrier}; use anyhow::anyhow; @@ -227,7 +228,7 @@ pub struct Tpm { } impl Tpm { - pub fn new(path: &str) -> Result { + pub fn new(path: impl AsRef) -> Result { let emulator = Emulator::new(path) .map_err(|e| Error::Init(anyhow!("Failed while initializing tpm Emulator: {e:?}")))?; let mut tpm = Tpm { diff --git a/tpm/src/emulator.rs b/tpm/src/emulator.rs index 3699c3da7..b27a069aa 100644 --- a/tpm/src/emulator.rs +++ b/tpm/src/emulator.rs @@ -86,10 +86,12 @@ impl Emulator { /// /// * `path` - A path to the Unix Domain Socket swtpm is listening on /// - pub fn new(path: &str) -> Result { - if !Path::new(&path).exists() { + pub fn new(path: impl AsRef) -> Result { + let path = path.as_ref(); + if !path.exists() { return Err(Error::InitializeEmulator(anyhow!( - "The input TPM Socket path: {path:?} does not exist" + "The input TPM Socket path: {:?} does not exist", + path.to_str().unwrap() ))); } let mut socket = SocketDev::new(); diff --git a/tpm/src/socket.rs b/tpm/src/socket.rs index 4fe5b6370..b77768153 100644 --- a/tpm/src/socket.rs +++ b/tpm/src/socket.rs @@ -6,6 +6,7 @@ use std::io::Read; use std::os::unix::io::{AsRawFd, RawFd}; use std::os::unix::net::UnixStream; +use std::path::Path; use anyhow::anyhow; use log::debug; @@ -58,21 +59,23 @@ impl SocketDev { } } - pub fn init(&mut self, path: &str) -> Result<()> { + pub fn init(&mut self, path: impl AsRef) -> Result<()> { self.connect(path)?; Ok(()) } - pub fn connect(&mut self, socket_path: &str) -> Result<()> { + pub fn connect(&mut self, socket_path: impl AsRef) -> Result<()> { + let socket_path = socket_path.as_ref(); self.state = SocketDevState::Connecting; + let socket_path_s = socket_path.to_str().unwrap(); let s = UnixStream::connect(socket_path).map_err(|e| { Error::ConnectToSocket(anyhow!("Failed to connect to tpm Socket. Error: {e:?}")) })?; self.control_fd = s.as_raw_fd(); self.stream = Some(s); self.state = SocketDevState::Connected; - debug!("Connected to tpm socket path : {socket_path:?}"); + debug!("Connected to tpm socket path : {socket_path_s:?}"); Ok(()) } diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 93d863c52..fac6ef7da 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -2491,7 +2491,7 @@ impl DeviceManager { tpm_path: &Path, ) -> DeviceManagerResult>> { // Create TPM Device - let tpm = devices::tpm::Tpm::new(tpm_path.to_str().unwrap()).map_err(|e| { + let tpm = devices::tpm::Tpm::new(tpm_path).map_err(|e| { DeviceManagerError::CreateTpmDevice(anyhow!("Failed to create TPM Device : {e:?}")) })?; let tpm = Arc::new(Mutex::new(tpm));