]> Cypherpunks repositories - gostls13.git/commitdiff
all: update to use filepath.WalkDir instead of filepath.Walk
authorRuss Cox <rsc@golang.org>
Wed, 4 Nov 2020 23:20:17 +0000 (18:20 -0500)
committerRuss Cox <rsc@golang.org>
Wed, 2 Dec 2020 16:33:57 +0000 (16:33 +0000)
Now that filepath.WalkDir is available, it is more efficient
and should be used in place of filepath.Walk.
Update the tree to reflect best practices.

As usual, the code compiled with Go 1.4 during bootstrap is excluded.
(In this CL, that's only cmd/dist.)

For #42027.

Change-Id: Ib0f7b1e43e50b789052f9835a63ced701d8c411c
Reviewed-on: https://go-review.googlesource.com/c/go/+/267719
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
17 files changed:
src/cmd/compile/fmt_test.go
src/cmd/dist/test.go
src/cmd/fix/main.go
src/cmd/go/go_test.go
src/cmd/go/internal/modfetch/fetch.go
src/cmd/go/internal/version/version.go
src/cmd/go/testdata/addmod.go
src/cmd/go/testdata/savedir.go
src/cmd/gofmt/gofmt.go
src/cmd/gofmt/long_test.go
src/cmd/internal/moddeps/moddeps_test.go
src/compress/gzip/issue14937_test.go
src/go/build/deps_test.go
src/go/doc/headscan.go
src/index/suffixarray/suffixarray_test.go
test/run.go
test/winbatch.go

index e372259c788e184cf0d9f6fa5b97c2ee5065f161..6625ccf5e24a80ba7c2a7f7ad84ef65643d5f879 100644 (file)
@@ -52,6 +52,7 @@ import (
        "go/types"
        "internal/testenv"
        "io"
+       "io/fs"
        "io/ioutil"
        "log"
        "os"
@@ -89,7 +90,7 @@ func TestFormats(t *testing.T) {
        testenv.MustHaveGoBuild(t) // more restrictive than necessary, but that's ok
 
        // process all directories
-       filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
+       filepath.WalkDir(".", func(path string, info fs.DirEntry, err error) error {
                if info.IsDir() {
                        if info.Name() == "testdata" {
                                return filepath.SkipDir
index 2a17ab9caebb0bd6d94b3a394811fea5c6a6ba96..955ce2a063c83330efb7b8e2aef01c94f62d31b9 100644 (file)
@@ -1509,6 +1509,7 @@ func (t *tester) makeGOROOTUnwritable() (undo func()) {
        }
        gocacheSubdir, _ := filepath.Rel(dir, gocache)
 
+       // Note: Can't use WalkDir here, because this has to compile with Go 1.4.
        filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
                if suffix := strings.TrimPrefix(path, dir+string(filepath.Separator)); suffix != "" {
                        if suffix == gocacheSubdir {
index 1cea9a876af190db157aeff7e9766f44b887aff8..1cedf992cf3458c3d9934a10c85579c990f8d32a 100644 (file)
@@ -234,10 +234,10 @@ func report(err error) {
 }
 
 func walkDir(path string) {
-       filepath.Walk(path, visitFile)
+       filepath.WalkDir(path, visitFile)
 }
 
-func visitFile(path string, f fs.FileInfo, err error) error {
+func visitFile(path string, f fs.DirEntry, err error) error {
        if err == nil && isGoFile(f) {
                err = processFile(path, false)
        }
@@ -247,7 +247,7 @@ func visitFile(path string, f fs.FileInfo, err error) error {
        return nil
 }
 
-func isGoFile(f fs.FileInfo) bool {
+func isGoFile(f fs.DirEntry) bool {
        // ignore non-Go files
        name := f.Name()
        return !f.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
index a02231fa98324df1eeff128399a233d829ea014f..a730c87f977814e8575545b7c49265541983b7c7 100644 (file)
@@ -774,7 +774,7 @@ func (tg *testgoData) cleanup() {
 func removeAll(dir string) error {
        // module cache has 0444 directories;
        // make them writable in order to remove content.
-       filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
+       filepath.WalkDir(dir, func(path string, info fs.DirEntry, err error) error {
                // chmod not only directories, but also things that we couldn't even stat
                // due to permission errors: they may also be unreadable directories.
                if err != nil || info.IsDir() {
@@ -820,8 +820,8 @@ func TestNewReleaseRebuildsStalePackagesInGOPATH(t *testing.T) {
        } {
                srcdir := filepath.Join(testGOROOT, copydir)
                tg.tempDir(filepath.Join("goroot", copydir))
-               err := filepath.Walk(srcdir,
-                       func(path string, info fs.FileInfo, err error) error {
+               err := filepath.WalkDir(srcdir,
+                       func(path string, info fs.DirEntry, err error) error {
                                if err != nil {
                                        return err
                                }
@@ -838,9 +838,6 @@ func TestNewReleaseRebuildsStalePackagesInGOPATH(t *testing.T) {
                                        return err
                                }
                                tg.tempFile(dest, string(data))
-                               if err := os.Chmod(tg.path(dest), info.Mode()|0200); err != nil {
-                                       return err
-                               }
                                return nil
                        })
                if err != nil {
index a3e2cd1f9d66ccd9258b484cd8cfa3b97c7afb38..2ee78de5b24847f4fc6c251620fd48f2bc5da491 100644 (file)
@@ -318,9 +318,10 @@ func makeDirsReadOnly(dir string) {
                mode fs.FileMode
        }
        var dirs []pathMode // in lexical order
-       filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
-               if err == nil && info.Mode()&0222 != 0 {
-                       if info.IsDir() {
+       filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
+               if err == nil && d.IsDir() {
+                       info, err := d.Info()
+                       if err == nil && info.Mode()&0222 != 0 {
                                dirs = append(dirs, pathMode{path, info.Mode()})
                        }
                }
@@ -337,7 +338,7 @@ func makeDirsReadOnly(dir string) {
 // any permission changes needed to do so.
 func RemoveAll(dir string) error {
        // Module cache has 0555 directories; make them writable in order to remove content.
-       filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
+       filepath.WalkDir(dir, func(path string, info fs.DirEntry, err error) error {
                if err != nil {
                        return nil // ignore errors walking in file system
                }
index 44ac24c62d418ad6690472a164a7b02a7cb8383b..58cbd32e78d1158700c64c2d207f40e4456802c1 100644 (file)
@@ -88,8 +88,15 @@ func runVersion(ctx context.Context, cmd *base.Command, args []string) {
 
 // scanDir scans a directory for executables to run scanFile on.
 func scanDir(dir string) {
-       filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
-               if info.Mode().IsRegular() || info.Mode()&fs.ModeSymlink != 0 {
+       filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
+               if d.Type().IsRegular() || d.Type()&fs.ModeSymlink != 0 {
+                       info, err := d.Info()
+                       if err != nil {
+                               if *versionV {
+                                       fmt.Fprintf(os.Stderr, "%s: %v\n", path, err)
+                               }
+                               return nil
+                       }
                        scanFile(path, info, *versionV)
                }
                return nil
@@ -120,6 +127,7 @@ func scanFile(file string, info fs.FileInfo, mustPrint bool) {
                }
                info = i
        }
+
        if !isExe(file, info) {
                if mustPrint {
                        fmt.Fprintf(os.Stderr, "%s: not executable file\n", file)
index d1b6467c5d1cd9886409ef18d80862d164d8d044..71ac47fdc1250f53ce5107b45bcb9bec93d02126 100644 (file)
@@ -122,8 +122,8 @@ func main() {
                        {Name: ".info", Data: info},
                }
                dir = filepath.Clean(dir)
-               err = filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
-                       if !info.Mode().IsRegular() {
+               err = filepath.WalkDir(dir, func(path string, info fs.DirEntry, err error) error {
+                       if !info.Type().IsRegular() {
                                return nil
                        }
                        name := info.Name()
index 04902df61ef2254ea94a4116684e10eb6af9e155..75895ee279f96dda176700794323dca149326a03 100644 (file)
@@ -49,7 +49,7 @@ func main() {
 
        a := new(txtar.Archive)
        dir = filepath.Clean(dir)
-       filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
+       filepath.WalkDir(dir, func(path string, info fs.DirEntry, err error) error {
                if path == dir {
                        return nil
                }
@@ -60,7 +60,7 @@ func main() {
                        }
                        return nil
                }
-               if !info.Mode().IsRegular() {
+               if !info.Type().IsRegular() {
                        return nil
                }
                data, err := ioutil.ReadFile(path)
index dba2411eed356a9446685a9ee7cccb8c00a84d03..719c681a3e37ec88499a42d2f571c601a55d8673 100644 (file)
@@ -74,7 +74,7 @@ func initParserMode() {
        }
 }
 
-func isGoFile(f fs.FileInfo) bool {
+func isGoFile(f fs.DirEntry) bool {
        // ignore non-Go files
        name := f.Name()
        return !f.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
@@ -164,7 +164,7 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
        return err
 }
 
-func visitFile(path string, f fs.FileInfo, err error) error {
+func visitFile(path string, f fs.DirEntry, err error) error {
        if err == nil && isGoFile(f) {
                err = processFile(path, nil, os.Stdout, false)
        }
@@ -177,7 +177,7 @@ func visitFile(path string, f fs.FileInfo, err error) error {
 }
 
 func walkDir(path string) {
-       filepath.Walk(path, visitFile)
+       filepath.WalkDir(path, visitFile)
 }
 
 func main() {
index 28306ce83e71dfde4eab53f147312d815fa72929..4a821705f16d06e3de015257cd70d633b82b7c91 100644 (file)
@@ -108,12 +108,12 @@ func testFiles(t *testing.T, filenames <-chan string, done chan<- int) {
 func genFilenames(t *testing.T, filenames chan<- string) {
        defer close(filenames)
 
-       handleFile := func(filename string, fi fs.FileInfo, err error) error {
+       handleFile := func(filename string, d fs.DirEntry, err error) error {
                if err != nil {
                        t.Error(err)
                        return nil
                }
-               if isGoFile(fi) {
+               if isGoFile(d) {
                        filenames <- filename
                        nfiles++
                }
@@ -124,13 +124,13 @@ func genFilenames(t *testing.T, filenames chan<- string) {
        if *files != "" {
                for _, filename := range strings.Split(*files, ",") {
                        fi, err := os.Stat(filename)
-                       handleFile(filename, fi, err)
+                       handleFile(filename, &statDirEntry{fi}, err)
                }
                return // ignore files under -root
        }
 
        // otherwise, test all Go files under *root
-       filepath.Walk(*root, handleFile)
+       filepath.WalkDir(*root, handleFile)
 }
 
 func TestAll(t *testing.T) {
@@ -164,3 +164,12 @@ func TestAll(t *testing.T) {
                fmt.Printf("processed %d files\n", nfiles)
        }
 }
+
+type statDirEntry struct {
+       info fs.FileInfo
+}
+
+func (d *statDirEntry) Name() string               { return d.info.Name() }
+func (d *statDirEntry) IsDir() bool                { return d.info.IsDir() }
+func (d *statDirEntry) Type() fs.FileMode          { return d.info.Mode().Type() }
+func (d *statDirEntry) Info() (fs.FileInfo, error) { return d.info, nil }
index 7362e7868b67da016e7b247148a80ade36265fb7..9ea21873c5ed9075b75cc49332402547cea8613b 100644 (file)
@@ -33,7 +33,7 @@ func findGorootModules(t *testing.T) []gorootModule {
        goBin := testenv.GoToolPath(t)
 
        goroot.once.Do(func() {
-               goroot.err = filepath.Walk(runtime.GOROOT(), func(path string, info fs.FileInfo, err error) error {
+               goroot.err = filepath.WalkDir(runtime.GOROOT(), func(path string, info fs.DirEntry, err error) error {
                        if err != nil {
                                return err
                        }
index 24db3641aa74a9e022390ff46432161406e6e8e7..20da0b6824a6a922d2a3e95b2b407aa3a59a294c 100644 (file)
@@ -31,7 +31,7 @@ func TestGZIPFilesHaveZeroMTimes(t *testing.T) {
                t.Fatal("error evaluating GOROOT: ", err)
        }
        var files []string
-       err = filepath.Walk(goroot, func(path string, info fs.FileInfo, err error) error {
+       err = filepath.WalkDir(goroot, func(path string, info fs.DirEntry, err error) error {
                if err != nil {
                        return err
                }
index bf1367355d15044ef0b489fd748aec34748ad4b1..e9ed26aa5fdbff7d94245de97258ec7d36031e47 100644 (file)
@@ -510,8 +510,8 @@ func listStdPkgs(goroot string) ([]string, error) {
        var pkgs []string
 
        src := filepath.Join(goroot, "src") + string(filepath.Separator)
-       walkFn := func(path string, fi fs.FileInfo, err error) error {
-               if err != nil || !fi.IsDir() || path == src {
+       walkFn := func(path string, d fs.DirEntry, err error) error {
+               if err != nil || !d.IsDir() || path == src {
                        return nil
                }
 
@@ -528,7 +528,7 @@ func listStdPkgs(goroot string) ([]string, error) {
                pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
                return nil
        }
-       if err := filepath.Walk(src, walkFn); err != nil {
+       if err := filepath.WalkDir(src, walkFn); err != nil {
                return nil, err
        }
        return pkgs, nil
index 8ea462366e045e6db228eb2b913e8384b354c12a..fe26a0ea8426950cd4fea7100d181099ace14dfc 100644 (file)
@@ -69,8 +69,8 @@ func main() {
        flag.Parse()
        fset := token.NewFileSet()
        nheadings := 0
-       err := filepath.Walk(*root, func(path string, fi fs.FileInfo, err error) error {
-               if !fi.IsDir() {
+       err := filepath.WalkDir(*root, func(path string, info fs.DirEntry, err error) error {
+               if !info.IsDir() {
                        return nil
                }
                pkgs, err := parser.ParseDir(fset, path, isGoFile, parser.ParseComments)
index b6a81123b7406d6207932148fc563d01fc15cb92..a11a98dae0de1a43f188404ea4ec8ae2c7b0272e 100644 (file)
@@ -503,7 +503,7 @@ func makeText(name string) ([]byte, error) {
                        return nil, err
                }
        case "go":
-               err := filepath.Walk("../..", func(path string, info fs.FileInfo, err error) error {
+               err := filepath.WalkDir("../..", func(path string, info fs.DirEntry, err error) error {
                        if err == nil && strings.HasSuffix(path, ".go") && !info.IsDir() {
                                file, err := ioutil.ReadFile(path)
                                if err != nil {
index 672861c8d78136364c3d8247e708c5e5a51b0288..4abf32d25c8b6755d9d36e942d40579ac111dcc8 100644 (file)
@@ -14,6 +14,7 @@ import (
        "fmt"
        "hash/fnv"
        "io"
+       "io/fs"
        "io/ioutil"
        "log"
        "os"
@@ -1793,7 +1794,7 @@ func overlayDir(dstRoot, srcRoot string) error {
                return err
        }
 
-       return filepath.Walk(srcRoot, func(srcPath string, info os.FileInfo, err error) error {
+       return filepath.WalkDir(srcRoot, func(srcPath string, d fs.DirEntry, err error) error {
                if err != nil || srcPath == srcRoot {
                        return err
                }
@@ -1804,14 +1805,16 @@ func overlayDir(dstRoot, srcRoot string) error {
                }
                dstPath := filepath.Join(dstRoot, suffix)
 
-               perm := info.Mode() & os.ModePerm
-               if info.Mode()&os.ModeSymlink != 0 {
+               var info fs.FileInfo
+               if d.Type()&os.ModeSymlink != 0 {
                        info, err = os.Stat(srcPath)
-                       if err != nil {
-                               return err
-                       }
-                       perm = info.Mode() & os.ModePerm
+               } else {
+                       info, err = d.Info()
                }
+               if err != nil {
+                       return err
+               }
+               perm := info.Mode() & os.ModePerm
 
                // Always copy directories (don't symlink them).
                // If we add a file in the overlay, we don't want to add it in the original.
index c3b48d385cecc8bd1dd4ddfa5e1a8b1377750283..54c2fff13463bdf3a51bbb1ea3a321d37bc26f1e 100644 (file)
@@ -27,11 +27,11 @@ func main() {
        // Walk the entire Go repository source tree (without GOROOT/pkg),
        // skipping directories that start with "." and named "testdata",
        // and ensure all .bat files found have exact CRLF line endings.
-       err := filepath.Walk(runtime.GOROOT(), func(path string, fi os.FileInfo, err error) error {
+       err := filepath.WalkDir(runtime.GOROOT(), func(path string, d os.DirEntry, err error) error {
                if err != nil {
                        return err
                }
-               if fi.IsDir() && (strings.HasPrefix(fi.Name(), ".") || fi.Name() == "testdata") {
+               if d.IsDir() && (strings.HasPrefix(d.Name(), ".") || d.Name() == "testdata") {
                        return filepath.SkipDir
                }
                if path == filepath.Join(runtime.GOROOT(), "pkg") {
@@ -39,7 +39,7 @@ func main() {
                        // Skip it to avoid false positives. (Also see golang.org/issue/37929.)
                        return filepath.SkipDir
                }
-               if filepath.Ext(fi.Name()) == ".bat" {
+               if filepath.Ext(d.Name()) == ".bat" {
                        enforceBatchStrictCRLF(path)
                }
                return nil