decompress: match mime type correctly

This commit is contained in:
phiresky 2019-06-16 12:05:27 +02:00
parent 21f5178d15
commit 8400a4a2bb
13 changed files with 34 additions and 19 deletions

Binary file not shown.

BIN
exampledir/tar/test.tar.zip Normal file

Binary file not shown.

View File

@ -45,22 +45,33 @@ impl GetMetadata for DecompressAdapter {
}
}
fn decompress_any<'a, R>(filename: &Path, inp: &'a mut R) -> Fallible<Box<dyn Read + 'a>>
fn decompress_any<'a, R>(reason: &SlowMatcher, inp: &'a mut R) -> Fallible<Box<dyn Read + 'a>>
where
R: Read,
{
let extension = filename.extension().map(|e| e.to_string_lossy().to_owned());
use FastMatcher::*;
use SlowMatcher::*;
let gz = |inp: &'a mut R| Box::new(flate2::read::MultiGzDecoder::new(inp));
let bz2 = |inp: &'a mut R| Box::new(bzip2::read::BzDecoder::new(inp));
let xz = |inp: &'a mut R| Box::new(xz2::read::XzDecoder::new_multi_decoder(inp));
let zst = |inp: &'a mut R| zstd::stream::read::Decoder::new(inp); // returns result
match extension {
Some(e) => Ok(match e.to_owned().as_ref() {
"tgz" | "gz" => Box::new(flate2::read::MultiGzDecoder::new(inp)),
"tbz" | "tbz2" | "bz2" => Box::new(bzip2::read::BzDecoder::new(inp)),
"xz" => Box::new(xz2::read::XzDecoder::new_multi_decoder(inp)),
"zst" => Box::new(zstd::stream::read::Decoder::new(inp)?),
Ok(match reason {
Fast(FileExtension(ext)) => match ext.as_ref() {
"tgz" | "gz" => gz(inp),
"tbz" | "tbz2" | "bz2" => bz2(inp),
"xz" => xz(inp),
"zst" => Box::new(zst(inp)?),
ext => Err(format_err!("don't know how to decompress {}", ext))?,
}),
None => Err(format_err!("no extension")),
}
},
MimeType(mime) => match mime.as_ref() {
"application/gzip" => gz(inp),
"application/x-bzip" => bz2(inp),
"application/x-xz" => xz(inp),
"application/zstd" => Box::new(zst(inp)?),
mime => Err(format_err!("don't know how to decompress mime {}", mime))?,
},
})
}
fn get_inner_filename(filename: &Path) -> PathBuf {
let extension = filename
@ -90,7 +101,7 @@ impl FileAdapter for DecompressAdapter {
..
} = ai;
let mut decompress = decompress_any(filepath_hint, &mut inp)?;
let mut decompress = decompress_any(detection_reason, &mut inp)?;
let ai2: AdaptInfo = AdaptInfo {
filepath_hint: &get_inner_filename(filepath_hint),
is_real_file: false,

View File

@ -59,10 +59,14 @@ pub fn adapter_matcher<T: AsRef<str>>(
use SlowMatcher::*;
for matcher in metadata.get_matchers(slow) {
match matcher.as_ref() {
f @ MimeType(re) => mime_regexes.push((re.clone(), adapter.clone(), f)),
f @ Fast(FastMatcher::FileExtension(re)) => {
fname_regexes.push((extension_to_regex(re), adapter.clone(), f))
MimeType(re) => {
mime_regexes.push((re.clone(), adapter.clone(), MimeType(re.clone())))
}
Fast(FastMatcher::FileExtension(re)) => fname_regexes.push((
extension_to_regex(re),
adapter.clone(),
Fast(FastMatcher::FileExtension(re.clone())),
)),
};
}
}
@ -112,11 +116,11 @@ pub fn adapter_matcher<T: AsRef<str>>(
if fname_matches.is_empty() {
None
} else {
let (_, adapter, matcher) = fname_regexes[fname_matches[0]];
let (_, adapter, matcher) = &fname_regexes[fname_matches[0]];
Some((adapter.clone(), matcher.clone()))
}
} else {
let (_, adapter, matcher) = mime_regexes[mime_matches[0]];
let (_, adapter, matcher) = &mime_regexes[mime_matches[0]];
Some((adapter.clone(), matcher.clone()))
}
})

View File

@ -101,7 +101,7 @@ pub fn rga_preproc(ai: AdaptInfo) -> Result<(), Error> {
archive_recursion_depth,
config: PreprocConfig { cache: None, args },
},
detection_reason,
&detection_reason,
)?;
let compressed = compbuf
.into_inner()
@ -134,7 +134,7 @@ pub fn rga_preproc(ai: AdaptInfo) -> Result<(), Error> {
archive_recursion_depth,
config: PreprocConfig { cache: None, args },
},
detection_reason,
&detection_reason,
)?;
Ok(())
}