Commit 157fc454 authored by Ian Lance Taylor's avatar Ian Lance Taylor Committed by Brad Fitzpatrick

path/filepath: don't return SkipDir at top

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: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 307de654
......@@ -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
......
......@@ -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)
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment