Skip to content

Commit

Permalink
Include valid symlinks with both --follow and --type symlink
Browse files Browse the repository at this point in the history
This changes it so that if you specify `--type symlink` then it will
check if each entry is a symlink even if we would otherwise follow it.

Fixes: sharkdp#939
  • Loading branch information
tmccombs committed Jan 10, 2022
1 parent 125cb81 commit 414e812
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
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)
|| (!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

0 comments on commit 414e812

Please sign in to comment.