Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for not following a root symlink #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ pub struct WalkDir {

struct WalkDirOptions {
follow_links: bool,
follow_root_link: bool,
max_open: usize,
min_depth: usize,
max_depth: usize,
Expand Down Expand Up @@ -287,6 +288,7 @@ impl WalkDir {
WalkDir {
opts: WalkDirOptions {
follow_links: false,
follow_root_link: true,
max_open: 10,
min_depth: 0,
max_depth: ::std::usize::MAX,
Expand Down Expand Up @@ -344,6 +346,17 @@ impl WalkDir {
self
}

/// Always follow the root if it is a symbolic link. By default, this is
/// enabled.
///
/// When `yes` is `true`, and the root path is a symbolic link, it will
/// always be followed as if [`follow_links`] were enabled, even if it that
/// is `false`.
pub fn follow_root_link(mut self, yes: bool) -> Self {
self.opts.follow_root_link = yes;
self
}

/// Set the maximum number of simultaneously open file descriptors used
/// by the iterator.
///
Expand Down Expand Up @@ -830,7 +843,10 @@ impl IntoIter {
} else {
itry!(self.push(&dent));
}
} else if dent.depth() == 0 && dent.file_type().is_symlink() {
} else if dent.depth() == 0
&& dent.file_type().is_symlink()
&& self.opts.follow_root_link
{
// As a special case, if we are processing a root entry, then we
// always follow it even if it's a symlink and follow_links is
// false. We are careful to not let this change the semantics of
Expand Down
32 changes: 32 additions & 0 deletions src/tests/recursive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,38 @@ fn sym_root_dir_follow() {
assert!(!link_zzz.path_is_symlink());
}

#[test]
fn sym_root_dir_nofollow_root() {
let dir = Dir::tmp();
dir.mkdirp("a");
dir.symlink_dir("a", "a-link");
dir.touch("a/zzz");

let wd = WalkDir::new(dir.join("a-link")).follow_root_link(false);
let r = dir.run_recursive(wd);
r.assert_no_errors();

let ents = r.sorted_ents();
assert_eq!(1, ents.len());
let link = &ents[0];

assert_eq!(dir.join("a-link"), link.path());

assert!(link.path_is_symlink());

assert_eq!(dir.join("a"), fs::read_link(link.path()).unwrap());

assert_eq!(0, link.depth());

assert!(link.file_type().is_symlink());
assert!(!link.file_type().is_file());
assert!(!link.file_type().is_dir());

assert!(link.metadata().unwrap().file_type().is_symlink());
assert!(!link.metadata().unwrap().is_file());
assert!(!link.metadata().unwrap().is_dir());
}

#[test]
fn sym_file_nofollow() {
let dir = Dir::tmp();
Expand Down