From b756ed0ead4901f64e29598981b375f319df2926 Mon Sep 17 00:00:00 2001 From: simlecode <69969590+simlecode@users.noreply.github.com> Date: Wed, 28 Jun 2023 15:15:02 +0800 Subject: [PATCH] chore: Remove dependency on io/ioutil package --- cli/storage-deals.go | 3 +-- cli/util.go | 9 ++++----- client/client_test.go | 6 +++--- client/import.go | 3 +-- client/import_test.go | 5 ++--- config/save.go | 5 ++--- dagstore/mount_test.go | 6 +++--- piecestorage/filestore.go | 3 +-- piecestorage/memstore.go | 3 +-- rpc/piece_storage_server_test.go | 4 ++-- rpc/rpc.go | 6 +++--- utils/algn_zero_reader_test.go | 3 +-- 12 files changed, 24 insertions(+), 32 deletions(-) diff --git a/cli/storage-deals.go b/cli/storage-deals.go index 3723f5b9..ca7d3753 100644 --- a/cli/storage-deals.go +++ b/cli/storage-deals.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "os" "path/filepath" "sort" @@ -244,7 +243,7 @@ part states: } fpath := cctx.Args().Get(0) - data, err := ioutil.ReadFile(fpath) + data, err := os.ReadFile(fpath) if err != nil { return fmt.Errorf("read deal file(%s) failed: %v", fpath, err) } diff --git a/cli/util.go b/cli/util.go index 21fff2ba..da4e9631 100644 --- a/cli/util.go +++ b/cli/util.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "os" "os/signal" "path" @@ -88,12 +87,12 @@ func NewMarketNode(cctx *cli.Context) (marketapi.IMarket, jsonrpc.ClientCloser, return nil, nil, err } - apiUrl, err := ioutil.ReadFile(path.Join(homePath, "api")) + apiUrl, err := os.ReadFile(path.Join(homePath, "api")) if err != nil { return nil, nil, err } - token, err := ioutil.ReadFile(path.Join(homePath, "token")) + token, err := os.ReadFile(path.Join(homePath, "token")) if err != nil { return nil, nil, err } @@ -111,12 +110,12 @@ func NewMarketClientNode(cctx *cli.Context) (clientapi.IMarketClient, jsonrpc.Cl if err != nil { return nil, nil, err } - apiUrl, err := ioutil.ReadFile(path.Join(homePath, "api")) + apiUrl, err := os.ReadFile(path.Join(homePath, "api")) if err != nil { return nil, nil, err } - token, err := ioutil.ReadFile(path.Join(homePath, "token")) + token, err := os.ReadFile(path.Join(homePath, "token")) if err != nil { return nil, nil, err } diff --git a/client/client_test.go b/client/client_test.go index aa12a2fb..7a2839d0 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "embed" - "io/ioutil" + "os" "path/filepath" "strings" "testing" @@ -73,7 +73,7 @@ func TestImportLocal(t *testing.T) { }) require.NoError(t, err) - outBytes, err := ioutil.ReadFile(out1) + outBytes, err := os.ReadFile(out1) require.NoError(t, err) require.Equal(t, b, outBytes) @@ -124,7 +124,7 @@ func TestImportLocal(t *testing.T) { err = files.WriteTo(file, exportedPath) require.NoError(t, err) - exportedBytes, err := ioutil.ReadFile(exportedPath) + exportedBytes, err := os.ReadFile(exportedPath) require.NoError(t, err) // compare original file to recreated unixfs file. diff --git a/client/import.go b/client/import.go index cb18ecf2..d3a27d3a 100644 --- a/client/import.go +++ b/client/import.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "github.com/filecoin-project/go-fil-markets/stores" @@ -62,7 +61,7 @@ func (a *API) createUnixFSFilestore(ctx context.Context, srcPath string, dstPath return cid.Undef, fmt.Errorf("failed to create reader path file: %w", err) } - f, err := ioutil.TempFile("", "") + f, err := os.CreateTemp("", "") if err != nil { return cid.Undef, fmt.Errorf("failed to create temp file: %w", err) } diff --git a/client/import_test.go b/client/import_test.go index 9f41830b..410512de 100644 --- a/client/import_test.go +++ b/client/import_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "io" - "io/ioutil" "os" "strings" "testing" @@ -67,7 +66,7 @@ func TestRoundtripUnixFS_Dense(t *testing.T) { // ensure contents of the initial input file and the output file are identical. fo, err := os.Open(tmpOutput) require.NoError(t, err) - bz2, err := ioutil.ReadAll(fo) + bz2, err := io.ReadAll(fo) require.NoError(t, err) require.NoError(t, fo.Close()) require.Equal(t, inputContents, bz2) @@ -109,7 +108,7 @@ func TestRoundtripUnixFS_Filestore(t *testing.T) { // ensure contents of the initial input file and the output file are identical. fo, err := os.Open(tmpOutput) require.NoError(t, err) - bz2, err := ioutil.ReadAll(fo) + bz2, err := io.ReadAll(fo) require.NoError(t, err) require.NoError(t, fo.Close()) require.Equal(t, inputContents, bz2) diff --git a/config/save.go b/config/save.go index c74a4b5c..86057afe 100644 --- a/config/save.go +++ b/config/save.go @@ -2,7 +2,6 @@ package config import ( "bytes" - "io/ioutil" "os" "path" @@ -25,7 +24,7 @@ func SaveConfig(cfg IHome) error { } _ = os.MkdirAll(path.Dir(cfgPath), os.ModePerm) - return ioutil.WriteFile(cfgPath, buf.Bytes(), 0o644) + return os.WriteFile(cfgPath, buf.Bytes(), 0o644) } func LoadConfig(cfgPath string, cfg IHome) error { @@ -34,7 +33,7 @@ func LoadConfig(cfgPath string, cfg IHome) error { return err } - cfgBytes, err := ioutil.ReadFile(homeDir) + cfgBytes, err := os.ReadFile(homeDir) if err != nil { return err } diff --git a/dagstore/mount_test.go b/dagstore/mount_test.go index 20879f8c..670c27d3 100644 --- a/dagstore/mount_test.go +++ b/dagstore/mount_test.go @@ -3,7 +3,7 @@ package dagstore import ( "bytes" "context" - "io/ioutil" + "io" "net/url" "testing" @@ -44,7 +44,7 @@ func TestLotusMount(t *testing.T) { rd, err := mnt.Fetch(context.Background()) require.NoError(t, err) - bz, err := ioutil.ReadAll(rd) + bz, err := io.ReadAll(rd) require.NoError(t, err) require.NoError(t, rd.Close()) require.Equal(t, []byte("testing"), bz) @@ -63,7 +63,7 @@ func TestLotusMount(t *testing.T) { // fetching on this mount should get us back the same data. rd, err = mnt2.Fetch(context.Background()) require.NoError(t, err) - bz, err = ioutil.ReadAll(rd) + bz, err = io.ReadAll(rd) require.NoError(t, err) require.NoError(t, rd.Close()) require.Equal(t, []byte("testing"), bz) diff --git a/piecestorage/filestore.go b/piecestorage/filestore.go index 31916710..5f246960 100644 --- a/piecestorage/filestore.go +++ b/piecestorage/filestore.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "os" "path" @@ -54,7 +53,7 @@ func (f *fsPieceStorage) SaveTo(_ context.Context, resourceId string, r io.Reade } dstPath := path.Join(f.baseUrl, resourceId) - tempFile, err := ioutil.TempFile("", "piece-*") + tempFile, err := os.CreateTemp("", "piece-*") if err != nil { return 0, err } diff --git a/piecestorage/memstore.go b/piecestorage/memstore.go index b4ae6c01..4012f07b 100644 --- a/piecestorage/memstore.go +++ b/piecestorage/memstore.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "sync" "github.com/filecoin-project/venus/venus-shared/types/market" @@ -44,7 +43,7 @@ func (m *MemPieceStore) GetName() string { func (m *MemPieceStore) SaveTo(ctx context.Context, resourceId string, reader io.Reader) (int64, error) { m.dataLk.Lock() defer m.dataLk.Unlock() - bytes, err := ioutil.ReadAll(reader) + bytes, err := io.ReadAll(reader) if err != nil { return 0, err } diff --git a/rpc/piece_storage_server_test.go b/rpc/piece_storage_server_test.go index b96e5b9f..e9154c99 100644 --- a/rpc/piece_storage_server_test.go +++ b/rpc/piece_storage_server_test.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "testing" @@ -59,7 +59,7 @@ func TestResouceDownload(t *testing.T) { psm.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code) - result, err := ioutil.ReadAll(w.Body) + result, err := io.ReadAll(w.Body) assert.Nil(t, err) assert.Equal(t, "mock resource2 content", string(result)) }) diff --git a/rpc/rpc.go b/rpc/rpc.go index f4d0b61f..20259704 100644 --- a/rpc/rpc.go +++ b/rpc/rpc.go @@ -4,8 +4,8 @@ import ( "context" "encoding/hex" "fmt" - "io/ioutil" "net/http" + "os" "path" "regexp" @@ -140,7 +140,7 @@ func saveAPIInfo(home config.IHome, apiCfg *config.API, token []byte) error { if err != nil { return fmt.Errorf("unable to home path to save api/token") } - _ = ioutil.WriteFile(path.Join(string(homePath), "api"), []byte(apiCfg.ListenAddress), 0o644) - _ = ioutil.WriteFile(path.Join(string(homePath), "token"), token, 0o644) + _ = os.WriteFile(path.Join(string(homePath), "api"), []byte(apiCfg.ListenAddress), 0o644) + _ = os.WriteFile(path.Join(string(homePath), "token"), token, 0o644) return nil } diff --git a/utils/algn_zero_reader_test.go b/utils/algn_zero_reader_test.go index 3adbb250..691aab7e 100644 --- a/utils/algn_zero_reader_test.go +++ b/utils/algn_zero_reader_test.go @@ -3,7 +3,6 @@ package utils import ( "bytes" "io" - "io/ioutil" "testing" "github.com/stretchr/testify/assert" @@ -92,7 +91,7 @@ func TestReadALl(t *testing.T) { r := getPayloadReader(payloadSize) algnR := NewAlgnZeroMountReader(r, payloadSize, size) - p, err := ioutil.ReadAll(algnR) + p, err := io.ReadAll(algnR) assert.Nil(t, err) countOne := 0 for i := 0; i < len(p); i++ {