From c30e74666640e2116879e2784afee9d9d883252d Mon Sep 17 00:00:00 2001 From: Jan Mercl Date: Wed, 13 Jun 2012 16:24:35 -0400 Subject: [PATCH] [release-branch.go1] path/filepath: implement documented SkipDir behavior MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ««« backport 4ef88bab4b0d 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 »»» --- src/pkg/path/filepath/path.go | 7 +++++-- src/pkg/path/filepath/path_test.go | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/pkg/path/filepath/path.go b/src/pkg/path/filepath/path.go index a4e429baec1..815021bd040 100644 --- a/src/pkg/path/filepath/path.go +++ b/src/pkg/path/filepath/path.go @@ -320,8 +320,11 @@ func walk(path string, info os.FileInfo, walkFn WalkFunc) error { } for _, fileInfo := range list { - if err = walk(Join(path, fileInfo.Name()), fileInfo, walkFn); err != nil { - return err + err = walk(Join(path, fileInfo.Name()), fileInfo, walkFn) + if err != nil { + if !fileInfo.IsDir() || err != SkipDir { + return err + } } } return nil diff --git a/src/pkg/path/filepath/path_test.go b/src/pkg/path/filepath/path_test.go index b8766588cf5..59e79350b7e 100644 --- a/src/pkg/path/filepath/path_test.go +++ b/src/pkg/path/filepath/path_test.go @@ -869,3 +869,26 @@ func TestDriveLetterInEvalSymlinks(t *testing.T) { t.Errorf("Results of EvalSymlinks do not match: %q and %q", flp, fup) } } + +func TestBug3486(t *testing.T) { // http://code.google.com/p/go/issues/detail?id=3486 + root := os.Getenv("GOROOT") + lib := filepath.Join(root, "lib") + src := filepath.Join(root, "src") + seenSrc := false + filepath.Walk(root, func(pth string, info os.FileInfo, err error) error { + if err != nil { + t.Fatal(err) + } + + switch pth { + case lib: + return filepath.SkipDir + case src: + seenSrc = true + } + return nil + }) + if !seenSrc { + t.Fatalf("%q not seen", src) + } +}