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

Include valid symlinks with both --follow and --type symlink #940

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 4 additions & 3 deletions src/filetypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ pub struct FileTypes {

impl FileTypes {
pub fn should_ignore(&self, entry: &walk::DirEntry) -> bool {
let is_symlink = entry.is_symlink();
if let Some(ref entry_type) = entry.file_type() {
(!self.files && entry_type.is_file())
|| (!self.directories && entry_type.is_dir())
|| (!self.symlinks && entry_type.is_symlink())
(!self.files && entry_type.is_file() && !is_symlink)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better for this (and the following line) to be something like:

Suggested change
(!self.files && entry_type.is_file() && !is_symlink)
(!self.files && entry_type.is_file() && !(self.symlinks && is_symlink))

(maybe factoring that out self.symlinks && is_symlink into a variable?)

so that if you specify only --type file it doesn't include symlinks to directories and vice versa.

|| (!self.directories && entry_type.is_dir() && !is_symlink)
|| (!self.symlinks && is_symlink)
|| (!self.sockets && filesystem::is_socket(*entry_type))
|| (!self.pipes && filesystem::is_pipe(*entry_type))
|| (self.executables_only
Expand Down
7 changes: 7 additions & 0 deletions src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,13 @@ impl DirEntry {
}
}

pub fn is_symlink(&self) -> bool {
match &self.inner {
DirEntryInner::Normal(e) => e.path_is_symlink(),
DirEntryInner::BrokenSymlink(_) => true,
}
}

pub fn metadata(&self) -> Option<&Metadata> {
self.metadata
.get_or_init(|| match &self.inner {
Expand Down
9 changes: 8 additions & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,11 +758,18 @@ fn test_follow_broken_symlink() {

te.assert_output(
&["--follow", "--type", "symlink", "symlink"],
"./broken_symlink",
"./broken_symlink\n./symlink",
);
te.assert_output(&["--follow", "--type", "file", "symlink"], "");
}

#[test]
fn test_follow_symlink() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);

te.assert_output(&["--type", "symlink", "--follow"], "./symlink");
}

/// Null separator (--print0)
#[test]
fn test_print0() {
Expand Down