Skip to content

Commit

Permalink
ioutil: remove most references to ioutil
Browse files Browse the repository at this point in the history
This change removes most references to `ioutil` and replaces them with
equivalent methods in `io` and `os`. All of the changes in this PR were
made by the attached script. The script was used to migrate the
cockroach cloud repository.

The script does not migrate `ioutil.ReadDir` to os.ReadDir. os.ReadDir has a
different type and is not a drop in replacement.

```bash
function rewrite {
    fastmod -F "${1?}" "${2?}" -g '!vendor/' -g '*go'
}

rewrite ioutil.ReadAll io.ReadAll
rewrite ioutil.ReadFile os.ReadFile
rewrite ioutil.TempDir os.MkdirTemp
rewrite ioutil.TempFile os.CreateTemp
rewrite ioutil.WriteFile os.WriteFile

goimports -w .
```

Release justification: required by go1.19 linter
Release note: None
  • Loading branch information
jeffswenson authored and yuzefovich committed Aug 25, 2022
1 parent ec73c69 commit 1af6635
Show file tree
Hide file tree
Showing 193 changed files with 429 additions and 524 deletions.
5 changes: 2 additions & 3 deletions build/bazelutil/unused_checker/unused_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -74,7 +73,7 @@ func impl() error {
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("%w (got output %s while processing file %s)", err, string(output), nogoX)
}
encoded, err := ioutil.ReadFile(filepath.Join(tmpdir, "unused.out"))
encoded, err := os.ReadFile(filepath.Join(tmpdir, "unused.out"))
if err != nil {
return err
}
Expand All @@ -92,7 +91,7 @@ func impl() error {
var failures []string
for pkgPath, objs := range unused {
usedPkgMap := used[pkgPath]
for obj, _ := range objs {
for obj := range objs {
_, ok := usedPkgMap[obj]
if !ok {
failure := fmt.Sprintf("%s.%s", pkgPath, obj)
Expand Down
2 changes: 1 addition & 1 deletion pkg/acceptance/cluster/dockercluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func CreateDocker(
clusterIDS := clusterID.Short()

if volumesDir == "" {
volumesDir, err = ioutil.TempDir("", fmt.Sprintf("cockroach-acceptance-%s", clusterIDS))
volumesDir, err = os.MkdirTemp("", fmt.Sprintf("cockroach-acceptance-%s", clusterIDS))
maybePanic(err)
} else {
volumesDir = filepath.Join(volumesDir, clusterIDS)
Expand Down
5 changes: 2 additions & 3 deletions pkg/acceptance/localcluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"fmt"
"go/build"
"io"
"io/ioutil"
"math/rand"
"net"
"net/url"
Expand Down Expand Up @@ -664,7 +663,7 @@ func (n *Node) httpAddrFile() string {
}

func readFileOrEmpty(f string) string {
c, err := ioutil.ReadFile(f)
c, err := os.ReadFile(f)
if err != nil {
if !oserror.IsNotExist(err) {
panic(err)
Expand Down Expand Up @@ -710,7 +709,7 @@ func (n *Node) waitUntilLive(dur time.Duration) error {
return nil
}

urlBytes, err := ioutil.ReadFile(n.listeningURLFile())
urlBytes, err := os.ReadFile(n.listeningURLFile())
if err != nil {
log.Infof(ctx, "%v", err)
continue
Expand Down
9 changes: 4 additions & 5 deletions pkg/acceptance/util_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package acceptance
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -90,7 +89,7 @@ func testDocker(
}
testdataDir := filepath.Join(pwd, "testdata")
if bazel.BuiltWithBazel() {
testdataDir, err = ioutil.TempDir("", "")
testdataDir, err = os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
Expand All @@ -108,7 +107,7 @@ func testDocker(
Binds: []string{testdataDir + ":/mnt/data"},
}
if bazel.BuiltWithBazel() {
interactivetestsDir, err := ioutil.TempDir("", "")
interactivetestsDir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -149,11 +148,11 @@ func copyRunfiles(source, destination string) error {
if dirEntry.IsDir() {
return os.Mkdir(filepath.Join(destination, relPath), 0755)
}
data, err := ioutil.ReadFile(filepath.Join(source, relPath))
data, err := os.ReadFile(filepath.Join(source, relPath))
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(destination, relPath), data, 0755)
return os.WriteFile(filepath.Join(destination, relPath), data, 0755)
})
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/base/store_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ package base
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"regexp"
"sort"
Expand Down Expand Up @@ -483,7 +483,7 @@ func (ssl StoreSpecList) PriorCriticalAlertError() (err error) {
if path == "" {
continue
}
b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if err != nil {
if !oserror.IsNotExist(err) {
addError(errors.Wrapf(err, "%s", path))
Expand Down
3 changes: 1 addition & 2 deletions pkg/base/store_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package base_test

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -287,7 +286,7 @@ func TestStoreSpecListPreventedStartupMessage(t *testing.T) {
err := ssl.PriorCriticalAlertError()
require.NoError(t, err)

require.NoError(t, ioutil.WriteFile(ssl.Specs[2].PreventedStartupFile(), []byte("boom"), 0644))
require.NoError(t, os.WriteFile(ssl.Specs[2].PreventedStartupFile(), []byte("boom"), 0644))

err = ssl.PriorCriticalAlertError()
require.Error(t, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/bench/rttanalysis/rtt_analysis_bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package rttanalysis

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
Expand Down Expand Up @@ -177,7 +177,7 @@ func executeRoundTripTest(b testingB, tc RoundTripBenchTestCase, cc ClusterConst
jaegerJSON, err := r.ToJaegerJSON(tc.Stmt, "", "n0")
require.NoError(b, err)
path := filepath.Join(dir, strings.Replace(b.Name(), "/", "_", -1)) + ".jaeger.json"
require.NoError(b, ioutil.WriteFile(path, []byte(jaegerJSON), 0666))
require.NoError(b, os.WriteFile(path, []byte(jaegerJSON), 0666))
b.Errorf("wrote jaeger trace to %s", path)
}
b.ReportMetric(res, roundTripsMetric)
Expand Down
4 changes: 2 additions & 2 deletions pkg/bench/tpcc/tpcc_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package tpcc

import (
"context"
"io/ioutil"
"io"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -185,7 +185,7 @@ type event struct {
}

func (bm *benchmark) ServeHTTP(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(r.Body)
data, err := io.ReadAll(r.Body)
if err != nil {
bm.addEvent(err.Error())
w.WriteHeader(500)
Expand Down
3 changes: 1 addition & 2 deletions pkg/blobs/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"bytes"
"context"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -48,7 +47,7 @@ func writeLargeFile(t testing.TB, file string, size int64) {
t.Fatal(err)
}
content := make([]byte, size)
err = ioutil.WriteFile(file, content, 0600)
err = os.WriteFile(file, content, 0600)
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/blobs/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"bytes"
"context"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -102,7 +101,7 @@ func writeTestFile(t testing.TB, file string, content []byte) {
if err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(file, content, 0600)
err = os.WriteFile(file, content, 0600)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -273,7 +272,7 @@ func TestBlobClientWriteFile(t *testing.T) {
t.Fatal(err)
}
// Check that file is now in correct node
content, err := ioutil.ReadFile(filepath.Join(tc.destinationNodeDir, tc.filename))
content, err := os.ReadFile(filepath.Join(tc.destinationNodeDir, tc.filename))
if err != nil {
t.Fatal(err, "unable to read fetched file")
}
Expand Down Expand Up @@ -469,7 +468,7 @@ func TestBlobClientDeleteFrom(t *testing.T) {
t.Fatal(err)
}

_, err = ioutil.ReadFile(filepath.Join(localExternalDir, tc.filename))
_, err = os.ReadFile(filepath.Join(localExternalDir, tc.filename))
if err == nil {
t.Fatal(err, "file should have been deleted")
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/blobs/local_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package blobs
import (
"context"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -122,9 +121,9 @@ func (l *LocalStorage) Writer(ctx context.Context, filename string) (io.WriteClo
// - it avoids a cross-filesystem rename in the common case.
// (There can still be cross-filesystem renames in very
// exotic edge cases, hence the use fileutil.Move below.)
// See the explanatory comment for ioutil.TempFile to understand
// See the explanatory comment for os.CreateTemp to understand
// what the "*" in the suffix means.
tmpFile, err := ioutil.TempFile(targetDir, filepath.Base(fullPath)+"*.tmp")
tmpFile, err := os.CreateTemp(targetDir, filepath.Base(fullPath)+"*.tmp")
if err != nil {
return nil, errors.Wrap(err, "creating temporary file")
}
Expand Down
34 changes: 17 additions & 17 deletions pkg/ccl/backupccl/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestBackupRestoreStatementResult(t *testing.T) {
t.Run("GZipBackupManifest", func(t *testing.T) {
backupDir := fmt.Sprintf("%s/foo", dir)
backupManifestFile := backupDir + "/" + backupbase.BackupManifestName
backupManifestBytes, err := ioutil.ReadFile(backupManifestFile)
backupManifestBytes, err := os.ReadFile(backupManifestFile)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -297,7 +297,7 @@ func TestBackupRestorePartitioned(t *testing.T) {
fName := f.Name()
if partitionMatcher.MatchString(fName) {
backupPartitionFile := subDir + "/" + fName
backupPartitionBytes, err := ioutil.ReadFile(backupPartitionFile)
backupPartitionBytes, err := os.ReadFile(backupPartitionFile)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1589,7 +1589,7 @@ func TestBackupRestoreResume(t *testing.T) {
t.Fatal(err)
}
checkpointFile := backupDir + item.checkpointDirectory + "/" + backupinfo.BackupManifestCheckpointName
if err := ioutil.WriteFile(checkpointFile, mockManifest, 0644); err != nil {
if err := os.WriteFile(checkpointFile, mockManifest, 0644); err != nil {
t.Fatal(err)
}
createAndWaitForJob(
Expand All @@ -1604,7 +1604,7 @@ func TestBackupRestoreResume(t *testing.T) {
// If the backup properly took the (incorrect) checkpoint into account, it
// won't have tried to re-export any keys within backupCompletedSpan.
backupManifestFile := backupDir + "/" + backupbase.BackupManifestName
backupManifestBytes, err := ioutil.ReadFile(backupManifestFile)
backupManifestBytes, err := os.ReadFile(backupManifestFile)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -3952,7 +3952,7 @@ func TestBackupRestoreChecksum(t *testing.T) {

var backupManifest backuppb.BackupManifest
{
backupManifestBytes, err := ioutil.ReadFile(filepath.Join(dir, backupbase.BackupManifestName))
backupManifestBytes, err := os.ReadFile(filepath.Join(dir, backupbase.BackupManifestName))
if err != nil {
t.Fatalf("%+v", err)
}
Expand Down Expand Up @@ -4110,7 +4110,7 @@ func checkBackupStatsEncrypted(t *testing.T, rawDir string) {
return err
}
if partitionMatcher.MatchString(fName) {
statsBytes, err := ioutil.ReadFile(fName)
statsBytes, err := os.ReadFile(fName)
if err != nil {
return err
}
Expand All @@ -4129,7 +4129,7 @@ func checkBackupFilesEncrypted(t *testing.T, rawDir string) {
checkedFiles := 0
if err := filepath.Walk(rawDir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() && !strings.Contains(path, "foo/cleartext") {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -8220,7 +8220,7 @@ func TestManifestTooNew(t *testing.T) {

// Load/deserialize the manifest so we can mess with it.
manifestPath := filepath.Join(rawDir, "too_new", backupbase.BackupManifestName)
manifestData, err := ioutil.ReadFile(manifestPath)
manifestData, err := os.ReadFile(manifestPath)
require.NoError(t, err)
manifestData, err = backupinfo.DecompressData(context.Background(), nil, manifestData)
require.NoError(t, err)
Expand All @@ -8231,11 +8231,11 @@ func TestManifestTooNew(t *testing.T) {
backupManifest.ClusterVersion = roachpb.Version{Major: 99, Minor: 1}
manifestData, err = protoutil.Marshal(&backupManifest)
require.NoError(t, err)
require.NoError(t, ioutil.WriteFile(manifestPath, manifestData, 0644 /* perm */))
require.NoError(t, os.WriteFile(manifestPath, manifestData, 0644 /* perm */))
// Also write the checksum file to match the new manifest.
checksum, err := backupinfo.GetChecksum(manifestData)
require.NoError(t, err)
require.NoError(t, ioutil.WriteFile(manifestPath+backupinfo.BackupManifestChecksumSuffix, checksum, 0644 /* perm */))
require.NoError(t, os.WriteFile(manifestPath+backupinfo.BackupManifestChecksumSuffix, checksum, 0644 /* perm */))

// Verify we reject it.
sqlDB.ExpectErr(t, "backup from version 99.1 is newer than current version", `RESTORE DATABASE r1 FROM 'nodelocal://0/too_new'`)
Expand All @@ -8244,11 +8244,11 @@ func TestManifestTooNew(t *testing.T) {
backupManifest.ClusterVersion = roachpb.Version{Major: 20, Minor: 2, Internal: 2}
manifestData, err = protoutil.Marshal(&backupManifest)
require.NoError(t, err)
require.NoError(t, ioutil.WriteFile(manifestPath, manifestData, 0644 /* perm */))
require.NoError(t, os.WriteFile(manifestPath, manifestData, 0644 /* perm */))
// Also write the checksum file to match the new manifest.
checksum, err = backupinfo.GetChecksum(manifestData)
require.NoError(t, err)
require.NoError(t, ioutil.WriteFile(manifestPath+backupinfo.BackupManifestChecksumSuffix, checksum, 0644 /* perm */))
require.NoError(t, os.WriteFile(manifestPath+backupinfo.BackupManifestChecksumSuffix, checksum, 0644 /* perm */))

// Prove we can restore again.
sqlDB.Exec(t, `RESTORE DATABASE r1 FROM 'nodelocal://0/too_new'`)
Expand All @@ -8258,11 +8258,11 @@ func TestManifestTooNew(t *testing.T) {
backupManifest.ClusterVersion = roachpb.Version{}
manifestData, err = protoutil.Marshal(&backupManifest)
require.NoError(t, err)
require.NoError(t, ioutil.WriteFile(manifestPath, manifestData, 0644 /* perm */))
require.NoError(t, os.WriteFile(manifestPath, manifestData, 0644 /* perm */))
// Also write the checksum file to match the new manifest.
checksum, err = backupinfo.GetChecksum(manifestData)
require.NoError(t, err)
require.NoError(t, ioutil.WriteFile(manifestPath+backupinfo.BackupManifestChecksumSuffix, checksum, 0644 /* perm */))
require.NoError(t, os.WriteFile(manifestPath+backupinfo.BackupManifestChecksumSuffix, checksum, 0644 /* perm */))
// Prove we can restore again.
sqlDB.Exec(t, `RESTORE DATABASE r1 FROM 'nodelocal://0/too_new'`)
sqlDB.Exec(t, `DROP DATABASE r1`)
Expand Down Expand Up @@ -8303,10 +8303,10 @@ func flipBitInManifests(t *testing.T, rawDir string) {
log.Infof(context.Background(), "visiting %s", path)
if filepath.Base(path) == backupbase.BackupManifestName {
foundManifest = true
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
require.NoError(t, err)
data[20] ^= 1
if err := ioutil.WriteFile(path, data, 0644 /* perm */); err != nil {
if err := os.WriteFile(path, data, 0644 /* perm */); err != nil {
t.Fatal(err)
}
}
Expand Down Expand Up @@ -10221,7 +10221,7 @@ func TestBackupDoNotIncludeViewSpans(t *testing.T) {
// Read the backup manifest.
var backupManifest backuppb.BackupManifest
{
backupManifestBytes, err := ioutil.ReadFile(filepath.Join(dir, "foo", res[0][0], backupbase.BackupManifestName))
backupManifestBytes, err := os.ReadFile(filepath.Join(dir, "foo", res[0][0], backupbase.BackupManifestName))
if err != nil {
t.Fatalf("%+v", err)
}
Expand Down
Loading

0 comments on commit 1af6635

Please sign in to comment.