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 <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun 2022-09-09 19:53:40 +08:00
parent 0594e33ed7
commit abef90423d
No known key found for this signature in database
GPG Key ID: DAEBBD2E34C111E6
6 changed files with 14 additions and 11 deletions

View File

@ -6,6 +6,7 @@ import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
@ -72,7 +73,7 @@ func TestLogRestore(t *testing.T) {
require.NoError(t, bds.Close()) require.NoError(t, bds.Close())
fls, err := ioutil.ReadDir(logdir) fls, err := os.ReadDir(logdir)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(fls)) require.Equal(t, 1, len(fls))

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -23,7 +22,7 @@ func (d *Datastore) startLog(logdir string) error {
return xerrors.Errorf("mkdir logdir ('%s'): %w", logdir, err) return xerrors.Errorf("mkdir logdir ('%s'): %w", logdir, err)
} }
files, err := ioutil.ReadDir(logdir) files, err := os.ReadDir(logdir)
if err != nil { if err != nil {
return xerrors.Errorf("read logdir ('%s'): %w", logdir, err) return xerrors.Errorf("read logdir ('%s'): %w", logdir, err)
} }

View File

@ -370,7 +370,7 @@ func (st *Local) declareSectors(ctx context.Context, p string, id storiface.ID,
} }
for _, t := range storiface.PathTypes { 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 err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
if err := os.MkdirAll(filepath.Join(p, t.String()), 0755); err != nil { // nolint if err := os.MkdirAll(filepath.Join(p, t.String()), 0755); err != nil { // nolint

View File

@ -585,7 +585,7 @@ func BenchmarkWriteWithAlignment(b *testing.B) {
} }
func openFDs(t *testing.T) int { func openFDs(t *testing.T) int {
dent, err := ioutil.ReadDir("/proc/self/fd") dent, err := os.ReadDir("/proc/self/fd")
require.NoError(t, err) require.NoError(t, err)
var skip int var skip int
@ -611,7 +611,7 @@ func requireFDsClosed(t *testing.T, start int) {
openNow := openFDs(t) openNow := openFDs(t)
if start != openNow { if start != openNow {
dent, err := ioutil.ReadDir("/proc/self/fd") dent, err := os.ReadDir("/proc/self/fd")
require.NoError(t, err) require.NoError(t, err)
for _, info := range dent { for _, info := range dent {

View File

@ -3,7 +3,6 @@ package tarutil
import ( import (
"archive/tar" "archive/tar"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "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 { func TarDirectory(dir string, w io.Writer, buf []byte) error {
tw := tar.NewWriter(w) tw := tar.NewWriter(w)
files, err := ioutil.ReadDir(dir) files, err := os.ReadDir(dir)
if err != nil { if err != nil {
return err return err
} }
for _, file := range files { 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 { if err != nil {
return xerrors.Errorf("getting header for file %s: %w", file.Name(), err) return xerrors.Errorf("getting header for file %s: %w", file.Name(), err)
} }

View File

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
) )
@ -24,7 +23,7 @@ func sanityCheck() {
panic(enhanceMsg("/var/tmp/filecoin-proof-parameters is not a directory; aborting")) 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 { if err != nil {
panic(enhanceMsg("failed list directory /var/tmp/filecoin-proof-parameters: %s", err)) panic(enhanceMsg("failed list directory /var/tmp/filecoin-proof-parameters: %s", err))
} }