forked from brl/citadel-tools
121 lines
3.3 KiB
Rust
121 lines
3.3 KiB
Rust
use std::collections::HashMap;
|
|
use std::convert::TryInto;
|
|
use zbus::blocking::Connection;
|
|
use zbus::{fdo, interface};
|
|
use zvariant::{ObjectPath, OwnedObjectPath};
|
|
use libcitadel::{RealmFS, RealmManager};
|
|
use crate::next::REALMS2_SERVER_OBJECT_PATH;
|
|
|
|
const BLOCK_SIZE: u64 = 4096;
|
|
#[derive(Clone)]
|
|
pub struct RealmFSItem {
|
|
object_path: OwnedObjectPath,
|
|
index: u32,
|
|
realmfs: RealmFS,
|
|
}
|
|
impl RealmFSItem {
|
|
pub(crate) fn new_from_realmfs(index: u32, realmfs: RealmFS) -> RealmFSItem {
|
|
let object_path = format!("{}/RealmFS{}", REALMS2_SERVER_OBJECT_PATH, index).try_into().unwrap();
|
|
RealmFSItem {
|
|
object_path,
|
|
index,
|
|
realmfs,
|
|
}
|
|
}
|
|
|
|
pub fn index(&self) -> u32 {
|
|
self.index
|
|
}
|
|
|
|
pub fn object_path(&self) -> ObjectPath {
|
|
self.object_path.as_ref()
|
|
}
|
|
}
|
|
|
|
#[interface(
|
|
name = "com.subgraph.realms.RealmFS"
|
|
)]
|
|
impl RealmFSItem {
|
|
|
|
#[zbus(property, name = "Name")]
|
|
fn name(&self) -> &str {
|
|
self.realmfs.name()
|
|
}
|
|
|
|
#[zbus(property, name = "Activated")]
|
|
fn activated(&self) -> bool {
|
|
self.realmfs.is_activated()
|
|
}
|
|
|
|
#[zbus(property, name = "InUse")]
|
|
fn in_use(&self) -> bool {
|
|
self.realmfs.is_activated()
|
|
}
|
|
#[zbus(property, name = "Mountpoint")]
|
|
fn mountpoint(&self) -> String {
|
|
self.realmfs.mountpoint().to_string()
|
|
}
|
|
|
|
#[zbus(property, name = "Path")]
|
|
fn path(&self) -> String {
|
|
format!("{}", self.realmfs.path().display())
|
|
}
|
|
|
|
#[zbus(property, name = "FreeSpace")]
|
|
fn free_space(&self) -> fdo::Result<u64> {
|
|
let blocks = self.realmfs.free_size_blocks()
|
|
.map_err(|err| fdo::Error::Failed(err.to_string()))?;
|
|
Ok(blocks as u64 * BLOCK_SIZE)
|
|
}
|
|
|
|
#[zbus(property, name = "AllocatedSpace")]
|
|
fn allocated_space(&self) -> fdo::Result<u64> {
|
|
let blocks = self.realmfs.allocated_size_blocks()
|
|
.map_err(|err| fdo::Error::Failed(err.to_string()))?;
|
|
Ok(blocks as u64 * BLOCK_SIZE)
|
|
}
|
|
}
|
|
|
|
pub struct RealmFSState {
|
|
connection: Connection,
|
|
next_index: u32,
|
|
items: HashMap<String, RealmFSItem>,
|
|
}
|
|
|
|
impl RealmFSState {
|
|
pub fn new(connection: Connection) -> Self {
|
|
RealmFSState {
|
|
connection,
|
|
next_index: 1,
|
|
items: HashMap::new(),
|
|
}
|
|
}
|
|
|
|
pub fn load(&mut self, manager: &RealmManager) -> zbus::Result<()> {
|
|
for realmfs in manager.realmfs_list() {
|
|
self.add_realmfs(realmfs)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn add_realmfs(&mut self, realmfs: RealmFS) -> zbus::Result<()> {
|
|
if !self.items.contains_key(realmfs.name()) {
|
|
let name = realmfs.name().to_string();
|
|
let item = RealmFSItem::new_from_realmfs(self.next_index, realmfs);
|
|
self.connection.object_server().at(item.object_path(), item.clone())?;
|
|
self.items.insert(name, item);
|
|
self.next_index += 1;
|
|
} else {
|
|
warn!("Attempted to add duplicate realmfs '{}'", realmfs.name());
|
|
}
|
|
Ok(())
|
|
}
|
|
pub fn realmfs_by_name(&self, name: &str) -> Option<&RealmFSItem> {
|
|
let res = self.items.get(name);
|
|
if res.is_none() {
|
|
warn!("Failed to find RealmFS with name '{}'", name);
|
|
}
|
|
res
|
|
}
|
|
}
|