async-ify xenstore and xenclient

This commit is contained in:
Alex Zenla
2024-02-23 04:37:53 +00:00
parent cf0b62c9f5
commit 79acf4e814
15 changed files with 395 additions and 294 deletions

View File

@ -35,85 +35,71 @@ impl XsPermission {
}
}
#[allow(async_fn_in_trait)]
pub trait XsdInterface {
fn list(&mut self, path: &str) -> Result<Vec<String>>;
fn read(&mut self, path: &str) -> Result<Vec<u8>>;
fn read_string(&mut self, path: &str) -> Result<String>;
fn write(&mut self, path: &str, data: Vec<u8>) -> Result<bool>;
fn write_string(&mut self, path: &str, data: &str) -> Result<bool>;
fn mkdir(&mut self, path: &str) -> Result<bool>;
fn rm(&mut self, path: &str) -> Result<bool>;
fn set_perms(&mut self, path: &str, perms: &[XsPermission]) -> Result<bool>;
async fn list(&mut self, path: &str) -> Result<Vec<String>>;
async fn read(&mut self, path: &str) -> Result<Option<Vec<u8>>>;
async fn read_string(&mut self, path: &str) -> Result<Option<String>>;
async fn write(&mut self, path: &str, data: Vec<u8>) -> Result<bool>;
async fn write_string(&mut self, path: &str, data: &str) -> Result<bool>;
async fn mkdir(&mut self, path: &str) -> Result<bool>;
async fn rm(&mut self, path: &str) -> Result<bool>;
async fn set_perms(&mut self, path: &str, perms: &[XsPermission]) -> Result<bool>;
fn mknod(&mut self, path: &str, perms: &[XsPermission]) -> Result<bool> {
let result1 = self.write_string(path, "")?;
let result2 = self.set_perms(path, perms)?;
async fn mknod(&mut self, path: &str, perms: &[XsPermission]) -> Result<bool> {
let result1 = self.write_string(path, "").await?;
let result2 = self.set_perms(path, perms).await?;
Ok(result1 && result2)
}
fn read_string_optional(&mut self, path: &str) -> Result<Option<String>> {
Ok(match self.read_string(path) {
Ok(value) => Some(value),
Err(error) => {
if error.is_noent_response() {
None
} else {
return Err(error);
}
}
})
}
fn list_any(&mut self, path: &str) -> Result<Vec<String>> {
Ok(match self.list(path) {
Ok(value) => value,
Err(error) => {
if error.is_noent_response() {
Vec::new()
} else {
return Err(error);
}
}
})
}
}
impl XsdClient {
pub fn open() -> Result<XsdClient> {
let socket = XsdSocket::dial()?;
pub async fn open() -> Result<XsdClient> {
let socket = XsdSocket::open().await?;
Ok(XsdClient { socket })
}
fn list(&mut self, tx: u32, path: &str) -> Result<Vec<String>> {
async fn list(&mut self, tx: u32, path: &str) -> Result<Vec<String>> {
trace!("list tx={tx} path={path}");
let response = self.socket.send_single(tx, XSD_DIRECTORY, path)?;
let response = self.socket.send_single(tx, XSD_DIRECTORY, path).await?;
response.parse_string_vec()
}
fn read(&mut self, tx: u32, path: &str) -> Result<Vec<u8>> {
async fn read(&mut self, tx: u32, path: &str) -> Result<Option<Vec<u8>>> {
trace!("read tx={tx} path={path}");
let response = self.socket.send_single(tx, XSD_READ, path)?;
Ok(response.payload)
match self.socket.send_single(tx, XSD_READ, path).await {
Ok(response) => Ok(Some(response.payload)),
Err(error) => {
if error.is_noent_response() {
Ok(None)
} else {
Err(error)
}
}
}
}
fn write(&mut self, tx: u32, path: &str, data: Vec<u8>) -> Result<bool> {
async fn write(&mut self, tx: u32, path: &str, data: Vec<u8>) -> Result<bool> {
trace!("write tx={tx} path={path} data={:?}", data);
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());
let response = self.socket.send(tx, XSD_WRITE, buffer.as_slice())?;
let response = self.socket.send(tx, XSD_WRITE, buffer.as_slice()).await?;
response.parse_bool()
}
fn mkdir(&mut self, tx: u32, path: &str) -> Result<bool> {
async fn mkdir(&mut self, tx: u32, path: &str) -> Result<bool> {
trace!("mkdir tx={tx} path={path}");
self.socket.send_single(tx, XSD_MKDIR, path)?.parse_bool()
self.socket
.send_single(tx, XSD_MKDIR, path)
.await?
.parse_bool()
}
fn rm(&mut self, tx: u32, path: &str) -> Result<bool> {
async fn rm(&mut self, tx: u32, path: &str) -> Result<bool> {
trace!("rm tx={tx} path={path}");
let result = self.socket.send_single(tx, XSD_RM, path);
let result = self.socket.send_single(tx, XSD_RM, path).await;
if let Err(error) = result {
if error.is_noent_response() {
return Ok(true);
@ -123,7 +109,7 @@ impl XsdClient {
result.unwrap().parse_bool()
}
fn set_perms(&mut self, tx: u32, path: &str, perms: &[XsPermission]) -> Result<bool> {
async fn set_perms(&mut self, tx: u32, path: &str, perms: &[XsPermission]) -> Result<bool> {
trace!("set_perms tx={tx} path={path} perms={:?}", perms);
let mut items: Vec<String> = Vec::new();
items.push(path.to_string());
@ -131,36 +117,46 @@ impl XsdClient {
items.push(perm.encode()?);
}
let items_str: Vec<&str> = items.iter().map(|x| x.as_str()).collect();
let response = self.socket.send_multiple(tx, XSD_SET_PERMS, &items_str)?;
let response = self
.socket
.send_multiple(tx, XSD_SET_PERMS, &items_str)
.await?;
response.parse_bool()
}
pub fn transaction(&mut self) -> Result<XsdTransaction> {
pub async fn transaction(&mut self) -> Result<XsdTransaction> {
trace!("transaction start");
let response = self.socket.send_single(0, XSD_TRANSACTION_START, "")?;
let response = self
.socket
.send_single(0, XSD_TRANSACTION_START, "")
.await?;
let str = response.parse_string()?;
let tx = str.parse::<u32>()?;
Ok(XsdTransaction { client: self, tx })
}
pub fn get_domain_path(&mut self, domid: u32) -> Result<String> {
let response =
self.socket
.send_single(0, XSD_GET_DOMAIN_PATH, domid.to_string().as_str())?;
pub async fn get_domain_path(&mut self, domid: u32) -> Result<String> {
let response = self
.socket
.send_single(0, XSD_GET_DOMAIN_PATH, domid.to_string().as_str())
.await?;
response.parse_string()
}
pub fn introduce_domain(&mut self, domid: u32, mfn: u64, evtchn: u32) -> Result<bool> {
pub async fn introduce_domain(&mut self, domid: u32, mfn: u64, evtchn: u32) -> Result<bool> {
trace!("introduce domain domid={domid} mfn={mfn} evtchn={evtchn}");
let response = self.socket.send_multiple(
0,
XSD_INTRODUCE,
&[
domid.to_string().as_str(),
mfn.to_string().as_str(),
evtchn.to_string().as_str(),
],
)?;
let response = self
.socket
.send_multiple(
0,
XSD_INTRODUCE,
&[
domid.to_string().as_str(),
mfn.to_string().as_str(),
evtchn.to_string().as_str(),
],
)
.await?;
response.parse_bool()
}
}
@ -171,89 +167,104 @@ pub struct XsdTransaction<'a> {
}
impl XsdInterface for XsdClient {
fn list(&mut self, path: &str) -> Result<Vec<String>> {
self.list(0, path)
async fn list(&mut self, path: &str) -> Result<Vec<String>> {
self.list(0, path).await
}
fn read(&mut self, path: &str) -> Result<Vec<u8>> {
self.read(0, path)
async fn read(&mut self, path: &str) -> Result<Option<Vec<u8>>> {
self.read(0, path).await
}
fn read_string(&mut self, path: &str) -> Result<String> {
Ok(String::from_utf8(self.read(0, path)?)?)
async fn read_string(&mut self, path: &str) -> Result<Option<String>> {
match self.read(0, path).await {
Ok(value) => match value {
Some(value) => Ok(Some(String::from_utf8(value)?)),
None => Ok(None),
},
Err(error) => Err(error),
}
}
fn write(&mut self, path: &str, data: Vec<u8>) -> Result<bool> {
self.write(0, path, data)
async fn write(&mut self, path: &str, data: Vec<u8>) -> Result<bool> {
self.write(0, path, data).await
}
fn write_string(&mut self, path: &str, data: &str) -> Result<bool> {
self.write(0, path, data.as_bytes().to_vec())
async fn write_string(&mut self, path: &str, data: &str) -> Result<bool> {
self.write(0, path, data.as_bytes().to_vec()).await
}
fn mkdir(&mut self, path: &str) -> Result<bool> {
self.mkdir(0, path)
async fn mkdir(&mut self, path: &str) -> Result<bool> {
self.mkdir(0, path).await
}
fn rm(&mut self, path: &str) -> Result<bool> {
self.rm(0, path)
async fn rm(&mut self, path: &str) -> Result<bool> {
self.rm(0, path).await
}
fn set_perms(&mut self, path: &str, perms: &[XsPermission]) -> Result<bool> {
self.set_perms(0, path, perms)
async fn set_perms(&mut self, path: &str, perms: &[XsPermission]) -> Result<bool> {
self.set_perms(0, path, perms).await
}
}
impl XsdInterface for XsdTransaction<'_> {
fn list(&mut self, path: &str) -> Result<Vec<String>> {
self.client.list(self.tx, path)
async fn list(&mut self, path: &str) -> Result<Vec<String>> {
self.client.list(self.tx, path).await
}
fn read(&mut self, path: &str) -> Result<Vec<u8>> {
self.client.read(self.tx, path)
async fn read(&mut self, path: &str) -> Result<Option<Vec<u8>>> {
self.client.read(self.tx, path).await
}
fn read_string(&mut self, path: &str) -> Result<String> {
Ok(String::from_utf8(self.client.read(self.tx, path)?)?)
async fn read_string(&mut self, path: &str) -> Result<Option<String>> {
match self.client.read(self.tx, path).await {
Ok(value) => match value {
Some(value) => Ok(Some(String::from_utf8(value)?)),
None => Ok(None),
},
Err(error) => Err(error),
}
}
fn write(&mut self, path: &str, data: Vec<u8>) -> Result<bool> {
self.client.write(self.tx, path, data)
async fn write(&mut self, path: &str, data: Vec<u8>) -> Result<bool> {
self.client.write(self.tx, path, data).await
}
fn write_string(&mut self, path: &str, data: &str) -> Result<bool> {
self.client.write(self.tx, path, data.as_bytes().to_vec())
async fn write_string(&mut self, path: &str, data: &str) -> Result<bool> {
self.client
.write(self.tx, path, data.as_bytes().to_vec())
.await
}
fn mkdir(&mut self, path: &str) -> Result<bool> {
self.client.mkdir(self.tx, path)
async fn mkdir(&mut self, path: &str) -> Result<bool> {
self.client.mkdir(self.tx, path).await
}
fn rm(&mut self, path: &str) -> Result<bool> {
self.client.rm(self.tx, path)
async fn rm(&mut self, path: &str) -> Result<bool> {
self.client.rm(self.tx, path).await
}
fn set_perms(&mut self, path: &str, perms: &[XsPermission]) -> Result<bool> {
self.client.set_perms(self.tx, path, perms)
async fn set_perms(&mut self, path: &str, perms: &[XsPermission]) -> Result<bool> {
self.client.set_perms(self.tx, path, perms).await
}
}
impl XsdTransaction<'_> {
pub fn end(&mut self, abort: bool) -> Result<bool> {
pub async fn end(&mut self, abort: bool) -> Result<bool> {
let abort_str = if abort { "F" } else { "T" };
trace!("transaction end abort={}", abort);
self.client
.socket
.send_single(self.tx, XSD_TRANSACTION_END, abort_str)?
.send_single(self.tx, XSD_TRANSACTION_END, abort_str)
.await?
.parse_bool()
}
pub fn commit(&mut self) -> Result<bool> {
self.end(false)
pub async fn commit(&mut self) -> Result<bool> {
self.end(false).await
}
pub fn abort(&mut self) -> Result<bool> {
self.end(true)
pub async fn abort(&mut self) -> Result<bool> {
self.end(true).await
}
}