aboutsummaryrefslogtreecommitdiff
path: root/src/path
diff options
context:
space:
mode:
authorPaschalis Tsilias <paschalis.tsilias@gmail.com>2021-11-13 02:18:30 +0200
committerGopher Robot <gobot@golang.org>2022-08-25 18:50:37 +0000
commit95a786da1265d84290c1a0d1186352f71475ff9f (patch)
treec5dd8a434728b36da18f6c552af916eed331997d /src/path
parent6a801d3082c6ab28372c115d13ef0c238e3535ae (diff)
downloadgo-95a786da1265d84290c1a0d1186352f71475ff9f.tar.xz
path/filepath, io/fs: add SkipAll
Fixes #47209 Change-Id: If75b0dd38f2c30a23517205d80c7a6683a5c921c Reviewed-on: https://go-review.googlesource.com/c/go/+/363814 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Bryan Mills <bcmills@google.com>
Diffstat (limited to 'src/path')
-rw-r--r--src/path/filepath/path.go16
-rw-r--r--src/path/filepath/path_test.go58
2 files changed, 69 insertions, 5 deletions
diff --git a/src/path/filepath/path.go b/src/path/filepath/path.go
index de7a2c758b..c86b0c0ff8 100644
--- a/src/path/filepath/path.go
+++ b/src/path/filepath/path.go
@@ -352,6 +352,11 @@ func Rel(basepath, targpath string) (string, error) {
// as an error by any function.
var SkipDir error = fs.SkipDir
+// SkipAll is used as a return value from WalkFuncs to indicate that
+// all remaining files and directories are to be skipped. It is not returned
+// as an error by any function.
+var SkipAll error = fs.SkipAll
+
// WalkFunc is the type of the function called by Walk to visit each
// file or directory.
//
@@ -370,8 +375,9 @@ var SkipDir error = fs.SkipDir
// The error result returned by the function controls how Walk continues.
// If the function returns the special value SkipDir, Walk skips the
// current directory (path if info.IsDir() is true, otherwise path's
-// parent directory). Otherwise, if the function returns a non-nil error,
-// Walk stops entirely and returns that error.
+// parent directory). If the function returns the special value SkipAll,
+// Walk skips all remaining files and directories. Otherwise, if the function
+// returns a non-nil error, Walk stops entirely and returns that error.
//
// The err argument reports an error related to path, signaling that Walk
// will not walk into that directory. The function can decide how to
@@ -441,7 +447,7 @@ func walk(path string, info fs.FileInfo, walkFn WalkFunc) error {
if err != nil || err1 != nil {
// The caller's behavior is controlled by the return value, which is decided
// by walkFn. walkFn may ignore err and return nil.
- // If walkFn returns SkipDir, it will be handled by the caller.
+ // If walkFn returns SkipDir or SkipAll, it will be handled by the caller.
// So walk should return whatever walkFn returns.
return err1
}
@@ -483,7 +489,7 @@ func WalkDir(root string, fn fs.WalkDirFunc) error {
} else {
err = walkDir(root, &statDirEntry{info}, fn)
}
- if err == SkipDir {
+ if err == SkipDir || err == SkipAll {
return nil
}
return err
@@ -519,7 +525,7 @@ func Walk(root string, fn WalkFunc) error {
} else {
err = walk(root, info, fn)
}
- if err == SkipDir {
+ if err == SkipDir || err == SkipAll {
return nil
}
return err
diff --git a/src/path/filepath/path_test.go b/src/path/filepath/path_test.go
index a783d6be28..9bdc58ea35 100644
--- a/src/path/filepath/path_test.go
+++ b/src/path/filepath/path_test.go
@@ -638,6 +638,64 @@ func TestWalkSkipDirOnFile(t *testing.T) {
})
}
+func TestWalkSkipAllOnFile(t *testing.T) {
+ td := t.TempDir()
+
+ if err := os.MkdirAll(filepath.Join(td, "dir", "subdir"), 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.MkdirAll(filepath.Join(td, "dir2"), 0755); err != nil {
+ t.Fatal(err)
+ }
+
+ touch(t, filepath.Join(td, "dir", "foo1"))
+ touch(t, filepath.Join(td, "dir", "foo2"))
+ touch(t, filepath.Join(td, "dir", "subdir", "foo3"))
+ touch(t, filepath.Join(td, "dir", "foo4"))
+ touch(t, filepath.Join(td, "dir2", "bar"))
+ touch(t, filepath.Join(td, "last"))
+
+ remainingWereSkipped := true
+ walker := func(path string) error {
+ if strings.HasSuffix(path, "foo2") {
+ return filepath.SkipAll
+ }
+
+ if strings.HasSuffix(path, "foo3") ||
+ strings.HasSuffix(path, "foo4") ||
+ strings.HasSuffix(path, "bar") ||
+ strings.HasSuffix(path, "last") {
+ remainingWereSkipped = false
+ }
+ return nil
+ }
+
+ walkFn := func(path string, _ fs.FileInfo, _ error) error { return walker(path) }
+ walkDirFn := func(path string, _ fs.DirEntry, _ error) error { return walker(path) }
+
+ check := func(t *testing.T, walk func(root string) error, root string) {
+ t.Helper()
+ remainingWereSkipped = true
+ if err := walk(root); err != nil {
+ t.Fatal(err)
+ }
+ if !remainingWereSkipped {
+ t.Errorf("SkipAll on file foo2 did not block processing of remaining files and directories")
+ }
+ }
+
+ t.Run("Walk", func(t *testing.T) {
+ Walk := func(_ string) error { return filepath.Walk(td, walkFn) }
+ check(t, Walk, td)
+ check(t, Walk, filepath.Join(td, "dir"))
+ })
+ t.Run("WalkDir", func(t *testing.T) {
+ WalkDir := func(_ string) error { return filepath.WalkDir(td, walkDirFn) }
+ check(t, WalkDir, td)
+ check(t, WalkDir, filepath.Join(td, "dir"))
+ })
+}
+
func TestWalkFileError(t *testing.T) {
td := t.TempDir()