Skip to content

Commit 2b57a87

Browse files
bflmrsc
authored andcommitted
path/filepath: implement documented SkipDir behavior
Currently walk() doesn't check for err == SkipDir when iterating a directory list, but such promise is made in the docs for WalkFunc. Fixes #3486. R=rsc, r CC=golang-dev https://golang.org/cl/6257059
1 parent d87bc2f commit 2b57a87

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/pkg/path/filepath/path.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,11 @@ func walk(path string, info os.FileInfo, walkFn WalkFunc) error {
320320
}
321321

322322
for _, fileInfo := range list {
323-
if err = walk(Join(path, fileInfo.Name()), fileInfo, walkFn); err != nil {
324-
return err
323+
err = walk(Join(path, fileInfo.Name()), fileInfo, walkFn)
324+
if err != nil {
325+
if !fileInfo.IsDir() || err != SkipDir {
326+
return err
327+
}
325328
}
326329
}
327330
return nil

src/pkg/path/filepath/path_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -874,3 +874,26 @@ func TestDriveLetterInEvalSymlinks(t *testing.T) {
874874
t.Errorf("Results of EvalSymlinks do not match: %q and %q", flp, fup)
875875
}
876876
}
877+
878+
func TestBug3486(t *testing.T) { // http://code.google.com/p/go/issues/detail?id=3486
879+
root := os.Getenv("GOROOT")
880+
lib := filepath.Join(root, "lib")
881+
src := filepath.Join(root, "src")
882+
seenSrc := false
883+
filepath.Walk(root, func(pth string, info os.FileInfo, err error) error {
884+
if err != nil {
885+
t.Fatal(err)
886+
}
887+
888+
switch pth {
889+
case lib:
890+
return filepath.SkipDir
891+
case src:
892+
seenSrc = true
893+
}
894+
return nil
895+
})
896+
if !seenSrc {
897+
t.Fatalf("%q not seen", src)
898+
}
899+
}

0 commit comments

Comments
 (0)