[#1250] *: Remove io/ioutil imports

It is deprecated starting from go1.16.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-03-18 14:12:58 +03:00 committed by Alex Vanin
parent 3bdab77c42
commit 622ea4818f
9 changed files with 28 additions and 32 deletions

View file

@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "os"
"sort" "sort"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/crypto/hash"
@ -112,7 +112,7 @@ func dumpContainers(cmd *cobra.Command, _ []string) error {
if err != nil { if err != nil {
return err return err
} }
return ioutil.WriteFile(filename, out, 0o660) return os.WriteFile(filename, out, 0o660)
} }
func restoreContainers(cmd *cobra.Command, _ []string) error { func restoreContainers(cmd *cobra.Command, _ []string) error {
@ -136,7 +136,7 @@ func restoreContainers(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("can't fetch container contract hash: %w", err) return fmt.Errorf("can't fetch container contract hash: %w", err)
} }
data, err := ioutil.ReadFile(filename) data, err := os.ReadFile(filename)
if err != nil { if err != nil {
return fmt.Errorf("can't read dump file: %w", err) return fmt.Errorf("can't read dump file: %w", err)
} }

View file

@ -2,7 +2,7 @@ package morph
import ( import (
"bytes" "bytes"
"io/ioutil" "io"
"math/rand" "math/rand"
"os" "os"
"path/filepath" "path/filepath"
@ -101,7 +101,7 @@ func setupTestTerminal(t *testing.T) *bytes.Buffer {
in := bytes.NewBuffer(nil) in := bytes.NewBuffer(nil)
input.Terminal = term.NewTerminal(input.ReadWriter{ input.Terminal = term.NewTerminal(input.ReadWriter{
Reader: in, Reader: in,
Writer: ioutil.Discard, Writer: io.Discard,
}, "") }, "")
t.Cleanup(func() { input.Terminal = nil }) t.Cleanup(func() { input.Terminal = nil })

View file

@ -3,7 +3,7 @@ package morph
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"time" "time"
@ -186,7 +186,7 @@ func (c *initializeContext) nativeHash(name string) util.Uint160 {
} }
func openAlphabetWallets(walletDir string) ([]*wallet.Wallet, error) { func openAlphabetWallets(walletDir string) ([]*wallet.Wallet, error) {
walletFiles, err := ioutil.ReadDir(walletDir) walletFiles, err := os.ReadDir(walletDir)
if err != nil { if err != nil {
return nil, fmt.Errorf("can't read alphabet wallets dir: %w", err) return nil, fmt.Errorf("can't read alphabet wallets dir: %w", err)
} }

View file

@ -8,7 +8,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -392,11 +391,11 @@ func (c *initializeContext) readContracts(names []string) error {
if c.ContractPath != "" && fi.IsDir() { if c.ContractPath != "" && fi.IsDir() {
for _, ctrName := range names { for _, ctrName := range names {
cs := new(contractState) cs := new(contractState)
cs.RawNEF, err = ioutil.ReadFile(filepath.Join(c.ContractPath, ctrName, ctrName+"_contract.nef")) cs.RawNEF, err = os.ReadFile(filepath.Join(c.ContractPath, ctrName, ctrName+"_contract.nef"))
if err != nil { if err != nil {
return fmt.Errorf("can't read NEF file for %s contract: %w", ctrName, err) return fmt.Errorf("can't read NEF file for %s contract: %w", ctrName, err)
} }
cs.RawManifest, err = ioutil.ReadFile(filepath.Join(c.ContractPath, ctrName, "config.json")) cs.RawManifest, err = os.ReadFile(filepath.Join(c.ContractPath, ctrName, "config.json"))
if err != nil { if err != nil {
return fmt.Errorf("can't read manifest file for %s contract: %w", ctrName, err) return fmt.Errorf("can't read manifest file for %s contract: %w", ctrName, err)
} }
@ -481,12 +480,12 @@ func readContractsFromArchive(file io.Reader, names []string) (map[string]*contr
switch { switch {
case strings.HasSuffix(h.Name, filepath.Join(ctrName, ctrName+"_contract.nef")): case strings.HasSuffix(h.Name, filepath.Join(ctrName, ctrName+"_contract.nef")):
cs.RawNEF, err = ioutil.ReadAll(r) cs.RawNEF, err = io.ReadAll(r)
if err != nil { if err != nil {
return nil, fmt.Errorf("can't read NEF file for %s contract: %w", ctrName, err) return nil, fmt.Errorf("can't read NEF file for %s contract: %w", ctrName, err)
} }
case strings.HasSuffix(h.Name, "config.json"): case strings.HasSuffix(h.Name, "config.json"):
cs.RawManifest, err = ioutil.ReadAll(r) cs.RawManifest, err = io.ReadAll(r)
if err != nil { if err != nil {
return nil, fmt.Errorf("can't read manifest file for %s contract: %w", ctrName, err) return nil, fmt.Errorf("can't read manifest file for %s contract: %w", ctrName, err)
} }

View file

@ -6,7 +6,6 @@ import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"math/big" "math/big"
"math/rand" "math/rand"
"net" "net"
@ -200,7 +199,7 @@ func storageConfig(cmd *cobra.Command, args []string) {
} }
out := applyTemplate(c) out := applyTemplate(c)
fatalOnErr(ioutil.WriteFile(outPath, out, 0644)) fatalOnErr(os.WriteFile(outPath, out, 0644))
cmd.Println("Node is ready for work! Run `neofs-node -config " + outPath + "`") cmd.Println("Node is ready for work! Run `neofs-node -config " + outPath + "`")
} }

View file

@ -6,7 +6,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"strings" "strings"
@ -123,7 +122,7 @@ func createEACL(cmd *cobra.Command, _ []string) {
return return
} }
err = ioutil.WriteFile(outArg, buf.Bytes(), 0644) err = os.WriteFile(outArg, buf.Bytes(), 0644)
if err != nil { if err != nil {
cmd.PrintErrln(err) cmd.PrintErrln(err)
os.Exit(1) os.Exit(1)
@ -135,7 +134,7 @@ func getRulesFromFile(filename string) ([]string, error) {
return nil, nil return nil, nil
} }
data, err := ioutil.ReadFile(filename) data, err := os.ReadFile(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -3,7 +3,7 @@ package cmd
import ( import (
"bytes" "bytes"
"errors" "errors"
"io/ioutil" "io"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -39,7 +39,7 @@ func Test_getKey(t *testing.T) {
keyPath := filepath.Join(dir, "binary.key") keyPath := filepath.Join(dir, "binary.key")
rawKey, err := keys.NewPrivateKey() rawKey, err := keys.NewPrivateKey()
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, ioutil.WriteFile(keyPath, rawKey.Bytes(), os.ModePerm)) require.NoError(t, os.WriteFile(keyPath, rawKey.Bytes(), os.ModePerm))
wifKey, err := keys.NewPrivateKey() wifKey, err := keys.NewPrivateKey()
require.NoError(t, err) require.NoError(t, err)
@ -52,7 +52,7 @@ func Test_getKey(t *testing.T) {
in := bytes.NewBuffer(nil) in := bytes.NewBuffer(nil)
input.Terminal = term.NewTerminal(input.ReadWriter{ input.Terminal = term.NewTerminal(input.ReadWriter{
Reader: in, Reader: in,
Writer: ioutil.Discard, Writer: io.Discard,
}, "") }, "")
checkKeyError(t, filepath.Join(dir, "badfile"), errInvalidKey) checkKeyError(t, filepath.Join(dir, "badfile"), errInvalidKey)

View file

@ -2,7 +2,7 @@ package inspect
import ( import (
"fmt" "fmt"
"io/ioutil" "os"
common "github.com/nspcc-dev/neofs-node/cmd/neofs-lens/internal" common "github.com/nspcc-dev/neofs-node/cmd/neofs-lens/internal"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza"
@ -111,7 +111,7 @@ func printObjectInfo(cmd *cobra.Command, data []byte) {
} }
if vOut != "" { if vOut != "" {
err := ioutil.WriteFile(vOut, obj.Payload(), 0644) err := os.WriteFile(vOut, obj.Payload(), 0644)
common.ExitOnErr(cmd, common.Errf("couldn't write payload: %w", err)) common.ExitOnErr(cmd, common.Errf("couldn't write payload: %w", err))
} }
} }

View file

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"errors" "errors"
"io" "io"
"io/ioutil"
"math/rand" "math/rand"
"os" "os"
"path/filepath" "path/filepath"
@ -126,19 +125,19 @@ func testDump(t *testing.T, objCount int, hasWriteCache bool) {
t.Run("invalid file", func(t *testing.T) { t.Run("invalid file", func(t *testing.T) {
t.Run("invalid magic", func(t *testing.T) { t.Run("invalid magic", func(t *testing.T) {
out := out + ".wrongmagic" out := out + ".wrongmagic"
require.NoError(t, ioutil.WriteFile(out, []byte{0, 0, 0, 0}, os.ModePerm)) require.NoError(t, os.WriteFile(out, []byte{0, 0, 0, 0}, os.ModePerm))
_, err := sh.Restore(new(shard.RestorePrm).WithPath(out)) _, err := sh.Restore(new(shard.RestorePrm).WithPath(out))
require.True(t, errors.Is(err, shard.ErrInvalidMagic), "got: %v", err) require.True(t, errors.Is(err, shard.ErrInvalidMagic), "got: %v", err)
}) })
fileData, err := ioutil.ReadFile(out) fileData, err := os.ReadFile(out)
require.NoError(t, err) require.NoError(t, err)
t.Run("incomplete size", func(t *testing.T) { t.Run("incomplete size", func(t *testing.T) {
out := out + ".wrongsize" out := out + ".wrongsize"
fileData := append(fileData, 1) fileData := append(fileData, 1)
require.NoError(t, ioutil.WriteFile(out, fileData, os.ModePerm)) require.NoError(t, os.WriteFile(out, fileData, os.ModePerm))
_, err := sh.Restore(new(shard.RestorePrm).WithPath(out)) _, err := sh.Restore(new(shard.RestorePrm).WithPath(out))
require.True(t, errors.Is(err, io.ErrUnexpectedEOF), "got: %v", err) require.True(t, errors.Is(err, io.ErrUnexpectedEOF), "got: %v", err)
@ -146,7 +145,7 @@ func testDump(t *testing.T, objCount int, hasWriteCache bool) {
t.Run("incomplete object data", func(t *testing.T) { t.Run("incomplete object data", func(t *testing.T) {
out := out + ".wrongsize" out := out + ".wrongsize"
fileData := append(fileData, 1, 0, 0, 0) fileData := append(fileData, 1, 0, 0, 0)
require.NoError(t, ioutil.WriteFile(out, fileData, os.ModePerm)) require.NoError(t, os.WriteFile(out, fileData, os.ModePerm))
_, err := sh.Restore(new(shard.RestorePrm).WithPath(out)) _, err := sh.Restore(new(shard.RestorePrm).WithPath(out))
require.True(t, errors.Is(err, io.EOF), "got: %v", err) require.True(t, errors.Is(err, io.EOF), "got: %v", err)
@ -154,7 +153,7 @@ func testDump(t *testing.T, objCount int, hasWriteCache bool) {
t.Run("invalid object", func(t *testing.T) { t.Run("invalid object", func(t *testing.T) {
out := out + ".wrongobj" out := out + ".wrongobj"
fileData := append(fileData, 1, 0, 0, 0, 0xFF, 4, 0, 0, 0, 1, 2, 3, 4) fileData := append(fileData, 1, 0, 0, 0, 0xFF, 4, 0, 0, 0, 1, 2, 3, 4)
require.NoError(t, ioutil.WriteFile(out, fileData, os.ModePerm)) require.NoError(t, os.WriteFile(out, fileData, os.ModePerm))
_, err := sh.Restore(new(shard.RestorePrm).WithPath(out)) _, err := sh.Restore(new(shard.RestorePrm).WithPath(out))
require.Error(t, err) require.Error(t, err)
@ -300,7 +299,7 @@ func TestDumpIgnoreErrors(t *testing.T) {
addr := cidtest.ID().String() + "." + generateOID().String() addr := cidtest.ID().String() + "." + generateOID().String()
dirName := filepath.Join(bsPath, addr[:2]) dirName := filepath.Join(bsPath, addr[:2])
require.NoError(t, os.MkdirAll(dirName, os.ModePerm)) require.NoError(t, os.MkdirAll(dirName, os.ModePerm))
require.NoError(t, ioutil.WriteFile(filepath.Join(dirName, addr[2:]), corruptedData, os.ModePerm)) require.NoError(t, os.WriteFile(filepath.Join(dirName, addr[2:]), corruptedData, os.ModePerm))
// 1.2. Unreadable file. // 1.2. Unreadable file.
addr = cidtest.ID().String() + "." + generateOID().String() addr = cidtest.ID().String() + "." + generateOID().String()
@ -308,7 +307,7 @@ func TestDumpIgnoreErrors(t *testing.T) {
require.NoError(t, os.MkdirAll(dirName, os.ModePerm)) require.NoError(t, os.MkdirAll(dirName, os.ModePerm))
fname := filepath.Join(dirName, addr[2:]) fname := filepath.Join(dirName, addr[2:])
require.NoError(t, ioutil.WriteFile(fname, []byte{}, 0)) require.NoError(t, os.WriteFile(fname, []byte{}, 0))
// 1.3. Unreadable dir. // 1.3. Unreadable dir.
require.NoError(t, os.MkdirAll(filepath.Join(bsPath, "ZZ"), 0)) require.NoError(t, os.MkdirAll(filepath.Join(bsPath, "ZZ"), 0))
@ -324,7 +323,7 @@ func TestDumpIgnoreErrors(t *testing.T) {
bTree := filepath.Join(bsPath, "blobovnicza") bTree := filepath.Join(bsPath, "blobovnicza")
data := make([]byte, 1024) data := make([]byte, 1024)
rand.Read(data) rand.Read(data)
require.NoError(t, ioutil.WriteFile(filepath.Join(bTree, "0", "2"), data, 0)) require.NoError(t, os.WriteFile(filepath.Join(bTree, "0", "2"), data, 0))
// 2.2. Invalid object in valid blobovnicza. // 2.2. Invalid object in valid blobovnicza.
prm := new(blobovnicza.PutPrm) prm := new(blobovnicza.PutPrm)
@ -343,7 +342,7 @@ func TestDumpIgnoreErrors(t *testing.T) {
addr := cidtest.ID().String() + "." + objecttest.ID().String() addr := cidtest.ID().String() + "." + objecttest.ID().String()
dir := filepath.Join(wcPath, addr[:1]) dir := filepath.Join(wcPath, addr[:1])
require.NoError(t, os.MkdirAll(dir, os.ModePerm)) require.NoError(t, os.MkdirAll(dir, os.ModePerm))
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, addr[1:]), nil, 0)) require.NoError(t, os.WriteFile(filepath.Join(dir, addr[1:]), nil, 0))
} }
out := filepath.Join(t.TempDir(), "out.dump") out := filepath.Join(t.TempDir(), "out.dump")