libcryptsetup_rs/
backup.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 std::{path::Path, ptr};
6
7use crate::{consts::vals::EncryptionFormat, device::CryptDevice, err::LibcryptErr};
8
9/// Handle for backup operations on a device
10pub struct CryptBackupHandle<'a> {
11    reference: &'a mut CryptDevice,
12}
13
14impl<'a> CryptBackupHandle<'a> {
15    pub(crate) fn new(reference: &'a mut CryptDevice) -> Self {
16        CryptBackupHandle { reference }
17    }
18
19    /// Back up header and keyslots to a file
20    pub fn header_backup(
21        &mut self,
22        requested_type: Option<EncryptionFormat>,
23        backup_file: &Path,
24    ) -> Result<(), LibcryptErr> {
25        let backup_file_cstring = path_to_cstring!(backup_file)?;
26        errno!(mutex!(libcryptsetup_rs_sys::crypt_header_backup(
27            self.reference.as_ptr(),
28            requested_type
29                .map(|ty| ty.as_ptr())
30                .unwrap_or_else(ptr::null),
31            backup_file_cstring.as_ptr(),
32        )))
33    }
34
35    /// Restore header and keyslots from a file
36    pub fn header_restore(
37        &mut self,
38        requested_type: Option<EncryptionFormat>,
39        backup_file: &Path,
40    ) -> Result<(), LibcryptErr> {
41        let backup_file_cstring = path_to_cstring!(backup_file)?;
42        errno!(mutex!(libcryptsetup_rs_sys::crypt_header_restore(
43            self.reference.as_ptr(),
44            requested_type
45                .map(|ty| ty.as_ptr())
46                .unwrap_or_else(ptr::null),
47            backup_file_cstring.as_ptr(),
48        )))
49    }
50}