mirror of
https://github.com/NixOS/nix
synced 2025-06-25 06:31:14 +02:00
More Rust FFI adventures
We can now convert Rust Errors to C++ exceptions. At the Rust->C++ FFI boundary, Result<T, Error> will cause Error to be converted to and thrown as a C++ exception.
This commit is contained in:
parent
8110b4ebb2
commit
f738cd4d97
6 changed files with 133 additions and 18 deletions
|
@ -1,5 +1,30 @@
|
|||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
IOError(std::io::Error),
|
||||
Misc(String),
|
||||
Foreign(libc::c_void), // == std::exception_ptr
|
||||
Foreign(CppException),
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(err: std::io::Error) -> Self {
|
||||
Error::IOError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Error> for CppException {
|
||||
fn from(err: Error) -> Self {
|
||||
match err {
|
||||
Error::Foreign(ex) => ex,
|
||||
Error::Misc(s) => unsafe { make_error(&s) },
|
||||
Error::IOError(err) => unsafe { make_error(&err.to_string()) },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
pub struct CppException(*const libc::c_void); // == std::exception_ptr*
|
||||
|
||||
extern "C" {
|
||||
fn make_error(s: &str) -> CppException;
|
||||
}
|
||||
|
|
|
@ -4,7 +4,30 @@ mod tarfile;
|
|||
|
||||
pub use error::Error;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn unpack_tarfile(source: foreign::Source, dest_dir: &str) {
|
||||
tarfile::unpack_tarfile(source, dest_dir).unwrap();
|
||||
pub struct CBox<T> {
|
||||
ptr: *mut libc::c_void,
|
||||
phantom: std::marker::PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> CBox<T> {
|
||||
fn new(t: T) -> Self {
|
||||
unsafe {
|
||||
let size = std::mem::size_of::<T>();
|
||||
let ptr = libc::malloc(size);
|
||||
eprintln!("PTR = {:?}, SIZE = {}", ptr, size);
|
||||
*(ptr as *mut T) = t; // FIXME: probably UB
|
||||
Self {
|
||||
ptr,
|
||||
phantom: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn unpack_tarfile(
|
||||
source: foreign::Source,
|
||||
dest_dir: &str,
|
||||
) -> CBox<Result<(), error::CppException>> {
|
||||
CBox::new(tarfile::unpack_tarfile(source, dest_dir).map_err(|err| err.into()))
|
||||
}
|
||||
|
|
|
@ -10,19 +10,19 @@ pub fn unpack_tarfile(source: Source, dest_dir: &str) -> Result<(), Error> {
|
|||
|
||||
let mut tar = Archive::new(source);
|
||||
|
||||
for file in tar.entries().unwrap() {
|
||||
let mut file = file.unwrap();
|
||||
for file in tar.entries()? {
|
||||
let mut file = file?;
|
||||
|
||||
let dest_file = dest_dir.join(file.path().unwrap());
|
||||
let dest_file = dest_dir.join(file.path()?);
|
||||
|
||||
fs::create_dir_all(dest_file.parent().unwrap()).unwrap();
|
||||
fs::create_dir_all(dest_file.parent().unwrap())?;
|
||||
|
||||
match file.header().entry_type() {
|
||||
tar::EntryType::Directory => {
|
||||
fs::create_dir(dest_file).unwrap();
|
||||
fs::create_dir(dest_file)?;
|
||||
}
|
||||
tar::EntryType::Regular => {
|
||||
let mode = if file.header().mode().unwrap() & libc::S_IXUSR == 0 {
|
||||
let mode = if file.header().mode()? & libc::S_IXUSR == 0 {
|
||||
0o666
|
||||
} else {
|
||||
0o777
|
||||
|
@ -31,13 +31,11 @@ pub fn unpack_tarfile(source: Source, dest_dir: &str) -> Result<(), Error> {
|
|||
.create(true)
|
||||
.write(true)
|
||||
.mode(mode)
|
||||
.open(dest_file)
|
||||
.unwrap();
|
||||
io::copy(&mut file, &mut f).unwrap();
|
||||
.open(dest_file)?;
|
||||
io::copy(&mut file, &mut f)?;
|
||||
}
|
||||
tar::EntryType::Symlink => {
|
||||
std::os::unix::fs::symlink(file.header().link_name().unwrap().unwrap(), dest_file)
|
||||
.unwrap();
|
||||
std::os::unix::fs::symlink(file.header().link_name()?.unwrap(), dest_file)?;
|
||||
}
|
||||
t => return Err(Error::Misc(format!("unsupported tar entry type '{:?}'", t))),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue