1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use_prelude!();
use core::ffi::c_void;
use std::sync::{Arc, Mutex, Weak};
use ffi_sdk::{BoxedDitto, FsComponent};
use serde::Deserialize;
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)]
pub enum FileSystemType {
Directory = 0,
File,
SymLink,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct DiskUsageChild {
pub fs_type: FileSystemType,
pub path: String,
pub size_in_bytes: usize,
pub children: Option<Vec<DiskUsageChild>>,
}
type DiskUsageObserverCtx = Arc<Mutex<DiskUsageObserver>>;
pub struct DiskUsageObserver {
handle: Option<repr_c::Box<ffi_sdk::DiskUsageObserver>>,
on_update: Box<dyn FnMut(DiskUsageChild) + 'static + Send + Sync>,
}
impl DiskUsageObserver {
pub(crate) fn new(on_update: impl FnMut(DiskUsageChild) + 'static + Send + Sync) -> Self {
Self {
on_update: Box::new(on_update),
handle: None,
}
}
pub(crate) fn set_handle(&mut self, handle: repr_c::Box<ffi_sdk::DiskUsageObserver>) {
self.handle = Some(handle);
}
pub(crate) unsafe extern "C" fn on_event(ctx: *mut c_void, cbor: c_slice::Ref<'_, u8>) {
let weak_ctx = Weak::from_raw(ctx as *const Mutex<DiskUsageObserver>);
if let Some(strong_ctx) = weak_ctx.upgrade() {
let tree = serde_cbor::from_slice(cbor.as_slice()).unwrap();
(strong_ctx.lock().unwrap().on_update)(tree);
}
let _ = weak_ctx.into_raw();
}
}
pub struct DiskUsage {
ditto: Arc<BoxedDitto>,
component: FsComponent,
}
impl DiskUsage {
pub(crate) fn new(ditto: Arc<BoxedDitto>, component: FsComponent) -> Self {
Self { ditto, component }
}
pub fn exec(&self) -> DiskUsageChild {
let cval = ffi_sdk::ditto_disk_usage(&self.ditto, self.component);
serde_cbor::from_slice(cval.as_slice()).unwrap()
}
pub fn observe(
&self,
callback: impl Fn(DiskUsageChild) + 'static + Send + Sync,
) -> DiskUsageObserverCtx {
let observer = DiskUsageObserver::new(Box::new(callback));
let observer = Arc::new(Mutex::new(observer));
let weak_observer = Arc::downgrade(&observer);
let raw_observer = weak_observer.into_raw() as *mut _;
let handle = unsafe {
ffi_sdk::ditto_register_disk_usage_callback(
&self.ditto,
self.component,
raw_observer,
None,
None,
Some(<unsafe extern "C" fn(_, c_slice::Ref<'_, _>)>::into(
DiskUsageObserver::on_event,
)),
)
};
observer.lock().unwrap().set_handle(handle.unwrap());
observer
}
}