2024-01-08 12:43:16 -08:00
|
|
|
use crate::bus::{XsdBusError, XsdSocket};
|
2024-01-08 15:04:06 -08:00
|
|
|
use crate::sys::{
|
2024-01-08 23:23:26 -08:00
|
|
|
XSD_DIRECTORY, XSD_GET_DOMAIN_PATH, XSD_MKDIR, XSD_READ, XSD_RM, XSD_TRANSACTION_END,
|
|
|
|
XSD_TRANSACTION_START, XSD_WRITE,
|
2024-01-08 15:04:06 -08:00
|
|
|
};
|
2024-01-08 14:13:51 -08:00
|
|
|
use std::ffi::CString;
|
2024-01-08 12:43:16 -08:00
|
|
|
|
|
|
|
pub struct XsdClient {
|
2024-01-08 15:04:06 -08:00
|
|
|
pub socket: XsdSocket,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait XsdInterface {
|
|
|
|
fn list(&mut self, path: &str) -> Result<Vec<String>, XsdBusError>;
|
|
|
|
fn read(&mut self, path: &str) -> Result<Vec<u8>, XsdBusError>;
|
|
|
|
fn write(&mut self, path: &str, data: Vec<u8>) -> Result<bool, XsdBusError>;
|
|
|
|
fn mkdir(&mut self, path: &str) -> Result<bool, XsdBusError>;
|
|
|
|
fn rm(&mut self, path: &str) -> Result<bool, XsdBusError>;
|
2024-01-08 12:43:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl XsdClient {
|
2024-01-08 23:23:26 -08:00
|
|
|
pub fn open() -> Result<XsdClient, XsdBusError> {
|
2024-01-08 12:43:16 -08:00
|
|
|
let socket = XsdSocket::dial()?;
|
|
|
|
Ok(XsdClient { socket })
|
|
|
|
}
|
|
|
|
|
2024-01-08 15:04:06 -08:00
|
|
|
fn list(&mut self, tx: u32, path: &str) -> Result<Vec<String>, XsdBusError> {
|
|
|
|
let response = self.socket.send_single(tx, XSD_DIRECTORY, path)?;
|
2024-01-08 14:13:51 -08:00
|
|
|
response.parse_string_vec()
|
2024-01-08 12:43:16 -08:00
|
|
|
}
|
|
|
|
|
2024-01-08 15:04:06 -08:00
|
|
|
fn read(&mut self, tx: u32, path: &str) -> Result<Vec<u8>, XsdBusError> {
|
|
|
|
let response = self.socket.send_single(tx, XSD_READ, path)?;
|
2024-01-08 12:43:16 -08:00
|
|
|
Ok(response.payload)
|
|
|
|
}
|
2024-01-08 14:13:51 -08:00
|
|
|
|
2024-01-08 15:04:06 -08:00
|
|
|
fn write(&mut self, tx: u32, path: &str, data: Vec<u8>) -> Result<bool, XsdBusError> {
|
2024-01-08 14:13:51 -08:00
|
|
|
let mut buffer = Vec::new();
|
|
|
|
let path = CString::new(path)?;
|
|
|
|
buffer.extend_from_slice(path.as_bytes_with_nul());
|
|
|
|
buffer.extend_from_slice(data.as_slice());
|
2024-01-08 15:04:06 -08:00
|
|
|
let response = self.socket.send(tx, XSD_WRITE, buffer.as_slice())?;
|
2024-01-08 14:13:51 -08:00
|
|
|
response.parse_bool()
|
|
|
|
}
|
|
|
|
|
2024-01-08 15:04:06 -08:00
|
|
|
fn mkdir(&mut self, tx: u32, path: &str) -> Result<bool, XsdBusError> {
|
|
|
|
self.socket.send_single(tx, XSD_MKDIR, path)?.parse_bool()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rm(&mut self, tx: u32, path: &str) -> Result<bool, XsdBusError> {
|
|
|
|
self.socket.send_single(tx, XSD_RM, path)?.parse_bool()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn transaction(&mut self) -> Result<XsdTransaction, XsdBusError> {
|
|
|
|
let response = self.socket.send(0, XSD_TRANSACTION_START, &[])?;
|
|
|
|
let tx = response.parse_string()?.parse::<u32>()?;
|
|
|
|
Ok(XsdTransaction { client: self, tx })
|
|
|
|
}
|
2024-01-08 23:23:26 -08:00
|
|
|
|
|
|
|
pub fn get_domain_path(&mut self, domid: u32) -> Result<String, XsdBusError> {
|
|
|
|
let response =
|
|
|
|
self.socket
|
|
|
|
.send_single(0, XSD_GET_DOMAIN_PATH, domid.to_string().as_str())?;
|
|
|
|
response.parse_string()
|
|
|
|
}
|
2024-01-08 15:04:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct XsdTransaction<'a> {
|
|
|
|
client: &'a mut XsdClient,
|
|
|
|
tx: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl XsdInterface for XsdClient {
|
|
|
|
fn list(&mut self, path: &str) -> Result<Vec<String>, XsdBusError> {
|
|
|
|
self.list(0, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read(&mut self, path: &str) -> Result<Vec<u8>, XsdBusError> {
|
|
|
|
self.read(0, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, path: &str, data: Vec<u8>) -> Result<bool, XsdBusError> {
|
|
|
|
self.write(0, path, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mkdir(&mut self, path: &str) -> Result<bool, XsdBusError> {
|
|
|
|
self.mkdir(0, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rm(&mut self, path: &str) -> Result<bool, XsdBusError> {
|
|
|
|
self.rm(0, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl XsdInterface for XsdTransaction<'_> {
|
|
|
|
fn list(&mut self, path: &str) -> Result<Vec<String>, XsdBusError> {
|
|
|
|
self.client.list(self.tx, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read(&mut self, path: &str) -> Result<Vec<u8>, XsdBusError> {
|
|
|
|
self.client.read(self.tx, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, path: &str, data: Vec<u8>) -> Result<bool, XsdBusError> {
|
|
|
|
self.client.write(self.tx, path, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mkdir(&mut self, path: &str) -> Result<bool, XsdBusError> {
|
|
|
|
self.client.mkdir(self.tx, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rm(&mut self, path: &str) -> Result<bool, XsdBusError> {
|
|
|
|
self.client.rm(self.tx, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl XsdTransaction<'_> {
|
|
|
|
pub fn end(&mut self, abort: bool) -> Result<bool, XsdBusError> {
|
|
|
|
let abort_str = if abort { "F" } else { "T" };
|
|
|
|
|
|
|
|
self.client
|
|
|
|
.socket
|
|
|
|
.send_single(self.tx, XSD_TRANSACTION_END, abort_str)?
|
|
|
|
.parse_bool()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn commit(&mut self) -> Result<bool, XsdBusError> {
|
|
|
|
self.end(false)
|
2024-01-08 14:13:51 -08:00
|
|
|
}
|
|
|
|
|
2024-01-08 15:04:06 -08:00
|
|
|
pub fn abort(&mut self) -> Result<bool, XsdBusError> {
|
|
|
|
self.end(true)
|
2024-01-08 14:13:51 -08:00
|
|
|
}
|
2024-01-08 12:43:16 -08:00
|
|
|
}
|