src/cmd/gofix/main.go | 6 +++++- src/cmd/gofmt/gofmt.go | 6 +++++- src/cmd/govet/govet.go | 18 +++++++++++------- src/pkg/path/filepath/path.go | 24 +++++++++--------------- src/pkg/path/filepath/path_test.go | 31 +++++++++++++++---------------- diff --git a/src/cmd/gofix/main.go b/src/cmd/gofix/main.go index e7e7013c568b6eefe5f17ddba806a10529fe394a..514bf38edbc8a58f8b18ef0ebea3fb880003929a 100644 --- a/src/cmd/gofix/main.go +++ b/src/cmd/gofix/main.go @@ -200,7 +200,7 @@ func walkDir(path string) { v := make(fileVisitor) go func() { - filepath.Walk(path, v, v) + filepath.Walk(path, v) close(v) }() for err := range v { @@ -223,6 +223,10 @@ if err := processFile(path, false); err != nil { v <- err } } +} + +func (v fileVisitor) Error(path string, err os.Error) { + v <- err } func isGoFile(f *os.FileInfo) bool { diff --git a/src/cmd/gofmt/gofmt.go b/src/cmd/gofmt/gofmt.go index 975ae6ac6f5807e5a819f3beb0073b4a9999e322..1225618031d8c64afc10a7bbc0f46aa09f86df11 100644 --- a/src/cmd/gofmt/gofmt.go +++ b/src/cmd/gofmt/gofmt.go @@ -164,10 +164,14 @@ } } } +func (v fileVisitor) Error(path string, err os.Error) { + v <- err +} + func walkDir(path string) { v := make(fileVisitor) go func() { - filepath.Walk(path, v, v) + filepath.Walk(path, v) close(v) }() for err := range v { diff --git a/src/cmd/govet/govet.go b/src/cmd/govet/govet.go index 98d3d5c17f0c0773c3903052de3dcbdcdc024c7b..c53515d3bfd9567af09f6f77ba5f51a623c7fb65 100644 --- a/src/cmd/govet/govet.go +++ b/src/cmd/govet/govet.go @@ -104,30 +104,34 @@ // Visitor for filepath.Walk - trivial. Just calls doFile on each file. // TODO: if govet becomes richer, might want to process // a directory (package) at a time. -type V struct{} +type fileVisitor chan os.Error -func (v V) VisitDir(path string, f *os.FileInfo) bool { +func (v fileVisitor) VisitDir(path string, f *os.FileInfo) bool { return true } -func (v V) VisitFile(path string, f *os.FileInfo) { +func (v fileVisitor) VisitFile(path string, f *os.FileInfo) { if strings.HasSuffix(path, ".go") { doFile(path, nil) } } +func (v fileVisitor) Error(path string, err os.Error) { + v <- err +} + // walkDir recursively walks the tree looking for .go files. func walkDir(root string) { - errors := make(chan os.Error) + v := make(fileVisitor) done := make(chan bool) go func() { - for e := range errors { + for e := range v { errorf("walk error: %s", e) } done <- true }() - filepath.Walk(root, V{}, errors) - close(errors) + filepath.Walk(root, v) + close(v) <-done } diff --git a/src/pkg/path/filepath/path.go b/src/pkg/path/filepath/path.go index 3d5b915c1013641049ef592e9fbc5a213397f6eb..d6a7d08e83ca0427913adc19b6707b3474194250 100644 --- a/src/pkg/path/filepath/path.go +++ b/src/pkg/path/filepath/path.go @@ -255,14 +255,14 @@ return Join(wd, path), nil } // Visitor methods are invoked for corresponding file tree entries -// visited by Walk. The parameter path is the full path of f relative -// to root. +// visited by Walk. 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, errors chan<- os.Error) { +func walk(path string, f *os.FileInfo, v Visitor) { if !f.IsDirectory() { v.VisitFile(path, f) return @@ -274,13 +274,11 @@ } list, err := readDir(path) if err != nil { - if errors != nil { - errors <- err - } + v.Error(path, err) } for _, e := range list { - walk(Join(path, e.Name), e, v, errors) + walk(Join(path, e.Name), e, v) } } @@ -316,18 +314,14 @@ // Walk walks the file tree rooted at root, calling v.VisitDir or // 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. -// 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) { +// Walk calls v.Error if an error happens while reading a directory. +func Walk(root string, v Visitor) { f, err := os.Lstat(root) if err != nil { - if errors != nil { - errors <- err - } + v.Error(root, err) return // can't progress } - walk(root, f, v, errors) + walk(root, f, v) } // 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 395b12775a9ed62c4854e9b4aace65efee29afba..8c566c700259ef2501daaf660c05357bacbd1d3a 100644 --- a/src/pkg/path/filepath/path_test.go +++ b/src/pkg/path/filepath/path_test.go @@ -299,30 +299,30 @@ } }) } -type TestVisitor struct{} +type TestVisitor chan os.Error -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) - // 1) ignore error handling, expect none - v := &TestVisitor{} - filepath.Walk(tree.name, v, nil) - checkMarks(t) + v := make(TestVisitor, 64) - // 2) handle errors, expect none - errors := make(chan os.Error, 64) - filepath.Walk(tree.name, v, errors) + // 1) no errors expected. + filepath.Walk(tree.name, v) select { - case err := <-errors: + case err := <-v: t.Errorf("no error expected, found: %s", err) default: // ok @@ -343,14 +343,13 @@ // correct double-marking of directory itself tree.entries[1].mark-- tree.entries[3].mark-- - // 3) handle errors, expect two - errors = make(chan os.Error, 64) + // 2) expect two errors os.Chmod(filepath.Join(tree.name, tree.entries[1].name), 0) - filepath.Walk(tree.name, v, errors) + filepath.Walk(tree.name, v) Loop: for i := 1; i <= 2; i++ { select { - case <-errors: + case <-v: // ok default: t.Errorf("%d. error expected, none found", i) @@ -358,7 +357,7 @@ break Loop } } select { - case err := <-errors: + case err := <-v: t.Errorf("only two errors expected, found 3rd: %v", err) default: // ok