diff --git a/rust-bindings/rust/src/functions.rs b/rust-bindings/rust/src/functions.rs index d3a20ffa..a56e0da2 100644 --- a/rust-bindings/rust/src/functions.rs +++ b/rust-bindings/rust/src/functions.rs @@ -32,3 +32,35 @@ pub fn checksum_file, Q: IsA>( } } } + +pub fn checksum_file_from_input, Q: IsA>( + file_info: &gio::FileInfo, + xattrs: Option<&glib::Variant>, + in_: Option<&P>, + objtype: ObjectType, + cancellable: Option<&Q>, +) -> Result> { + unsafe { + let mut out_csum = ptr::null_mut(); + let mut error = ptr::null_mut(); + let ret = ostree_sys::ostree_checksum_file_from_input( + file_info.to_glib_none().0, + xattrs.to_glib_none().0, + in_.map(|p| p.as_ref()).to_glib_none().0, + objtype.to_glib(), + &mut out_csum, + cancellable.map(|p| p.as_ref()).to_glib_none().0, + &mut error, + ); + + if !error.is_null() { + Err(Box::::new(from_glib_full(error))) + } else if ret == GFALSE { + Err(Box::new(glib_bool_error!( + "unknown error in ostree_checksum_file_from_input" + ))) + } else { + Ok(Checksum::from_glib_full(out_csum)) + } + } +} diff --git a/rust-bindings/rust/tests/functions/mod.rs b/rust-bindings/rust/tests/functions/mod.rs index 196c21b4..71b557cb 100644 --- a/rust-bindings/rust/tests/functions/mod.rs +++ b/rust-bindings/rust/tests/functions/mod.rs @@ -1,6 +1,6 @@ use gio::NONE_CANCELLABLE; use glib::Cast; -use ostree::{checksum_file, ObjectType, RepoFile, RepoFileExt}; +use ostree::{checksum_file, checksum_file_from_input, ObjectType, RepoFile, RepoFileExt}; use util::TestRepo; #[test] @@ -19,3 +19,32 @@ fn should_checksum_file() { assert_eq!(file.get_checksum().unwrap(), result.to_string()); } + +#[test] +fn should_checksum_file_from_input() { + let repo = TestRepo::new(); + let commit_checksum = repo.test_commit("test"); + + let objects = repo + .repo + .traverse_commit(&commit_checksum, -1, NONE_CANCELLABLE) + .expect("traverse commit"); + for obj in objects { + if obj.object_type() != ObjectType::File { + continue; + } + let (stream, file_info, xattrs) = repo + .repo + .load_file(obj.checksum(), NONE_CANCELLABLE) + .expect("load file"); + let result = checksum_file_from_input( + file_info.as_ref().unwrap(), + xattrs.as_ref(), + stream.as_ref(), + ObjectType::File, + NONE_CANCELLABLE, + ) + .expect("checksum file from input"); + assert_eq!(obj.checksum(), result.to_string()); + } +}