Skip to content

Do not stop ReadDirRecursive* on broken symlinks #30

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

Merged
merged 4 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 1 addition & 3 deletions readdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ func (p *Path) ReadDirRecursiveFiltered(recursionFilter ReadDirFilter, filters .
}

if recursionFilter == nil || recursionFilter(path) {
if isDir, err := path.IsDirCheck(); err != nil {
return nil, err
} else if isDir {
if path.IsDir() {
subPaths, err := search(path)
if err != nil {
return nil, err
Expand Down
26 changes: 15 additions & 11 deletions readdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,21 +250,11 @@ func TestReadDirRecursiveFiltered(t *testing.T) {
func TestReadDirRecursiveLoopDetection(t *testing.T) {
loopsPath := New("testdata", "loops")
unbuondedReaddir := func(testdir string) (PathList, error) {
// This is required to unbound the recursion, otherwise it will stop
// when the paths becomes too long due to the symlink loop: this is not
// what we want, we are looking for an early detection of the loop.
skipBrokenLinks := func(p *Path) bool {
_, err := p.Stat()
return err == nil
}

var files PathList
var err error
done := make(chan bool)
go func() {
files, err = loopsPath.Join(testdir).ReadDirRecursiveFiltered(
skipBrokenLinks,
)
files, err = loopsPath.Join(testdir).ReadDirRecursive()
done <- true
}()
require.Eventually(
Expand Down Expand Up @@ -313,4 +303,18 @@ func TestReadDirRecursiveLoopDetection(t *testing.T) {
pathEqualsTo(t, "testdata/loops/regular_2/dir2/dir1/file1", l[4])
pathEqualsTo(t, "testdata/loops/regular_2/dir2/file2", l[5])
}

{
l, err := unbuondedReaddir("regular_3")
require.NoError(t, err)
require.Len(t, l, 7)
l.Sort()
pathEqualsTo(t, "testdata/loops/regular_3/dir1", l[0])
pathEqualsTo(t, "testdata/loops/regular_3/dir1/file1", l[1])
pathEqualsTo(t, "testdata/loops/regular_3/dir2", l[2])
pathEqualsTo(t, "testdata/loops/regular_3/dir2/dir1", l[3])
pathEqualsTo(t, "testdata/loops/regular_3/dir2/dir1/file1", l[4])
pathEqualsTo(t, "testdata/loops/regular_3/dir2/file2", l[5])
pathEqualsTo(t, "testdata/loops/regular_3/link", l[6]) // broken symlink is reported in files
}
}
Empty file.
1 change: 1 addition & 0 deletions testdata/loops/regular_3/dir2/dir1
Empty file.
1 change: 1 addition & 0 deletions testdata/loops/regular_3/link