Skip to content

Commit

Permalink
Merge pull request #357 from cgwalters/rust-bind-get-fsverity
Browse files Browse the repository at this point in the history
Expose optimized fsverity interface to Rust
  • Loading branch information
cgwalters committed Sep 23, 2024
2 parents df1517d + efcbcd9 commit 5ed1fb6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
8 changes: 4 additions & 4 deletions rust/composefs-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern "C" {
digest: *mut u8,
fd: std::os::raw::c_int,
) -> std::os::raw::c_int;
pub fn lcfs_fd_get_fsverity(digest: *mut u8, fd: std::os::raw::c_int) -> std::os::raw::c_int;
#[cfg(feature = "v1_0_4")]
pub fn lcfs_fd_enable_fsverity(fd: std::os::raw::c_int) -> std::os::raw::c_int;
}
Expand Down Expand Up @@ -42,21 +43,20 @@ mod tests {

#[test]
fn test_digest() -> Result<()> {
unsafe {
for f in [lcfs_compute_fsverity_from_fd, lcfs_fd_get_fsverity] {
let mut tf = tempfile::tempfile()?;
tf.write_all(b"hello world")?;
let mut buf = [0u8; LCFS_SHA256_DIGEST_LEN];
tf.seek(std::io::SeekFrom::Start(0))?;
let r = lcfs_compute_fsverity_from_fd(buf.as_mut_ptr(), tf.as_raw_fd());
assert_eq!(r, 0);
unsafe { f(buf.as_mut_ptr(), tf.as_raw_fd()) };
assert_eq!(
buf,
[
30, 46, 170, 66, 2, 215, 80, 164, 17, 116, 238, 69, 73, 112, 185, 44, 27, 194,
249, 37, 177, 227, 80, 118, 216, 199, 213, 245, 99, 98, 186, 100
]
);
Ok(())
}
Ok(())
}
}
3 changes: 2 additions & 1 deletion rust/composefs/src/fsverity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ impl Digest {
}

/// Compute the composefs fsverity digest from the provided file descriptor.
/// If fsverity is already enabled, this will return the digest computed by the kernel.
#[allow(unsafe_code)]
pub fn fsverity_digest_from_fd(fd: BorrowedFd, digest: &mut Digest) -> std::io::Result<()> {
unsafe {
map_result(composefs_sys::lcfs_compute_fsverity_from_fd(
map_result(composefs_sys::lcfs_fd_get_fsverity(
digest.0.as_mut_ptr(),
fd.as_raw_fd(),
))
Expand Down

0 comments on commit 5ed1fb6

Please sign in to comment.