From ad26abaa7e058f484df9acebf97c66d692e0752d Mon Sep 17 00:00:00 2001 From: Felix Krull Date: Mon, 2 Sep 2019 14:39:56 +0200 Subject: [PATCH] Implement ostree::checksum_file_at --- rust-bindings/rust/src/functions.rs | 49 ++++++++++++++++--- .../rust/tests/functions/checksum_file_at.rs | 27 ++++++++++ rust-bindings/rust/tests/functions/mod.rs | 8 ++- 3 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 rust-bindings/rust/tests/functions/checksum_file_at.rs diff --git a/rust-bindings/rust/src/functions.rs b/rust-bindings/rust/src/functions.rs index a017e049..5a640b2b 100644 --- a/rust-bindings/rust/src/functions.rs +++ b/rust-bindings/rust/src/functions.rs @@ -1,3 +1,5 @@ +#[cfg(any(feature = "v2017_13", feature = "dox"))] +use crate::ChecksumFlags; use crate::{Checksum, ObjectType}; #[cfg(feature = "futures")] use futures::future; @@ -6,7 +8,7 @@ use glib::translate::*; use glib_sys::GFALSE; #[cfg(feature = "futures")] use std::boxed::Box as Box_; -use std::error::Error; +use std::error; use std::mem::MaybeUninit; use std::ptr; @@ -14,7 +16,7 @@ pub fn checksum_file, Q: IsA>( f: &P, objtype: ObjectType, cancellable: Option<&Q>, -) -> Result> { +) -> Result> { unsafe { let mut out_csum = ptr::null_mut(); let mut error = ptr::null_mut(); @@ -32,7 +34,7 @@ pub fn checksum_file, Q: IsA>( pub fn checksum_file_async< P: IsA, Q: IsA, - R: FnOnce(Result>) + Send + 'static, + R: FnOnce(Result>) + Send + 'static, >( f: &P, objtype: ObjectType, @@ -42,7 +44,7 @@ pub fn checksum_file_async< ) { let user_data: Box = Box::new(callback); unsafe extern "C" fn checksum_file_async_trampoline< - R: FnOnce(Result>) + Send + 'static, + R: FnOnce(Result>) + Send + 'static, >( _source_object: *mut gobject_sys::GObject, res: *mut gio_sys::GAsyncResult, @@ -79,7 +81,8 @@ pub fn checksum_file_async_future + Clone + 'static>( f: &P, objtype: ObjectType, io_priority: i32, -) -> Box_>> + std::marker::Unpin> { +) -> Box_>> + std::marker::Unpin> +{ use fragile::Fragile; use gio::GioFuture; @@ -100,7 +103,7 @@ pub fn checksum_file_from_input, Q: IsA, objtype: ObjectType, cancellable: Option<&Q>, -) -> Result> { +) -> Result> { unsafe { let mut out_csum = ptr::null_mut(); let mut error = ptr::null_mut(); @@ -117,11 +120,43 @@ pub fn checksum_file_from_input, Q: IsA>( + dfd: i32, + path: &std::path::Path, + stbuf: Option<&libc::stat>, + objtype: ObjectType, + flags: ChecksumFlags, + cancellable: Option<&P>, +) -> Result { + unsafe { + let mut out_checksum = ptr::null_mut(); + let mut error = ptr::null_mut(); + ostree_sys::ostree_checksum_file_at( + dfd, + path.to_glib_none().0, + stbuf + .map(|p| p as *const libc::stat as *mut libc::stat) + .unwrap_or(ptr::null_mut()), + objtype.to_glib(), + flags.to_glib(), + &mut out_checksum, + cancellable.map(|p| p.as_ref()).to_glib_none().0, + &mut error, + ); + if error.is_null() { + Ok(from_glib_full(out_checksum)) + } else { + Err(from_glib_full(error)) + } + } +} + unsafe fn checksum_file_error( out_csum: *mut [*mut u8; 32], error: *mut glib_sys::GError, ret: i32, -) -> Result> { +) -> Result> { if !error.is_null() { Err(Box::::new(from_glib_full(error))) } else if ret == GFALSE { diff --git a/rust-bindings/rust/tests/functions/checksum_file_at.rs b/rust-bindings/rust/tests/functions/checksum_file_at.rs new file mode 100644 index 00000000..7db75921 --- /dev/null +++ b/rust-bindings/rust/tests/functions/checksum_file_at.rs @@ -0,0 +1,27 @@ +use gio::NONE_CANCELLABLE; +use ostree::{checksum_file_at, ChecksumFlags, ObjectType, RepoMode}; +use std::path::PathBuf; +use util::TestRepo; + +#[test] +fn should_checksum_file_at() { + let repo = TestRepo::new_with_mode(RepoMode::BareUser); + repo.test_commit("test"); + + let result = checksum_file_at( + repo.repo.get_dfd(), + &PathBuf::from( + "objects/89/f84ca9854a80e85b583e46a115ba4985254437027bad34f0b113219323d3f8.file", + ), + None, + ObjectType::File, + ChecksumFlags::IGNORE_XATTRS, + NONE_CANCELLABLE, + ) + .expect("checksum file at"); + + assert_eq!( + result.as_str(), + "89f84ca9854a80e85b583e46a115ba4985254437027bad34f0b113219323d3f8", + ); +} diff --git a/rust-bindings/rust/tests/functions/mod.rs b/rust-bindings/rust/tests/functions/mod.rs index 71b557cb..3656d95d 100644 --- a/rust-bindings/rust/tests/functions/mod.rs +++ b/rust-bindings/rust/tests/functions/mod.rs @@ -1,8 +1,12 @@ use gio::NONE_CANCELLABLE; -use glib::Cast; -use ostree::{checksum_file, checksum_file_from_input, ObjectType, RepoFile, RepoFileExt}; +use glib::prelude::*; +use ostree::prelude::*; +use ostree::{checksum_file, checksum_file_from_input, ObjectType, RepoFile}; use util::TestRepo; +#[cfg(feature = "v2017_13")] +mod checksum_file_at; + #[test] fn should_checksum_file() { let repo = TestRepo::new();