Skip to content

Commit

Permalink
all: update to use os.ReadDir where appropriate
Browse files Browse the repository at this point in the history
os.ReadDir is a replacement for ioutil.ReadDir that returns
a slice of fs.DirEntry instead of fs.FileInfo, meaning it is the
more efficient form.

This CL updates call sites throughout the Go source tree
wherever possible. As usual, code built using the Go 1.4
bootstrap toolchain is not included. There is also a use in
go/build that appears in the public API and can't be changed,
at least not without additional changes.

Fixes #42026.

Change-Id: Icfc9dd52c6045020f6830e22c72128499462d561
Reviewed-on: https://go-review.googlesource.com/c/go/+/266366
Trust: Russ Cox <[email protected]>
Run-TryBot: Russ Cox <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
rsc committed Dec 9, 2020
1 parent 4f1b0a4 commit f1980ef
Show file tree
Hide file tree
Showing 19 changed files with 91 additions and 94 deletions.
3 changes: 1 addition & 2 deletions src/cmd/go/internal/clean/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -244,7 +243,7 @@ func clean(p *load.Package) {
base.Errorf("%v", p.Error)
return
}
dirs, err := ioutil.ReadDir(p.Dir)
dirs, err := os.ReadDir(p.Dir)
if err != nil {
base.Errorf("go clean %s: %v", p.Dir, err)
return
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/go/internal/imports/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package imports
import (
"bytes"
"internal/testenv"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -58,7 +57,7 @@ func TestScan(t *testing.T) {
func TestScanDir(t *testing.T) {
testenv.MustHaveGoBuild(t)

dirs, err := ioutil.ReadDir("testdata")
dirs, err := os.ReadDir("testdata")
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 3 additions & 4 deletions src/cmd/go/internal/load/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"go/scanner"
"go/token"
"io/fs"
"io/ioutil"
"os"
"path"
pathpkg "path"
Expand Down Expand Up @@ -1296,9 +1295,9 @@ HaveGoMod:
// Otherwise it is not possible to vendor just a/b/c and still import the
// non-vendored a/b. See golang.org/issue/13832.
func hasGoFiles(dir string) bool {
fis, _ := ioutil.ReadDir(dir)
for _, fi := range fis {
if !fi.IsDir() && strings.HasSuffix(fi.Name(), ".go") {
files, _ := os.ReadDir(dir)
for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".go") {
return true
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/cmd/go/internal/modcmd/vendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -244,7 +243,7 @@ var metaPrefixes = []string{
}

// matchMetadata reports whether info is a metadata file.
func matchMetadata(dir string, info fs.FileInfo) bool {
func matchMetadata(dir string, info fs.DirEntry) bool {
name := info.Name()
for _, p := range metaPrefixes {
if strings.HasPrefix(name, p) {
Expand All @@ -255,7 +254,7 @@ func matchMetadata(dir string, info fs.FileInfo) bool {
}

// matchPotentialSourceFile reports whether info may be relevant to a build operation.
func matchPotentialSourceFile(dir string, info fs.FileInfo) bool {
func matchPotentialSourceFile(dir string, info fs.DirEntry) bool {
if strings.HasSuffix(info.Name(), "_test.go") {
return false
}
Expand All @@ -281,16 +280,16 @@ func matchPotentialSourceFile(dir string, info fs.FileInfo) bool {
}

// copyDir copies all regular files satisfying match(info) from src to dst.
func copyDir(dst, src string, match func(dir string, info fs.FileInfo) bool) {
files, err := ioutil.ReadDir(src)
func copyDir(dst, src string, match func(dir string, info fs.DirEntry) bool) {
files, err := os.ReadDir(src)
if err != nil {
base.Fatalf("go mod vendor: %v", err)
}
if err := os.MkdirAll(dst, 0777); err != nil {
base.Fatalf("go mod vendor: %v", err)
}
for _, file := range files {
if file.IsDir() || !file.Mode().IsRegular() || !match(src, file) {
if file.IsDir() || !file.Type().IsRegular() || !match(src, file) {
continue
}
r, err := os.Open(filepath.Join(src, file.Name()))
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/go/internal/modfetch/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -598,7 +597,7 @@ func rewriteVersionList(dir string) {
}
defer unlock()

infos, err := ioutil.ReadDir(dir)
infos, err := os.ReadDir(dir)
if err != nil {
return
}
Expand Down
17 changes: 8 additions & 9 deletions src/cmd/go/internal/modload/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"fmt"
"go/build"
"internal/lazyregexp"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -445,13 +444,13 @@ func CreateModFile(ctx context.Context, modPath string) {
// this is an existing project. Walking the tree for packages would be more
// accurate, but could take much longer.
empty := true
fis, _ := ioutil.ReadDir(modRoot)
for _, fi := range fis {
name := fi.Name()
files, _ := os.ReadDir(modRoot)
for _, f := range files {
name := f.Name()
if strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") {
continue
}
if strings.HasSuffix(name, ".go") || fi.IsDir() {
if strings.HasSuffix(name, ".go") || f.IsDir() {
empty = false
break
}
Expand Down Expand Up @@ -731,19 +730,19 @@ func findModulePath(dir string) (string, error) {

// Cast about for import comments,
// first in top-level directory, then in subdirectories.
list, _ := ioutil.ReadDir(dir)
list, _ := os.ReadDir(dir)
for _, info := range list {
if info.Mode().IsRegular() && strings.HasSuffix(info.Name(), ".go") {
if info.Type().IsRegular() && strings.HasSuffix(info.Name(), ".go") {
if com := findImportComment(filepath.Join(dir, info.Name())); com != "" {
return com, nil
}
}
}
for _, info1 := range list {
if info1.IsDir() {
files, _ := ioutil.ReadDir(filepath.Join(dir, info1.Name()))
files, _ := os.ReadDir(filepath.Join(dir, info1.Name()))
for _, info2 := range files {
if info2.Mode().IsRegular() && strings.HasSuffix(info2.Name(), ".go") {
if info2.Type().IsRegular() && strings.HasSuffix(info2.Name(), ".go") {
if com := findImportComment(filepath.Join(dir, info1.Name(), info2.Name())); com != "" {
return path.Dir(com), nil
}
Expand Down
12 changes: 8 additions & 4 deletions src/cmd/go/internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"go/build"
"io"
"io/fs"
"io/ioutil"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -1561,13 +1560,18 @@ func hashOpen(name string) (cache.ActionID, error) {
}
hashWriteStat(h, info)
if info.IsDir() {
names, err := ioutil.ReadDir(name)
files, err := os.ReadDir(name)
if err != nil {
fmt.Fprintf(h, "err %v\n", err)
}
for _, f := range names {
for _, f := range files {
fmt.Fprintf(h, "file %s ", f.Name())
hashWriteStat(h, f)
finfo, err := f.Info()
if err != nil {
fmt.Fprintf(h, "err %v\n", err)
} else {
hashWriteStat(h, finfo)
}
}
} else if info.Mode().IsRegular() {
// Because files might be very large, do not attempt
Expand Down
7 changes: 3 additions & 4 deletions src/cmd/go/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -75,12 +74,12 @@ func StartProxy() {
var modList []module.Version

func readModList() {
infos, err := ioutil.ReadDir("testdata/mod")
files, err := os.ReadDir("testdata/mod")
if err != nil {
log.Fatal(err)
}
for _, info := range infos {
name := info.Name()
for _, f := range files {
name := f.Name()
if !strings.HasSuffix(name, ".txt") {
continue
}
Expand Down
21 changes: 10 additions & 11 deletions src/crypto/x509/root_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package x509

import (
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -82,28 +81,28 @@ func loadSystemRoots() (*CertPool, error) {
return nil, firstErr
}

// readUniqueDirectoryEntries is like ioutil.ReadDir but omits
// readUniqueDirectoryEntries is like os.ReadDir but omits
// symlinks that point within the directory.
func readUniqueDirectoryEntries(dir string) ([]fs.FileInfo, error) {
fis, err := ioutil.ReadDir(dir)
func readUniqueDirectoryEntries(dir string) ([]fs.DirEntry, error) {
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
uniq := fis[:0]
for _, fi := range fis {
if !isSameDirSymlink(fi, dir) {
uniq = append(uniq, fi)
uniq := files[:0]
for _, f := range files {
if !isSameDirSymlink(f, dir) {
uniq = append(uniq, f)
}
}
return uniq, nil
}

// isSameDirSymlink reports whether fi in dir is a symlink with a
// target not containing a slash.
func isSameDirSymlink(fi fs.FileInfo, dir string) bool {
if fi.Mode()&fs.ModeSymlink == 0 {
func isSameDirSymlink(f fs.DirEntry, dir string) bool {
if f.Type()&fs.ModeSymlink == 0 {
return false
}
target, err := os.Readlink(filepath.Join(dir, fi.Name()))
target, err := os.Readlink(filepath.Join(dir, f.Name()))
return err == nil && !strings.Contains(target, "/")
}
3 changes: 1 addition & 2 deletions src/go/build/deps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"fmt"
"internal/testenv"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -597,7 +596,7 @@ func findImports(pkg string) ([]string, error) {
vpkg = "vendor/" + pkg
}
dir := filepath.Join(Default.GOROOT, "src", vpkg)
files, err := ioutil.ReadDir(dir)
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions src/go/internal/gcimporter/gcimporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bytes"
"fmt"
"internal/testenv"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -66,7 +65,7 @@ const maxTime = 30 * time.Second

func testDir(t *testing.T, dir string, endTime time.Time) (nimports int) {
dirname := filepath.Join(runtime.GOROOT(), "pkg", runtime.GOOS+"_"+runtime.GOARCH, dir)
list, err := ioutil.ReadDir(dirname)
list, err := os.ReadDir(dirname)
if err != nil {
t.Fatalf("testDir(%s): %s", dirname, err)
}
Expand Down Expand Up @@ -144,7 +143,7 @@ func TestVersionHandling(t *testing.T) {
}

const dir = "./testdata/versions"
list, err := ioutil.ReadDir(dir)
list, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions src/go/internal/srcimporter/srcimporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"go/token"
"go/types"
"internal/testenv"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -59,7 +58,7 @@ func walkDir(t *testing.T, path string, endTime time.Time) (int, bool) {
return 0, false
}

list, err := ioutil.ReadDir(filepath.Join(runtime.GOROOT(), "src", path))
list, err := os.ReadDir(filepath.Join(runtime.GOROOT(), "src", path))
if err != nil {
t.Fatalf("walkDir %s failed (%v)", path, err)
}
Expand Down
10 changes: 5 additions & 5 deletions src/go/parser/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ package parser
import (
"go/scanner"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -174,13 +174,13 @@ func checkErrors(t *testing.T, filename string, input interface{}) {
}

func TestErrors(t *testing.T) {
list, err := ioutil.ReadDir(testdata)
list, err := os.ReadDir(testdata)
if err != nil {
t.Fatal(err)
}
for _, fi := range list {
name := fi.Name()
if !fi.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".src") {
for _, d := range list {
name := d.Name()
if !d.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".src") {
checkErrors(t, filepath.Join(testdata, name), nil)
}
}
Expand Down
Loading

0 comments on commit f1980ef

Please sign in to comment.