2019-09-10 19:55:32 +00:00
|
|
|
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);
|
|
|
|
|
2019-09-10 23:15:20 +00:00
|
|
|
for file in tar.entries()? {
|
|
|
|
let mut file = file?;
|
2019-09-10 19:55:32 +00:00
|
|
|
|
2019-09-10 23:15:20 +00:00
|
|
|
let dest_file = dest_dir.join(file.path()?);
|
2019-09-10 19:55:32 +00:00
|
|
|
|
2019-09-10 23:15:20 +00:00
|
|
|
fs::create_dir_all(dest_file.parent().unwrap())?;
|
2019-09-10 19:55:32 +00:00
|
|
|
|
|
|
|
match file.header().entry_type() {
|
|
|
|
tar::EntryType::Directory => {
|
2019-09-10 23:15:20 +00:00
|
|
|
fs::create_dir(dest_file)?;
|
2019-09-10 19:55:32 +00:00
|
|
|
}
|
|
|
|
tar::EntryType::Regular => {
|
2019-11-26 23:17:24 +00:00
|
|
|
let mode = if file.header().mode()? & (libc::S_IXUSR as u32) == 0 {
|
2019-09-10 19:55:32 +00:00
|
|
|
0o666
|
|
|
|
} else {
|
|
|
|
0o777
|
|
|
|
};
|
|
|
|
let mut f = fs::OpenOptions::new()
|
|
|
|
.create(true)
|
|
|
|
.write(true)
|
|
|
|
.mode(mode)
|
2019-09-10 23:15:20 +00:00
|
|
|
.open(dest_file)?;
|
|
|
|
io::copy(&mut file, &mut f)?;
|
2019-09-10 19:55:32 +00:00
|
|
|
}
|
|
|
|
tar::EntryType::Symlink => {
|
2019-09-10 23:15:20 +00:00
|
|
|
std::os::unix::fs::symlink(file.header().link_name()?.unwrap(), dest_file)?;
|
2019-09-10 19:55:32 +00:00
|
|
|
}
|
2019-09-11 13:03:17 +00:00
|
|
|
tar::EntryType::XGlobalHeader | tar::EntryType::XHeader => {}
|
2019-09-10 19:55:32 +00:00
|
|
|
t => return Err(Error::Misc(format!("unsupported tar entry type '{:?}'", t))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|