Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wrapper for crypt_dump_json #379

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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