Initial version of parallel directory traversal
Before adoption: this commit came in with the history the project adopted, so no landing records it.
see #32
authored by sharkdp · committed by David Peter · parent 2d1d24c · Browse files · 1 file·+42 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+42 added−28 removed
- other:+42 added−28 removed
Provenance
- Commit
e06189e- Tree
c35d3d4- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
2d1d24c
1 file changed+42 added−28 removed
modified:src/main.rs+42 added-28 removed
15 unmodified lines16 #[cfg(target_family = "unix")]17 use std::os::unix::fs::PermissionsExt;18 use std::path::{Path, Component};19 use std::process;20+use std::sync::Arc;21 22 use clap::{App, AppSettings, Arg};23 use atty::Stream;24 use regex::{Regex, RegexBuilder};68 unmodified lines93 94 #[cfg(not(target_family = "unix"))]95 let is_executable = |p: &std::path::PathBuf| {false};96 97+ let stdout = std::io::stdout();98+ let mut handle = stdout.lock();99+100 if let Some(ref ls_colors) = config.ls_colors {101 let default_style = ansi_term::Style::default();102 103 let mut component_path = base.to_path_buf();38 unmodified lines142 .unwrap_or(&default_style)143 }144 };145 142- print!("{}", style.paint(comp_str));146+ write!(handle, "{}", style.paint(comp_str));147 148 if component_path.is_dir() && component_path != path_full {149 let sep = std::path::MAIN_SEPARATOR.to_string();146- print!("{}", style.paint(sep));150+ write!(handle, "{}", style.paint(sep));151 }152 }153 if config.null_separator {150- print!("{}", '\0');154+ writeln!(handle, "{}", '\0');155 } else {152- println!();156+ writeln!(handle);157 }158 } else {159 // Uncolorized output160 161 let prefix = if config.path_display == PathDisplay::Absolute { ROOT_DIR } else { "" };162 let separator = if config.null_separator { "\0" } else { "\n" };163 160- let r = write!(&mut std::io::stdout(), "{}{}{}", prefix, path_str, separator);164+ let r = writeln!(&mut std::io::stdout(), "{}{}{}", prefix, path_str, separator);165 166 if r.is_err() {167 // Probably a broken pipe. Exit gracefully.168 process::exit(0);1 unmodified line170 }171 }172 173 /// Recursively scan the given search path and search for files / pathnames matching the pattern.170-fn scan(root: &Path, pattern: &Regex, base: &Path, config: &FdOptions) {174+fn scan(root: &Path, pattern: Arc<Regex>, base: &Path, config: Arc<FdOptions>) {175 let walker = WalkBuilder::new(root)176 .hidden(config.ignore_hidden)177 .ignore(config.read_ignore)178 .git_ignore(config.read_ignore)1 unmodified line180 .git_global(config.read_ignore)181 .git_exclude(config.read_ignore)182 .follow_links(config.follow_links)183 .max_depth(config.max_depth)180- .build()181- .into_iter()182- .filter_map(|e| e.ok())183- .filter(|e| e.path() != root);184+ .build_parallel();185 185- for entry in walker {186- let path_rel_buf = match fshelper::path_relative_from(entry.path(), base) {187- Some(p) => p,188- None => error("Error: could not get relative path for directory entry.")189- };190- let path_rel = path_rel_buf.as_path();186+ walker.run(|| {187+ let base = base.to_owned();188+ let config = config.clone();189+ let pattern = pattern.clone();190 192- let search_str_o =193- if config.search_full_path {194- Some(path_rel.to_string_lossy())195- } else {196- path_rel.file_name()197- .map(|f| f.to_string_lossy())191+ Box::new(move |entry_o| {192+ let entry = match entry_o {193+ Ok(e) => e,194+ Err(_) => return ignore::WalkState::Continue195 };196 197+ let path_rel_buf = match fshelper::path_relative_from(entry.path(), &*base) {198+ Some(p) => p,199+ None => error("Error: could not get relative path for directory entry.")200+ };201+ let path_rel = path_rel_buf.as_path();202+203+ let search_str_o =204+ if config.search_full_path {205+ Some(path_rel.to_string_lossy())206+ } else {207+ path_rel.file_name()208+ .map(|f| f.to_string_lossy())209+ };210+200- if let Some(search_str) = search_str_o {201- pattern.find(&*search_str)202- .map(|_| print_entry(base, path_rel, config));203- }204- }211+ if let Some(search_str) = search_str_o {212+ pattern.find(&*search_str)213+ .map(|_| print_entry(&base, path_rel, &config));214+ }215+216+ ignore::WalkState::Continue217+ })218+ });219 }220 221 /// Print error message to stderr and exit with status `1`.222 fn error(message: &str) -> ! {128 unmodified lines351 352 match RegexBuilder::new(pattern)353 .case_insensitive(!config.case_sensitive)354 .build() {341- Ok(re) => scan(root_dir, &re, base, &config),355+ Ok(re) => scan(root_dir, Arc::new(re), base, Arc::new(config)),356 Err(err) => error(err.description())357 }358 }