summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsharkdp <davidpeter@web.de>2017-09-18 19:27:19 +0200
committersharkdp <davidpeter@web.de>2017-09-18 19:27:26 +0200
commitadaf4f6d73aa6935d381a4eb9be647815aeb46cd (patch)
treef48d50dd6fce8b08facedf83222bf2c34670683a
parentd8c588d8051af19cb8c45e420b31dd8cd494333e (diff)
Reduce number of 'stat' syscalls from 3 to 1, see #36
-rw-r--r--src/main.rs15
1 files changed, 8 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs
index b2d88bb..a4d4157 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -118,10 +118,8 @@ fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {
let path_str = entry.to_string_lossy();
#[cfg(target_family = "unix")]
- let is_executable = |p: &std::path::PathBuf| {
- p.metadata()
- .ok()
- .map(|f| f.permissions().mode() & 0o111 != 0)
+ let is_executable = |p: Option<&std::fs::Metadata>| {
+ p.map(|f| f.permissions().mode() & 0o111 != 0)
.unwrap_or(false)
};
@@ -150,14 +148,17 @@ fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {
component_path.push(Path::new(comp_str.deref()));
+ let metadata = component_path.metadata().ok();
+ let is_directory = metadata.as_ref().map(|md| md.is_dir()).unwrap_or(false);
+
let style =
if component_path.symlink_metadata()
.map(|md| md.file_type().is_symlink())
.unwrap_or(false) {
&ls_colors.symlink
- } else if component_path.is_dir() {
+ } else if is_directory {
&ls_colors.directory
- } else if is_executable(&component_path) {
+ } else if is_executable(metadata.as_ref()) {
&ls_colors.executable
} else {
// Look up file name
@@ -179,7 +180,7 @@ fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {
write!(handle, "{}", style.paint(comp_str)).ok();
- if component_path.is_dir() && component_path != path_full {
+ if is_directory && component_path != path_full {
let sep = std::path::MAIN_SEPARATOR.to_string();
write!(handle, "{}", style.paint(sep)).ok();
}