Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: replace non-trivial uses of ioutil with os #24886

Merged
merged 2 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions accounts/abi/bind/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package bind

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -1986,7 +1985,7 @@ func TestGolangBindings(t *testing.T) {
if err != nil {
t.Fatalf("test %d: failed to generate binding: %v", i, err)
}
if err = ioutil.WriteFile(filepath.Join(pkg, strings.ToLower(tt.name)+".go"), []byte(bind), 0600); err != nil {
if err = os.WriteFile(filepath.Join(pkg, strings.ToLower(tt.name)+".go"), []byte(bind), 0600); err != nil {
t.Fatalf("test %d: failed to write binding: %v", i, err)
}
// Generate the test file with the injected test code
Expand All @@ -2002,7 +2001,7 @@ func TestGolangBindings(t *testing.T) {
%s
}
`, tt.imports, tt.name, tt.tester)
if err := ioutil.WriteFile(filepath.Join(pkg, strings.ToLower(tt.name)+"_test.go"), []byte(code), 0600); err != nil {
if err := os.WriteFile(filepath.Join(pkg, strings.ToLower(tt.name)+"_test.go"), []byte(code), 0600); err != nil {
t.Fatalf("test %d: failed to write tests: %v", i, err)
}
})
Expand Down
13 changes: 8 additions & 5 deletions accounts/keystore/file_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package keystore

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand All @@ -41,7 +40,7 @@ func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, er
t0 := time.Now()

// List all the failes from the keystore folder
files, err := ioutil.ReadDir(keyDir)
files, err := os.ReadDir(keyDir)
if err != nil {
return nil, nil, nil, err
}
Expand All @@ -65,7 +64,11 @@ func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, er
// Gather the set of all and fresly modified files
all.Add(path)

modified := fi.ModTime()
info, err := fi.Info()
if err != nil {
return nil, nil, nil, err
}
modified := info.ModTime()
if modified.After(fc.lastMod) {
mods.Add(path)
}
Expand All @@ -89,13 +92,13 @@ func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, er
}

// nonKeyFile ignores editor backups, hidden files and folders/symlinks.
func nonKeyFile(fi os.FileInfo) bool {
func nonKeyFile(fi os.DirEntry) bool {
// Skip editor backups and UNIX-style hidden files.
if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") {
return true
}
// Skip misc special files, directories (yes, symlinks too).
if fi.IsDir() || fi.Mode()&os.ModeType != 0 {
if fi.IsDir() || !fi.Type().IsRegular() {
return true
}
return false
Expand Down
3 changes: 1 addition & 2 deletions cmd/geth/accountcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package main

import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -163,7 +162,7 @@ Password: {{.InputLine "foo"}}
Address: {d4584b5f6229b7be90727b0fc8c6b91bb427821f}
`)

files, err := ioutil.ReadDir(filepath.Join(geth.Datadir, "keystore"))
files, err := os.ReadDir(filepath.Join(geth.Datadir, "keystore"))
if len(files) != 1 {
t.Errorf("expected one key file in keystore directory, found %d files (error: %v)", len(files), err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/geth/version_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -55,7 +54,7 @@ func testVerification(t *testing.T, pubkey, sigdir string) {
t.Fatal(err)
}
// Signatures, with and without comments, both trusted and untrusted
files, err := ioutil.ReadDir(sigdir)
files, err := os.ReadDir(sigdir)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions core/rawdb/freezer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package rawdb
import (
"errors"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -472,7 +471,7 @@ func (f *Freezer) MigrateTable(kind string, convert convertLegacyFn) error {
if err := newTable.Close(); err != nil {
return err
}
files, err := ioutil.ReadDir(migrationPath)
files, err := os.ReadDir(migrationPath)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions eth/tracers/internal/tracetest/calltrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package tracetest

import (
"encoding/json"
"io/ioutil"
"math/big"
"os"
"path/filepath"
Expand Down Expand Up @@ -136,7 +135,7 @@ func TestCallTracerNative(t *testing.T) {
}

func testCallTracer(tracerName string, dirPath string, t *testing.T) {
files, err := ioutil.ReadDir(filepath.Join("testdata", dirPath))
files, err := os.ReadDir(filepath.Join("testdata", dirPath))
if err != nil {
t.Fatalf("failed to retrieve tracer test suite: %v", err)
}
Expand Down Expand Up @@ -241,7 +240,7 @@ func camel(str string) string {
return strings.Join(pieces, "")
}
func BenchmarkTracers(b *testing.B) {
files, err := ioutil.ReadDir(filepath.Join("testdata", "call_tracer"))
files, err := os.ReadDir(filepath.Join("testdata", "call_tracer"))
if err != nil {
b.Fatalf("failed to retrieve tracer test suite: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/build/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"go/parser"
"go/token"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -177,7 +176,7 @@ func UploadSFTP(identityFile, host, dir string, files []string) error {
// package paths.
func FindMainPackages(dir string) []string {
var commands []string
cmds, err := ioutil.ReadDir(dir)
cmds, err := os.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions rpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bufio"
"bytes"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -53,7 +52,7 @@ func TestServerRegisterName(t *testing.T) {
}

func TestServer(t *testing.T) {
files, err := ioutil.ReadDir("testdata")
files, err := os.ReadDir("testdata")
if err != nil {
t.Fatal("where'd my testdata go?")
}
Expand Down
5 changes: 2 additions & 3 deletions signer/core/signed_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
Expand Down Expand Up @@ -354,7 +353,7 @@ func sign(typedData apitypes.TypedData) ([]byte, []byte, error) {
}

func TestJsonFiles(t *testing.T) {
testfiles, err := ioutil.ReadDir("testdata/")
testfiles, err := os.ReadDir("testdata/")
if err != nil {
t.Fatalf("failed reading files: %v", err)
}
Expand Down Expand Up @@ -389,7 +388,7 @@ func TestJsonFiles(t *testing.T) {
// crashes or hangs.
func TestFuzzerFiles(t *testing.T) {
corpusdir := path.Join("testdata", "fuzzing")
testfiles, err := ioutil.ReadDir(corpusdir)
testfiles, err := os.ReadDir(corpusdir)
if err != nil {
t.Fatalf("failed reading files: %v", err)
}
Expand Down