diff options
author | Andrew Gallant <jamslam@gmail.com> | 2016-09-26 18:44:00 -0400 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2016-09-26 18:44:19 -0400 |
commit | 104d740f769256ee6beeaef5a0d1c245947c0ce9 (patch) | |
tree | 6f6e2b14cc2944a18d8b80106d31e2a842240def /src | |
parent | 2da0eab2b809b7633f13c7bf3eff28be970d4db6 (diff) |
Don't quit if opening a file fails.
This was already working correctly in multithreaded mode, but in single
threaded mode, a file failing to open caused search to stop. That's bad.
Fixes #98.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 48 |
1 files changed, 11 insertions, 37 deletions
diff --git a/src/main.rs b/src/main.rs index 7a40c07b..74326e1f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,7 +23,7 @@ extern crate winapi; use std::error::Error; use std::fs::File; use std::io::{self, Write}; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::process; use std::result; use std::sync::{Arc, Mutex}; @@ -89,16 +89,15 @@ fn run(args: Args) -> Result<u64> { let args = Arc::new(args); let paths = args.paths(); let threads = cmp::max(1, args.threads() - 1); + let isone = + paths.len() == 1 && (paths[0] == Path::new("-") || paths[0].is_file()); if args.files() { return run_files(args.clone()); } if args.type_list() { return run_types(args.clone()); } - if paths.len() == 1 && (paths[0] == Path::new("-") || paths[0].is_file()) { - return run_one(args.clone(), &paths[0]); - } - if threads == 1 { + if threads == 1 || isone { return run_one_thread(args.clone()); } @@ -183,7 +182,13 @@ fn run_one_thread(args: Arc<Args>) -> Result<u64> { } paths_searched += 1; let mut printer = args.printer(&mut term); - let file = try!(File::open(ent.path())); + let file = match File::open(ent.path()) { + Ok(file) => file, + Err(err) => { + eprintln!("{}: {}", ent.path().display(), err); + continue; + } + }; worker.do_work(&mut printer, WorkReady::DirFile(ent, file)); } } @@ -196,25 +201,6 @@ fn run_one_thread(args: Arc<Args>) -> Result<u64> { Ok(worker.match_count) } -fn run_one(args: Arc<Args>, path: &Path) -> Result<u64> { - let mut worker = Worker { - args: args.clone(), - inpbuf: args.input_buffer(), - grep: args.grep(), - match_count: 0, - }; - let term = args.stdout(); - let mut printer = args.printer(term); - let work = - if path == Path::new("-") { - WorkReady::Stdin - } else { - WorkReady::PathFile(path.to_path_buf(), try!(File::open(path))) - }; - worker.do_work(&mut printer, work); - Ok(worker.match_count) -} - fn run_files(args: Arc<Args>) -> Result<u64> { let term = args.stdout(); let mut printer = args.printer(term); @@ -253,7 +239,6 @@ enum Work { enum WorkReady { Stdin, DirFile(DirEntry, File), - PathFile(PathBuf, File), } struct MultiWorker { @@ -328,17 +313,6 @@ impl Worker { self.search(printer, path, file) } } - WorkReady::PathFile(path, file) => { - let mut path = &*path; - if let Some(p) = strip_prefix("./", path) { - path = p; - } - if self.args.mmap() { - self.search_mmap(printer, path, &file) - } else { - self.search(printer, path, file) - } - } }; match result { Ok(count) => { |