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

Fix the infinite recursion with afero.Walk #409

Open
wants to merge 2 commits 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
2 changes: 1 addition & 1 deletion afero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
Fss = []Fs{&MemMapFs{}, &OsFs{}}
)

var testRegistry map[Fs][]string = make(map[Fs][]string)
var testRegistry = make(map[Fs][]string)

func testDir(fs Fs) string {
name, err := TempDir(fs, "", "afero")
Expand Down
4 changes: 4 additions & 0 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func walk(fs Fs, path string, info os.FileInfo, walkFn filepath.WalkFunc) error
}

for _, name := range names {
if name == "" {
// skip empty names to avoid infinite recursion
continue
}
filename := filepath.Join(path, name)
fileInfo, err := lstatIfPossible(fs, filename)
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,27 @@ func TestWalk(t *testing.T) {
t.Fail()
}
}

func TestWalkRemoveRoot(t *testing.T) {
defer removeAllTestFiles(t)
fs := Fss[0]
rootPath := "."

err := fs.RemoveAll(rootPath)
if err != nil {
t.Error(err)
}

testRegistry[fs] = append(testRegistry[fs], rootPath)
setupTestFiles(t, fs, rootPath)

walkFn := func(path string, info os.FileInfo, err error) error {
fmt.Println(path, info.Name(), info.IsDir(), info.Size(), err)
return err
}

err = Walk(fs, rootPath, walkFn)
if err != nil {
t.Error(err)
}
}