mirror of
https://github.com/edera-dev/krata.git
synced 2025-08-03 13:11:31 +00:00
introduce xencl for high-level interaction with xen
This commit is contained in:
@ -2,6 +2,7 @@
|
|||||||
members = [
|
members = [
|
||||||
"xenstore",
|
"xenstore",
|
||||||
"xenevtchn",
|
"xenevtchn",
|
||||||
"xencall"
|
"xencall",
|
||||||
|
"xencl"
|
||||||
]
|
]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
15
xencl/Cargo.toml
Normal file
15
xencl/Cargo.toml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
[package]
|
||||||
|
name = "xencl"
|
||||||
|
version = "0.0.1"
|
||||||
|
edition = "2021"
|
||||||
|
resolver = "2"
|
||||||
|
|
||||||
|
[dependencies.xenstore]
|
||||||
|
path = "../xenstore"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "xencl-simple"
|
||||||
|
path = "examples/simple.rs"
|
9
xencl/examples/simple.rs
Normal file
9
xencl/examples/simple.rs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use xencl::{XenClient, XenClientError};
|
||||||
|
|
||||||
|
fn main() -> Result<(), XenClientError> {
|
||||||
|
let mut client = XenClient::open()?;
|
||||||
|
let entries: HashMap<String, String> = HashMap::new();
|
||||||
|
client.create(2, entries)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
68
xencl/src/lib.rs
Normal file
68
xencl/src/lib.rs
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
|
use xenstore::bus::XsdBusError;
|
||||||
|
use xenstore::client::{XsdClient, XsdInterface};
|
||||||
|
|
||||||
|
pub struct XenClient {
|
||||||
|
store: XsdClient,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct XenClientError {
|
||||||
|
message: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl XenClientError {
|
||||||
|
pub fn new(msg: &str) -> XenClientError {
|
||||||
|
XenClientError {
|
||||||
|
message: msg.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for XenClientError {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{}", self.message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error for XenClientError {
|
||||||
|
fn description(&self) -> &str {
|
||||||
|
&self.message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<std::io::Error> for XenClientError {
|
||||||
|
fn from(value: std::io::Error) -> Self {
|
||||||
|
XenClientError::new(value.to_string().as_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<XsdBusError> for XenClientError {
|
||||||
|
fn from(value: XsdBusError) -> Self {
|
||||||
|
XenClientError::new(value.to_string().as_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl XenClient {
|
||||||
|
pub fn open() -> Result<XenClient, XenClientError> {
|
||||||
|
let store = XsdClient::open()?;
|
||||||
|
Ok(XenClient { store })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create(
|
||||||
|
&mut self,
|
||||||
|
domid: u32,
|
||||||
|
entries: HashMap<String, String>,
|
||||||
|
) -> Result<(), XenClientError> {
|
||||||
|
let domain = self.store.get_domain_path(domid)?;
|
||||||
|
let mut tx = self.store.transaction()?;
|
||||||
|
for (key, value) in entries {
|
||||||
|
let path = format!("{}/{}", domain, key);
|
||||||
|
tx.write(path.as_str(), value.into_bytes())?;
|
||||||
|
}
|
||||||
|
tx.commit()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
@ -29,7 +29,7 @@ fn list_recursive(client: &mut XsdClient, level: usize, path: &str) -> Result<()
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), XsdBusError> {
|
fn main() -> Result<(), XsdBusError> {
|
||||||
let mut client = XsdClient::new()?;
|
let mut client = XsdClient::open()?;
|
||||||
list_recursive(&mut client, 0, "/")?;
|
list_recursive(&mut client, 0, "/")?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::bus::{XsdBusError, XsdSocket};
|
use crate::bus::{XsdBusError, XsdSocket};
|
||||||
use crate::sys::{
|
use crate::sys::{
|
||||||
XSD_DIRECTORY, XSD_MKDIR, XSD_READ, XSD_RM, XSD_TRANSACTION_END, XSD_TRANSACTION_START,
|
XSD_DIRECTORY, XSD_GET_DOMAIN_PATH, XSD_MKDIR, XSD_READ, XSD_RM, XSD_TRANSACTION_END,
|
||||||
XSD_WRITE,
|
XSD_TRANSACTION_START, XSD_WRITE,
|
||||||
};
|
};
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ pub trait XsdInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl XsdClient {
|
impl XsdClient {
|
||||||
pub fn new() -> Result<XsdClient, XsdBusError> {
|
pub fn open() -> Result<XsdClient, XsdBusError> {
|
||||||
let socket = XsdSocket::dial()?;
|
let socket = XsdSocket::dial()?;
|
||||||
Ok(XsdClient { socket })
|
Ok(XsdClient { socket })
|
||||||
}
|
}
|
||||||
@ -55,6 +55,13 @@ impl XsdClient {
|
|||||||
let tx = response.parse_string()?.parse::<u32>()?;
|
let tx = response.parse_string()?.parse::<u32>()?;
|
||||||
Ok(XsdTransaction { client: self, tx })
|
Ok(XsdTransaction { client: self, tx })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct XsdTransaction<'a> {
|
pub struct XsdTransaction<'a> {
|
||||||
|
Reference in New Issue
Block a user