refactor: move from io/ioutil to io and os package

The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun
2021-09-10 20:28:11 +08:00
parent c16be1a5e2
commit 50da673592
126 changed files with 291 additions and 399 deletions

View File

@@ -19,7 +19,6 @@ package content
import (
"context"
"io"
"io/ioutil"
"math/rand"
"sync"
"time"
@@ -230,7 +229,7 @@ func seekReader(r io.Reader, offset, size int64) (io.Reader, error) {
}
// well then, let's just discard up to the offset
n, err := copyWithBuffer(ioutil.Discard, io.LimitReader(r, offset))
n, err := copyWithBuffer(io.Discard, io.LimitReader(r, offset))
if err != nil {
return nil, errors.Wrap(err, "failed to discard to offset")
}

View File

@@ -20,7 +20,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
@@ -568,7 +567,7 @@ func (s *store) writer(ctx context.Context, ref string, total int64, expected di
// the ingest is new, we need to setup the target location.
// write the ref to a file for later use
if err := ioutil.WriteFile(refp, []byte(ref), 0666); err != nil {
if err := os.WriteFile(refp, []byte(ref), 0666); err != nil {
return nil, err
}
@@ -581,7 +580,7 @@ func (s *store) writer(ctx context.Context, ref string, total int64, expected di
}
if total > 0 {
if err := ioutil.WriteFile(filepath.Join(path, "total"), []byte(fmt.Sprint(total)), 0666); err != nil {
if err := os.WriteFile(filepath.Join(path, "total"), []byte(fmt.Sprint(total)), 0666); err != nil {
return nil, err
}
}
@@ -656,13 +655,13 @@ func (s *store) ingestPaths(ref string) (string, string, string) {
}
func readFileString(path string) (string, error) {
p, err := ioutil.ReadFile(path)
p, err := os.ReadFile(path)
return string(p), err
}
// readFileTimestamp reads a file with just a timestamp present.
func readFileTimestamp(p string) (time.Time, error) {
b, err := ioutil.ReadFile(p)
b, err := os.ReadFile(p)
if err != nil {
if os.IsNotExist(err) {
err = errors.Wrap(errdefs.ErrNotFound, err.Error())

View File

@@ -23,7 +23,6 @@ import (
_ "crypto/sha256" // required for digest package
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
@@ -158,7 +157,7 @@ func TestContentWriter(t *testing.T) {
}
expected := digest.FromBytes(p)
checkCopy(t, int64(len(p)), cw, bufio.NewReader(ioutil.NopCloser(bytes.NewReader(p))))
checkCopy(t, int64(len(p)), cw, bufio.NewReader(io.NopCloser(bytes.NewReader(p))))
if err := cw.Commit(ctx, int64(len(p)), expected); err != nil {
t.Fatal(err)
@@ -174,7 +173,7 @@ func TestContentWriter(t *testing.T) {
}
// now, attempt to write the same data again
checkCopy(t, int64(len(p)), cw, bufio.NewReader(ioutil.NopCloser(bytes.NewReader(p))))
checkCopy(t, int64(len(p)), cw, bufio.NewReader(io.NopCloser(bytes.NewReader(p))))
if err := cw.Commit(ctx, int64(len(p)), expected); err == nil {
t.Fatal("expected already exists error")
} else if !errdefs.IsAlreadyExists(err) {
@@ -184,7 +183,7 @@ func TestContentWriter(t *testing.T) {
path := checkBlobPath(t, cs, expected)
// read the data back, make sure its the same
pp, err := ioutil.ReadFile(path)
pp, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
@@ -299,7 +298,7 @@ func contentStoreEnv(t checker) (context.Context, string, content.Store, func())
}
fn := runtime.FuncForPC(pc)
tmpdir, err := ioutil.TempDir("", filepath.Base(fn.Name())+"-")
tmpdir, err := os.MkdirTemp("", filepath.Base(fn.Name())+"-")
if err != nil {
t.Fatal(err)
}
@@ -362,7 +361,7 @@ func checkWrite(ctx context.Context, t checker, cs content.Store, dgst digest.Di
}
func TestWriterTruncateRecoversFromIncompleteWrite(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test-local-content-store-recover")
tmpdir, err := os.MkdirTemp("", "test-local-content-store-recover")
assert.NilError(t, err)
defer os.RemoveAll(tmpdir)
@@ -401,7 +400,7 @@ func setupIncompleteWrite(ctx context.Context, t *testing.T, cs content.Store, r
}
func TestWriteReadEmptyFileTimestamp(t *testing.T) {
root, err := ioutil.TempDir("", "test-write-read-file-timestamp")
root, err := os.MkdirTemp("", "test-write-read-file-timestamp")
if err != nil {
t.Errorf("failed to create a tmp dir: %v", err)
}

View File

@@ -21,7 +21,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"runtime"
@@ -104,7 +103,7 @@ func makeTest(t *testing.T, name string, storeFn func(ctx context.Context, root
ctx := context.WithValue(context.Background(), nameKey{}, name)
ctx = logtest.WithT(ctx, t)
tmpDir, err := ioutil.TempDir("", "content-suite-"+name+"-")
tmpDir, err := os.MkdirTemp("", "content-suite-"+name+"-")
if err != nil {
t.Fatal(err)
}
@@ -782,7 +781,7 @@ func checkSmallBlob(ctx context.Context, t *testing.T, store content.Store) {
}
defer ra.Close()
r := io.NewSectionReader(ra, 0, readSize)
b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil {
t.Fatal(err)
}
@@ -1080,7 +1079,7 @@ func createContent(size int64) ([]byte, digest.Digest) {
// test runs. An atomic integer works just good enough for this.
seed := atomic.AddInt64(&contentSeed, 1)
b, err := ioutil.ReadAll(io.LimitReader(rand.New(rand.NewSource(seed)), size))
b, err := io.ReadAll(io.LimitReader(rand.New(rand.NewSource(seed)), size))
if err != nil {
panic(err)
}