From 7f3bd56d0dbadba3110e1fff9f3be0c98494eceb Mon Sep 17 00:00:00 2001 From: Felix Krull Date: Mon, 2 Sep 2019 12:22:58 +0200 Subject: [PATCH] Implement ostree::checksum_file --- rust-bindings/rust/src/functions.rs | 34 +++++++++++++++++++++++ rust-bindings/rust/src/lib.rs | 2 ++ rust-bindings/rust/src/object_name.rs | 2 +- rust-bindings/rust/src/repo.rs | 1 + rust-bindings/rust/tests/functions/mod.rs | 21 ++++++++++++++ rust-bindings/rust/tests/tests.rs | 1 + 6 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 rust-bindings/rust/src/functions.rs create mode 100644 rust-bindings/rust/tests/functions/mod.rs diff --git a/rust-bindings/rust/src/functions.rs b/rust-bindings/rust/src/functions.rs new file mode 100644 index 00000000..d3a20ffa --- /dev/null +++ b/rust-bindings/rust/src/functions.rs @@ -0,0 +1,34 @@ +use crate::{Checksum, ObjectType}; +use glib::prelude::*; +use glib::translate::*; +use glib_sys::GFALSE; +use std::error::Error; +use std::ptr; + +pub fn checksum_file, Q: IsA>( + f: &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( + f.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" + ))) + } else { + Ok(Checksum::from_glib_full(out_csum)) + } + } +} diff --git a/rust-bindings/rust/src/lib.rs b/rust-bindings/rust/src/lib.rs index 274b4798..f35ff5c6 100644 --- a/rust-bindings/rust/src/lib.rs +++ b/rust-bindings/rust/src/lib.rs @@ -37,6 +37,7 @@ pub use crate::auto::*; mod checksum; #[cfg(any(feature = "v2018_6", feature = "dox"))] mod collection_ref; +mod functions; #[cfg(any(feature = "v2019_3", feature = "dox"))] mod kernel_args; mod object_name; @@ -47,6 +48,7 @@ mod se_policy; pub use crate::checksum::*; #[cfg(any(feature = "v2018_6", feature = "dox"))] pub use crate::collection_ref::*; +pub use crate::functions::*; #[cfg(any(feature = "v2019_3", feature = "dox"))] pub use crate::kernel_args::*; pub use crate::object_name::*; diff --git a/rust-bindings/rust/src/object_name.rs b/rust-bindings/rust/src/object_name.rs index 35998a41..098c7f9a 100644 --- a/rust-bindings/rust/src/object_name.rs +++ b/rust-bindings/rust/src/object_name.rs @@ -1,4 +1,4 @@ -use functions::{object_name_deserialize, object_name_serialize, object_to_string}; +use crate::{object_name_deserialize, object_name_serialize, object_to_string}; use glib; use glib::translate::*; use glib::GString; diff --git a/rust-bindings/rust/src/repo.rs b/rust-bindings/rust/src/repo.rs index a6b2aaff..3098ce2d 100644 --- a/rust-bindings/rust/src/repo.rs +++ b/rust-bindings/rust/src/repo.rs @@ -11,6 +11,7 @@ use glib::Error; use glib::IsA; use glib_sys; use ostree_sys; +#[cfg(feature = "futures")] use std::boxed::Box as Box_; use std::collections::{HashMap, HashSet}; use std::path::Path; diff --git a/rust-bindings/rust/tests/functions/mod.rs b/rust-bindings/rust/tests/functions/mod.rs new file mode 100644 index 00000000..196c21b4 --- /dev/null +++ b/rust-bindings/rust/tests/functions/mod.rs @@ -0,0 +1,21 @@ +use gio::NONE_CANCELLABLE; +use glib::Cast; +use ostree::{checksum_file, ObjectType, RepoFile, RepoFileExt}; +use util::TestRepo; + +#[test] +fn should_checksum_file() { + let repo = TestRepo::new(); + repo.test_commit("test"); + + let file = repo + .repo + .read_commit("test", NONE_CANCELLABLE) + .expect("read commit") + .0 + .downcast::() + .expect("downcast"); + let result = checksum_file(&file, ObjectType::File, NONE_CANCELLABLE).expect("checksum file"); + + assert_eq!(file.get_checksum().unwrap(), result.to_string()); +} diff --git a/rust-bindings/rust/tests/tests.rs b/rust-bindings/rust/tests/tests.rs index 3a8502de..2e6f7178 100644 --- a/rust-bindings/rust/tests/tests.rs +++ b/rust-bindings/rust/tests/tests.rs @@ -6,5 +6,6 @@ extern crate tempfile; #[macro_use] extern crate maplit; +mod functions; mod repo; mod util;