1use std::{
4 env,
5 error::Error,
6 fmt::{self, Display},
7 path::Path,
8};
9
10use libblkid_rs::BlkidProbe;
11
12#[derive(Debug)]
13struct ExampleError(String);
14
15impl ExampleError {
16 fn new<D>(d: D) -> Self
17 where
18 D: Display,
19 {
20 ExampleError(d.to_string())
21 }
22}
23
24impl Display for ExampleError {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 write!(f, "{}", self.0)
27 }
28}
29
30impl Error for ExampleError {}
31
32fn main() -> Result<(), Box<dyn Error>> {
33 let path = env::args()
34 .nth(1)
35 .ok_or_else(|| ExampleError::new("Path of device to check required as argument"))?;
36
37 let mut probe = BlkidProbe::new_from_filename(Path::new(&path))?;
38 probe.enable_superblocks(true)?;
39 probe.enable_partitions(true)?;
40 probe.do_safeprobe()?;
41
42 let partitions = probe
43 .get_partitions()
44 .and_then(|mut list| list.number_of_partitions());
45 let detected_use = probe.lookup_value("TYPE");
46
47 if partitions.as_ref().map(|num| *num > 0).unwrap_or(false) || detected_use.is_ok() {
48 println!("In use");
49 if let Ok(num) = partitions {
50 println!("{num} partitions found on block device");
51 }
52 if let Ok(ty) = detected_use {
53 println!("Device determined to be of type {ty}");
54 }
55 } else {
56 println!("Free");
57 }
58
59 Ok(())
60}