libcryptsetup_rs/luks2/
flags.rs

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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::marker::PhantomData;

use crate::{
    consts::{
        flags::{CryptActivate, CryptRequirement},
        vals::CryptFlagsType,
    },
    device::CryptDevice,
    err::LibcryptErr,
};

/// Handle for LUKS2 persistent flag operations
pub struct CryptLuks2FlagsHandle<'a, T> {
    reference: &'a mut CryptDevice,
    data: PhantomData<T>,
}

impl<'a, T> CryptLuks2FlagsHandle<'a, T> {
    pub(crate) fn new(reference: &'a mut CryptDevice) -> Self {
        CryptLuks2FlagsHandle {
            reference,
            data: PhantomData,
        }
    }
}

impl CryptLuks2FlagsHandle<'_, CryptActivate> {
    /// Implementation for setting persistent flags for activation
    pub fn persistent_flags_set(&mut self, flags: CryptActivate) -> Result<(), LibcryptErr> {
        errno!(mutex!(libcryptsetup_rs_sys::crypt_persistent_flags_set(
            self.reference.as_ptr(),
            CryptFlagsType::Activation as u32,
            flags.bits(),
        )))
    }

    /// Implementation for getting persistent flags for activation
    pub fn persistent_flags_get(&mut self) -> Result<CryptActivate, LibcryptErr> {
        let mut flags_u32 = 0u32;
        errno!(unsafe {
            libcryptsetup_rs_sys::crypt_persistent_flags_get(
                self.reference.as_ptr(),
                CryptFlagsType::Activation as u32,
                &mut flags_u32 as *mut _,
            )
        })
        .and_then(|_| CryptActivate::from_bits(flags_u32).ok_or(LibcryptErr::InvalidConversion))
    }
}

impl CryptLuks2FlagsHandle<'_, CryptRequirement> {
    /// Implementation for setting persistent flags for requirements
    pub fn persistent_flags_set(&mut self, flags: CryptRequirement) -> Result<(), LibcryptErr> {
        errno!(unsafe {
            libcryptsetup_rs_sys::crypt_persistent_flags_set(
                self.reference.as_ptr(),
                CryptFlagsType::Requirements as u32,
                flags.bits(),
            )
        })
    }

    /// Implementation for getting persistent flags for requirements
    pub fn persistent_flags_get(&mut self) -> Result<CryptRequirement, LibcryptErr> {
        let mut flags_u32 = 0u32;
        errno!(unsafe {
            libcryptsetup_rs_sys::crypt_persistent_flags_get(
                self.reference.as_ptr(),
                CryptFlagsType::Requirements as u32,
                &mut flags_u32 as *mut _,
            )
        })
        .and_then(|_| CryptRequirement::from_bits(flags_u32).ok_or(LibcryptErr::InvalidConversion))
    }
}