Replace most usages of ioutil with the underlying function

The ioutil functions are deprecated since Go 1.17 and only wrap another
library function. Thus directly call the underlying function.

This commit only mechanically replaces the function calls.
This commit is contained in:
Michael Eischer
2022-12-02 19:36:43 +01:00
parent 2d5e28e777
commit ff7ef5007e
57 changed files with 119 additions and 159 deletions
+1 -2
View File
@@ -6,7 +6,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
@@ -185,7 +184,7 @@ func readLines(filename string) ([]string, error) {
)
if filename == "-" {
data, err = ioutil.ReadAll(os.Stdin)
data, err = io.ReadAll(os.Stdin)
} else {
data, err = textfile.Read(filename)
}
+2 -2
View File
@@ -2,8 +2,8 @@ package main
import (
"context"
"io/ioutil"
"math/rand"
"os"
"strconv"
"strings"
"sync"
@@ -171,7 +171,7 @@ func prepareCheckCache(opts CheckOptions, gopts *GlobalOptions) (cleanup func())
}
// use a cache in a temporary directory
tempdir, err := ioutil.TempDir(cachedir, "restic-check-cache-")
tempdir, err := os.MkdirTemp(cachedir, "restic-check-cache-")
if err != nil {
// if an error occurs, don't use any cache
Warnf("unable to create temporary directory for cache during check, disabling cache: %v\n", err)
+1 -2
View File
@@ -3,7 +3,6 @@ package main
import (
"context"
"encoding/json"
"io/ioutil"
"os"
"strings"
"sync"
@@ -255,7 +254,7 @@ func runKey(ctx context.Context, gopts GlobalOptions, args []string) error {
}
func loadPasswordFromFile(pwdFile string) (string, error) {
s, err := ioutil.ReadFile(pwdFile)
s, err := os.ReadFile(pwdFile)
if os.IsNotExist(err) {
return "", errors.Fatalf("%s does not exist", pwdFile)
}
+3 -4
View File
@@ -1,7 +1,6 @@
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -89,13 +88,13 @@ func TestIsExcludedByFile(t *testing.T) {
defer cleanup()
foo := filepath.Join(tempDir, "foo")
err := ioutil.WriteFile(foo, []byte("foo"), 0666)
err := os.WriteFile(foo, []byte("foo"), 0666)
if err != nil {
t.Fatalf("could not write file: %v", err)
}
if tc.tagFile != "" {
tagFile := filepath.Join(tempDir, tc.tagFile)
err = ioutil.WriteFile(tagFile, []byte(tc.content), 0666)
err = os.WriteFile(tagFile, []byte(tc.content), 0666)
if err != nil {
t.Fatalf("could not write tagfile: %v", err)
}
@@ -150,7 +149,7 @@ func TestMultipleIsExcludedByFile(t *testing.T) {
// create directories first, then the file
p := filepath.Join(tempDir, filepath.FromSlash(f.path))
errs = append(errs, os.MkdirAll(filepath.Dir(p), 0700))
errs = append(errs, ioutil.WriteFile(p, []byte(f.path), 0600))
errs = append(errs, os.WriteFile(p, []byte(f.path), 0600))
}
test.OKs(t, errs) // see if anything went wrong during the creation
+2 -2
View File
@@ -1,7 +1,7 @@
package main
import (
"io/ioutil"
"io"
"testing"
)
@@ -10,7 +10,7 @@ import (
func TestFlags(t *testing.T) {
for _, cmd := range cmdRoot.Commands() {
t.Run(cmd.Name(), func(t *testing.T) {
cmd.Flags().SetOutput(ioutil.Discard)
cmd.Flags().SetOutput(io.Discard)
err := cmd.ParseFlags([]string{"--help"})
if err.Error() == "pflag: help requested" {
err = nil
+2 -2
View File
@@ -2,7 +2,7 @@ package main
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -43,7 +43,7 @@ func TestReadRepo(t *testing.T) {
// test --repository-file option
foo := filepath.Join(tempDir, "foo")
err = ioutil.WriteFile(foo, []byte(tempDir+"\n"), 0666)
err = os.WriteFile(foo, []byte(tempDir+"\n"), 0666)
rtest.OK(t, err)
var opts2 GlobalOptions
@@ -1,7 +1,7 @@
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -39,7 +39,7 @@ func TestBackupFailsWhenUsingInvalidPatternsFromFile(t *testing.T) {
// Create an exclude file with some invalid patterns
excludeFile := env.base + "/excludefile"
fileErr := ioutil.WriteFile(excludeFile, []byte("*.go\n*[._]log[.-][0-9]\n!*[._]log[.-][0-9]"), 0644)
fileErr := os.WriteFile(excludeFile, []byte("*.go\n*[._]log[.-][0-9]\n!*[._]log[.-][0-9]"), 0644)
if fileErr != nil {
t.Fatalf("Could not write exclude file: %v", fileErr)
}
+1 -2
View File
@@ -3,7 +3,6 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@@ -174,7 +173,7 @@ func withTestEnvironment(t testing.TB) (env *testEnvironment, cleanup func()) {
restic.TestDisableCheckPolynomial(t)
retry.TestFastRetries(t)
tempdir, err := ioutil.TempDir(rtest.TestTempDir, "restic-test-")
tempdir, err := os.MkdirTemp(rtest.TestTempDir, "restic-test-")
rtest.OK(t, err)
env = &testEnvironment{
+7 -8
View File
@@ -8,7 +8,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
mrand "math/rand"
"os"
"path/filepath"
@@ -65,7 +64,7 @@ func testRunBackupAssumeFailure(t testing.TB, dir string, target []string, opts
term := termstatus.New(gopts.stdout, gopts.stderr, gopts.Quiet)
wg.Go(func() error { term.Run(ctx); return nil })
gopts.stdout = ioutil.Discard
gopts.stdout = io.Discard
t.Logf("backing up %v in %v", target, dir)
if dir != "" {
cleanup := rtest.Chdir(t, dir)
@@ -183,7 +182,7 @@ func testRunDiffOutput(gopts GlobalOptions, firstSnapshotID string, secondSnapsh
}
func testRunRebuildIndex(t testing.TB, gopts GlobalOptions) {
globalOptions.stdout = ioutil.Discard
globalOptions.stdout = io.Discard
defer func() {
globalOptions.stdout = os.Stdout
}()
@@ -419,7 +418,7 @@ func TestBackupNonExistingFile(t *testing.T) {
defer cleanup()
testSetupBackupData(t, env)
globalOptions.stderr = ioutil.Discard
globalOptions.stderr = io.Discard
defer func() {
globalOptions.stderr = os.Stderr
}()
@@ -641,7 +640,7 @@ func TestBackupErrors(t *testing.T) {
}()
opts := BackupOptions{}
gopts := env.gopts
gopts.stderr = ioutil.Discard
gopts.stderr = io.Discard
err := testRunBackupAssumeFailure(t, filepath.Dir(env.testdata), []string{"testdata"}, opts, gopts)
rtest.Assert(t, err != nil, "Assumed failure, but no error occurred.")
rtest.Assert(t, err == ErrInvalidSourceData, "Wrong error returned")
@@ -1243,7 +1242,7 @@ func TestRestoreLatest(t *testing.T) {
opts := BackupOptions{}
// chdir manually here so we can get the current directory. This is not the
// same as the temp dir returned by ioutil.TempDir() on darwin.
// same as the temp dir returned by os.MkdirTemp() on darwin.
back := rtest.Chdir(t, filepath.Dir(env.testdata))
defer back()
@@ -1308,7 +1307,7 @@ func TestRestoreWithPermissionFailure(t *testing.T) {
rtest.Assert(t, len(snapshots) > 0,
"no snapshots found in repo (%v)", datafile)
globalOptions.stderr = ioutil.Discard
globalOptions.stderr = io.Discard
defer func() {
globalOptions.stderr = os.Stderr
}()
@@ -1542,7 +1541,7 @@ func TestRebuildIndexFailsOnAppendOnly(t *testing.T) {
datafile := filepath.Join("..", "..", "internal", "checker", "testdata", "duplicate-packs-in-index-test-repo.tar.gz")
rtest.SetupTarTestFixture(t, env.base, datafile)
globalOptions.stdout = ioutil.Discard
globalOptions.stdout = io.Discard
defer func() {
globalOptions.stdout = os.Stdout
}()
+2 -2
View File
@@ -1,7 +1,7 @@
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -167,7 +167,7 @@ func TestFillSecondaryGlobalOpts(t *testing.T) {
defer cleanup()
//Create temporary password file
err := ioutil.WriteFile(filepath.Join(dir, "passwordFileDst"), []byte("secretDst"), 0666)
err := os.WriteFile(filepath.Join(dir, "passwordFileDst"), []byte("secretDst"), 0666)
rtest.OK(t, err)
// Test all valid cases