aboutsummaryrefslogtreecommitdiff
path: root/src/path
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2022-03-12 16:18:23 -0800
committerIan Lance Taylor <iant@golang.org>2022-03-28 22:03:07 +0000
commit460fd63cccd2f1d16fc4b1b761545b1649e14e28 (patch)
treed578069779c03ac6c2dc2ae2b427ce0d62014db6 /src/path
parent3c7f9b80222376fa01f8c6e3b411281c23dd74ed (diff)
downloadgo-460fd63cccd2f1d16fc4b1b761545b1649e14e28.tar.xz
io/fs, path/filepath: honor SkipDir on second WalkDirFunc error call
Fixes #51617 Change-Id: I03e9e575d9bad1481e7e4f051b50a077ba5f2fe0 Reviewed-on: https://go-review.googlesource.com/c/go/+/392154 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Diffstat (limited to 'src/path')
-rw-r--r--src/path/filepath/path.go3
-rw-r--r--src/path/filepath/path_test.go35
2 files changed, 38 insertions, 0 deletions
diff --git a/src/path/filepath/path.go b/src/path/filepath/path.go
index b56534dead..0554deb2ff 100644
--- a/src/path/filepath/path.go
+++ b/src/path/filepath/path.go
@@ -396,6 +396,9 @@ func walkDir(path string, d fs.DirEntry, walkDirFn fs.WalkDirFunc) error {
// Second call, to report ReadDir error.
err = walkDirFn(path, d, err)
if err != nil {
+ if err == SkipDir && d.IsDir() {
+ err = nil
+ }
return err
}
}
diff --git a/src/path/filepath/path_test.go b/src/path/filepath/path_test.go
index cfd0c8244d..1456ea737a 100644
--- a/src/path/filepath/path_test.go
+++ b/src/path/filepath/path_test.go
@@ -1526,3 +1526,38 @@ func TestEvalSymlinksAboveRootChdir(t *testing.T) {
t.Logf("EvalSymlinks(%q) = %q", check, resolved)
}
}
+
+func TestIssue51617(t *testing.T) {
+ dir := t.TempDir()
+ for _, sub := range []string{"a", filepath.Join("a", "bad"), filepath.Join("a", "next")} {
+ if err := os.Mkdir(filepath.Join(dir, sub), 0755); err != nil {
+ t.Fatal(err)
+ }
+ }
+ bad := filepath.Join(dir, "a", "bad")
+ if err := os.Chmod(bad, 0); err != nil {
+ t.Fatal(err)
+ }
+ defer os.Chmod(bad, 0700) // avoid errors on cleanup
+ var saw []string
+ err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
+ if err != nil {
+ return filepath.SkipDir
+ }
+ if d.IsDir() {
+ rel, err := filepath.Rel(dir, path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ saw = append(saw, rel)
+ }
+ return nil
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ want := []string{".", "a", filepath.Join("a", "bad"), filepath.Join("a", "next")}
+ if !reflect.DeepEqual(saw, want) {
+ t.Errorf("got directories %v, want %v", saw, want)
+ }
+}