diff options
author | Michael Stapelberg <stapelberg@debian.org> | 2014-06-19 09:22:53 +0200 |
---|---|---|
committer | Michael Stapelberg <stapelberg@debian.org> | 2014-06-19 09:22:53 +0200 |
commit | 8a39ee361feb9bf46d728ff1ba4f07ca1d9610b1 (patch) | |
tree | 4449f2036cccf162e8417cc5841a35815b3e7ac5 /src/pkg/path/filepath/path.go | |
parent | c8bf49ef8a92e2337b69c14b9b88396efe498600 (diff) | |
download | golang-upstream/1.3.tar.gz |
Imported Upstream version 1.3upstream/1.3
Diffstat (limited to 'src/pkg/path/filepath/path.go')
-rw-r--r-- | src/pkg/path/filepath/path.go | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/src/pkg/path/filepath/path.go b/src/pkg/path/filepath/path.go index f8c7e4b2f..71603cc59 100644 --- a/src/pkg/path/filepath/path.go +++ b/src/pkg/path/filepath/path.go @@ -67,7 +67,7 @@ const ( // along with the non-.. element that precedes it. // 4. Eliminate .. elements that begin a rooted path: // that is, replace "/.." by "/" at the beginning of a path, -// assuming Separator is '/'. +// assuming Separator is '/'. // // The returned path ends in a slash only if it represents a root directory, // such as "/" on Unix or `C:\` on Windows. @@ -336,6 +336,8 @@ var SkipDir = errors.New("skip this directory") // the next file. type WalkFunc func(path string, info os.FileInfo, err error) error +var lstat = os.Lstat // for testing + // walk recursively descends path, calling w. func walk(path string, info os.FileInfo, walkFn WalkFunc) error { err := walkFn(path, info, nil) @@ -350,17 +352,25 @@ func walk(path string, info os.FileInfo, walkFn WalkFunc) error { return nil } - list, err := readDir(path) + names, err := readDirNames(path) if err != nil { return walkFn(path, info, err) } - for _, fileInfo := range list { - err = walk(Join(path, fileInfo.Name()), fileInfo, walkFn) + for _, name := range names { + filename := Join(path, name) + fileInfo, err := lstat(filename) if err != nil { - if !fileInfo.IsDir() || err != SkipDir { + if err := walkFn(filename, fileInfo, err); err != nil && err != SkipDir { return err } + } else { + err = walk(filename, fileInfo, walkFn) + if err != nil { + if !fileInfo.IsDir() || err != SkipDir { + return err + } + } } } return nil @@ -380,30 +390,22 @@ func Walk(root string, walkFn WalkFunc) error { return walk(root, info, walkFn) } -// readDir reads the directory named by dirname and returns +// readDirNames reads the directory named by dirname and returns // a sorted list of directory entries. -// Copied from io/ioutil to avoid the circular import. -func readDir(dirname string) ([]os.FileInfo, error) { +func readDirNames(dirname string) ([]string, error) { f, err := os.Open(dirname) if err != nil { return nil, err } - list, err := f.Readdir(-1) + names, err := f.Readdirnames(-1) f.Close() if err != nil { return nil, err } - sort.Sort(byName(list)) - return list, nil + sort.Strings(names) + return names, nil } -// byName implements sort.Interface. -type byName []os.FileInfo - -func (f byName) Len() int { return len(f) } -func (f byName) Less(i, j int) bool { return f[i].Name() < f[j].Name() } -func (f byName) Swap(i, j int) { f[i], f[j] = f[j], f[i] } - // Base returns the last element of path. // Trailing path separators are removed before extracting the last element. // If the path is empty, Base returns ".". |