Commit e5c20dc2 authored by Gustavo Niemeyer's avatar Gustavo Niemeyer Committed by Robert Griesemer

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 #2237.

R=golang-dev, gri, r
CC=golang-dev
https://golang.org/cl/4964067
parent 1e480cd1
...@@ -200,7 +200,7 @@ func report(err os.Error) { ...@@ -200,7 +200,7 @@ func report(err os.Error) {
func walkDir(path string) { func walkDir(path string) {
v := make(fileVisitor) v := make(fileVisitor)
go func() { go func() {
filepath.Walk(path, v, v) filepath.Walk(path, v)
close(v) close(v)
}() }()
for err := range v { for err := range v {
...@@ -225,6 +225,10 @@ func (v fileVisitor) VisitFile(path string, f *os.FileInfo) { ...@@ -225,6 +225,10 @@ func (v fileVisitor) VisitFile(path string, f *os.FileInfo) {
} }
} }
func (v fileVisitor) Error(path string, err os.Error) {
v <- err
}
func isGoFile(f *os.FileInfo) bool { func isGoFile(f *os.FileInfo) bool {
// ignore non-Go files // ignore non-Go files
return f.IsRegular() && !strings.HasPrefix(f.Name, ".") && strings.HasSuffix(f.Name, ".go") return f.IsRegular() && !strings.HasPrefix(f.Name, ".") && strings.HasSuffix(f.Name, ".go")
......
...@@ -164,10 +164,14 @@ func (v fileVisitor) VisitFile(path string, f *os.FileInfo) { ...@@ -164,10 +164,14 @@ func (v fileVisitor) VisitFile(path string, f *os.FileInfo) {
} }
} }
func (v fileVisitor) Error(path string, err os.Error) {
v <- err
}
func walkDir(path string) { func walkDir(path string) {
v := make(fileVisitor) v := make(fileVisitor)
go func() { go func() {
filepath.Walk(path, v, v) filepath.Walk(path, v)
close(v) close(v)
}() }()
for err := range v { for err := range v {
......
...@@ -104,30 +104,34 @@ func doFile(name string, reader io.Reader) { ...@@ -104,30 +104,34 @@ func doFile(name string, reader io.Reader) {
// Visitor for filepath.Walk - trivial. Just calls doFile on each file. // Visitor for filepath.Walk - trivial. Just calls doFile on each file.
// TODO: if govet becomes richer, might want to process // TODO: if govet becomes richer, might want to process
// a directory (package) at a time. // 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 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") { if strings.HasSuffix(path, ".go") {
doFile(path, nil) doFile(path, nil)
} }
} }
func (v fileVisitor) Error(path string, err os.Error) {
v <- err
}
// walkDir recursively walks the tree looking for .go files. // walkDir recursively walks the tree looking for .go files.
func walkDir(root string) { func walkDir(root string) {
errors := make(chan os.Error) v := make(fileVisitor)
done := make(chan bool) done := make(chan bool)
go func() { go func() {
for e := range errors { for e := range v {
errorf("walk error: %s", e) errorf("walk error: %s", e)
} }
done <- true done <- true
}() }()
filepath.Walk(root, V{}, errors) filepath.Walk(root, v)
close(errors) close(v)
<-done <-done
} }
......
...@@ -255,14 +255,14 @@ func Abs(path string) (string, os.Error) { ...@@ -255,14 +255,14 @@ func Abs(path string) (string, os.Error) {
} }
// Visitor methods are invoked for corresponding file tree entries // Visitor methods are invoked for corresponding file tree entries
// visited by Walk. The parameter path is the full path of f relative // visited by Walk.
// to root.
type Visitor interface { type Visitor interface {
VisitDir(path string, f *os.FileInfo) bool VisitDir(path string, f *os.FileInfo) bool
VisitFile(path string, f *os.FileInfo) 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() { if !f.IsDirectory() {
v.VisitFile(path, f) v.VisitFile(path, f)
return return
...@@ -274,13 +274,11 @@ func walk(path string, f *os.FileInfo, v Visitor, errors chan<- os.Error) { ...@@ -274,13 +274,11 @@ func walk(path string, f *os.FileInfo, v Visitor, errors chan<- os.Error) {
list, err := readDir(path) list, err := readDir(path)
if err != nil { if err != nil {
if errors != nil { v.Error(path, err)
errors <- err
}
} }
for _, e := range list { for _, e := range list {
walk(Join(path, e.Name), e, v, errors) walk(Join(path, e.Name), e, v)
} }
} }
...@@ -316,18 +314,14 @@ func (f fileInfoList) Swap(i, j int) { f[i], f[j] = f[j], f[i] } ...@@ -316,18 +314,14 @@ 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. // v.VisitFile for each directory or file in the tree, including root.
// If v.VisitDir returns false, Walk skips the directory's entries; // If v.VisitDir returns false, Walk skips the directory's entries;
// otherwise it invokes itself for each directory entry in sorted order. // otherwise it invokes itself for each directory entry in sorted order.
// An error reading a directory does not abort the Walk. // Walk calls v.Error if an error happens while reading a directory.
// If errors != nil, Walk sends each directory read error func Walk(root string, v Visitor) {
// to the channel. Otherwise Walk discards the error.
func Walk(root string, v Visitor, errors chan<- os.Error) {
f, err := os.Lstat(root) f, err := os.Lstat(root)
if err != nil { if err != nil {
if errors != nil { v.Error(root, err)
errors <- err
}
return // can't progress return // can't progress
} }
walk(root, f, v, errors) walk(root, f, v)
} }
// Base returns the last element of path. // Base returns the last element of path.
......
...@@ -299,30 +299,30 @@ func mark(name string) { ...@@ -299,30 +299,30 @@ func mark(name string) {
}) })
} }
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) mark(f.Name)
return true return true
} }
func (v *TestVisitor) VisitFile(path string, f *os.FileInfo) { func (v TestVisitor) VisitFile(path string, f *os.FileInfo) {
mark(f.Name) mark(f.Name)
} }
func (v TestVisitor) Error(path string, err os.Error) {
v <- err
}
func TestWalk(t *testing.T) { func TestWalk(t *testing.T) {
makeTree(t) makeTree(t)
// 1) ignore error handling, expect none v := make(TestVisitor, 64)
v := &TestVisitor{}
filepath.Walk(tree.name, v, nil)
checkMarks(t)
// 2) handle errors, expect none // 1) no errors expected.
errors := make(chan os.Error, 64) filepath.Walk(tree.name, v)
filepath.Walk(tree.name, v, errors)
select { select {
case err := <-errors: case err := <-v:
t.Errorf("no error expected, found: %s", err) t.Errorf("no error expected, found: %s", err)
default: default:
// ok // ok
...@@ -343,14 +343,13 @@ func TestWalk(t *testing.T) { ...@@ -343,14 +343,13 @@ func TestWalk(t *testing.T) {
tree.entries[1].mark-- tree.entries[1].mark--
tree.entries[3].mark-- tree.entries[3].mark--
// 3) handle errors, expect two // 2) expect two errors
errors = make(chan os.Error, 64)
os.Chmod(filepath.Join(tree.name, tree.entries[1].name), 0) os.Chmod(filepath.Join(tree.name, tree.entries[1].name), 0)
filepath.Walk(tree.name, v, errors) filepath.Walk(tree.name, v)
Loop: Loop:
for i := 1; i <= 2; i++ { for i := 1; i <= 2; i++ {
select { select {
case <-errors: case <-v:
// ok // ok
default: default:
t.Errorf("%d. error expected, none found", i) t.Errorf("%d. error expected, none found", i)
...@@ -358,7 +357,7 @@ func TestWalk(t *testing.T) { ...@@ -358,7 +357,7 @@ func TestWalk(t *testing.T) {
} }
} }
select { select {
case err := <-errors: case err := <-v:
t.Errorf("only two errors expected, found 3rd: %v", err) t.Errorf("only two errors expected, found 3rd: %v", err)
default: default:
// ok // ok
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment