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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::{
ffi::{CStr, CString},
path::Path,
};
use either::Either;
use libblkid_rs_sys::blkid_cache;
use crate::{
consts::BlkidDevFlags,
dev::{BlkidDev, BlkidDevIter},
err::{BlkidErr, Result},
};
pub struct BlkidCache(blkid_cache);
impl BlkidCache {
pub(crate) fn as_mut_ptr(&mut self) -> *mut blkid_cache {
&mut self.0 as *mut _
}
pub fn put_cache(&mut self) {
unsafe { libblkid_rs_sys::blkid_put_cache(self.0) }
}
pub fn get_cache(&mut self, filename: &Path) -> Result<()> {
let filename_cstring = CString::new(filename.to_str().ok_or(BlkidErr::InvalidConv)?)?;
errno!(unsafe {
libblkid_rs_sys::blkid_get_cache(&mut self.0 as *mut _, filename_cstring.as_ptr())
})
}
pub fn gc_cache(&mut self) {
unsafe { libblkid_rs_sys::blkid_gc_cache(self.0) }
}
pub fn iter(&self) -> BlkidDevIter {
BlkidDevIter::new(unsafe { libblkid_rs_sys::blkid_dev_iterate_begin(self.0) })
}
pub fn probe_all(&mut self) -> Result<()> {
errno!(unsafe { libblkid_rs_sys::blkid_probe_all(self.0) })
}
pub fn probe_all_new(&mut self) -> Result<()> {
errno!(unsafe { libblkid_rs_sys::blkid_probe_all_new(self.0) })
}
pub fn probe_all_removable(&mut self) -> Result<()> {
errno!(unsafe { libblkid_rs_sys::blkid_probe_all_removable(self.0) })
}
pub fn get_dev(&self, devname: &str, flags: BlkidDevFlags) -> Result<BlkidDev> {
let devname_cstring = CString::new(devname.as_bytes())?;
Ok(BlkidDev::new(unsafe {
libblkid_rs_sys::blkid_get_dev(self.0, devname_cstring.as_ptr(), flags.into())
}))
}
pub fn get_tag_value(&self, tag_name: &str, devname: &str) -> Result<&str> {
let tag_name_cstring = CString::new(tag_name.as_bytes())?;
let devname_cstring = CString::new(devname.as_bytes())?;
let ptr = errno_ptr!(unsafe {
libblkid_rs_sys::blkid_get_tag_value(
self.0,
tag_name_cstring.as_ptr(),
devname_cstring.as_ptr(),
)
})?;
Ok(unsafe { CStr::from_ptr(ptr) }.to_str()?)
}
pub fn get_devname(&self, token_or_pair: Either<&str, (&str, &str)>) -> Result<&str> {
let (name, value) = match token_or_pair {
Either::Left(token) => {
if !token.contains('=') {
return Err(BlkidErr::Other(
"Token input requires the format NAME=value".to_string(),
));
}
let mut split = token.split('=');
match (split.next(), split.next()) {
(Some(name), Some(value)) => (name, value),
(_, _) => {
return Err(BlkidErr::Other(
"Token input requires the format NAME=value".to_string(),
));
}
}
}
Either::Right((name, value)) => (name, value),
};
let name_cstring = CString::new(name.as_bytes())?;
let value_cstring = CString::new(value.as_bytes())?;
let ptr = errno_ptr!(unsafe {
libblkid_rs_sys::blkid_get_devname(
self.0,
name_cstring.as_ptr(),
value_cstring.as_ptr(),
)
})?;
Ok(unsafe { CStr::from_ptr(ptr) }.to_str()?)
}
pub fn find_dev_with_tag(&self, type_: &str, value: &str) -> Result<BlkidDev> {
let type_cstring = CString::new(type_)?;
let value_cstring = CString::new(value)?;
let ptr = errno_ptr!(unsafe {
libblkid_rs_sys::blkid_find_dev_with_tag(
self.0,
type_cstring.as_ptr(),
value_cstring.as_ptr(),
)
})?;
Ok(BlkidDev::new(ptr))
}
}