Skip to content

Commit

Permalink
all: replace uses of ioutil with io and os (ethereum#24869)
Browse files Browse the repository at this point in the history
  • Loading branch information
estensen authored May 16, 2022
1 parent 330e53f commit 07508ac
Show file tree
Hide file tree
Showing 97 changed files with 203 additions and 257 deletions.
5 changes: 2 additions & 3 deletions accounts/abi/bind/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"crypto/ecdsa"
"errors"
"io"
"io/ioutil"
"math/big"

"github.com/ethereum/go-ethereum/accounts"
Expand All @@ -45,7 +44,7 @@ var ErrNotAuthorized = errors.New("not authorized to sign this account")
// Deprecated: Use NewTransactorWithChainID instead.
func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
log.Warn("WARNING: NewTransactor has been deprecated in favour of NewTransactorWithChainID")
json, err := ioutil.ReadAll(keyin)
json, err := io.ReadAll(keyin)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -106,7 +105,7 @@ func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
// NewTransactorWithChainID is a utility method to easily create a transaction signer from
// an encrypted json key stream and the associated passphrase.
func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) {
json, err := ioutil.ReadAll(keyin)
json, err := io.ReadAll(keyin)
if err != nil {
return nil, err
}
Expand Down
9 changes: 4 additions & 5 deletions accounts/keystore/account_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package keystore

import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
Expand Down Expand Up @@ -380,11 +379,11 @@ func TestUpdatedKeyfileContents(t *testing.T) {
return
}

// needed so that modTime of `file` is different to its current value after ioutil.WriteFile
// needed so that modTime of `file` is different to its current value after os.WriteFile
time.Sleep(1000 * time.Millisecond)

// Now replace file contents with crap
if err := ioutil.WriteFile(file, []byte("foo"), 0644); err != nil {
if err := os.WriteFile(file, []byte("foo"), 0644); err != nil {
t.Fatal(err)
return
}
Expand All @@ -397,9 +396,9 @@ func TestUpdatedKeyfileContents(t *testing.T) {

// forceCopyFile is like cp.CopyFile, but doesn't complain if the destination exists.
func forceCopyFile(dst, src string) error {
data, err := ioutil.ReadFile(src)
data, err := os.ReadFile(src)
if err != nil {
return err
}
return ioutil.WriteFile(dst, data, 0644)
return os.WriteFile(dst, data, 0644)
}
3 changes: 1 addition & 2 deletions accounts/keystore/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -197,7 +196,7 @@ func writeTemporaryKeyFile(file string, content []byte) (string, error) {
}
// Atomic write: create a temporary hidden file first
// then move it into place. TempFile assigns mode 0600.
f, err := ioutil.TempFile(filepath.Dir(file), "."+filepath.Base(file)+".tmp")
f, err := os.CreateTemp(filepath.Dir(file), "."+filepath.Base(file)+".tmp")
if err != nil {
return "", err
}
Expand Down
3 changes: 1 addition & 2 deletions accounts/keystore/passphrase.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -82,7 +81,7 @@ type keyStorePassphrase struct {

func (ks keyStorePassphrase) GetKey(addr common.Address, filename, auth string) (*Key, error) {
// Load the key from the keystore and decrypt its contents
keyjson, err := ioutil.ReadFile(filename)
keyjson, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions accounts/keystore/passphrase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package keystore

import (
"io/ioutil"
"os"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -30,7 +30,7 @@ const (

// Tests that a json key file can be decrypted and encrypted in multiple rounds.
func TestKeyEncryptDecrypt(t *testing.T) {
keyjson, err := ioutil.ReadFile("testdata/very-light-scrypt.json")
keyjson, err := os.ReadFile("testdata/very-light-scrypt.json")
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions accounts/scwallet/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ package scwallet

import (
"encoding/json"
"io/ioutil"
"io"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -96,7 +96,7 @@ func (hub *Hub) readPairings() error {
return err
}

pairingData, err := ioutil.ReadAll(pairingFile)
pairingData, err := io.ReadAll(pairingFile)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions build/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import (
"encoding/base64"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -736,7 +735,7 @@ func ppaUpload(workdir, ppa, sshUser string, files []string) {
if sshkey := getenvBase64("PPA_SSH_KEY"); len(sshkey) > 0 {
idfile = filepath.Join(workdir, "sshkey")
if !common.FileExist(idfile) {
ioutil.WriteFile(idfile, sshkey, 0600)
os.WriteFile(idfile, sshkey, 0600)
}
}
// Upload
Expand All @@ -759,7 +758,7 @@ func makeWorkdir(wdflag string) string {
if wdflag != "" {
err = os.MkdirAll(wdflag, 0744)
} else {
wdflag, err = ioutil.TempDir("", "geth-build-")
wdflag, err = os.MkdirTemp("", "geth-build-")
}
if err != nil {
log.Fatal(err)
Expand Down
9 changes: 4 additions & 5 deletions build/update-license.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -241,7 +240,7 @@ func gitAuthors(files []string) []string {
}

func readAuthors() []string {
content, err := ioutil.ReadFile("AUTHORS")
content, err := os.ReadFile("AUTHORS")
if err != nil && !os.IsNotExist(err) {
log.Fatalln("error reading AUTHORS:", err)
}
Expand Down Expand Up @@ -305,7 +304,7 @@ func writeAuthors(files []string) {
content.WriteString("\n")
}
fmt.Println("writing AUTHORS")
if err := ioutil.WriteFile("AUTHORS", content.Bytes(), 0644); err != nil {
if err := os.WriteFile("AUTHORS", content.Bytes(), 0644); err != nil {
log.Fatalln(err)
}
}
Expand Down Expand Up @@ -381,7 +380,7 @@ func writeLicense(info *info) {
if err != nil {
log.Fatalf("error stat'ing %s: %v\n", info.file, err)
}
content, err := ioutil.ReadFile(info.file)
content, err := os.ReadFile(info.file)
if err != nil {
log.Fatalf("error reading %s: %v\n", info.file, err)
}
Expand All @@ -400,7 +399,7 @@ func writeLicense(info *info) {
return
}
fmt.Println("writing", info.ShortLicense(), info.file)
if err := ioutil.WriteFile(info.file, buf.Bytes(), fi.Mode()); err != nil {
if err := os.WriteFile(info.file, buf.Bytes(), fi.Mode()); err != nil {
log.Fatalf("error writing %s: %v", info.file, err)
}
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/abigen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -155,9 +155,9 @@ func abigen(c *cli.Context) error {
)
input := c.GlobalString(abiFlag.Name)
if input == "-" {
abi, err = ioutil.ReadAll(os.Stdin)
abi, err = io.ReadAll(os.Stdin)
} else {
abi, err = ioutil.ReadFile(input)
abi, err = os.ReadFile(input)
}
if err != nil {
utils.Fatalf("Failed to read input ABI: %v", err)
Expand All @@ -166,7 +166,7 @@ func abigen(c *cli.Context) error {

var bin []byte
if binFile := c.GlobalString(binFlag.Name); binFile != "" {
if bin, err = ioutil.ReadFile(binFile); err != nil {
if bin, err = os.ReadFile(binFile); err != nil {
utils.Fatalf("Failed to read input bytecode: %v", err)
}
if strings.Contains(string(bin), "//") {
Expand Down Expand Up @@ -213,7 +213,7 @@ func abigen(c *cli.Context) error {
}

case c.GlobalIsSet(jsonFlag.Name):
jsonOutput, err := ioutil.ReadFile(c.GlobalString(jsonFlag.Name))
jsonOutput, err := os.ReadFile(c.GlobalString(jsonFlag.Name))
if err != nil {
utils.Fatalf("Failed to read combined-json from compiler: %v", err)
}
Expand Down Expand Up @@ -263,7 +263,7 @@ func abigen(c *cli.Context) error {
fmt.Printf("%s\n", code)
return nil
}
if err := ioutil.WriteFile(c.GlobalString(outFlag.Name), []byte(code), 0600); err != nil {
if err := os.WriteFile(c.GlobalString(outFlag.Name), []byte(code), 0600); err != nil {
utils.Fatalf("Failed to write ABI binding: %v", err)
}
return nil
Expand Down
7 changes: 3 additions & 4 deletions cmd/clef/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/big"
"os"
"os/signal"
Expand Down Expand Up @@ -374,7 +373,7 @@ func initializeSecrets(c *cli.Context) error {
return fmt.Errorf("master key %v already exists, will not overwrite", location)
}
// Write the file and print the usual warning message
if err = ioutil.WriteFile(location, cipherSeed, 0400); err != nil {
if err = os.WriteFile(location, cipherSeed, 0400); err != nil {
return err
}
fmt.Printf("A master seed has been generated into %s\n", location)
Expand Down Expand Up @@ -593,7 +592,7 @@ func signer(c *cli.Context) error {

// Do we have a rule-file?
if ruleFile := c.GlobalString(ruleFlag.Name); ruleFile != "" {
ruleJS, err := ioutil.ReadFile(ruleFile)
ruleJS, err := os.ReadFile(ruleFile)
if err != nil {
log.Warn("Could not load rules, disabling", "file", ruleFile, "err", err)
} else {
Expand Down Expand Up @@ -751,7 +750,7 @@ func readMasterKey(ctx *cli.Context, ui core.UIClientAPI) ([]byte, error) {
if err := checkFile(file); err != nil {
return nil, err
}
cipherKey, err := ioutil.ReadFile(file)
cipherKey, err := os.ReadFile(file)
if err != nil {
return nil, err
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/devp2p/dnscmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"crypto/ecdsa"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -253,7 +252,7 @@ func dnsNukeRoute53(ctx *cli.Context) error {

// loadSigningKey loads a private key in Ethereum keystore format.
func loadSigningKey(keyfile string) *ecdsa.PrivateKey {
keyjson, err := ioutil.ReadFile(keyfile)
keyjson, err := os.ReadFile(keyfile)
if err != nil {
exit(fmt.Errorf("failed to read the keyfile at '%s': %v", keyfile, err))
}
Expand Down Expand Up @@ -382,7 +381,7 @@ func writeTreeMetadata(directory string, def *dnsDefinition) {
exit(err)
}
metaFile, _ := treeDefinitionFiles(directory)
if err := ioutil.WriteFile(metaFile, metaJSON, 0644); err != nil {
if err := os.WriteFile(metaFile, metaJSON, 0644); err != nil {
exit(err)
}
}
Expand Down Expand Up @@ -411,7 +410,7 @@ func writeTXTJSON(file string, txt map[string]string) {
fmt.Println()
return
}
if err := ioutil.WriteFile(file, txtJSON, 0644); err != nil {
if err := os.WriteFile(file, txtJSON, 0644); err != nil {
exit(err)
}
}
5 changes: 2 additions & 3 deletions cmd/devp2p/enrcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"strconv"
Expand Down Expand Up @@ -54,9 +53,9 @@ func enrdump(ctx *cli.Context) error {
var b []byte
var err error
if file == "-" {
b, err = ioutil.ReadAll(os.Stdin)
b, err = io.ReadAll(os.Stdin)
} else {
b, err = ioutil.ReadFile(file)
b, err = os.ReadFile(file)
}
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions cmd/devp2p/internal/ethtest/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/big"
"os"
"strings"
Expand Down Expand Up @@ -153,7 +152,7 @@ func loadChain(chainfile string, genesis string) (*Chain, error) {
}

func loadGenesis(genesisFile string) (core.Genesis, error) {
chainConfig, err := ioutil.ReadFile(genesisFile)
chainConfig, err := os.ReadFile(genesisFile)
if err != nil {
return core.Genesis{}, err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/devp2p/nodeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"sort"
"time"
Expand Down Expand Up @@ -66,7 +65,7 @@ func writeNodesJSON(file string, nodes nodeSet) {
os.Stdout.Write(nodesJSON)
return
}
if err := ioutil.WriteFile(file, nodesJSON, 0644); err != nil {
if err := os.WriteFile(file, nodesJSON, 0644); err != nil {
exit(err)
}
}
Expand Down
Loading

0 comments on commit 07508ac

Please sign in to comment.