Skip to content

Commit 816f712

Browse files
committed
Ensure directory wildcards avoid reading files.
Previously a wildcard like */* run on a tree like - foo - bar/ - baz would return an `Err` for `foo`, since it was trying to read it as a directory. Fixes #29.
1 parent 42f80f7 commit 816f712

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ fn fill_todo(todo: &mut Vec<Result<(Path, usize), GlobError>>, patterns: &[Patte
706706
};
707707

708708
let pattern = &patterns[idx];
709-
709+
let is_dir = path.is_dir();
710710
match pattern_as_str(pattern) {
711711
Some(s) => {
712712
// This pattern component doesn't have any metacharacters, so we
@@ -716,11 +716,11 @@ fn fill_todo(todo: &mut Vec<Result<(Path, usize), GlobError>>, patterns: &[Patte
716716
// right away.
717717
let special = "." == s.as_slice() || ".." == s.as_slice();
718718
let next_path = path.join(s.as_slice());
719-
if (special && path.is_dir()) || (!special && next_path.exists()) {
719+
if (special && is_dir) || (!special && next_path.exists()) {
720720
add(todo, next_path);
721721
}
722722
},
723-
None => {
723+
None if is_dir => {
724724
match fs::readdir(path) {
725725
Ok(mut children) => {
726726
children.sort_by(|p1, p2| p2.filename().cmp(&p1.filename()));
@@ -743,6 +743,7 @@ fn fill_todo(todo: &mut Vec<Result<(Path, usize), GlobError>>, patterns: &[Patte
743743
},
744744
}
745745
}
746+
None => {/* not a directory, nothing more to find */}
746747
}
747748
}
748749

tests/glob-std.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn main() {
3939
}
4040

4141
fn glob_vec(pattern: &str) -> Vec<Path> {
42-
glob(pattern).unwrap().filter_map(|r| r.ok()).collect()
42+
glob(pattern).unwrap().map(|r| r.unwrap()).collect()
4343
}
4444

4545
let root = TempDir::new("glob-tests");

0 commit comments

Comments
 (0)