xenstore: adopt thiserror

This commit is contained in:
Alex Zenla 2024-01-30 01:49:56 -08:00
parent cc0b2d97b5
commit 812e357bc9
No known key found for this signature in database
GPG Key ID: 067B238899B51269
4 changed files with 29 additions and 69 deletions

View File

@ -13,6 +13,7 @@ resolver = "2"
version = "0.0.1" version = "0.0.1"
[workspace.dependencies] [workspace.dependencies]
thiserror = "1.0"
log = "0.4.20" log = "0.4.20"
libc = "0.2" libc = "0.2"
nix = "0.27.1" nix = "0.27.1"

View File

@ -8,6 +8,7 @@ resolver = "2"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
thiserror = { workspace = true }
libc = { workspace = true } libc = { workspace = true }
log = { workspace = true } log = { workspace = true }

View File

@ -1,7 +1,7 @@
use crate::sys::{XsdMessageHeader, XSD_ERROR}; use crate::sys::{XsdMessageHeader, XSD_ERROR};
use std::error::Error;
use std::ffi::{CString, FromVecWithNulError, IntoStringError, NulError}; use std::ffi::{CString, FromVecWithNulError, IntoStringError, NulError};
use std::fs::metadata; use std::fs::metadata;
use std::io;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::mem::size_of; use std::mem::size_of;
use std::net::Shutdown; use std::net::Shutdown;
@ -9,6 +9,7 @@ use std::num::ParseIntError;
use std::os::unix::net::UnixStream; use std::os::unix::net::UnixStream;
use std::str::Utf8Error; use std::str::Utf8Error;
use std::string::FromUtf8Error; use std::string::FromUtf8Error;
use thiserror::Error;
const XEN_BUS_PATHS: &[&str] = &["/var/run/xenstored/socket"]; const XEN_BUS_PATHS: &[&str] = &["/var/run/xenstored/socket"];
@ -22,71 +23,28 @@ fn find_bus_path() -> Option<String> {
None None
} }
#[derive(Debug)] #[derive(Error, Debug)]
pub struct XsdBusError { pub enum XsdBusError {
message: String, #[error("io issue encountered")]
} Io(#[from] io::Error),
#[error("utf8 string decode failed")]
impl XsdBusError { Utf8DecodeString(#[from] FromUtf8Error),
pub fn new(msg: &str) -> XsdBusError { #[error("utf8 str decode failed")]
XsdBusError { Utf8DecodeStr(#[from] Utf8Error),
message: msg.to_string(), #[error("unable to decode cstring as utf8")]
} Utf8DecodeCstring(#[from] IntoStringError),
} #[error("nul byte found in string")]
} NulByteFoundString(#[from] NulError),
#[error("unable to find nul byte in vec")]
impl std::fmt::Display for XsdBusError { VecNulByteNotFound(#[from] FromVecWithNulError),
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { #[error("unable to parse integer")]
write!(f, "{}", self.message) ParseInt(#[from] ParseIntError),
} #[error("bus was not found on any available path")]
} BusNotFound,
#[error("store responded with error: `{0}`")]
impl Error for XsdBusError { ResponseError(String),
fn description(&self) -> &str { #[error("invalid permissions provided")]
&self.message InvalidPermissions,
}
}
impl From<std::io::Error> for XsdBusError {
fn from(value: std::io::Error) -> Self {
XsdBusError::new(value.to_string().as_str())
}
}
impl From<NulError> for XsdBusError {
fn from(_: NulError) -> Self {
XsdBusError::new("Unable to coerce data into a C string.")
}
}
impl From<FromVecWithNulError> for XsdBusError {
fn from(_: FromVecWithNulError) -> Self {
XsdBusError::new("Unable to coerce data into a C string.")
}
}
impl From<Utf8Error> for XsdBusError {
fn from(_: Utf8Error) -> Self {
XsdBusError::new("Unable to coerce data into a UTF8 string.")
}
}
impl From<FromUtf8Error> for XsdBusError {
fn from(_: FromUtf8Error) -> Self {
XsdBusError::new("Unable to coerce data into a UTF8 string.")
}
}
impl From<ParseIntError> for XsdBusError {
fn from(_: ParseIntError) -> Self {
XsdBusError::new("Unable to coerce data into an integer.")
}
}
impl From<IntoStringError> for XsdBusError {
fn from(_: IntoStringError) -> Self {
XsdBusError::new("Unable to coerce data into a string.")
}
} }
pub struct XsdSocket { pub struct XsdSocket {
@ -128,7 +86,7 @@ impl XsdSocket {
pub fn dial() -> Result<XsdSocket, XsdBusError> { pub fn dial() -> Result<XsdSocket, XsdBusError> {
let path = match find_bus_path() { let path = match find_bus_path() {
Some(path) => path, Some(path) => path,
None => return Err(XsdBusError::new("Failed to find valid bus path.")), None => return Err(XsdBusError::BusNotFound),
}; };
let stream = UnixStream::connect(path)?; let stream = UnixStream::connect(path)?;
Ok(XsdSocket { handle: stream }) Ok(XsdSocket { handle: stream })
@ -150,7 +108,7 @@ impl XsdSocket {
self.handle.read_exact(payload.as_mut_slice())?; self.handle.read_exact(payload.as_mut_slice())?;
if result_header.typ == XSD_ERROR { if result_header.typ == XSD_ERROR {
let error = CString::from_vec_with_nul(payload)?; let error = CString::from_vec_with_nul(payload)?;
return Err(XsdBusError::new(error.to_str()?)); return Err(XsdBusError::ResponseError(error.into_string()?));
} }
let response = XsdResponse { header, payload }; let response = XsdResponse { header, payload };
Ok(response) Ok(response)

View File

@ -28,7 +28,7 @@ impl XsPermission {
XS_PERM_WRITE => 'w', XS_PERM_WRITE => 'w',
XS_PERM_READ => 'r', XS_PERM_READ => 'r',
XS_PERM_NONE => 'n', XS_PERM_NONE => 'n',
_ => return Err(XsdBusError::new("invalid permissions")), _ => return Err(XsdBusError::InvalidPermissions),
}; };
Ok(format!("{}{}", c, self.id)) Ok(format!("{}{}", c, self.id))
} }