From abef90423d0a9b12fb3f4fb2313d2afd35a35d1e Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Fri, 9 Sep 2022 19:53:40 +0800 Subject: [PATCH] refactor: use `os.ReadDir` for lightweight directory reading `os.ReadDir` was added in Go 1.16 as part of the deprecation of `ioutil` package. It is a more efficient implementation than `ioutil.ReadDir`. Reference: https://pkg.go.dev/io/ioutil#ReadDir Signed-off-by: Eng Zer Jun --- lib/backupds/backupds_test.go | 3 ++- lib/backupds/log.go | 3 +-- storage/paths/local.go | 2 +- storage/sealer/ffiwrapper/sealer_test.go | 4 ++-- storage/sealer/tarutil/systar.go | 10 +++++++--- testplans/lotus-soup/sanity.go | 3 +-- 6 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/backupds/backupds_test.go b/lib/backupds/backupds_test.go index 48b2a8d25..b76799bfb 100644 --- a/lib/backupds/backupds_test.go +++ b/lib/backupds/backupds_test.go @@ -6,6 +6,7 @@ import ( "context" "fmt" "io/ioutil" + "os" "path/filepath" "strings" "testing" @@ -72,7 +73,7 @@ func TestLogRestore(t *testing.T) { require.NoError(t, bds.Close()) - fls, err := ioutil.ReadDir(logdir) + fls, err := os.ReadDir(logdir) require.NoError(t, err) require.Equal(t, 1, len(fls)) diff --git a/lib/backupds/log.go b/lib/backupds/log.go index eb1174b46..13ff7227f 100644 --- a/lib/backupds/log.go +++ b/lib/backupds/log.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "os" "path/filepath" "strconv" @@ -23,7 +22,7 @@ func (d *Datastore) startLog(logdir string) error { return xerrors.Errorf("mkdir logdir ('%s'): %w", logdir, err) } - files, err := ioutil.ReadDir(logdir) + files, err := os.ReadDir(logdir) if err != nil { return xerrors.Errorf("read logdir ('%s'): %w", logdir, err) } diff --git a/storage/paths/local.go b/storage/paths/local.go index 552b1738d..ec146ba5a 100644 --- a/storage/paths/local.go +++ b/storage/paths/local.go @@ -370,7 +370,7 @@ func (st *Local) declareSectors(ctx context.Context, p string, id storiface.ID, } for _, t := range storiface.PathTypes { - ents, err := ioutil.ReadDir(filepath.Join(p, t.String())) + ents, err := os.ReadDir(filepath.Join(p, t.String())) if err != nil { if os.IsNotExist(err) { if err := os.MkdirAll(filepath.Join(p, t.String()), 0755); err != nil { // nolint diff --git a/storage/sealer/ffiwrapper/sealer_test.go b/storage/sealer/ffiwrapper/sealer_test.go index fb6a2249c..71fd7c4a0 100644 --- a/storage/sealer/ffiwrapper/sealer_test.go +++ b/storage/sealer/ffiwrapper/sealer_test.go @@ -585,7 +585,7 @@ func BenchmarkWriteWithAlignment(b *testing.B) { } func openFDs(t *testing.T) int { - dent, err := ioutil.ReadDir("/proc/self/fd") + dent, err := os.ReadDir("/proc/self/fd") require.NoError(t, err) var skip int @@ -611,7 +611,7 @@ func requireFDsClosed(t *testing.T, start int) { openNow := openFDs(t) if start != openNow { - dent, err := ioutil.ReadDir("/proc/self/fd") + dent, err := os.ReadDir("/proc/self/fd") require.NoError(t, err) for _, info := range dent { diff --git a/storage/sealer/tarutil/systar.go b/storage/sealer/tarutil/systar.go index 2d7bc34a5..4cd2e2bbb 100644 --- a/storage/sealer/tarutil/systar.go +++ b/storage/sealer/tarutil/systar.go @@ -3,7 +3,6 @@ package tarutil import ( "archive/tar" "io" - "io/ioutil" "os" "path/filepath" @@ -52,13 +51,18 @@ func ExtractTar(body io.Reader, dir string, buf []byte) error { func TarDirectory(dir string, w io.Writer, buf []byte) error { tw := tar.NewWriter(w) - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { return err } for _, file := range files { - h, err := tar.FileInfoHeader(file, "") + info, err := file.Info() + if err != nil { + return xerrors.Errorf("getting file info for file %s: %w", file.Name(), err) + } + + h, err := tar.FileInfoHeader(info, "") if err != nil { return xerrors.Errorf("getting header for file %s: %w", file.Name(), err) } diff --git a/testplans/lotus-soup/sanity.go b/testplans/lotus-soup/sanity.go index b06a653c5..ca9799a50 100644 --- a/testplans/lotus-soup/sanity.go +++ b/testplans/lotus-soup/sanity.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "io/ioutil" "os" ) @@ -24,7 +23,7 @@ func sanityCheck() { panic(enhanceMsg("/var/tmp/filecoin-proof-parameters is not a directory; aborting")) } - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { panic(enhanceMsg("failed list directory /var/tmp/filecoin-proof-parameters: %s", err)) }