libcryptsetup_rs/
wipe.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::{
6    os::raw::{c_int, c_void},
7    path::Path,
8};
9
10use crate::{
11    consts::{flags::CryptWipe, vals::CryptWipePattern},
12    device::CryptDevice,
13    err::LibcryptErr,
14};
15
16type WipeProgressCallback =
17    unsafe extern "C" fn(size: u64, offset: u64, usrptr: *mut c_void) -> c_int;
18
19/// Handle for volume key operations
20pub struct CryptWipeHandle<'a> {
21    reference: &'a mut CryptDevice,
22}
23
24impl<'a> CryptWipeHandle<'a> {
25    pub(crate) fn new(reference: &'a mut CryptDevice) -> Self {
26        CryptWipeHandle { reference }
27    }
28
29    /// Wipe a device with the selected pattern
30    #[allow(clippy::too_many_arguments)]
31    pub fn wipe<T>(
32        &mut self,
33        dev_path: &Path,
34        pattern: CryptWipePattern,
35        offset: u64,
36        length: u64,
37        wipe_block_size: crate::size_t,
38        flags: CryptWipe,
39        callback: Option<WipeProgressCallback>,
40        usrptr: Option<&mut T>,
41    ) -> Result<(), LibcryptErr> {
42        let dev_path_cstring = path_to_cstring!(dev_path)?;
43        errno!(mutex!(libcryptsetup_rs_sys::crypt_wipe(
44            self.reference.as_ptr(),
45            dev_path_cstring.as_ptr(),
46            pattern.into(),
47            offset,
48            length,
49            wipe_block_size,
50            flags.bits(),
51            callback,
52            match usrptr {
53                Some(up) => (up as *mut T).cast::<c_void>(),
54                None => std::ptr::null_mut(),
55            },
56        )))
57    }
58}