faster search by reading metadata on the direntry
instead of the path
use std::collections::VecDeque;
-use std::fs;
+use std::fs::{self, Metadata};
+use std::ffi::OsString;
use std::os::unix::fs::MetadataExt;
use std::path::PathBuf;
use std::time::{Duration, Instant};
@@ -34,7 +35,6 @@ enum BLineResult {
FilteredOutByPattern,
FilteredOutAsNonFolder,
GitIgnored,
- Invalid,
}
impl BLine {
@@ -74,6 +74,8 @@ impl BLine {
// (pattern, no_hidden, only_folders)
fn from(
parent_idx: usize,
+ metadata: Metadata,
+ name: OsString,
path: PathBuf,
depth: u16,
no_hidden: bool,
@@ -83,13 +85,7 @@ impl BLine {
) -> BLineResult {
let mut has_match = true;
let mut score = 0;
- let name = {
- let name = match &path.file_name() {
- Some(name) => name.to_string_lossy(),
- None => {
- return BLineResult::Invalid;
- }
- };
+ let name = name.to_string_lossy();
if no_hidden && name.starts_with('.') {
return BLineResult::FilteredOutAsHidden;
}
@@ -100,12 +96,8 @@ impl BLine {
has_match = false;
}
}
- name.to_string()
- };
- let mut has_error = false;
let mut is_dir = false;
- let line_type = match fs::symlink_metadata(&path) {
- Ok(metadata) => {
+ let line_type = {
let ft = metadata.file_type();
if ft.is_dir() {
is_dir = true;
@@ -130,15 +122,6 @@ impl BLine {
}
LineType::File
}
- }
- Err(err) => {
- debug!("Error while fetching metadata: {:?}", err);
- has_error = true;
- if !has_match {
- return BLineResult::FilteredOutByPattern;
- }
- LineType::File
- }
};
let mut ignore_filter = None;
if let Some(gif) = parent_ignore_filter {
@@ -158,7 +141,7 @@ impl BLine {
childs: Vec::new(),
next_child_idx: 0,
line_type,
- has_error,
+ has_error: false,
has_match,
score,
ignore_filter,
@@ -226,8 +209,11 @@ impl TreeBuilder {
let mut childs: Vec<usize> = Vec::new();
for e in entries {
if let Ok(e) = e {
+ if let Ok(metadata) = e.metadata() {
let bl = BLine::from(
bline_idx,
+ metadata,
+ e.file_name(),
e.path(),
self.blines[bline_idx].depth + 1,
!self.options.show_hidden,
@@ -253,6 +239,7 @@ impl TreeBuilder {
}
}
}
+ }
childs.sort_by(|&a, &b| {
self.blines[a]
.name
