mirror of
https://github.com/FliegendeWurst/ripgrep-all.git
synced 2024-11-24 04:14:57 +00:00
decompress: match mime type correctly
This commit is contained in:
parent
21f5178d15
commit
8400a4a2bb
BIN
exampledir/decompress/testlogbutwithoutextension
Normal file
BIN
exampledir/decompress/testlogbutwithoutextension
Normal file
Binary file not shown.
BIN
exampledir/tar/test.tar.zip
Normal file
BIN
exampledir/tar/test.tar.zip
Normal file
Binary file not shown.
@ -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
|
where
|
||||||
R: Read,
|
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 {
|
Ok(match reason {
|
||||||
Some(e) => Ok(match e.to_owned().as_ref() {
|
Fast(FileExtension(ext)) => match ext.as_ref() {
|
||||||
"tgz" | "gz" => Box::new(flate2::read::MultiGzDecoder::new(inp)),
|
"tgz" | "gz" => gz(inp),
|
||||||
"tbz" | "tbz2" | "bz2" => Box::new(bzip2::read::BzDecoder::new(inp)),
|
"tbz" | "tbz2" | "bz2" => bz2(inp),
|
||||||
"xz" => Box::new(xz2::read::XzDecoder::new_multi_decoder(inp)),
|
"xz" => xz(inp),
|
||||||
"zst" => Box::new(zstd::stream::read::Decoder::new(inp)?),
|
"zst" => Box::new(zst(inp)?),
|
||||||
ext => Err(format_err!("don't know how to decompress {}", ext))?,
|
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 {
|
fn get_inner_filename(filename: &Path) -> PathBuf {
|
||||||
let extension = filename
|
let extension = filename
|
||||||
@ -90,7 +101,7 @@ impl FileAdapter for DecompressAdapter {
|
|||||||
..
|
..
|
||||||
} = ai;
|
} = ai;
|
||||||
|
|
||||||
let mut decompress = decompress_any(filepath_hint, &mut inp)?;
|
let mut decompress = decompress_any(detection_reason, &mut inp)?;
|
||||||
let ai2: AdaptInfo = AdaptInfo {
|
let ai2: AdaptInfo = AdaptInfo {
|
||||||
filepath_hint: &get_inner_filename(filepath_hint),
|
filepath_hint: &get_inner_filename(filepath_hint),
|
||||||
is_real_file: false,
|
is_real_file: false,
|
||||||
|
@ -59,10 +59,14 @@ pub fn adapter_matcher<T: AsRef<str>>(
|
|||||||
use SlowMatcher::*;
|
use SlowMatcher::*;
|
||||||
for matcher in metadata.get_matchers(slow) {
|
for matcher in metadata.get_matchers(slow) {
|
||||||
match matcher.as_ref() {
|
match matcher.as_ref() {
|
||||||
f @ MimeType(re) => mime_regexes.push((re.clone(), adapter.clone(), f)),
|
MimeType(re) => {
|
||||||
f @ Fast(FastMatcher::FileExtension(re)) => {
|
mime_regexes.push((re.clone(), adapter.clone(), MimeType(re.clone())))
|
||||||
fname_regexes.push((extension_to_regex(re), adapter.clone(), f))
|
|
||||||
}
|
}
|
||||||
|
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() {
|
if fname_matches.is_empty() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
let (_, adapter, matcher) = fname_regexes[fname_matches[0]];
|
let (_, adapter, matcher) = &fname_regexes[fname_matches[0]];
|
||||||
Some((adapter.clone(), matcher.clone()))
|
Some((adapter.clone(), matcher.clone()))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let (_, adapter, matcher) = mime_regexes[mime_matches[0]];
|
let (_, adapter, matcher) = &mime_regexes[mime_matches[0]];
|
||||||
Some((adapter.clone(), matcher.clone()))
|
Some((adapter.clone(), matcher.clone()))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -101,7 +101,7 @@ pub fn rga_preproc(ai: AdaptInfo) -> Result<(), Error> {
|
|||||||
archive_recursion_depth,
|
archive_recursion_depth,
|
||||||
config: PreprocConfig { cache: None, args },
|
config: PreprocConfig { cache: None, args },
|
||||||
},
|
},
|
||||||
detection_reason,
|
&detection_reason,
|
||||||
)?;
|
)?;
|
||||||
let compressed = compbuf
|
let compressed = compbuf
|
||||||
.into_inner()
|
.into_inner()
|
||||||
@ -134,7 +134,7 @@ pub fn rga_preproc(ai: AdaptInfo) -> Result<(), Error> {
|
|||||||
archive_recursion_depth,
|
archive_recursion_depth,
|
||||||
config: PreprocConfig { cache: None, args },
|
config: PreprocConfig { cache: None, args },
|
||||||
},
|
},
|
||||||
detection_reason,
|
&detection_reason,
|
||||||
)?;
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user