diff options
author | Robert Griesemer <gri@golang.org> | 2011-09-07 15:19:53 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2011-09-07 15:19:53 -0700 |
commit | 6961b9116c9f39712665b619616e068a4fa3a61c (patch) | |
tree | ed9326db5d2ec82f0b7d43d720c47ee4cc0701f6 /src/pkg/path | |
parent | 54dd28b5d22ce4ea1e0df35ac79c29998ad7b755 (diff) | |
download | go-6961b9116c9f39712665b619616e068a4fa3a61c.tar.gz |
undo CL 4964067 / 661cb84cc6f0
API change. Needs further reflection.
??? original CL description
path/filepath: Simplify Walk interface
The last argument of filepath.Walk was removed, and the Visitor
interface now contains an Error method that is called on errors.
Fixes issue 2237.
R=golang-dev, gri, r
CC=golang-dev
http://codereview.appspot.com/4964067
Committer: Robert Griesemer <gri@golang.org>
???
R=r
CC=golang-dev
http://codereview.appspot.com/4974065
Diffstat (limited to 'src/pkg/path')
-rw-r--r-- | src/pkg/path/filepath/path.go | 24 | ||||
-rw-r--r-- | src/pkg/path/filepath/path_test.go | 31 |
2 files changed, 31 insertions, 24 deletions
diff --git a/src/pkg/path/filepath/path.go b/src/pkg/path/filepath/path.go index d6a7d08e8..3d5b915c1 100644 --- a/src/pkg/path/filepath/path.go +++ b/src/pkg/path/filepath/path.go @@ -255,14 +255,14 @@ func Abs(path string) (string, os.Error) { } // Visitor methods are invoked for corresponding file tree entries -// visited by Walk. +// visited by Walk. The parameter path is the full path of f relative +// to root. type Visitor interface { VisitDir(path string, f *os.FileInfo) bool VisitFile(path string, f *os.FileInfo) - Error(path string, err os.Error) } -func walk(path string, f *os.FileInfo, v Visitor) { +func walk(path string, f *os.FileInfo, v Visitor, errors chan<- os.Error) { if !f.IsDirectory() { v.VisitFile(path, f) return @@ -274,11 +274,13 @@ func walk(path string, f *os.FileInfo, v Visitor) { list, err := readDir(path) if err != nil { - v.Error(path, err) + if errors != nil { + errors <- err + } } for _, e := range list { - walk(Join(path, e.Name), e, v) + walk(Join(path, e.Name), e, v, errors) } } @@ -314,14 +316,18 @@ func (f fileInfoList) Swap(i, j int) { f[i], f[j] = f[j], f[i] } // v.VisitFile for each directory or file in the tree, including root. // If v.VisitDir returns false, Walk skips the directory's entries; // otherwise it invokes itself for each directory entry in sorted order. -// Walk calls v.Error if an error happens while reading a directory. -func Walk(root string, v Visitor) { +// An error reading a directory does not abort the Walk. +// If errors != nil, Walk sends each directory read error +// to the channel. Otherwise Walk discards the error. +func Walk(root string, v Visitor, errors chan<- os.Error) { f, err := os.Lstat(root) if err != nil { - v.Error(root, err) + if errors != nil { + errors <- err + } return // can't progress } - walk(root, f, v) + walk(root, f, v, errors) } // Base returns the last element of path. diff --git a/src/pkg/path/filepath/path_test.go b/src/pkg/path/filepath/path_test.go index 8c566c700..395b12775 100644 --- a/src/pkg/path/filepath/path_test.go +++ b/src/pkg/path/filepath/path_test.go @@ -299,30 +299,30 @@ func mark(name string) { }) } -type TestVisitor chan os.Error +type TestVisitor struct{} -func (v TestVisitor) VisitDir(path string, f *os.FileInfo) bool { +func (v *TestVisitor) VisitDir(path string, f *os.FileInfo) bool { mark(f.Name) return true } -func (v TestVisitor) VisitFile(path string, f *os.FileInfo) { +func (v *TestVisitor) VisitFile(path string, f *os.FileInfo) { mark(f.Name) } -func (v TestVisitor) Error(path string, err os.Error) { - v <- err -} - func TestWalk(t *testing.T) { makeTree(t) - v := make(TestVisitor, 64) + // 1) ignore error handling, expect none + v := &TestVisitor{} + filepath.Walk(tree.name, v, nil) + checkMarks(t) - // 1) no errors expected. - filepath.Walk(tree.name, v) + // 2) handle errors, expect none + errors := make(chan os.Error, 64) + filepath.Walk(tree.name, v, errors) select { - case err := <-v: + case err := <-errors: t.Errorf("no error expected, found: %s", err) default: // ok @@ -343,13 +343,14 @@ func TestWalk(t *testing.T) { tree.entries[1].mark-- tree.entries[3].mark-- - // 2) expect two errors + // 3) handle errors, expect two + errors = make(chan os.Error, 64) os.Chmod(filepath.Join(tree.name, tree.entries[1].name), 0) - filepath.Walk(tree.name, v) + filepath.Walk(tree.name, v, errors) Loop: for i := 1; i <= 2; i++ { select { - case <-v: + case <-errors: // ok default: t.Errorf("%d. error expected, none found", i) @@ -357,7 +358,7 @@ func TestWalk(t *testing.T) { } } select { - case err := <-v: + case err := <-errors: t.Errorf("only two errors expected, found 3rd: %v", err) default: // ok |