oh my god, we have a console

This commit is contained in:
Alex Zenla
2024-01-17 05:22:47 -08:00
parent 6ca61410ad
commit 15ba27e573
14 changed files with 643 additions and 559 deletions

View File

@ -1,6 +1,6 @@
use crate::sys::{XsdMessageHeader, XSD_ERROR};
use std::error::Error;
use std::ffi::{CString, FromVecWithNulError, NulError};
use std::ffi::{CString, FromVecWithNulError, IntoStringError, NulError};
use std::fs::metadata;
use std::io::{Read, Write};
use std::mem::size_of;
@ -83,6 +83,12 @@ impl From<ParseIntError> for XsdBusError {
}
}
impl From<IntoStringError> for XsdBusError {
fn from(_: IntoStringError) -> Self {
XsdBusError::new("Unable to coerce data into a string.")
}
}
pub struct XsdSocket {
handle: UnixStream,
}
@ -95,7 +101,7 @@ pub struct XsdResponse {
impl XsdResponse {
pub fn parse_string(&self) -> Result<String, XsdBusError> {
Ok(String::from_utf8(self.payload.clone())?)
Ok(CString::from_vec_with_nul(self.payload.clone())?.into_string()?)
}
pub fn parse_string_vec(&self) -> Result<Vec<String>, XsdBusError> {
@ -114,8 +120,10 @@ impl XsdResponse {
}
pub fn parse_bool(&self) -> Result<bool, XsdBusError> {
if self.payload.len() != 1 {
Err(XsdBusError::new("Expected payload to be a single byte."))
if self.payload.is_empty() {
Err(XsdBusError::new(
"Expected bool payload to be at least one byte.",
))
} else {
Ok(self.payload[0] == 0)
}

View File

@ -9,14 +9,23 @@ pub struct XsdClient {
pub socket: XsdSocket,
}
pub struct XsPermissions {
pub id: u32,
pub perms: u32,
}
pub trait XsdInterface {
fn list(&mut self, path: &str) -> Result<Vec<String>, XsdBusError>;
fn read(&mut self, path: &str) -> Result<Vec<u8>, XsdBusError>;
fn read_string(&mut self, path: &str) -> Result<String, XsdBusError>;
fn write(&mut self, path: &str, data: Vec<u8>) -> Result<bool, XsdBusError>;
fn write_string(&mut self, path: &str, data: String) -> Result<bool, XsdBusError>;
fn write_string(&mut self, path: &str, data: &str) -> Result<bool, XsdBusError>;
fn mkdir(&mut self, path: &str) -> Result<bool, XsdBusError>;
fn rm(&mut self, path: &str) -> Result<bool, XsdBusError>;
fn mknod(&mut self, path: &str, _perm: &XsPermissions) -> Result<bool, XsdBusError> {
self.write_string(path, "")
}
}
impl XsdClient {
@ -53,8 +62,9 @@ impl XsdClient {
}
pub fn transaction(&mut self) -> Result<XsdTransaction, XsdBusError> {
let response = self.socket.send(0, XSD_TRANSACTION_START, &[])?;
let tx = response.parse_string()?.parse::<u32>()?;
let response = self.socket.send_single(0, XSD_TRANSACTION_START, "")?;
let str = response.parse_string()?;
let tx = str.parse::<u32>()?;
Ok(XsdTransaction { client: self, tx })
}
@ -68,7 +78,7 @@ impl XsdClient {
pub fn introduce_domain(
&mut self,
domid: u32,
mfn: u32,
mfn: u64,
eventchn: u32,
) -> Result<String, XsdBusError> {
let response = self.socket.send_multiple(
@ -106,8 +116,8 @@ impl XsdInterface for XsdClient {
self.write(0, path, data)
}
fn write_string(&mut self, path: &str, data: String) -> Result<bool, XsdBusError> {
self.write(0, path, data.into_bytes())
fn write_string(&mut self, path: &str, data: &str) -> Result<bool, XsdBusError> {
self.write(0, path, data.as_bytes().to_vec())
}
fn mkdir(&mut self, path: &str) -> Result<bool, XsdBusError> {
@ -136,8 +146,8 @@ impl XsdInterface for XsdTransaction<'_> {
self.client.write(self.tx, path, data)
}
fn write_string(&mut self, path: &str, data: String) -> Result<bool, XsdBusError> {
self.client.write(self.tx, path, data.into_bytes())
fn write_string(&mut self, path: &str, data: &str) -> Result<bool, XsdBusError> {
self.client.write(self.tx, path, data.as_bytes().to_vec())
}
fn mkdir(&mut self, path: &str) -> Result<bool, XsdBusError> {