mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-05-19 01:30:21 -07:00
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.
This commit is contained in:
parent
2da0eab2b8
commit
104d740f76
48
src/main.rs
48
src/main.rs
@ -23,7 +23,7 @@ extern crate winapi;
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::Path;
|
||||||
use std::process;
|
use std::process;
|
||||||
use std::result;
|
use std::result;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
@ -89,16 +89,15 @@ fn run(args: Args) -> Result<u64> {
|
|||||||
let args = Arc::new(args);
|
let args = Arc::new(args);
|
||||||
let paths = args.paths();
|
let paths = args.paths();
|
||||||
let threads = cmp::max(1, args.threads() - 1);
|
let threads = cmp::max(1, args.threads() - 1);
|
||||||
|
let isone =
|
||||||
|
paths.len() == 1 && (paths[0] == Path::new("-") || paths[0].is_file());
|
||||||
if args.files() {
|
if args.files() {
|
||||||
return run_files(args.clone());
|
return run_files(args.clone());
|
||||||
}
|
}
|
||||||
if args.type_list() {
|
if args.type_list() {
|
||||||
return run_types(args.clone());
|
return run_types(args.clone());
|
||||||
}
|
}
|
||||||
if paths.len() == 1 && (paths[0] == Path::new("-") || paths[0].is_file()) {
|
if threads == 1 || isone {
|
||||||
return run_one(args.clone(), &paths[0]);
|
|
||||||
}
|
|
||||||
if threads == 1 {
|
|
||||||
return run_one_thread(args.clone());
|
return run_one_thread(args.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -183,7 +182,13 @@ fn run_one_thread(args: Arc<Args>) -> Result<u64> {
|
|||||||
}
|
}
|
||||||
paths_searched += 1;
|
paths_searched += 1;
|
||||||
let mut printer = args.printer(&mut term);
|
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));
|
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)
|
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> {
|
fn run_files(args: Arc<Args>) -> Result<u64> {
|
||||||
let term = args.stdout();
|
let term = args.stdout();
|
||||||
let mut printer = args.printer(term);
|
let mut printer = args.printer(term);
|
||||||
@ -253,7 +239,6 @@ enum Work {
|
|||||||
enum WorkReady {
|
enum WorkReady {
|
||||||
Stdin,
|
Stdin,
|
||||||
DirFile(DirEntry, File),
|
DirFile(DirEntry, File),
|
||||||
PathFile(PathBuf, File),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MultiWorker {
|
struct MultiWorker {
|
||||||
@ -328,17 +313,6 @@ impl Worker {
|
|||||||
self.search(printer, path, file)
|
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 {
|
match result {
|
||||||
Ok(count) => {
|
Ok(count) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user