]> Cypherpunks.ru repositories - gostls13.git/commitdiff
path/filepath: don't return SkipDir at top
authorIan Lance Taylor <iant@golang.org>
Thu, 7 Jul 2016 00:11:29 +0000 (17:11 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 25 Aug 2016 17:18:43 +0000 (17:18 +0000)
If the walker function called on a top-level file returns SkipDir,
then (before this change) Walk would return SkipDir, which the
documentation implies will not happen.

Fixes #16280.

Change-Id: I37d63bdcef7af4b56e342b624cf0d4b42e65c297
Reviewed-on: https://go-review.googlesource.com/24780
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/path/filepath/path.go
src/path/filepath/path_test.go

index 0dc559cdd67cf8128b170ceb7564a8cccd357df7..3c70cd8be68ad7be30441ccc0fcd407d509e70ad 100644 (file)
@@ -393,9 +393,14 @@ func walk(path string, info os.FileInfo, walkFn WalkFunc) error {
 func Walk(root string, walkFn WalkFunc) error {
        info, err := os.Lstat(root)
        if err != nil {
-               return walkFn(root, nil, err)
+               err = walkFn(root, nil, err)
+       } else {
+               err = walk(root, info, walkFn)
        }
-       return walk(root, info, walkFn)
+       if err == SkipDir {
+               return nil
+       }
+       return err
 }
 
 // readDirNames reads the directory named by dirname and returns
index 1be5b469f2a9d4f45e139dcb537382dcede47abd..0c495a5f1c07255d46e6ad7b5dfea256142ace96 100644 (file)
@@ -528,7 +528,7 @@ func TestWalkSkipDirOnFile(t *testing.T) {
        touch(t, filepath.Join(td, "dir/foo2"))
 
        sawFoo2 := false
-       filepath.Walk(td, func(path string, info os.FileInfo, err error) error {
+       walker := func(path string, info os.FileInfo, err error) error {
                if strings.HasSuffix(path, "foo2") {
                        sawFoo2 = true
                }
@@ -536,8 +536,20 @@ func TestWalkSkipDirOnFile(t *testing.T) {
                        return filepath.SkipDir
                }
                return nil
-       })
+       }
 
+       err = filepath.Walk(td, walker)
+       if err != nil {
+               t.Fatal(err)
+       }
+       if sawFoo2 {
+               t.Errorf("SkipDir on file foo1 did not block processing of foo2")
+       }
+
+       err = filepath.Walk(filepath.Join(td, "dir"), walker)
+       if err != nil {
+               t.Fatal(err)
+       }
        if sawFoo2 {
                t.Errorf("SkipDir on file foo1 did not block processing of foo2")
        }
@@ -1203,7 +1215,7 @@ func TestBug3486(t *testing.T) { // https://golang.org/issue/3486
        ken := filepath.Join(root, "ken")
        seenBugs := false
        seenKen := false
-       filepath.Walk(root, func(pth string, info os.FileInfo, err error) error {
+       err = filepath.Walk(root, func(pth string, info os.FileInfo, err error) error {
                if err != nil {
                        t.Fatal(err)
                }
@@ -1220,6 +1232,9 @@ func TestBug3486(t *testing.T) { // https://golang.org/issue/3486
                }
                return nil
        })
+       if err != nil {
+               t.Fatal(err)
+       }
        if !seenKen {
                t.Fatalf("%q not seen", ken)
        }