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 { 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) -> Result { } 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) -> Result { Ok(worker.match_count) } -fn run_one(args: Arc, path: &Path) -> Result { - 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) -> Result { 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) => {