Skip to content

Commit

Permalink
Add wrapper for crypt_dump_json
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaublitz committed Oct 24, 2024
1 parent 9f03c23 commit 7adb6fa
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 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/.

#[cfg(cryptsetup24supported)]
use std::ffi::CStr;
use std::{os::raw::c_int, path::Path, ptr, str::FromStr};

use crate::{
Expand All @@ -11,6 +13,8 @@ use crate::{
format::{CryptParamsIntegrity, CryptParamsVerity},
};

#[cfg(cryptsetup24supported)]
use serde_json::Value;
use uuid::Uuid;

/// Handle for crypt device status operations
Expand All @@ -30,6 +34,30 @@ impl<'a> CryptDeviceStatusHandle<'a> {
)))
}

/// Dump text info about device to JSON output
#[cfg(cryptsetup24supported)]
pub fn dump_json(&mut self) -> Result<Value, LibcryptErr> {
let mut buffer: *const i8 = ptr::null();
errno!(mutex!(libcryptsetup_rs_sys::crypt_dump_json(
self.reference.as_ptr(),
&mut buffer as *mut _,
0,
)))?;
let json = serde_json::from_str(
unsafe { CStr::from_ptr(buffer) }
.to_str()
.map_err(LibcryptErr::Utf8Error)?,
)
.map_err(LibcryptErr::JsonError)?;
// Here one would probably expect a free call because we are receiving an allocated buffer
// into a pointer. Adding a free call here results in a double free error. Furthermore
// json-c references reference counting in its documentation. Just to ensure that we are
// not causing a memory leak, I ran valgrind on a test program without the free and no
// memory appears to be lost. This leads me to believe that we are doing the right thing
// here.
Ok(json)
}

/// Get cipher used by device
pub fn get_cipher(&mut self) -> Result<String, LibcryptErr> {
from_str_ptr_to_owned!(libcryptsetup_rs_sys::crypt_get_cipher(
Expand Down

0 comments on commit 7adb6fa

Please sign in to comment.