Skip to content

Commit

Permalink
Don't quit if opening a file fails.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BurntSushi committed Sep 26, 2016
1 parent 2da0eab commit 104d740
Showing 1 changed file with 11 additions and 37 deletions.
48 changes: 11 additions & 37 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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));
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -253,7 +239,6 @@ enum Work {
enum WorkReady {
Stdin,
DirFile(DirEntry, File),
PathFile(PathBuf, File),
}

struct MultiWorker {
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 104d740

Please sign in to comment.