summaryrefslogtreecommitdiffstats
path: root/nix-rust
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2019-03-27 20:45:56 +0100
committerEelco Dolstra <edolstra@gmail.com>2019-11-26 22:07:28 +0100
commite60f6bd4ce831ced94fafeb527c429b6f88159ac (patch)
treeedd4c3711886c7b2e6ce7480732e6c67e72a5174 /nix-rust
parent11da5b2816e11b081d8ff38db4330addd2014f7e (diff)
Enable Rust code to call C++ Source objects
Diffstat (limited to 'nix-rust')
-rw-r--r--nix-rust/src/lib.rs19
1 files changed, 17 insertions, 2 deletions
diff --git a/nix-rust/src/lib.rs b/nix-rust/src/lib.rs
index abcb89055..799d52e31 100644
--- a/nix-rust/src/lib.rs
+++ b/nix-rust/src/lib.rs
@@ -7,13 +7,28 @@ use std::os::unix::fs::OpenOptionsExt;
use std::path::Path;
use tar::Archive;
+/// A wrapper around Nix's Source class that provides the Read trait.
+#[repr(C)]
+pub struct Source {
+ fun: extern "C" fn(this: *mut libc::c_void, data: &mut [u8]) -> usize,
+ this: *mut libc::c_void,
+}
+
+impl std::io::Read for Source {
+ fn read(&mut self, buf: &mut [u8]) -> std::result::Result<usize, std::io::Error> {
+ let n = (self.fun)(self.this, buf);
+ assert!(n <= buf.len());
+ Ok(n)
+ }
+}
+
#[no_mangle]
-pub extern "C" fn unpack_tarfile(data: &[u8], dest_dir: &str) -> bool {
+pub extern "C" fn unpack_tarfile(source: Source, dest_dir: &str) -> bool {
// FIXME: handle errors.
let dest_dir = Path::new(dest_dir);
- let mut tar = Archive::new(data);
+ let mut tar = Archive::new(source);
for file in tar.entries().unwrap() {
let mut file = file.unwrap();