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
| | // SPDX-License-Identifier: EUPL-1.2+
// SPDX-FileCopyrightText: 2026 Johannes Süllner <johannes.suellner@mailbox.org>
#[derive(Debug, Clone)]
pub struct DestinationDevice {
pub name: String,
pub path: String,
pub size_bytes: u64,
}
impl DestinationDevice {
pub fn get_destination_devices() -> Result<Vec<DestinationDevice>, &'static str> {
let get_devices_result = blockdev::get_devices();
if let Ok(block_devices) = get_devices_result {
let non_system_devices = block_devices.non_system();
let possible_block_devices = non_system_devices
.iter()
.filter(|device| device.is_disk())
.map(|device| DestinationDevice {
name: device.name.clone(),
path: format!("/dev/{}", device.name.clone()),
size_bytes: device.size,
});
Ok(possible_block_devices.collect())
} else {
Err("Fail")
}
}
pub fn size_as_human_readable_string(&self) -> String {
let mut size_factor: f64 = self.size_bytes as f64;
let mut exponent = 0; // base = 1024
while size_factor >= 1000.0 {
exponent += 1;
size_factor = size_factor / 1000.0;
}
let unit = match exponent {
0 => "",
1 => "K",
2 => "M",
3 => "G",
4 => "T",
5 => "P",
6 => "E",
7 => "Z",
8 => "Y",
9 => "R",
_ => "???",
};
format!("{:.2} {}B", size_factor, unit)
}
}
|