blob: ac6dee543a2929bf8c0da1d44608b3b6a6348e1d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
extern crate libc;
extern crate tar;
use std::fs;
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]
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(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
}
|