]> Cypherpunks.ru repositories - gostls13.git/commitdiff
path/filepath: document and test behavior of SkipDir on files
authorRuss Cox <rsc@golang.org>
Mon, 29 Jun 2015 15:53:51 +0000 (11:53 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 29 Jun 2015 21:16:35 +0000 (21:16 +0000)
This behavior is not what we might have designed from the start,
but it has been present since Go 1. Rather than make a visible
behavioral change that might cause programs to work differently
in Go ≤1.4 vs Go ≥1.5, document what SkipDir on a non-directory
has always meant. If code doesn't want this meaning, it is easy
enough not to return SkipDir on non-directories.

Fixes #10533.

Change-Id: Ic0612f032044bc7c69bf62583a02037e4b47530b
Reviewed-on: https://go-review.googlesource.com/11690
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
src/path/filepath/path.go
src/path/filepath/path_test.go

index f9b041bd463a59df639a435a80f501674cab6446..5dc5cfd49e734eb7c79a82fc219f41db7bbbb0a1 100644 (file)
@@ -335,10 +335,11 @@ var SkipDir = errors.New("skip this directory")
 // If there was a problem walking to the file or directory named by path, the
 // incoming error will describe the problem and the function can decide how
 // to handle that error (and Walk will not descend into that directory). If
-// an error is returned, processing stops. The sole exception is that if path
-// is a directory and the function returns the special value SkipDir, the
-// contents of the directory are skipped and processing continues as usual on
-// the next file.
+// an error is returned, processing stops. The sole exception is when the function
+// returns the special value SkipDir. If the function returns SkipDir when invoked
+// on a directory, Walk skips the directory's contents entirely.
+// If the function returns SkipDir when invoked on a non-directory file,
+// Walk skips the remaining files in the containing directory.
 type WalkFunc func(path string, info os.FileInfo, err error) error
 
 var lstat = os.Lstat // for testing
index 4ecaada983891979d56227b6438e4c75a3677843..91b6493c51b1423ad6654893a049972cf5e4ac89 100644 (file)
@@ -510,6 +510,35 @@ func touch(t *testing.T, name string) {
        }
 }
 
+func TestWalkSkipDirOnFile(t *testing.T) {
+       td, err := ioutil.TempDir("", "walktest")
+       if err != nil {
+               t.Fatal(err)
+       }
+       defer os.RemoveAll(td)
+
+       if err := os.MkdirAll(filepath.Join(td, "dir"), 0755); err != nil {
+               t.Fatal(err)
+       }
+       touch(t, filepath.Join(td, "dir/foo1"))
+       touch(t, filepath.Join(td, "dir/foo2"))
+
+       sawFoo2 := false
+       filepath.Walk(td, func(path string, info os.FileInfo, err error) error {
+               if strings.HasSuffix(path, "foo2") {
+                       sawFoo2 = true
+               }
+               if strings.HasSuffix(path, "foo1") {
+                       return filepath.SkipDir
+               }
+               return nil
+       })
+
+       if sawFoo2 {
+               t.Errorf("SkipDir on file foo1 did not block processing of foo2")
+       }
+}
+
 func TestWalkFileError(t *testing.T) {
        td, err := ioutil.TempDir("", "walktest")
        if err != nil {