Skip to content

Commit 0cf77c0

Browse files
committed
all: remove use io/ioutil deprecated package & fix some deprecated thing
Signed-off-by: Koichi Shiraishi <zchee.io@gmail.com>
1 parent 41c56d4 commit 0cf77c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+256
-314
lines changed

pkg/coretag/coretag.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package coretag
1717

1818
import (
1919
"fmt"
20-
"io/ioutil"
20+
"os"
2121
"strconv"
2222

2323
"golang.org/x/sys/unix"
@@ -77,7 +77,7 @@ func GetAllCoreTags(pid int) ([]uint64, error) {
7777
// getTids returns set of tids as reported by /proc/<pid>/task.
7878
func getTids(pid int) (map[int]struct{}, error) {
7979
tids := make(map[int]struct{})
80-
files, err := ioutil.ReadDir("/proc/" + strconv.Itoa(pid) + "/task")
80+
files, err := os.ReadDir("/proc/" + strconv.Itoa(pid) + "/task")
8181
if err != nil {
8282
return nil, err
8383
}

pkg/cpuid/cpuid_parse_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package cpuid
1616

1717
import (
18-
"io/ioutil"
1918
"os"
2019
"regexp"
2120
"strings"
@@ -38,7 +37,7 @@ func TestHostFeatureFlags(t *testing.T) {
3837
}
3938

4039
// Extract all cpuinfo flags.
41-
cpuinfoBytes, _ := ioutil.ReadFile("/proc/cpuinfo")
40+
cpuinfoBytes, _ := os.ReadFile("/proc/cpuinfo")
4241
cpuinfo := string(cpuinfoBytes)
4342
re := regexp.MustCompile(`(?m)^flags\s+: (.*)$`)
4443
m := re.FindStringSubmatch(cpuinfo)

pkg/cpuid/native_amd64.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package cpuid
1919

2020
import (
21-
"io/ioutil"
21+
"os"
2222
"strconv"
2323
"strings"
2424

@@ -180,7 +180,7 @@ var (
180180
// filter installation. This value is used to create the fake /proc/cpuinfo
181181
// from a FeatureSet.
182182
func readMaxCPUFreq() {
183-
cpuinfob, err := ioutil.ReadFile("/proc/cpuinfo")
183+
cpuinfob, err := os.ReadFile("/proc/cpuinfo")
184184
if err != nil {
185185
// Leave it as 0... the VDSO bails out in the same way.
186186
log.Warningf("Could not read /proc/cpuinfo: %v", err)

pkg/cpuid/native_arm64.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package cpuid
1919

2020
import (
21-
"io/ioutil"
21+
"os"
2222
"runtime"
2323
"strconv"
2424
"strings"
@@ -51,7 +51,7 @@ func initCPUInfo() {
5151
// warn about them not existing.
5252
return
5353
}
54-
cpuinfob, err := ioutil.ReadFile("/proc/cpuinfo")
54+
cpuinfob, err := os.ReadFile("/proc/cpuinfo")
5555
if err != nil {
5656
// Leave everything at 0, nothing can be done.
5757
log.Warningf("Could not read /proc/cpuinfo: %v", err)

pkg/fdchannel/fdchannel_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package fdchannel
1616

1717
import (
18-
"io/ioutil"
1918
"os"
2019
"syscall"
2120
"testing"
@@ -26,7 +25,7 @@ import (
2625
)
2726

2827
func TestSendRecvFD(t *testing.T) {
29-
sendFile, err := ioutil.TempFile("", "fdchannel_test_")
28+
sendFile, err := os.CreateTemp("", "fdchannel_test_")
3029
if err != nil {
3130
t.Fatalf("failed to create temporary file: %v", err)
3231
}
@@ -82,7 +81,7 @@ func TestSendRecvFD(t *testing.T) {
8281
}
8382

8483
func TestShutdownThenRecvFD(t *testing.T) {
85-
sendFile, err := ioutil.TempFile("", "fdchannel_test_")
84+
sendFile, err := os.CreateTemp("", "fdchannel_test_")
8685
if err != nil {
8786
t.Fatalf("failed to create temporary file: %v", err)
8887
}
@@ -104,7 +103,7 @@ func TestShutdownThenRecvFD(t *testing.T) {
104103
}
105104

106105
func TestRecvFDThenShutdown(t *testing.T) {
107-
sendFile, err := ioutil.TempFile("", "fdchannel_test_")
106+
sendFile, err := os.CreateTemp("", "fdchannel_test_")
108107
if err != nil {
109108
t.Fatalf("failed to create temporary file: %v", err)
110109
}

pkg/gohacks/gohacks_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package gohacks
1616

1717
import (
18-
"io/ioutil"
1918
"math/rand"
2019
"os"
2120
"runtime"
@@ -75,7 +74,7 @@ func TestSigbusOnMemmove(t *testing.T) {
7574
// Test that SIGBUS received by runtime.memmove when *not* doing
7675
// CopyIn or CopyOut work gets propagated to the runtime.
7776
const bufLen = pageSize
78-
f, err := ioutil.TempFile("", "sigbus_test")
77+
f, err := os.CreateTemp("", "sigbus_test")
7978
if err != nil {
8079
t.Fatalf("TempFile failed: %v", err)
8180
}

pkg/lisafs/testsuite/testsuite.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package testsuite
1919
import (
2020
"bytes"
2121
"fmt"
22-
"io/ioutil"
2322
"math/rand"
2423
"os"
2524
"testing"
@@ -54,7 +53,7 @@ type Tester interface {
5453
// RunAllLocalFSTests runs all local FS tests as subtests.
5554
func RunAllLocalFSTests(t *testing.T, tester Tester) {
5655
for name, testFn := range localFSTests {
57-
mountPath, err := ioutil.TempDir(os.Getenv("TEST_TMPDIR"), "")
56+
mountPath, err := os.MkdirTemp(os.Getenv("TEST_TMPDIR"), "")
5857
if err != nil {
5958
t.Fatalf("creation of temporary mountpoint failed: %v", err)
6059
}

pkg/p9/transport.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"errors"
1919
"fmt"
2020
"io"
21-
"io/ioutil"
2221

2322
"golang.org/x/sys/unix"
2423
"gvisor.dev/gvisor/pkg/fd"
@@ -223,7 +222,7 @@ func recv(s *unet.Socket, msize uint32, lookup lookupTagAndType) (Tag, message,
223222
if err != nil {
224223
// Throw away the contents of this message.
225224
if remaining > 0 {
226-
io.Copy(ioutil.Discard, &io.LimitedReader{R: s, N: int64(remaining)})
225+
io.Copy(io.Discard, &io.LimitedReader{R: s, N: int64(remaining)})
227226
}
228227
return tag, nil, err
229228
}
@@ -260,7 +259,7 @@ func recv(s *unet.Socket, msize uint32, lookup lookupTagAndType) (Tag, message,
260259
if fixedSize > remaining {
261260
// This is not a valid message.
262261
if remaining > 0 {
263-
io.Copy(ioutil.Discard, &io.LimitedReader{R: s, N: int64(remaining)})
262+
io.Copy(io.Discard, &io.LimitedReader{R: s, N: int64(remaining)})
264263
}
265264
return NoTag, nil, ErrNoValidMessage
266265
}

pkg/p9/transport_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package p9
1616

1717
import (
18-
"io/ioutil"
1918
"os"
2019
"testing"
2120

@@ -113,7 +112,7 @@ func TestSendRecvWithFile(t *testing.T) {
113112
defer client.Close()
114113

115114
// Create a tempfile.
116-
osf, err := ioutil.TempFile("", "p9")
115+
osf, err := os.CreateTemp("", "p9")
117116
if err != nil {
118117
t.Fatalf("tempfile got err %v expected nil", err)
119118
}

pkg/safecopy/safecopy_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ package safecopy
1717
import (
1818
"bytes"
1919
"fmt"
20-
"io/ioutil"
2120
"math/rand"
21+
"os"
2222
"testing"
2323
"unsafe"
2424

@@ -239,7 +239,7 @@ func withSegvErrorTestMapping(t *testing.T, fn func(m []byte)) {
239239
// withBusErrorTestMapping calls fn with a two-page mapping. The first page
240240
// contains random data, and the second page generates SIGBUS when accessed.
241241
func withBusErrorTestMapping(t *testing.T, fn func(m []byte)) {
242-
f, err := ioutil.TempFile("", "sigbus_test")
242+
f, err := os.CreateTemp("", "sigbus_test")
243243
if err != nil {
244244
t.Fatalf("TempFile failed: %v", err)
245245
}

pkg/seccomp/seccomp_test.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ import (
1919
_ "embed"
2020
"fmt"
2121
"io"
22-
"io/ioutil"
2322
"math"
2423
"math/rand"
2524
"os"
2625
"os/exec"
2726
"reflect"
2827
"strings"
2928
"testing"
30-
"time"
3129

3230
"golang.org/x/sys/unix"
3331
"gvisor.dev/gvisor/pkg/abi/linux"
@@ -39,7 +37,7 @@ var victimData []byte
3937

4038
// newVictim makes a victim binary.
4139
func newVictim() (string, error) {
42-
f, err := ioutil.TempFile("", "victim")
40+
f, err := os.CreateTemp("", "victim")
4341
if err != nil {
4442
return "", err
4543
}
@@ -1078,7 +1076,6 @@ func TestBasic(t *testing.T) {
10781076

10791077
// TestRandom tests that randomly generated rules are encoded correctly.
10801078
func TestRandom(t *testing.T) {
1081-
rand.Seed(time.Now().UnixNano())
10821079
size := rand.Intn(50) + 1
10831080
syscallRules := NewSyscallRules()
10841081
for syscallRules.Size() < size {

pkg/secio/secio_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"bytes"
1919
"errors"
2020
"io"
21-
"io/ioutil"
2221
"math"
2322
"testing"
2423
)
@@ -63,7 +62,7 @@ func newBufferString(s string) *buffer {
6362
func TestOffsetReader(t *testing.T) {
6463
buf := newBufferString("foobar")
6564
r := NewOffsetReader(buf, 3)
66-
dst, err := ioutil.ReadAll(r)
65+
dst, err := io.ReadAll(r)
6766
if want := []byte("bar"); !bytes.Equal(dst, want) || err != nil {
6867
t.Errorf("ReadAll: got (%q, %v), wanted (%q, nil)", dst, err, want)
6968
}
@@ -72,7 +71,7 @@ func TestOffsetReader(t *testing.T) {
7271
func TestSectionReader(t *testing.T) {
7372
buf := newBufferString("foobarbaz")
7473
r := NewSectionReader(buf, 3, 3)
75-
dst, err := ioutil.ReadAll(r)
74+
dst, err := io.ReadAll(r)
7675
if want, wantErr := []byte("bar"), ErrReachedLimit; !bytes.Equal(dst, want) || err != wantErr {
7776
t.Errorf("ReadAll: got (%q, %v), wanted (%q, %v)", dst, err, want, wantErr)
7877
}
@@ -82,7 +81,7 @@ func TestSectionReaderLimitOverflow(t *testing.T) {
8281
// SectionReader behaves like OffsetReader when limit overflows int64.
8382
buf := newBufferString("foobar")
8483
r := NewSectionReader(buf, 3, math.MaxInt64)
85-
dst, err := ioutil.ReadAll(r)
84+
dst, err := io.ReadAll(r)
8685
if want := []byte("bar"); !bytes.Equal(dst, want) || err != nil {
8786
t.Errorf("ReadAll: got (%q, %v), wanted (%q, nil)", dst, err, want)
8887
}

pkg/sentry/hostcpu/hostcpu.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package hostcpu
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
21+
"os"
2222
"strconv"
2323
"strings"
2424
"unicode"
@@ -33,7 +33,7 @@ func GetCPU() uint32
3333
// not to change for the lifetime of the host kernel.
3434
func MaxPossibleCPU() (uint32, error) {
3535
const path = "/sys/devices/system/cpu/possible"
36-
data, err := ioutil.ReadFile(path)
36+
data, err := os.ReadFile(path)
3737
if err != nil {
3838
return 0, err
3939
}

pkg/sentry/platform/mmap_min_addr.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package platform
1616

1717
import (
1818
"fmt"
19-
"io/ioutil"
19+
"os"
2020
"strconv"
2121
"strings"
2222

@@ -47,7 +47,7 @@ func (*MMapMinAddr) MinUserAddress() hostarch.Addr {
4747

4848
func init() {
4949
// Open the source file.
50-
b, err := ioutil.ReadFile(systemMMapMinAddrSource)
50+
b, err := os.ReadFile(systemMMapMinAddrSource)
5151
if err != nil {
5252
panic(fmt.Sprintf("couldn't open %s: %v", systemMMapMinAddrSource, err))
5353
}

pkg/sentry/seccheck/sinks/remote/test/server.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package test
1717

1818
import (
19-
"io/ioutil"
2019
"os"
2120
"path/filepath"
2221

@@ -53,7 +52,7 @@ type Message struct {
5352
// NewServer creates a new server that listens to a UDS that it creates under
5453
// os.TempDir.
5554
func NewServer() (*Server, error) {
56-
dir, err := ioutil.TempDir(os.TempDir(), "remote")
55+
dir, err := os.MkdirTemp(os.TempDir(), "remote")
5756
if err != nil {
5857
return nil, err
5958
}

pkg/sentry/socket/hostinet/stack.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package hostinet
1717
import (
1818
"fmt"
1919
"io"
20-
"io/ioutil"
2120
"os"
2221
"reflect"
2322
"strconv"
@@ -93,7 +92,7 @@ func (s *Stack) Configure(allowRawSockets bool) error {
9392
// SACK is important for performance and even compatibility, assume it's
9493
// enabled if we can't find the actual value.
9594
s.tcpSACKEnabled = true
96-
if sack, err := ioutil.ReadFile("/proc/sys/net/ipv4/tcp_sack"); err == nil {
95+
if sack, err := os.ReadFile("/proc/sys/net/ipv4/tcp_sack"); err == nil {
9796
s.tcpSACKEnabled = strings.TrimSpace(string(sack)) != "0"
9897
} else {
9998
log.Warningf("Failed to read if TCP SACK if enabled, setting to true")
@@ -120,7 +119,7 @@ func (s *Stack) Configure(allowRawSockets bool) error {
120119
}
121120

122121
func readTCPBufferSizeFile(filename string) (inet.TCPBufferSize, error) {
123-
contents, err := ioutil.ReadFile(filename)
122+
contents, err := os.ReadFile(filename)
124123
if err != nil {
125124
return inet.TCPBufferSize{}, fmt.Errorf("failed to read %s: %v", filename, err)
126125
}

pkg/sentry/socket/netstack/netstack.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"encoding/binary"
3030
"fmt"
3131
"io"
32-
"io/ioutil"
3332
"math"
3433
"reflect"
3534
"time"
@@ -2745,7 +2744,7 @@ func (s *sock) nonBlockingRead(ctx context.Context, dst usermem.IOSequence, peek
27452744

27462745
if !isPacket && trunc {
27472746
w = &tcpip.LimitedWriter{
2748-
W: ioutil.Discard,
2747+
W: io.Discard,
27492748
N: dst.NumBytes(),
27502749
}
27512750
res, err = s.Endpoint.Read(w, readOptions)

pkg/shim/runsc/state.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package runsc
1616

1717
import (
1818
"encoding/json"
19-
"io/ioutil"
19+
"os"
2020
"path/filepath"
2121
)
2222

@@ -32,7 +32,7 @@ type state struct {
3232
}
3333

3434
func (s state) load(path string) error {
35-
data, err := ioutil.ReadFile(filepath.Join(path, filename))
35+
data, err := os.ReadFile(filepath.Join(path, filename))
3636
if err != nil {
3737
return err
3838
}
@@ -44,5 +44,5 @@ func (s state) save(path string) error {
4444
if err != nil {
4545
return err
4646
}
47-
return ioutil.WriteFile(filepath.Join(path, filename), data, 0644)
47+
return os.WriteFile(filepath.Join(path, filename), data, 0644)
4848
}

0 commit comments

Comments
 (0)