libcryptsetup_rs/
runtime.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use crate::{consts::flags::CryptActivate, device::CryptDevice, err::LibcryptErr};
6
7/// Record containing data on the given active device
8pub struct ActiveDevice {
9    /// Device offset
10    pub offset: u64,
11    /// Initialization vector offset
12    pub iv_offset: u64,
13    /// Size of the device
14    pub size: u64,
15    /// Flags with activation options
16    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
32/// Handle for runtime attribute options
33pub 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    /// Get active crypt device attributes
44    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    /// Get detected number of integrity failures
61    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}