src: add CommitSizesEntry

This commit is contained in:
Felix Krull 2020-08-26 00:45:57 +02:00 committed by Colin Walters
parent 3d8d5ce53e
commit 5b1bc50418
10 changed files with 134 additions and 24 deletions

View File

@ -14,6 +14,7 @@ generate = [
"OSTree.AsyncProgress",
"OSTree.BootconfigParser",
"OSTree.ChecksumFlags",
"OSTree.CommitSizesEntry",
"OSTree.Deployment",
"OSTree.DeploymentUnlockedState",
"OSTree.DiffFlags",

View File

@ -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<ostree_sys::OstreeCommitSizesEntry>);
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<CommitSizesEntry> {
unsafe {
from_glib_full(ostree_sys::ostree_commit_sizes_entry_new(checksum.to_glib_none().0, objtype.to_glib(), unpacked, archived))
}
}
}

View File

@ -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<GSt
}
}
//#[cfg(any(feature = "v2020_1", feature = "dox"))]
//pub fn commit_get_object_sizes(commit_variant: &glib::Variant, out_sizes_entries: /*Ignored*/Vec<CommitSizesEntry>) -> 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<Vec<CommitSizesEntry>, 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<GString> {
unsafe {

View File

@ -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;

View File

@ -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)

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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)]

View File

@ -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)