Skip to content

Commit

Permalink
Merge pull request rust-lang-deprecated#15 from BurntSushi/fix-root-s…
Browse files Browse the repository at this point in the history
…ymlink

Fix bug reading root symlink.
  • Loading branch information
BurntSushi committed Dec 6, 2016
2 parents a36d60f + 3732789 commit 658f414
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
17 changes: 3 additions & 14 deletions src/lib.rs
Expand Up @@ -198,7 +198,8 @@ impl WalkDir {
/// Create a builder for a recursive directory iterator starting at the
/// file path `root`. If `root` is a directory, then it is the first item
/// yielded by the iterator. If `root` is a file, then it is the first
/// and only item yielded by the iterator.
/// and only item yielded by the iterator. If `root` is a symlink, then it
/// is always followed.
pub fn new<P: AsRef<Path>>(root: P) -> Self {
WalkDir {
opts: WalkDirOptions {
Expand Down Expand Up @@ -495,7 +496,7 @@ impl Iterator for Iter {

fn next(&mut self) -> Option<Result<DirEntry>> {
if let Some(start) = self.start.take() {
let dent = itry!(DirEntry::from_path(0, start));
let dent = itry!(DirEntry::from_link(0, start));
if let Some(result) = self.handle_entry(dent) {
return Some(result);
}
Expand Down Expand Up @@ -745,18 +746,6 @@ impl DirEntry {
depth: depth,
})
}

fn from_path(depth: usize, pb: PathBuf) -> Result<DirEntry> {
let md = try!(fs::symlink_metadata(&pb).map_err(|err| {
Error::from_path(depth, pb.clone(), err)
}));
Ok(DirEntry {
path: pb,
ty: md.file_type(),
follow_link: false,
depth: depth,
})
}
}

impl Clone for DirEntry {
Expand Down
27 changes: 27 additions & 0 deletions src/tests.rs
Expand Up @@ -416,6 +416,33 @@ fn walk_dir_sym_2() {
assert_tree_eq!(exp, got);
}

#[test]
#[cfg(unix)]
fn walk_dir_root_sym() {
let exp = td("foo", vec![
td("bar", vec![tf("a"), tf("b")]),
tl("bar", "alink"),
]);
let tmp = tmpdir();
let tmp_path = tmp.path();
let tmp_len = tmp_path.to_str().unwrap().len();
exp.create_in(tmp_path).unwrap();

let it = WalkDir::new(tmp_path.join("foo/alink")).into_iter();
let mut got = it
.map(|d| d.unwrap().path().to_str().unwrap()[tmp_len+1..].into())
.collect::<Vec<String>>();
got.sort();
assert_eq!(got, vec!["foo/alink", "foo/alink/a", "foo/alink/b"]);

let it = WalkDir::new(tmp_path.join("foo/alink/")).into_iter();
let mut got = it
.map(|d| d.unwrap().path().to_str().unwrap()[tmp_len+1..].into())
.collect::<Vec<String>>();
got.sort();
assert_eq!(got, vec!["foo/alink/", "foo/alink/a", "foo/alink/b"]);
}

#[test]
#[cfg(unix)]
fn walk_dir_sym_detect_no_follow_no_loop() {
Expand Down

0 comments on commit 658f414

Please sign in to comment.