libcryptsetup_rs/
runtime.rs1use crate::{consts::flags::CryptActivate, device::CryptDevice, err::LibcryptErr};
6
7pub struct ActiveDevice {
9 pub offset: u64,
11 pub iv_offset: u64,
13 pub size: u64,
15 pub flags: CryptActivate,
17}
18
19impl<'a> TryFrom<&'a libcryptsetup_rs_sys::crypt_active_device> for ActiveDevice {
20 type Error = LibcryptErr;
21
22 fn try_from(v: &'a libcryptsetup_rs_sys::crypt_active_device) -> Result<Self, Self::Error> {
23 Ok(ActiveDevice {
24 offset: v.offset,
25 iv_offset: v.iv_offset,
26 size: v.size,
27 flags: CryptActivate::from_bits(v.flags).ok_or(LibcryptErr::InvalidConversion)?,
28 })
29 }
30}
31
32pub struct CryptRuntimeHandle<'a> {
34 reference: &'a mut CryptDevice,
35 name: &'a str,
36}
37
38impl<'a> CryptRuntimeHandle<'a> {
39 pub(crate) fn new(reference: &'a mut CryptDevice, name: &'a str) -> Self {
40 CryptRuntimeHandle { reference, name }
41 }
42
43 pub fn get_active_device(&mut self) -> Result<ActiveDevice, LibcryptErr> {
45 let mut cad = libcryptsetup_rs_sys::crypt_active_device {
46 offset: 0,
47 iv_offset: 0,
48 size: 0,
49 flags: 0,
50 };
51 let name_cstring = to_cstring!(self.name)?;
52 errno!(mutex!(libcryptsetup_rs_sys::crypt_get_active_device(
53 self.reference.as_ptr(),
54 name_cstring.as_ptr(),
55 &mut cad as *mut _,
56 )))
57 .and_then(|_| ActiveDevice::try_from(&cad))
58 }
59
60 pub fn get_active_integrity_failures(&mut self) -> Result<u64, LibcryptErr> {
62 let name_cstring = to_cstring!(self.name)?;
63 Ok(mutex!(
64 libcryptsetup_rs_sys::crypt_get_active_integrity_failures(
65 self.reference.as_ptr(),
66 name_cstring.as_ptr(),
67 )
68 ))
69 }
70}