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

@@ -20,7 +20,6 @@ package config
import (
"context"
"crypto/tls"
"io/ioutil"
"net"
"net/http"
"net/url"
@@ -170,7 +169,7 @@ func ConfigureHosts(ctx context.Context, options HostOptions) docker.RegistryHos
tlsConfig.RootCAs = rootPool
}
for _, f := range host.caCerts {
data, err := ioutil.ReadFile(f)
data, err := os.ReadFile(f)
if err != nil {
return nil, errors.Wrapf(err, "unable to read CA cert %q", f)
}
@@ -182,13 +181,13 @@ func ConfigureHosts(ctx context.Context, options HostOptions) docker.RegistryHos
if host.clientPairs != nil {
for _, pair := range host.clientPairs {
certPEMBlock, err := ioutil.ReadFile(pair[0])
certPEMBlock, err := os.ReadFile(pair[0])
if err != nil {
return nil, errors.Wrapf(err, "unable to read CERT file %q", pair[0])
}
var keyPEMBlock []byte
if pair[1] != "" {
keyPEMBlock, err = ioutil.ReadFile(pair[1])
keyPEMBlock, err = os.ReadFile(pair[1])
if err != nil {
return nil, errors.Wrapf(err, "unable to read CERT file %q", pair[1])
}
@@ -251,7 +250,7 @@ func hostDirectory(host string) string {
}
func loadHostDir(ctx context.Context, hostsDir string) ([]hostConfig, error) {
b, err := ioutil.ReadFile(filepath.Join(hostsDir, "hosts.toml"))
b, err := os.ReadFile(filepath.Join(hostsDir, "hosts.toml"))
if err != nil && !os.IsNotExist(err) {
return nil, err
}
@@ -536,7 +535,7 @@ func makeAbsPath(p string, base string) string {
// the ".cert", which may contain the private key. If the ".cert" file
// does not contain the private key, the caller should detect and error.
func loadCertFiles(ctx context.Context, certsDir string) ([]hostConfig, error) {
fs, err := ioutil.ReadDir(certsDir)
fs, err := os.ReadDir(certsDir)
if err != nil && !os.IsNotExist(err) {
return nil, err
}

View File

@@ -20,7 +20,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
@@ -209,7 +208,7 @@ ca = "/etc/path/default"
}
func TestLoadCertFiles(t *testing.T) {
dir, err := ioutil.TempDir("", t.Name())
dir, err := os.MkdirTemp("", t.Name())
if err != nil {
t.Fatal(err)
}
@@ -257,16 +256,16 @@ func TestLoadCertFiles(t *testing.T) {
defer os.RemoveAll(hostDir)
for _, f := range tc.input.caCerts {
if err := ioutil.WriteFile(f, testKey, 0600); err != nil {
if err := os.WriteFile(f, testKey, 0600); err != nil {
t.Fatal(err)
}
}
for _, pair := range tc.input.clientPairs {
if err := ioutil.WriteFile(pair[0], testKey, 0600); err != nil {
if err := os.WriteFile(pair[0], testKey, 0600); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(pair[1], testKey, 0600); err != nil {
if err := os.WriteFile(pair[1], testKey, 0600); err != nil {
t.Fatal(err)
}
}

View File

@@ -21,7 +21,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
@@ -197,7 +196,7 @@ func (r dockerFetcher) open(ctx context.Context, req *request, mediatype string,
// Discard up to offset
// Could use buffer pool here but this case should be rare
n, err := io.Copy(ioutil.Discard, io.LimitReader(resp.Body, offset))
n, err := io.Copy(io.Discard, io.LimitReader(resp.Body, offset))
if err != nil {
return nil, errors.Wrap(err, "failed to discard to offset")
}

View File

@@ -21,7 +21,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"net/http/httptest"
@@ -73,7 +72,7 @@ func TestFetcherOpen(t *testing.T) {
if err != nil {
t.Fatalf("failed to open: %+v", err)
}
b, err := ioutil.ReadAll(rc)
b, err := io.ReadAll(rc)
if err != nil {
t.Fatal(err)
}

View File

@@ -19,7 +19,6 @@ package docker
import (
"bytes"
"io"
"io/ioutil"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/log"
@@ -162,7 +161,7 @@ func (hrs *httpReadSeeker) reader() (io.Reader, error) {
// as the length is already satisfied but we just return the empty
// reader instead.
hrs.rc = ioutil.NopCloser(bytes.NewReader([]byte{}))
hrs.rc = io.NopCloser(bytes.NewReader([]byte{}))
}
return hrs.rc, nil

View File

@@ -19,7 +19,6 @@ package docker
import (
"context"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
@@ -263,7 +262,7 @@ func (p dockerPusher) push(ctx context.Context, desc ocispec.Descriptor, ref str
pr, pw := io.Pipe()
respC := make(chan response, 1)
body := ioutil.NopCloser(pr)
body := io.NopCloser(pr)
req.body = func() (io.ReadCloser, error) {
if body == nil {

View File

@@ -20,7 +20,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
@@ -359,7 +358,7 @@ func (r *dockerResolver) Resolve(ctx context.Context, ref string) (string, ocisp
return "", ocispec.Descriptor{}, err
}
}
} else if _, err := io.Copy(ioutil.Discard, &bodyReader); err != nil {
} else if _, err := io.Copy(io.Discard, &bodyReader); err != nil {
return "", ocispec.Descriptor{}, err
}
size = bodyReader.bytesRead

View File

@@ -23,7 +23,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strconv"
@@ -574,7 +573,7 @@ func testocimanifest(ctx context.Context, f remotes.Fetcher, desc ocispec.Descri
if err != nil {
return nil, errors.Wrapf(err, "failed to fetch %s", desc.Digest)
}
p, err := ioutil.ReadAll(r)
p, err := io.ReadAll(r)
if err != nil {
return nil, err
}

View File

@@ -23,7 +23,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"strconv"
"strings"
"sync"
@@ -230,7 +229,7 @@ func (c *Converter) Convert(ctx context.Context, opts ...ConvertOpt) (ocispec.De
// ReadStripSignature reads in a schema1 manifest and returns a byte array
// with the "signatures" field stripped
func ReadStripSignature(schema1Blob io.Reader) ([]byte, error) {
b, err := ioutil.ReadAll(io.LimitReader(schema1Blob, manifestSizeLimit)) // limit to 8MB
b, err := io.ReadAll(io.LimitReader(schema1Blob, manifestSizeLimit)) // limit to 8MB
if err != nil {
return nil, err
}