From 5b1bc5041890954b4b779bf873b29a981e884456 Mon Sep 17 00:00:00 2001 From: Felix Krull Date: Wed, 26 Aug 2020 00:45:57 +0200 Subject: [PATCH] src: add CommitSizesEntry --- rust-bindings/rust/conf/ostree.toml | 1 + .../rust/src/auto/commit_sizes_entry.rs | 29 +++++++++++ rust-bindings/rust/src/auto/functions.rs | 15 ++++-- rust-bindings/rust/src/auto/mod.rs | 5 ++ rust-bindings/rust/src/auto/versions.txt | 2 +- rust-bindings/rust/src/checksum.rs | 45 ++++++++++++----- rust-bindings/rust/src/commit_sizes_entry.rs | 48 +++++++++++++++++++ rust-bindings/rust/src/lib.rs | 11 ++--- .../mod.rs} | 0 rust-bindings/rust/sys/src/auto/versions.txt | 2 +- 10 files changed, 134 insertions(+), 24 deletions(-) create mode 100644 rust-bindings/rust/src/auto/commit_sizes_entry.rs create mode 100644 rust-bindings/rust/src/commit_sizes_entry.rs rename rust-bindings/rust/src/{repo_checkout_at_options.rs => repo_checkout_at_options/mod.rs} (100%) diff --git a/rust-bindings/rust/conf/ostree.toml b/rust-bindings/rust/conf/ostree.toml index 8b905fcd..ae0a5710 100644 --- a/rust-bindings/rust/conf/ostree.toml +++ b/rust-bindings/rust/conf/ostree.toml @@ -14,6 +14,7 @@ generate = [ "OSTree.AsyncProgress", "OSTree.BootconfigParser", "OSTree.ChecksumFlags", + "OSTree.CommitSizesEntry", "OSTree.Deployment", "OSTree.DeploymentUnlockedState", "OSTree.DiffFlags", diff --git a/rust-bindings/rust/src/auto/commit_sizes_entry.rs b/rust-bindings/rust/src/auto/commit_sizes_entry.rs new file mode 100644 index 00000000..0dc86912 --- /dev/null +++ b/rust-bindings/rust/src/auto/commit_sizes_entry.rs @@ -0,0 +1,29 @@ +// This file was generated by gir (https://github.com/gtk-rs/gir) +// from gir-files (https://github.com/gtk-rs/gir-files) +// DO NOT EDIT + +#[cfg(any(feature = "v2020_1", feature = "dox"))] +use glib::translate::*; +use ostree_sys; +#[cfg(any(feature = "v2020_1", feature = "dox"))] +use ObjectType; + +glib_wrapper! { + #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] + pub struct CommitSizesEntry(Boxed); + + match fn { + copy => |ptr| ostree_sys::ostree_commit_sizes_entry_copy(mut_override(ptr)), + free => |ptr| ostree_sys::ostree_commit_sizes_entry_free(ptr), + get_type => || ostree_sys::ostree_commit_sizes_entry_get_type(), + } +} + +impl CommitSizesEntry { + #[cfg(any(feature = "v2020_1", feature = "dox"))] + pub fn new(checksum: &str, objtype: ObjectType, unpacked: u64, archived: u64) -> Option { + unsafe { + from_glib_full(ostree_sys::ostree_commit_sizes_entry_new(checksum.to_glib_none().0, objtype.to_glib(), unpacked, archived)) + } + } +} diff --git a/rust-bindings/rust/src/auto/functions.rs b/rust-bindings/rust/src/auto/functions.rs index 82e398b6..8749b57b 100644 --- a/rust-bindings/rust/src/auto/functions.rs +++ b/rust-bindings/rust/src/auto/functions.rs @@ -10,6 +10,8 @@ use glib::GString; use ostree_sys; use std::mem; use std::ptr; +#[cfg(any(feature = "v2020_1", feature = "dox"))] +use CommitSizesEntry; use DiffFlags; use DiffItem; use ObjectType; @@ -105,10 +107,15 @@ pub fn commit_get_content_checksum(commit_variant: &glib::Variant) -> Option) -> Result<(), glib::Error> { -// unsafe { TODO: call ostree_sys:ostree_commit_get_object_sizes() } -//} +#[cfg(any(feature = "v2020_1", feature = "dox"))] +pub fn commit_get_object_sizes(commit_variant: &glib::Variant) -> Result, glib::Error> { + unsafe { + let mut out_sizes_entries = ptr::null_mut(); + let mut error = ptr::null_mut(); + let _ = ostree_sys::ostree_commit_get_object_sizes(commit_variant.to_glib_none().0, &mut out_sizes_entries, &mut error); + if error.is_null() { Ok(FromGlibPtrContainer::from_glib_container(out_sizes_entries)) } else { Err(from_glib_full(error)) } + } +} pub fn commit_get_parent(commit_variant: &glib::Variant) -> Option { unsafe { diff --git a/rust-bindings/rust/src/auto/mod.rs b/rust-bindings/rust/src/auto/mod.rs index 468b873b..e6494dca 100644 --- a/rust-bindings/rust/src/auto/mod.rs +++ b/rust-bindings/rust/src/auto/mod.rs @@ -80,6 +80,11 @@ mod collection_ref; #[cfg(any(feature = "v2018_6", feature = "dox"))] pub use self::collection_ref::CollectionRef; +#[cfg(any(feature = "v2020_1", feature = "dox"))] +mod commit_sizes_entry; +#[cfg(any(feature = "v2020_1", feature = "dox"))] +pub use self::commit_sizes_entry::CommitSizesEntry; + mod diff_item; pub use self::diff_item::DiffItem; diff --git a/rust-bindings/rust/src/auto/versions.txt b/rust-bindings/rust/src/auto/versions.txt index 85a1b25d..557e5d3e 100644 --- a/rust-bindings/rust/src/auto/versions.txt +++ b/rust-bindings/rust/src/auto/versions.txt @@ -1,2 +1,2 @@ Generated by gir (https://github.com/gtk-rs/gir @ 2d1ffab1) -from gir-files (https://github.com/gtk-rs/gir-files @ eec42a9) +from gir-files (https://github.com/gtk-rs/gir-files @ ff904f0) diff --git a/rust-bindings/rust/src/checksum.rs b/rust-bindings/rust/src/checksum.rs index 097438b9..1058f718 100644 --- a/rust-bindings/rust/src/checksum.rs +++ b/rust-bindings/rust/src/checksum.rs @@ -1,9 +1,10 @@ -use glib::translate::{from_glib_full, FromGlibPtrFull}; -use glib::GString; +use glib::{ + translate::{from_glib_full, FromGlibPtrFull, FromGlibPtrNone}, + GString, +}; use glib_sys::{g_free, g_malloc, g_malloc0, gpointer}; use libc::c_char; -use std::fmt; -use std::ptr::copy_nonoverlapping; +use std::{fmt, ptr::copy_nonoverlapping}; const BYTES_LEN: usize = ostree_sys::OSTREE_SHA256_DIGEST_LEN as usize; const HEX_LEN: usize = ostree_sys::OSTREE_SHA256_STRING_LEN as usize; @@ -16,6 +17,8 @@ pub struct Checksum { } impl Checksum { + pub const DIGEST_LEN: usize = BYTES_LEN; + /// Create a `Checksum` value, taking ownership of the given memory location. /// /// # Safety @@ -23,11 +26,20 @@ impl Checksum { /// `g_free` (this is e.g. the case if the memory was allocated with `g_malloc`). The value /// takes ownership of the memory, i.e. the memory is freed when the value is dropped. The /// memory must not be freed by other code. - unsafe fn new(bytes: *mut [u8; BYTES_LEN]) -> Checksum { + unsafe fn new(bytes: *mut [u8; Self::DIGEST_LEN]) -> Checksum { assert!(!bytes.is_null()); Checksum { bytes } } + /// Create a `Checksum` from a byte array. + pub fn from_bytes(checksum: &[u8; Self::DIGEST_LEN]) -> Checksum { + let ptr = checksum as *const [u8; BYTES_LEN] as *mut [u8; BYTES_LEN]; + unsafe { + // Safety: we know this byte array is long enough. + Checksum::from_glib_none(ptr) + } + } + /// Create a `Checksum` from a hexadecimal SHA256 string. /// /// Unfortunately, the underlying libostree function has no way to report parsing errors. If the @@ -93,12 +105,7 @@ impl Drop for Checksum { impl Clone for Checksum { fn clone(&self) -> Self { - unsafe { - let cloned = g_malloc(BYTES_LEN) as *mut [u8; BYTES_LEN]; - // copy one array of 32 elements - copy_nonoverlapping::<[u8; BYTES_LEN]>(self.bytes, cloned, 1); - Checksum::new(cloned) - } + unsafe { Checksum::from_glib_none(self.bytes) } } } @@ -140,6 +147,15 @@ impl FromGlibPtrFull<*mut u8> for Checksum { } } +impl FromGlibPtrNone<*mut [u8; BYTES_LEN]> for Checksum { + unsafe fn from_glib_none(ptr: *mut [u8; BYTES_LEN]) -> Self { + let cloned = g_malloc(BYTES_LEN) as *mut [u8; BYTES_LEN]; + // copy one array of 32 elements + copy_nonoverlapping::<[u8; BYTES_LEN]>(ptr, cloned, 1); + Checksum::new(cloned) + } +} + #[cfg(test)] mod tests { use super::*; @@ -156,6 +172,13 @@ mod tests { assert_eq!(checksum.to_string(), "00".repeat(BYTES_LEN)); } + #[test] + fn should_create_checksum_from_bytes_copy() { + let bytes = [0u8; BYTES_LEN]; + let checksum = Checksum::from_bytes(&bytes); + assert_eq!(checksum.to_string(), "00".repeat(BYTES_LEN)); + } + #[test] fn should_parse_checksum_string_to_bytes() { let csum = Checksum::from_hex(CHECKSUM_STRING); diff --git a/rust-bindings/rust/src/commit_sizes_entry.rs b/rust-bindings/rust/src/commit_sizes_entry.rs new file mode 100644 index 00000000..d96b4f0f --- /dev/null +++ b/rust-bindings/rust/src/commit_sizes_entry.rs @@ -0,0 +1,48 @@ +use crate::{auto::CommitSizesEntry, auto::ObjectType}; +use glib::{ + translate::{FromGlib, FromGlibPtrNone, ToGlibPtr}, + GString, +}; + +impl CommitSizesEntry { + /// Object checksum as hex string. + pub fn checksum(&self) -> GString { + let underlying = self.to_glib_none(); + unsafe { GString::from_glib_none((*underlying.0).checksum) } + } + + /// The object type. + pub fn objtype(&self) -> ObjectType { + let underlying = self.to_glib_none(); + unsafe { ObjectType::from_glib((*underlying.0).objtype) } + } + + /// Unpacked object size. + pub fn unpacked(&self) -> u64 { + let underlying = self.to_glib_none(); + unsafe { (*underlying.0).unpacked } + } + + /// Compressed object size. + pub fn archived(&self) -> u64 { + let underlying = self.to_glib_none(); + unsafe { (*underlying.0).archived } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const CHECKSUM_STRING: &str = + "bf875306783efdc5bcab37ea10b6ca4e9b6aea8b94580d0ca94af120565c0e8a"; + + #[test] + fn should_get_values_from_commit_sizes_entry() { + let entry = CommitSizesEntry::new(CHECKSUM_STRING, ObjectType::Commit, 15, 16).unwrap(); + assert_eq!(entry.checksum(), CHECKSUM_STRING); + assert_eq!(entry.objtype(), ObjectType::Commit); + assert_eq!(entry.unpacked(), 15); + assert_eq!(entry.archived(), 16); + } +} diff --git a/rust-bindings/rust/src/lib.rs b/rust-bindings/rust/src/lib.rs index 6046c7c3..64f435cc 100644 --- a/rust-bindings/rust/src/lib.rs +++ b/rust-bindings/rust/src/lib.rs @@ -29,33 +29,30 @@ pub use crate::auto::*; // handwritten code mod checksum; pub use crate::checksum::*; - #[cfg(any(feature = "v2018_6", feature = "dox"))] mod collection_ref; #[cfg(any(feature = "v2018_6", feature = "dox"))] pub use crate::collection_ref::*; - mod functions; pub use crate::functions::*; - #[cfg(any(feature = "v2019_3", feature = "dox"))] mod kernel_args; #[cfg(any(feature = "v2019_3", feature = "dox"))] pub use crate::kernel_args::*; - mod object_name; pub use crate::object_name::*; - mod repo; pub use crate::repo::*; - #[cfg(any(feature = "v2016_8", feature = "dox"))] mod repo_checkout_at_options; #[cfg(any(feature = "v2016_8", feature = "dox"))] pub use crate::repo_checkout_at_options::*; - mod se_policy; pub use crate::se_policy::*; +#[cfg(any(feature = "v2020_1", feature = "dox"))] +mod commit_sizes_entry; +#[cfg(any(feature = "v2020_1", feature = "dox"))] +pub use crate::commit_sizes_entry::*; // tests #[cfg(test)] diff --git a/rust-bindings/rust/src/repo_checkout_at_options.rs b/rust-bindings/rust/src/repo_checkout_at_options/mod.rs similarity index 100% rename from rust-bindings/rust/src/repo_checkout_at_options.rs rename to rust-bindings/rust/src/repo_checkout_at_options/mod.rs diff --git a/rust-bindings/rust/sys/src/auto/versions.txt b/rust-bindings/rust/sys/src/auto/versions.txt index 85a1b25d..557e5d3e 100644 --- a/rust-bindings/rust/sys/src/auto/versions.txt +++ b/rust-bindings/rust/sys/src/auto/versions.txt @@ -1,2 +1,2 @@ Generated by gir (https://github.com/gtk-rs/gir @ 2d1ffab1) -from gir-files (https://github.com/gtk-rs/gir-files @ eec42a9) +from gir-files (https://github.com/gtk-rs/gir-files @ ff904f0)