2019-06-06 15:59:15 +00:00
|
|
|
use super::*;
|
|
|
|
use crate::preproc::rga_preproc;
|
|
|
|
use ::tar::EntryType::Regular;
|
|
|
|
use failure::*;
|
|
|
|
use lazy_static::lazy_static;
|
2019-06-06 21:43:30 +00:00
|
|
|
|
2019-06-06 15:59:15 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
static EXTENSIONS: &[&str] = &["tar", "tar.gz", "tar.bz2", "tar.xz", "tar.zst"];
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref METADATA: AdapterMeta = AdapterMeta {
|
|
|
|
name: "tar".to_owned(),
|
|
|
|
version: 1,
|
2019-06-07 22:04:48 +00:00
|
|
|
description: "Reads a tar file as a stream and recurses down into its contents".to_owned(),
|
2019-06-11 11:34:04 +00:00
|
|
|
fast_matchers: EXTENSIONS
|
2019-06-06 15:59:15 +00:00
|
|
|
.iter()
|
2019-06-11 11:34:04 +00:00
|
|
|
.map(|s| FastMatcher::FileExtension(s.to_string()))
|
2019-06-06 15:59:15 +00:00
|
|
|
.collect(),
|
2019-06-11 11:34:04 +00:00
|
|
|
slow_matchers: None
|
2019-06-06 15:59:15 +00:00
|
|
|
};
|
|
|
|
}
|
2019-06-06 21:43:30 +00:00
|
|
|
#[derive(Default)]
|
2019-06-06 15:59:15 +00:00
|
|
|
pub struct TarAdapter;
|
|
|
|
|
|
|
|
impl TarAdapter {
|
|
|
|
pub fn new() -> TarAdapter {
|
|
|
|
TarAdapter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl GetMetadata for TarAdapter {
|
2019-06-06 21:43:30 +00:00
|
|
|
fn metadata(&self) -> &AdapterMeta {
|
2019-06-06 15:59:15 +00:00
|
|
|
&METADATA
|
|
|
|
}
|
|
|
|
}
|
2019-06-06 21:19:59 +00:00
|
|
|
|
2019-06-07 11:12:44 +00:00
|
|
|
fn decompress_any<'a, R>(filename: &Path, inp: &'a mut R) -> Fallible<Box<dyn Read + 'a>>
|
2019-06-06 21:19:59 +00:00
|
|
|
where
|
|
|
|
R: Read,
|
|
|
|
{
|
2019-06-06 15:59:15 +00:00
|
|
|
let extension = filename.extension().map(|e| e.to_string_lossy().to_owned());
|
|
|
|
match extension {
|
|
|
|
Some(e) => Ok(match e.to_owned().as_ref() {
|
2019-06-12 10:25:02 +00:00
|
|
|
"tgz" | "gz" => Box::new(flate2::read::MultiGzDecoder::new(inp)),
|
|
|
|
"tbz" | "tbz2" | "bz2" => Box::new(bzip2::read::BzDecoder::new(inp)),
|
2019-06-07 11:12:44 +00:00
|
|
|
"xz" => Box::new(xz2::read::XzDecoder::new_multi_decoder(inp)),
|
|
|
|
"zst" => Box::new(zstd::stream::read::Decoder::new(inp)?),
|
|
|
|
"tar" => Box::new(inp),
|
2019-06-06 21:19:59 +00:00
|
|
|
ext => Err(format_err!("don't know how to decompress {}", ext))?,
|
2019-06-06 15:59:15 +00:00
|
|
|
}),
|
|
|
|
None => Err(format_err!("no extension")),
|
|
|
|
}
|
2019-06-06 21:19:59 +00:00
|
|
|
}
|
2019-06-06 15:59:15 +00:00
|
|
|
|
|
|
|
impl FileAdapter for TarAdapter {
|
2019-06-06 21:19:59 +00:00
|
|
|
fn adapt(&self, ai: AdaptInfo) -> Fallible<()> {
|
2019-06-06 15:59:15 +00:00
|
|
|
let AdaptInfo {
|
|
|
|
filepath_hint,
|
|
|
|
mut inp,
|
|
|
|
oup,
|
|
|
|
line_prefix,
|
2019-06-07 13:43:19 +00:00
|
|
|
archive_recursion_depth,
|
2019-06-07 17:00:24 +00:00
|
|
|
config,
|
2019-06-06 15:59:15 +00:00
|
|
|
..
|
|
|
|
} = ai;
|
2019-06-06 21:19:59 +00:00
|
|
|
|
|
|
|
let decompress = decompress_any(filepath_hint, &mut inp)?;
|
2019-06-06 15:59:15 +00:00
|
|
|
let mut archive = ::tar::Archive::new(decompress);
|
|
|
|
for entry in archive.entries()? {
|
|
|
|
let mut file = entry.unwrap();
|
|
|
|
if Regular == file.header().entry_type() {
|
2019-06-11 18:35:20 +00:00
|
|
|
let path = PathBuf::from(file.path()?.to_owned());
|
|
|
|
eprintln!(
|
|
|
|
"{}|{}: {} bytes",
|
|
|
|
filepath_hint.display(),
|
|
|
|
path.display(),
|
|
|
|
file.header().size()?,
|
|
|
|
);
|
2019-06-06 15:59:15 +00:00
|
|
|
let line_prefix = &format!("{}{}: ", line_prefix, path.display());
|
2019-06-06 21:19:59 +00:00
|
|
|
let ai2: AdaptInfo = AdaptInfo {
|
|
|
|
filepath_hint: &path,
|
2019-06-06 21:43:30 +00:00
|
|
|
is_real_file: false,
|
2019-06-07 13:43:19 +00:00
|
|
|
archive_recursion_depth: archive_recursion_depth + 1,
|
2019-06-06 21:19:59 +00:00
|
|
|
inp: &mut file,
|
2019-06-06 21:43:30 +00:00
|
|
|
oup,
|
2019-06-06 21:19:59 +00:00
|
|
|
line_prefix,
|
2019-06-07 18:12:24 +00:00
|
|
|
config: config.clone(),
|
2019-06-06 21:19:59 +00:00
|
|
|
};
|
2019-06-07 17:00:24 +00:00
|
|
|
rga_preproc(ai2)?;
|
2019-06-06 15:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|