Skip to content

Commit 07508ac

Browse files
authored
all: replace uses of ioutil with io and os (ethereum#24869)
1 parent 330e53f commit 07508ac

File tree

97 files changed

+203
-257
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+203
-257
lines changed

accounts/abi/bind/auth.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"crypto/ecdsa"
2222
"errors"
2323
"io"
24-
"io/ioutil"
2524
"math/big"
2625

2726
"github.com/ethereum/go-ethereum/accounts"
@@ -45,7 +44,7 @@ var ErrNotAuthorized = errors.New("not authorized to sign this account")
4544
// Deprecated: Use NewTransactorWithChainID instead.
4645
func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
4746
log.Warn("WARNING: NewTransactor has been deprecated in favour of NewTransactorWithChainID")
48-
json, err := ioutil.ReadAll(keyin)
47+
json, err := io.ReadAll(keyin)
4948
if err != nil {
5049
return nil, err
5150
}
@@ -106,7 +105,7 @@ func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
106105
// NewTransactorWithChainID is a utility method to easily create a transaction signer from
107106
// an encrypted json key stream and the associated passphrase.
108107
func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) {
109-
json, err := ioutil.ReadAll(keyin)
108+
json, err := io.ReadAll(keyin)
110109
if err != nil {
111110
return nil, err
112111
}

accounts/keystore/account_cache_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package keystore
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
2221
"math/rand"
2322
"os"
2423
"path/filepath"
@@ -380,11 +379,11 @@ func TestUpdatedKeyfileContents(t *testing.T) {
380379
return
381380
}
382381

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

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

398397
// forceCopyFile is like cp.CopyFile, but doesn't complain if the destination exists.
399398
func forceCopyFile(dst, src string) error {
400-
data, err := ioutil.ReadFile(src)
399+
data, err := os.ReadFile(src)
401400
if err != nil {
402401
return err
403402
}
404-
return ioutil.WriteFile(dst, data, 0644)
403+
return os.WriteFile(dst, data, 0644)
405404
}

accounts/keystore/key.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"encoding/json"
2424
"fmt"
2525
"io"
26-
"io/ioutil"
2726
"os"
2827
"path/filepath"
2928
"strings"
@@ -197,7 +196,7 @@ func writeTemporaryKeyFile(file string, content []byte) (string, error) {
197196
}
198197
// Atomic write: create a temporary hidden file first
199198
// then move it into place. TempFile assigns mode 0600.
200-
f, err := ioutil.TempFile(filepath.Dir(file), "."+filepath.Base(file)+".tmp")
199+
f, err := os.CreateTemp(filepath.Dir(file), "."+filepath.Base(file)+".tmp")
201200
if err != nil {
202201
return "", err
203202
}

accounts/keystore/passphrase.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"encoding/json"
3535
"fmt"
3636
"io"
37-
"io/ioutil"
3837
"os"
3938
"path/filepath"
4039

@@ -82,7 +81,7 @@ type keyStorePassphrase struct {
8281

8382
func (ks keyStorePassphrase) GetKey(addr common.Address, filename, auth string) (*Key, error) {
8483
// Load the key from the keystore and decrypt its contents
85-
keyjson, err := ioutil.ReadFile(filename)
84+
keyjson, err := os.ReadFile(filename)
8685
if err != nil {
8786
return nil, err
8887
}

accounts/keystore/passphrase_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package keystore
1818

1919
import (
20-
"io/ioutil"
20+
"os"
2121
"testing"
2222

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

3131
// Tests that a json key file can be decrypted and encrypted in multiple rounds.
3232
func TestKeyEncryptDecrypt(t *testing.T) {
33-
keyjson, err := ioutil.ReadFile("testdata/very-light-scrypt.json")
33+
keyjson, err := os.ReadFile("testdata/very-light-scrypt.json")
3434
if err != nil {
3535
t.Fatal(err)
3636
}

accounts/scwallet/hub.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ package scwallet
3434

3535
import (
3636
"encoding/json"
37-
"io/ioutil"
37+
"io"
3838
"os"
3939
"path/filepath"
4040
"sort"
@@ -96,7 +96,7 @@ func (hub *Hub) readPairings() error {
9696
return err
9797
}
9898

99-
pairingData, err := ioutil.ReadAll(pairingFile)
99+
pairingData, err := io.ReadAll(pairingFile)
100100
if err != nil {
101101
return err
102102
}

build/ci.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import (
4646
"encoding/base64"
4747
"flag"
4848
"fmt"
49-
"io/ioutil"
5049
"log"
5150
"os"
5251
"os/exec"
@@ -736,7 +735,7 @@ func ppaUpload(workdir, ppa, sshUser string, files []string) {
736735
if sshkey := getenvBase64("PPA_SSH_KEY"); len(sshkey) > 0 {
737736
idfile = filepath.Join(workdir, "sshkey")
738737
if !common.FileExist(idfile) {
739-
ioutil.WriteFile(idfile, sshkey, 0600)
738+
os.WriteFile(idfile, sshkey, 0600)
740739
}
741740
}
742741
// Upload
@@ -759,7 +758,7 @@ func makeWorkdir(wdflag string) string {
759758
if wdflag != "" {
760759
err = os.MkdirAll(wdflag, 0744)
761760
} else {
762-
wdflag, err = ioutil.TempDir("", "geth-build-")
761+
wdflag, err = os.MkdirTemp("", "geth-build-")
763762
}
764763
if err != nil {
765764
log.Fatal(err)

build/update-license.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import (
3939
"bufio"
4040
"bytes"
4141
"fmt"
42-
"io/ioutil"
4342
"log"
4443
"os"
4544
"os/exec"
@@ -241,7 +240,7 @@ func gitAuthors(files []string) []string {
241240
}
242241

243242
func readAuthors() []string {
244-
content, err := ioutil.ReadFile("AUTHORS")
243+
content, err := os.ReadFile("AUTHORS")
245244
if err != nil && !os.IsNotExist(err) {
246245
log.Fatalln("error reading AUTHORS:", err)
247246
}
@@ -305,7 +304,7 @@ func writeAuthors(files []string) {
305304
content.WriteString("\n")
306305
}
307306
fmt.Println("writing AUTHORS")
308-
if err := ioutil.WriteFile("AUTHORS", content.Bytes(), 0644); err != nil {
307+
if err := os.WriteFile("AUTHORS", content.Bytes(), 0644); err != nil {
309308
log.Fatalln(err)
310309
}
311310
}
@@ -381,7 +380,7 @@ func writeLicense(info *info) {
381380
if err != nil {
382381
log.Fatalf("error stat'ing %s: %v\n", info.file, err)
383382
}
384-
content, err := ioutil.ReadFile(info.file)
383+
content, err := os.ReadFile(info.file)
385384
if err != nil {
386385
log.Fatalf("error reading %s: %v\n", info.file, err)
387386
}
@@ -400,7 +399,7 @@ func writeLicense(info *info) {
400399
return
401400
}
402401
fmt.Println("writing", info.ShortLicense(), info.file)
403-
if err := ioutil.WriteFile(info.file, buf.Bytes(), fi.Mode()); err != nil {
402+
if err := os.WriteFile(info.file, buf.Bytes(), fi.Mode()); err != nil {
404403
log.Fatalf("error writing %s: %v", info.file, err)
405404
}
406405
}

cmd/abigen/main.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package main
1919
import (
2020
"encoding/json"
2121
"fmt"
22-
"io/ioutil"
22+
"io"
2323
"os"
2424
"path/filepath"
2525
"regexp"
@@ -155,9 +155,9 @@ func abigen(c *cli.Context) error {
155155
)
156156
input := c.GlobalString(abiFlag.Name)
157157
if input == "-" {
158-
abi, err = ioutil.ReadAll(os.Stdin)
158+
abi, err = io.ReadAll(os.Stdin)
159159
} else {
160-
abi, err = ioutil.ReadFile(input)
160+
abi, err = os.ReadFile(input)
161161
}
162162
if err != nil {
163163
utils.Fatalf("Failed to read input ABI: %v", err)
@@ -166,7 +166,7 @@ func abigen(c *cli.Context) error {
166166

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

215215
case c.GlobalIsSet(jsonFlag.Name):
216-
jsonOutput, err := ioutil.ReadFile(c.GlobalString(jsonFlag.Name))
216+
jsonOutput, err := os.ReadFile(c.GlobalString(jsonFlag.Name))
217217
if err != nil {
218218
utils.Fatalf("Failed to read combined-json from compiler: %v", err)
219219
}
@@ -263,7 +263,7 @@ func abigen(c *cli.Context) error {
263263
fmt.Printf("%s\n", code)
264264
return nil
265265
}
266-
if err := ioutil.WriteFile(c.GlobalString(outFlag.Name), []byte(code), 0600); err != nil {
266+
if err := os.WriteFile(c.GlobalString(outFlag.Name), []byte(code), 0600); err != nil {
267267
utils.Fatalf("Failed to write ABI binding: %v", err)
268268
}
269269
return nil

cmd/clef/main.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"encoding/json"
2626
"fmt"
2727
"io"
28-
"io/ioutil"
2928
"math/big"
3029
"os"
3130
"os/signal"
@@ -374,7 +373,7 @@ func initializeSecrets(c *cli.Context) error {
374373
return fmt.Errorf("master key %v already exists, will not overwrite", location)
375374
}
376375
// Write the file and print the usual warning message
377-
if err = ioutil.WriteFile(location, cipherSeed, 0400); err != nil {
376+
if err = os.WriteFile(location, cipherSeed, 0400); err != nil {
378377
return err
379378
}
380379
fmt.Printf("A master seed has been generated into %s\n", location)
@@ -593,7 +592,7 @@ func signer(c *cli.Context) error {
593592

594593
// Do we have a rule-file?
595594
if ruleFile := c.GlobalString(ruleFlag.Name); ruleFile != "" {
596-
ruleJS, err := ioutil.ReadFile(ruleFile)
595+
ruleJS, err := os.ReadFile(ruleFile)
597596
if err != nil {
598597
log.Warn("Could not load rules, disabling", "file", ruleFile, "err", err)
599598
} else {
@@ -751,7 +750,7 @@ func readMasterKey(ctx *cli.Context, ui core.UIClientAPI) ([]byte, error) {
751750
if err := checkFile(file); err != nil {
752751
return nil, err
753752
}
754-
cipherKey, err := ioutil.ReadFile(file)
753+
cipherKey, err := os.ReadFile(file)
755754
if err != nil {
756755
return nil, err
757756
}

cmd/devp2p/dnscmd.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"crypto/ecdsa"
2121
"encoding/json"
2222
"fmt"
23-
"io/ioutil"
2423
"os"
2524
"path/filepath"
2625
"time"
@@ -253,7 +252,7 @@ func dnsNukeRoute53(ctx *cli.Context) error {
253252

254253
// loadSigningKey loads a private key in Ethereum keystore format.
255254
func loadSigningKey(keyfile string) *ecdsa.PrivateKey {
256-
keyjson, err := ioutil.ReadFile(keyfile)
255+
keyjson, err := os.ReadFile(keyfile)
257256
if err != nil {
258257
exit(fmt.Errorf("failed to read the keyfile at '%s': %v", keyfile, err))
259258
}
@@ -382,7 +381,7 @@ func writeTreeMetadata(directory string, def *dnsDefinition) {
382381
exit(err)
383382
}
384383
metaFile, _ := treeDefinitionFiles(directory)
385-
if err := ioutil.WriteFile(metaFile, metaJSON, 0644); err != nil {
384+
if err := os.WriteFile(metaFile, metaJSON, 0644); err != nil {
386385
exit(err)
387386
}
388387
}
@@ -411,7 +410,7 @@ func writeTXTJSON(file string, txt map[string]string) {
411410
fmt.Println()
412411
return
413412
}
414-
if err := ioutil.WriteFile(file, txtJSON, 0644); err != nil {
413+
if err := os.WriteFile(file, txtJSON, 0644); err != nil {
415414
exit(err)
416415
}
417416
}

cmd/devp2p/enrcmd.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"encoding/hex"
2323
"fmt"
2424
"io"
25-
"io/ioutil"
2625
"net"
2726
"os"
2827
"strconv"
@@ -54,9 +53,9 @@ func enrdump(ctx *cli.Context) error {
5453
var b []byte
5554
var err error
5655
if file == "-" {
57-
b, err = ioutil.ReadAll(os.Stdin)
56+
b, err = io.ReadAll(os.Stdin)
5857
} else {
59-
b, err = ioutil.ReadFile(file)
58+
b, err = os.ReadFile(file)
6059
}
6160
if err != nil {
6261
return err

cmd/devp2p/internal/ethtest/chain.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"io"
24-
"io/ioutil"
2524
"math/big"
2625
"os"
2726
"strings"
@@ -153,7 +152,7 @@ func loadChain(chainfile string, genesis string) (*Chain, error) {
153152
}
154153

155154
func loadGenesis(genesisFile string) (core.Genesis, error) {
156-
chainConfig, err := ioutil.ReadFile(genesisFile)
155+
chainConfig, err := os.ReadFile(genesisFile)
157156
if err != nil {
158157
return core.Genesis{}, err
159158
}

cmd/devp2p/nodeset.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"bytes"
2121
"encoding/json"
2222
"fmt"
23-
"io/ioutil"
2423
"os"
2524
"sort"
2625
"time"
@@ -66,7 +65,7 @@ func writeNodesJSON(file string, nodes nodeSet) {
6665
os.Stdout.Write(nodesJSON)
6766
return
6867
}
69-
if err := ioutil.WriteFile(file, nodesJSON, 0644); err != nil {
68+
if err := os.WriteFile(file, nodesJSON, 0644); err != nil {
7069
exit(err)
7170
}
7271
}

0 commit comments

Comments
 (0)