Merge pull request #4063 from MichaelEischer/replace-ioutil-usage

Replace ioutil usage
This commit is contained in:
Michael Eischer
2022-12-02 21:49:40 +01:00
committed by GitHub
59 changed files with 122 additions and 165 deletions

View File

@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"sort"
"strings"
"testing"
@@ -109,7 +108,7 @@ func TestDry(t *testing.T) {
case "load":
data := ""
err = step.be.Load(ctx, handle, 100, 0, func(rd io.Reader) error {
buf, err := ioutil.ReadAll(rd)
buf, err := io.ReadAll(rd)
data = string(buf)
return err
})

View File

@@ -4,9 +4,9 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"net"
"net/http"
"os"
"strings"
"time"
@@ -30,7 +30,7 @@ type TransportOptions struct {
// readPEMCertKey reads a file and returns the PEM encoded certificate and key
// blocks.
func readPEMCertKey(filename string) (certs []byte, key []byte, err error) {
data, err := ioutil.ReadFile(filename)
data, err := os.ReadFile(filename)
if err != nil {
return nil, nil, errors.Wrap(err, "ReadFile")
}
@@ -105,7 +105,7 @@ func Transport(opts TransportOptions) (http.RoundTripper, error) {
if filename == "" {
return nil, errors.Errorf("empty filename for root certificate supplied")
}
b, err := ioutil.ReadFile(filename)
b, err := os.ReadFile(filename)
if err != nil {
return nil, errors.Errorf("unable to read root certificate: %v", err)
}

View File

@@ -4,7 +4,6 @@ import (
"context"
"hash"
"io"
"io/ioutil"
"os"
"path/filepath"
"syscall"
@@ -209,7 +208,7 @@ func (b *Local) Save(ctx context.Context, h restic.Handle, rd restic.RewindReade
return nil
}
var tempFile = ioutil.TempFile // Overridden by test.
var tempFile = os.CreateTemp // Overridden by test.
// Load runs fn with a reader that yields the contents of the file at h at the
// given offset.

View File

@@ -2,7 +2,6 @@ package local_test
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -17,7 +16,7 @@ func newTestSuite(t testing.TB) *test.Suite {
return &test.Suite{
// NewConfig returns a config for a new temporary backend that will be used in tests.
NewConfig: func() (interface{}, error) {
dir, err := ioutil.TempDir(rtest.TestTempDir, "restic-test-local-")
dir, err := os.MkdirTemp(rtest.TestTempDir, "restic-test-local-")
if err != nil {
t.Fatal(err)
}

View File

@@ -6,7 +6,6 @@ import (
"encoding/base64"
"hash"
"io"
"io/ioutil"
"sync"
"github.com/cespare/xxhash/v2"
@@ -96,7 +95,7 @@ func (be *MemoryBackend) Save(ctx context.Context, h restic.Handle, rd restic.Re
return errors.New("file already exists")
}
buf, err := ioutil.ReadAll(rd)
buf, err := io.ReadAll(rd)
if err != nil {
return err
}
@@ -168,7 +167,7 @@ func (be *MemoryBackend) openReader(ctx context.Context, h restic.Handle, length
buf = buf[:length]
}
return be.sem.ReleaseTokenOnClose(ioutil.NopCloser(bytes.NewReader(buf)), nil), ctx.Err()
return be.sem.ReleaseTokenOnClose(io.NopCloser(bytes.NewReader(buf)), nil), ctx.Err()
}
// Stat returns information about a file in the backend.

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"hash"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
@@ -89,7 +88,7 @@ func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (*Backend, er
return nil, errors.Fatalf("server response unexpected: %v (%v)", resp.Status, resp.StatusCode)
}
_, err = io.Copy(ioutil.Discard, resp.Body)
_, err = io.Copy(io.Discard, resp.Body)
if err != nil {
return nil, err
}
@@ -133,7 +132,7 @@ func (b *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindRea
// make sure that client.Post() cannot close the reader by wrapping it
req, err := http.NewRequestWithContext(ctx,
http.MethodPost, b.Filename(h), ioutil.NopCloser(rd))
http.MethodPost, b.Filename(h), io.NopCloser(rd))
if err != nil {
return errors.WithStack(err)
}
@@ -150,7 +149,7 @@ func (b *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindRea
var cerr error
if resp != nil {
_, _ = io.Copy(ioutil.Discard, resp.Body)
_, _ = io.Copy(io.Discard, resp.Body)
cerr = resp.Body.Close()
}
@@ -245,7 +244,7 @@ func (b *Backend) openReader(ctx context.Context, h restic.Handle, length int, o
if err != nil {
if resp != nil {
_, _ = io.Copy(ioutil.Discard, resp.Body)
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
}
return nil, errors.Wrap(err, "client.Do")
@@ -283,7 +282,7 @@ func (b *Backend) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, e
return restic.FileInfo{}, errors.WithStack(err)
}
_, _ = io.Copy(ioutil.Discard, resp.Body)
_, _ = io.Copy(io.Discard, resp.Body)
if err = resp.Body.Close(); err != nil {
return restic.FileInfo{}, errors.Wrap(err, "Close")
}
@@ -348,7 +347,7 @@ func (b *Backend) Remove(ctx context.Context, h restic.Handle) error {
return errors.Errorf("blob not removed, server response: %v (%v)", resp.Status, resp.StatusCode)
}
_, err = io.Copy(ioutil.Discard, resp.Body)
_, err = io.Copy(io.Discard, resp.Body)
if err != nil {
return errors.Wrap(err, "Copy")
}

View File

@@ -4,7 +4,6 @@ import (
"bytes"
"context"
"io"
"io/ioutil"
"testing"
"time"
@@ -22,7 +21,7 @@ func TestBackendSaveRetry(t *testing.T) {
SaveFn: func(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
if errcount == 0 {
errcount++
_, err := io.CopyN(ioutil.Discard, rd, 120)
_, err := io.CopyN(io.Discard, rd, 120)
if err != nil {
return err
}
@@ -267,7 +266,7 @@ func TestBackendLoadRetry(t *testing.T) {
var buf []byte
err := retryBackend.Load(context.TODO(), restic.Handle{}, 0, 0, func(rd io.Reader) (err error) {
buf, err = ioutil.ReadAll(rd)
buf, err = io.ReadAll(rd)
return err
})
test.OK(t, err)

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"hash"
"io"
"io/ioutil"
"net/http"
"os"
"path"
@@ -296,7 +295,7 @@ func (be *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindRe
opts.PartSize = 200 * 1024 * 1024
debug.Log("PutObject(%v, %v, %v)", be.cfg.Bucket, objName, rd.Length())
info, err := be.client.PutObject(ctx, be.cfg.Bucket, objName, ioutil.NopCloser(rd), int64(rd.Length()), opts)
info, err := be.client.PutObject(ctx, be.cfg.Bucket, objName, io.NopCloser(rd), int64(rd.Length()), opts)
debug.Log("%v -> %v bytes, err %#v: %v", objName, info.Size, err, err)

View File

@@ -3,7 +3,6 @@ package sftp_test
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@@ -34,7 +33,7 @@ func newTestSuite(t testing.TB) *test.Suite {
return &test.Suite{
// NewConfig returns a config for a new temporary backend that will be used in tests.
NewConfig: func() (interface{}, error) {
dir, err := ioutil.TempDir(rtest.TestTempDir, "restic-test-sftp-")
dir, err := os.MkdirTemp(rtest.TestTempDir, "restic-test-sftp-")
if err != nil {
t.Fatal(err)
}

View File

@@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"reflect"
@@ -148,7 +147,7 @@ func (s *Suite) TestLoad(t *testing.T) {
}
err = b.Load(context.TODO(), handle, 0, 0, func(rd io.Reader) error {
_, err := io.Copy(ioutil.Discard, rd)
_, err := io.Copy(io.Discard, rd)
if err != nil {
t.Fatal(err)
}
@@ -189,7 +188,7 @@ func (s *Suite) TestLoad(t *testing.T) {
var buf []byte
err := b.Load(context.TODO(), handle, getlen, int64(o), func(rd io.Reader) (ierr error) {
buf, ierr = ioutil.ReadAll(rd)
buf, ierr = io.ReadAll(rd)
return ierr
})
if err != nil {
@@ -522,7 +521,7 @@ func (s *Suite) TestSave(t *testing.T) {
}
// test saving from a tempfile
tmpfile, err := ioutil.TempFile("", "restic-backend-save-test-")
tmpfile, err := os.CreateTemp("", "restic-backend-save-test-")
if err != nil {
t.Fatal(err)
}
@@ -678,7 +677,7 @@ func store(t testing.TB, b restic.Backend, tpe restic.FileType, data []byte) res
// testLoad loads a blob (but discards its contents).
func testLoad(b restic.Backend, h restic.Handle, length int, offset int64) error {
return b.Load(context.TODO(), h, 0, 0, func(rd io.Reader) (ierr error) {
_, ierr = io.Copy(ioutil.Discard, rd)
_, ierr = io.Copy(io.Discard, rd)
return ierr
})
}

View File

@@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"math/rand"
"testing"
@@ -79,7 +78,7 @@ func TestLoadAllBroken(t *testing.T) {
data[0] ^= 0xff
b.OpenReaderFn = func(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(data)), nil
return io.NopCloser(bytes.NewReader(data)), nil
}
// must fail on first try