tpm: remove mixture of str and Path

`impl AsRef<Path>` is the most idiomatic way to consume paths
in Rust. I removed the mixture.

Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
On-behalf-of: SAP philipp.schuster@sap.com
This commit is contained in:
Philipp Schuster 2025-11-26 14:04:47 +01:00 committed by Rob Bradford
parent c53781bf5f
commit afcb2b285f
4 changed files with 14 additions and 8 deletions

View file

@ -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<Self> {
pub fn new(path: impl AsRef<Path>) -> Result<Self> {
let emulator = Emulator::new(path)
.map_err(|e| Error::Init(anyhow!("Failed while initializing tpm Emulator: {e:?}")))?;
let mut tpm = Tpm {

View file

@ -86,10 +86,12 @@ impl Emulator {
///
/// * `path` - A path to the Unix Domain Socket swtpm is listening on
///
pub fn new(path: &str) -> Result<Self> {
if !Path::new(&path).exists() {
pub fn new(path: impl AsRef<Path>) -> Result<Self> {
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();

View file

@ -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<Path>) -> Result<()> {
self.connect(path)?;
Ok(())
}
pub fn connect(&mut self, socket_path: &str) -> Result<()> {
pub fn connect(&mut self, socket_path: impl AsRef<Path>) -> 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(())
}

View file

@ -2491,7 +2491,7 @@ impl DeviceManager {
tpm_path: &Path,
) -> DeviceManagerResult<Arc<Mutex<devices::tpm::Tpm>>> {
// 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));