diff options
author | Balaji Sivaraman <balaji@balajisivaraman.com> | 2018-01-07 21:35:58 +0530 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2018-01-30 09:13:53 -0500 |
commit | f007f940c53a4818ead58f2fe2e0fac95cc3a40a (patch) | |
tree | 4612a86e94ffc22f44c9851925fdf01f18b920ad /src/worker.rs | |
parent | a8543f798d5cd0ccfb038c2b80a640f02521c370 (diff) |
search: add support for searching compressed files
This commit adds opt-in support for searching compressed files during
recursive search. This behavior is only enabled when the
`-z/--search-zip` flag is passed to ripgrep. When enabled, a limited set
of common compression formats are recognized via file extension, and a
new process is spawned to perform the decompression. ripgrep then
searches the stdout of that spawned process.
Closes #539
Diffstat (limited to 'src/worker.rs')
-rw-r--r-- | src/worker.rs | 46 |
1 files changed, 33 insertions, 13 deletions
diff --git a/src/worker.rs b/src/worker.rs index b487c7d5..3c00cc07 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -9,6 +9,7 @@ use memmap::Mmap; use termcolor::WriteColor; use decoder::DecodeReader; +use decompressor::{self, DecompressionReader}; use pathutil::strip_prefix; use printer::Printer; use search_buffer::BufferSearcher; @@ -42,6 +43,7 @@ struct Options { no_messages: bool, quiet: bool, text: bool, + search_zip_files: bool } impl Default for Options { @@ -61,6 +63,7 @@ impl Default for Options { no_messages: false, quiet: false, text: false, + search_zip_files: false, } } } @@ -190,6 +193,12 @@ impl WorkerBuilder { self.opts.text = yes; self } + + /// If enabled, search through compressed files as well + pub fn search_zip_files(mut self, yes: bool) -> Self { + self.opts.search_zip_files = yes; + self + } } /// Worker is responsible for executing searches on file paths, while choosing @@ -218,22 +227,33 @@ impl Worker { } Work::DirEntry(dent) => { let mut path = dent.path(); - let file = match File::open(path) { - Ok(file) => file, - Err(err) => { - if !self.opts.no_messages { - eprintln!("{}: {}", path.display(), err); + if self.opts.search_zip_files + && decompressor::is_compressed(path) + { + match DecompressionReader::from_path(path) { + Some(reader) => self.search(printer, path, reader), + None => { + return 0; } - return 0; } - }; - if let Some(p) = strip_prefix("./", path) { - path = p; - } - if self.opts.mmap { - self.search_mmap(printer, path, &file) } else { - self.search(printer, path, file) + let file = match File::open(path) { + Ok(file) => file, + Err(err) => { + if !self.opts.no_messages { + eprintln!("{}: {}", path.display(), err); + } + return 0; + } + }; + if let Some(p) = strip_prefix("./", path) { + path = p; + } + if self.opts.mmap { + self.search_mmap(printer, path, &file) + } else { + self.search(printer, path, file) + } } } }; |