Skip to content

Commit

Permalink
Simplify, stop tracking depth in read dir specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegrosjean committed Feb 12, 2019
1 parent e0d3e3e commit c1bffdd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 61 deletions.
7 changes: 1 addition & 6 deletions src/core/dir_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use super::ReadDirSpec;
/// descriptor.
#[derive(Debug)]
pub struct DirEntry {
/// Depth of this entry relative to the root directory where the walk started.
pub depth: usize,
/// File name of this entry without leading path component.
pub file_name: OsString,
/// File type result for the file/directory that this entry points at.
Expand All @@ -41,15 +39,13 @@ pub struct DirEntry {

impl DirEntry {
pub(crate) fn new(
depth: usize,
file_name: OsString,
file_type: Result<FileType>,
metadata: Option<Result<Metadata>>,
parent_spec: Option<Arc<ReadDirSpec>>,
content_spec: Option<Arc<ReadDirSpec>>,
) -> DirEntry {
DirEntry {
depth,
file_name,
file_type,
parent_spec,
Expand Down Expand Up @@ -86,10 +82,9 @@ impl DirEntry {
let file_name = path.file_name().unwrap_or(&root_name);
let parent_spec = path
.parent()
.map(|parent| Arc::new(ReadDirSpec::new(parent.to_path_buf(), 0, None)));
.map(|parent| Arc::new(ReadDirSpec::new(parent.to_path_buf(), None)));

Ok(DirEntry::new(
0,
file_name.to_owned(),
Ok(metadata.file_type()),
Some(Ok(metadata)),
Expand Down
2 changes: 1 addition & 1 deletion src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ where
let root_entry_result = DirEntry::try_from(&path);
let ordered_read_dir_spec = root_entry_result.as_ref().ok().and_then(|root_entry| {
if root_entry.file_type.as_ref().ok()?.is_dir() {
let read_dir_spec = Arc::new(ReadDirSpec::new(path, 0, None));
let read_dir_spec = Arc::new(ReadDirSpec::new(path, None));
return Some(Ordered::new(read_dir_spec, IndexPath::new(vec![0]), 0));
}
None
Expand Down
9 changes: 1 addition & 8 deletions src/core/read_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ pub struct ReadDir {
pub struct ReadDirSpec {
/// The directory to read.
pub path: PathBuf,
/// Depth of the directory to read relative to root of walk.
pub depth: usize,
/// Location where
/// [`process_entries`](struct.WalkDir.html#method.process_entries) callback
/// function can store walk state. This is a placeholder right now. One
Expand Down Expand Up @@ -66,14 +64,9 @@ impl IntoIterator for ReadDir {
}

impl ReadDirSpec {
pub fn new<P: Into<PathBuf>>(
path: P,
depth: usize,
state: Option<Box<Any + Send + Sync>>,
) -> ReadDirSpec {
pub fn new<P: Into<PathBuf>>(path: P, state: Option<Box<Any + Send + Sync>>) -> ReadDirSpec {
ReadDirSpec {
path: path.into(),
depth,
state,
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,15 @@ impl IntoIterator for WalkDir {
let num_threads = self.options.num_threads;
let skip_hidden = self.options.skip_hidden;
let max_depth = self.options.max_depth;
let limit_depth = max_depth != ::std::usize::MAX;
let root_depth = self.root.iter().count();
let preload_metadata = self.options.preload_metadata;
let process_entries = self.options.process_entries.clone();

core::walk(&self.root, num_threads, move |read_dir_spec| {
let depth = read_dir_spec.depth + 1;
let at_depth_limit =
limit_depth && read_dir_spec.path.components().count() + 1 == (root_depth + max_depth);

let mut dir_entry_results: Vec<_> = fs::read_dir(&read_dir_spec.path)?
.filter_map(|dir_entry_result| {
let dir_entry = match dir_entry_result {
Expand All @@ -163,9 +167,9 @@ impl IntoIterator for WalkDir {

let content_spec = match file_type {
Ok(file_type) => {
if file_type.is_dir() && depth < max_depth {
if file_type.is_dir() && !at_depth_limit {
let path = read_dir_spec.path.join(dir_entry.file_name());
Some(Arc::new(ReadDirSpec::new(path, depth, None)))
Some(Arc::new(ReadDirSpec::new(path, None)))
} else {
None
}
Expand All @@ -174,7 +178,6 @@ impl IntoIterator for WalkDir {
};

Some(Ok(DirEntry::new(
depth,
file_name,
file_type,
metadata,
Expand Down
74 changes: 32 additions & 42 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,34 @@ fn local_paths(walk_dir: WalkDir) -> Vec<String> {
let each_entry = each_result.unwrap();
let path = each_entry.path().to_path_buf();
let path = path.strip_prefix(&test_dir).unwrap().to_path_buf();
let mut path_string = path.to_str().unwrap().to_string();
path_string.push_str(&format!(" ({})", each_entry.depth));
path_string
path.to_str().unwrap().to_string()
})
.collect()
}

#[test]
fn walk() {
let paths = local_paths(WalkDir::new(test_dir()));
assert!(paths.contains(&"b.txt (1)".to_string()));
assert!(paths.contains(&"group 1 (1)".to_string()));
assert!(paths.contains(&"group 1/d.txt (2)".to_string()));
assert!(paths.contains(&"b.txt".to_string()));
assert!(paths.contains(&"group 1".to_string()));
assert!(paths.contains(&"group 1/d.txt".to_string()));
}

#[test]
fn sort_by_name_single_thread() {
let paths = local_paths(WalkDir::new(test_dir()).num_threads(1).sort(true));
println!("JESSE {:?}", paths);
assert!(
paths
== vec![
" (0)",
"a.txt (1)",
"b.txt (1)",
"c.txt (1)",
"group 1 (1)",
"group 1/d.txt (2)",
"group 2 (1)",
"group 2/e.txt (2)",
"",
"a.txt",
"b.txt",
"c.txt",
"group 1",
"group 1/d.txt",
"group 2",
"group 2/e.txt",
]
);
}
Expand All @@ -52,14 +51,14 @@ fn sort_by_name_rayon_pool_global() {
assert!(
paths
== vec![
" (0)",
"a.txt (1)",
"b.txt (1)",
"c.txt (1)",
"group 1 (1)",
"group 1/d.txt (2)",
"group 2 (1)",
"group 2/e.txt (2)",
"",
"a.txt",
"b.txt",
"c.txt",
"group 1",
"group 1/d.txt",
"group 2",
"group 2/e.txt",
]
);
}
Expand All @@ -70,38 +69,29 @@ fn sort_by_name_rayon_pool_2_threads() {
assert!(
paths
== vec![
" (0)",
"a.txt (1)",
"b.txt (1)",
"c.txt (1)",
"group 1 (1)",
"group 1/d.txt (2)",
"group 2 (1)",
"group 2/e.txt (2)",
"",
"a.txt",
"b.txt",
"c.txt",
"group 1",
"group 1/d.txt",
"group 2",
"group 2/e.txt",
]
);
}

#[test]
fn see_hidden_files() {
let paths = local_paths(WalkDir::new(test_dir()).skip_hidden(false).sort(true));
assert!(paths.contains(&"group 2/.hidden_file.txt (2)".to_string()));
assert!(paths.contains(&"group 2/.hidden_file.txt".to_string()));
}

#[test]
fn max_depth() {
let paths = local_paths(WalkDir::new(test_dir()).max_depth(1).sort(true));
assert!(
paths
== vec![
" (0)",
"a.txt (1)",
"b.txt (1)",
"c.txt (1)",
"group 1 (1)",
"group 2 (1)",
]
);
println!("{:?}", paths);
assert!(paths == vec!["", "a.txt", "b.txt", "c.txt", "group 1", "group 2",]);
}

#[test]
Expand Down

0 comments on commit c1bffdd

Please sign in to comment.