xenstore: move error type and make use of result type aliases

This commit is contained in:
Alex Zenla
2024-01-30 01:58:10 -08:00
parent 812e357bc9
commit eba623d61a
8 changed files with 95 additions and 112 deletions

View File

@ -1,15 +1,11 @@
use crate::error::{Error, Result};
use crate::sys::{XsdMessageHeader, XSD_ERROR};
use std::ffi::{CString, FromVecWithNulError, IntoStringError, NulError};
use std::ffi::CString;
use std::fs::metadata;
use std::io;
use std::io::{Read, Write};
use std::mem::size_of;
use std::net::Shutdown;
use std::num::ParseIntError;
use std::os::unix::net::UnixStream;
use std::str::Utf8Error;
use std::string::FromUtf8Error;
use thiserror::Error;
const XEN_BUS_PATHS: &[&str] = &["/var/run/xenstored/socket"];
@ -23,30 +19,6 @@ fn find_bus_path() -> Option<String> {
None
}
#[derive(Error, Debug)]
pub enum XsdBusError {
#[error("io issue encountered")]
Io(#[from] io::Error),
#[error("utf8 string decode failed")]
Utf8DecodeString(#[from] FromUtf8Error),
#[error("utf8 str decode failed")]
Utf8DecodeStr(#[from] Utf8Error),
#[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")]
VecNulByteNotFound(#[from] FromVecWithNulError),
#[error("unable to parse integer")]
ParseInt(#[from] ParseIntError),
#[error("bus was not found on any available path")]
BusNotFound,
#[error("store responded with error: `{0}`")]
ResponseError(String),
#[error("invalid permissions provided")]
InvalidPermissions,
}
pub struct XsdSocket {
handle: UnixStream,
}
@ -58,11 +30,11 @@ pub struct XsdResponse {
}
impl XsdResponse {
pub fn parse_string(&self) -> Result<String, XsdBusError> {
pub fn parse_string(&self) -> Result<String> {
Ok(CString::from_vec_with_nul(self.payload.clone())?.into_string()?)
}
pub fn parse_string_vec(&self) -> Result<Vec<String>, XsdBusError> {
pub fn parse_string_vec(&self) -> Result<Vec<String>> {
let mut strings: Vec<String> = Vec::new();
let mut buffer: Vec<u8> = Vec::new();
for b in &self.payload {
@ -77,22 +49,22 @@ impl XsdResponse {
Ok(strings)
}
pub fn parse_bool(&self) -> Result<bool, XsdBusError> {
pub fn parse_bool(&self) -> Result<bool> {
Ok(true)
}
}
impl XsdSocket {
pub fn dial() -> Result<XsdSocket, XsdBusError> {
pub fn dial() -> Result<XsdSocket> {
let path = match find_bus_path() {
Some(path) => path,
None => return Err(XsdBusError::BusNotFound),
None => return Err(Error::BusNotFound),
};
let stream = UnixStream::connect(path)?;
Ok(XsdSocket { handle: stream })
}
pub fn send(&mut self, tx: u32, typ: u32, buf: &[u8]) -> Result<XsdResponse, XsdBusError> {
pub fn send(&mut self, tx: u32, typ: u32, buf: &[u8]) -> Result<XsdResponse> {
let header = XsdMessageHeader {
typ,
req: 0,
@ -108,29 +80,19 @@ impl XsdSocket {
self.handle.read_exact(payload.as_mut_slice())?;
if result_header.typ == XSD_ERROR {
let error = CString::from_vec_with_nul(payload)?;
return Err(XsdBusError::ResponseError(error.into_string()?));
return Err(Error::ResponseError(error.into_string()?));
}
let response = XsdResponse { header, payload };
Ok(response)
}
pub fn send_single(
&mut self,
tx: u32,
typ: u32,
string: &str,
) -> Result<XsdResponse, XsdBusError> {
pub fn send_single(&mut self, tx: u32, typ: u32, string: &str) -> Result<XsdResponse> {
let text = CString::new(string)?;
let buf = text.as_bytes_with_nul();
self.send(tx, typ, buf)
}
pub fn send_multiple(
&mut self,
tx: u32,
typ: u32,
array: &[&str],
) -> Result<XsdResponse, XsdBusError> {
pub fn send_multiple(&mut self, tx: u32, typ: u32, array: &[&str]) -> Result<XsdResponse> {
let mut buf: Vec<u8> = Vec::new();
for item in array {
buf.extend_from_slice(item.as_bytes());