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
// 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::{
    os::raw::{c_int, c_void},
    path::Path,
};

use crate::{device::CryptDevice, err::LibcryptErr};

type WipeProgressCallback =
    unsafe extern "C" fn(size: u64, offset: u64, usrptr: *mut c_void) -> c_int;

consts_to_from_enum!(
    /// Pattern for disk wipe
    CryptWipePattern, u32,
    Zero => libcryptsetup_rs_sys::crypt_wipe_pattern_CRYPT_WIPE_ZERO,
    Random => libcryptsetup_rs_sys::crypt_wipe_pattern_CRYPT_WIPE_RANDOM,
    EncryptedZero => libcryptsetup_rs_sys::crypt_wipe_pattern_CRYPT_WIPE_ENCRYPTED_ZERO,
    Special => libcryptsetup_rs_sys::crypt_wipe_pattern_CRYPT_WIPE_SPECIAL
);

/// Handle for volume key operations
pub struct CryptWipe<'a> {
    reference: &'a mut CryptDevice,
}

impl<'a> CryptWipe<'a> {
    pub(crate) fn new(reference: &'a mut CryptDevice) -> Self {
        CryptWipe { reference }
    }

    /// Wipe a device with the selected pattern
    #[allow(clippy::too_many_arguments)]
    pub fn wipe<T>(
        &mut self,
        dev_path: &Path,
        pattern: CryptWipePattern,
        offset: u64,
        length: u64,
        wipe_block_size: crate::size_t,
        wipe_no_direct_io: bool,
        callback: Option<WipeProgressCallback>,
        usrptr: Option<&mut T>,
    ) -> Result<(), LibcryptErr> {
        let dev_path_cstring = path_to_cstring!(dev_path)?;
        errno!(unsafe {
            libcryptsetup_rs_sys::crypt_wipe(
                self.reference.as_ptr(),
                dev_path_cstring.as_ptr(),
                pattern.into(),
                offset,
                length,
                wipe_block_size,
                if wipe_no_direct_io {
                    libcryptsetup_rs_sys::CRYPT_WIPE_NO_DIRECT_IO
                } else {
                    0
                },
                callback,
                match usrptr {
                    Some(up) => up as *mut _ as *mut c_void,
                    None => std::ptr::null_mut(),
                },
            )
        })
    }
}