Rust cleanup

This commit is contained in:
Eelco Dolstra 2019-09-10 21:55:32 +02:00
parent 343ebcc048
commit 8110b4ebb2
6 changed files with 74 additions and 63 deletions

View file

@ -2,6 +2,7 @@
name = "nix-rust" name = "nix-rust"
version = "0.1.0" version = "0.1.0"
authors = ["Eelco Dolstra <edolstra@gmail.com>"] authors = ["Eelco Dolstra <edolstra@gmail.com>"]
edition = "2018"
[lib] [lib]
name = "nixrust" name = "nixrust"

5
nix-rust/src/error.rs Normal file
View file

@ -0,0 +1,5 @@
#[derive(Debug)]
pub enum Error {
Misc(String),
Foreign(libc::c_void), // == std::exception_ptr
}

14
nix-rust/src/foreign.rs Normal file
View file

@ -0,0 +1,14 @@
/// 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)
}
}

View file

@ -1,66 +1,10 @@
extern crate libc; mod error;
extern crate tar; mod foreign;
mod tarfile;
use std::fs; pub use error::Error;
use std::io;
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] #[no_mangle]
pub extern "C" fn unpack_tarfile(source: Source, dest_dir: &str) -> bool { pub extern "C" fn unpack_tarfile(source: foreign::Source, dest_dir: &str) {
// FIXME: handle errors. tarfile::unpack_tarfile(source, dest_dir).unwrap();
let dest_dir = Path::new(dest_dir);
let mut tar = Archive::new(source);
for file in tar.entries().unwrap() {
let mut file = file.unwrap();
let dest_file = dest_dir.join(file.path().unwrap());
fs::create_dir_all(dest_file.parent().unwrap()).unwrap();
match file.header().entry_type() {
tar::EntryType::Directory => {
fs::create_dir(dest_file).unwrap();
}
tar::EntryType::Regular => {
let mode = if file.header().mode().unwrap() & libc::S_IXUSR == 0 {
0o666
} else {
0o777
};
let mut f = fs::OpenOptions::new()
.create(true)
.write(true)
.mode(mode)
.open(dest_file)
.unwrap();
io::copy(&mut file, &mut f).unwrap();
}
tar::EntryType::Symlink => {
std::os::unix::fs::symlink(file.header().link_name().unwrap().unwrap(), dest_file).unwrap();
}
t => panic!("Unsupported tar entry type '{:?}'.", t),
}
}
true
} }

47
nix-rust/src/tarfile.rs Normal file
View file

@ -0,0 +1,47 @@
use crate::{foreign::Source, Error};
use std::fs;
use std::io;
use std::os::unix::fs::OpenOptionsExt;
use std::path::Path;
use tar::Archive;
pub fn unpack_tarfile(source: Source, dest_dir: &str) -> Result<(), Error> {
let dest_dir = Path::new(dest_dir);
let mut tar = Archive::new(source);
for file in tar.entries().unwrap() {
let mut file = file.unwrap();
let dest_file = dest_dir.join(file.path().unwrap());
fs::create_dir_all(dest_file.parent().unwrap()).unwrap();
match file.header().entry_type() {
tar::EntryType::Directory => {
fs::create_dir(dest_file).unwrap();
}
tar::EntryType::Regular => {
let mode = if file.header().mode().unwrap() & libc::S_IXUSR == 0 {
0o666
} else {
0o777
};
let mut f = fs::OpenOptions::new()
.create(true)
.write(true)
.mode(mode)
.open(dest_file)
.unwrap();
io::copy(&mut file, &mut f).unwrap();
}
tar::EntryType::Symlink => {
std::os::unix::fs::symlink(file.header().link_name().unwrap().unwrap(), dest_file)
.unwrap();
}
t => return Err(Error::Misc(format!("unsupported tar entry type '{:?}'", t))),
}
}
Ok(())
}

View file

@ -40,5 +40,5 @@ struct Source
} }
extern "C" { extern "C" {
bool unpack_tarfile(rust::Source source, rust::StringSlice dest_dir); void unpack_tarfile(rust::Source source, rust::StringSlice dest_dir);
} }