BurntSushi / walkdir

Rust library for walking directories recursively.
The Unlicense
1.21k stars 106 forks source link

Fix: filter_entry misbehaves when contents_first is enabled #198

Open kikuomax opened 2 months ago

kikuomax commented 2 months ago

Proposed changes

Details

The documentation of IntoIter::filter_entry states that:

Note that if the iterator has contents_first enabled, then this method is no different than calling the standard Iterator::filter method (because directory entries are yielded after they’ve been descended into).

However, if contents_first was enabled and some directories were filtered out, unexpected entries may have been skipped regardless of the filter_entry results. Please refer to #171 for an example.

FilterEntry::next used to call skip_current_dir when the filtered out entry was a directory to stop reading the filtered out directory. skip_current_dir realizes this by popping the last DirList from stack_list. Unfortunately, if contents_first was enabled, skip_current_dir popped a wrong DirList from stack_list, because the DirList of the filtered out directory had already been removed from stack_list.

We should not update stack_list if contents_first is enabled. So I introduced skip_current_dir_unless_contents_first which does nothing if contents_first is enabled, otherwise calls skip_current_dir.