Implement ostree::checksum_file_from_input

This commit is contained in:
Felix Krull 2019-09-02 13:08:35 +02:00 committed by Colin Walters
parent 7f3bd56d0d
commit 14f2ff43df
2 changed files with 62 additions and 1 deletions

View File

@ -32,3 +32,35 @@ pub fn checksum_file<P: IsA<gio::File>, Q: IsA<gio::Cancellable>>(
}
}
}
pub fn checksum_file_from_input<P: IsA<gio::InputStream>, Q: IsA<gio::Cancellable>>(
file_info: &gio::FileInfo,
xattrs: Option<&glib::Variant>,
in_: Option<&P>,
objtype: ObjectType,
cancellable: Option<&Q>,
) -> Result<Checksum, Box<dyn Error>> {
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::<glib::Error>::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))
}
}
}

View File

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