Skip to content

Commit 1700f35

Browse files
committed
libglob: only return dirs for globs ending in /
`foo.txt/` should not return `foo.txt` if `foo.txt` is in fact a text file and not a directory.
1 parent 4051bd9 commit 1700f35

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/libglob/lib.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use std::path::is_sep;
4343
pub struct Paths {
4444
root: Path,
4545
dir_patterns: Vec<Pattern>,
46+
require_dir: bool,
4647
options: MatchOptions,
4748
todo: Vec<(Path,uint)>,
4849
}
@@ -106,6 +107,7 @@ pub fn glob_with(pattern: &str, options: MatchOptions) -> Paths {
106107
return Paths {
107108
root: root,
108109
dir_patterns: Vec::new(),
110+
require_dir: false,
109111
options: options,
110112
todo: Vec::new(),
111113
};
@@ -118,6 +120,7 @@ pub fn glob_with(pattern: &str, options: MatchOptions) -> Paths {
118120
.split_terminator(is_sep)
119121
.map(|s| Pattern::new(s))
120122
.collect::<Vec<Pattern>>();
123+
let require_dir = pattern.chars().next_back().map(is_sep) == Some(true);
121124

122125
let mut todo = Vec::new();
123126
if dir_patterns.len() > 0 {
@@ -130,6 +133,7 @@ pub fn glob_with(pattern: &str, options: MatchOptions) -> Paths {
130133
Paths {
131134
root: root,
132135
dir_patterns: dir_patterns,
136+
require_dir: require_dir,
133137
options: options,
134138
todo: todo,
135139
}
@@ -146,7 +150,10 @@ impl Iterator<Path> for Paths {
146150
let (path,idx) = self.todo.pop().unwrap();
147151
// idx -1: was already checked by fill_todo, maybe path was '.' or
148152
// '..' that we can't match here because of normalization.
149-
if idx == -1 as uint { return Some(path); }
153+
if idx == -1 as uint {
154+
if self.require_dir && !path.is_dir() { continue; }
155+
return Some(path);
156+
}
150157
let ref pattern = *self.dir_patterns.get(idx);
151158

152159
if pattern.matches_with(match path.filename_str() {
@@ -161,7 +168,10 @@ impl Iterator<Path> for Paths {
161168
if idx == self.dir_patterns.len() - 1 {
162169
// it is not possible for a pattern to match a directory *AND* its children
163170
// so we don't need to check the children
164-
return Some(path);
171+
172+
if !self.require_dir || path.is_dir() {
173+
return Some(path);
174+
}
165175
} else {
166176
fill_todo(&mut self.todo, self.dir_patterns.as_slice(),
167177
idx + 1, &path, self.options);

src/test/run-pass/glob-std.rs

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ pub fn main() {
138138
assert_eq!(glob_vec("nonexistent/../bbb"), Vec::new());
139139
assert_eq!(glob_vec("aaa/tomato/tomato.txt/.."), Vec::new());
140140

141+
assert_eq!(glob_vec("aaa/tomato/tomato.txt/"), Vec::new());
142+
141143
assert_eq!(glob_vec("aa[a]"), vec!(abs_path("aaa")));
142144
assert_eq!(glob_vec("aa[abc]"), vec!(abs_path("aaa")));
143145
assert_eq!(glob_vec("a[bca]a"), vec!(abs_path("aaa")));

0 commit comments

Comments
 (0)