Skip to content

Commit 5bf078f

Browse files
committed
Remove deprecated io/ioutil (except ioutil.ReadDir)
Signed-off-by: Akihiro Suda <[email protected]>
1 parent d132b28 commit 5bf078f

22 files changed

Lines changed: 63 additions & 151 deletions

commands/apply.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package commands
1818

1919
import (
20-
"io/ioutil"
2120
"log"
21+
"os"
2222

2323
"github.com/containerd/continuity"
2424
"github.com/spf13/cobra"
@@ -30,7 +30,7 @@ var ApplyCmd = &cobra.Command{
3030
Run: func(cmd *cobra.Command, args []string) {
3131
root, path := args[0], args[1]
3232

33-
p, err := ioutil.ReadFile(path)
33+
p, err := os.ReadFile(path)
3434
if err != nil {
3535
log.Fatalf("error reading manifest: %v", err)
3636
}

commands/dump.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package commands
1818

1919
import (
20-
"io/ioutil"
20+
"io"
2121
"log"
2222
"os"
2323

@@ -34,12 +34,12 @@ var DumpCmd = &cobra.Command{
3434
var err error
3535

3636
if len(args) < 1 {
37-
p, err = ioutil.ReadAll(os.Stdin)
37+
p, err = io.ReadAll(os.Stdin)
3838
if err != nil {
3939
log.Fatalf("error reading manifest: %v", err)
4040
}
4141
} else {
42-
p, err = ioutil.ReadFile(args[0])
42+
p, err = os.ReadFile(args[0])
4343
if err != nil {
4444
log.Fatalf("error reading manifest: %v", err)
4545
}

commands/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package commands
1818

1919
import (
2020
"io"
21-
"io/ioutil"
2221
"os"
2322
"text/tabwriter"
2423

@@ -78,7 +77,7 @@ func init() {
7877
// readManifestFile reads the manifest from the given path. This should
7978
// probably be provided by the continuity library.
8079
func readManifestFile(path string) (*pb.Manifest, error) {
81-
p, err := ioutil.ReadFile(path)
80+
p, err := os.ReadFile(path)
8281
if err != nil {
8382
return nil, err
8483
}

commands/mount.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package commands
2121

2222
import (
23-
"io/ioutil"
2423
"log"
2524
"os"
2625
"os/signal"
@@ -50,7 +49,7 @@ var MountCmd = &cobra.Command{
5049

5150
manifestName := filepath.Base(manifest)
5251

53-
p, err := ioutil.ReadFile(manifest)
52+
p, err := os.ReadFile(manifest)
5453
if err != nil {
5554
log.Fatalf("error reading manifest: %v", err)
5655
}

commands/verify.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package commands
1818

1919
import (
20-
"io/ioutil"
2120
"log"
21+
"os"
2222

2323
"github.com/containerd/continuity"
2424
"github.com/spf13/cobra"
@@ -34,7 +34,7 @@ var VerifyCmd = &cobra.Command{
3434

3535
root, path := args[0], args[1]
3636

37-
p, err := ioutil.ReadFile(path)
37+
p, err := os.ReadFile(path)
3838
if err != nil {
3939
log.Fatalf("error reading manifest: %v", err)
4040
}

driver/utils.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,27 @@ package driver
1818

1919
import (
2020
"io"
21-
"io/ioutil"
2221
"os"
2322
"sort"
2423
)
2524

26-
// ReadFile works the same as ioutil.ReadFile with the Driver abstraction
25+
// ReadFile works the same as os.ReadFile with the Driver abstraction
2726
func ReadFile(r Driver, filename string) ([]byte, error) {
2827
f, err := r.Open(filename)
2928
if err != nil {
3029
return nil, err
3130
}
3231
defer f.Close()
3332

34-
data, err := ioutil.ReadAll(f)
33+
data, err := io.ReadAll(f)
3534
if err != nil {
3635
return nil, err
3736
}
3837

3938
return data, nil
4039
}
4140

42-
// WriteFile works the same as ioutil.WriteFile with the Driver abstraction
41+
// WriteFile works the same as os.WriteFile with the Driver abstraction
4342
func WriteFile(r Driver, filename string, data []byte, perm os.FileMode) error {
4443
f, err := r.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
4544
if err != nil {

fs/copy_linux_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package fs
2121

2222
import (
2323
"io"
24-
"io/ioutil"
2524
"math/rand"
2625
"os"
2726
"os/exec"
@@ -34,11 +33,7 @@ import (
3433

3534
func TestCopyReflinkWithXFS(t *testing.T) {
3635
testutil.RequiresRoot(t)
37-
mnt, err := ioutil.TempDir("", "containerd-test-copy-reflink-with-xfs")
38-
if err != nil {
39-
t.Fatal(err)
40-
}
41-
defer os.RemoveAll(mnt)
36+
mnt := t.TempDir()
4237

4338
loop, err := loopback.New(1 << 30) // sparse file (max=1GB)
4439
if err != nil {

fs/copy_test.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ package fs
1919
import (
2020
_ "crypto/sha256"
2121
"fmt"
22-
"io/ioutil"
23-
"os"
2422
"testing"
2523
"time"
2624

@@ -43,7 +41,7 @@ func TestCopyDirectory(t *testing.T) {
4341
fstest.CreateDir("/home", 0755),
4442
)
4543

46-
if err := testCopy(apply); err != nil {
44+
if err := testCopy(t, apply); err != nil {
4745
t.Fatalf("Copy test failed: %+v", err)
4846
}
4947
}
@@ -57,7 +55,7 @@ func TestCopyDirectoryWithLocalSymlink(t *testing.T) {
5755
fstest.Symlink("nothing.txt", "link-no-nothing.txt"),
5856
)
5957

60-
if err := testCopy(apply); err != nil {
58+
if err := testCopy(t, apply); err != nil {
6159
t.Fatalf("Copy test failed: %+v", err)
6260
}
6361
}
@@ -72,23 +70,14 @@ func TestCopyWithLargeFile(t *testing.T) {
7270
fstest.CreateRandomFile("/banana/split", time.Now().UnixNano(), 3*1024*1024*1024, 0644),
7371
)
7472

75-
if err := testCopy(apply); err != nil {
73+
if err := testCopy(t, apply); err != nil {
7674
t.Fatal(err)
7775
}
7876
}
7977

80-
func testCopy(apply fstest.Applier) error {
81-
t1, err := ioutil.TempDir("", "test-copy-src-")
82-
if err != nil {
83-
return fmt.Errorf("failed to create temporary directory: %w", err)
84-
}
85-
defer os.RemoveAll(t1)
86-
87-
t2, err := ioutil.TempDir("", "test-copy-dst-")
88-
if err != nil {
89-
return fmt.Errorf("failed to create temporary directory: %w", err)
90-
}
91-
defer os.RemoveAll(t2)
78+
func testCopy(t testing.TB, apply fstest.Applier) error {
79+
t1 := t.TempDir()
80+
t2 := t.TempDir()
9281

9382
if err := apply.Apply(t1); err != nil {
9483
return fmt.Errorf("failed to apply changes: %w", err)

fs/copy_unix_test.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package fs
2121

2222
import (
23-
"io/ioutil"
2423
"os"
2524
"path/filepath"
2625
"syscall"
@@ -45,12 +44,7 @@ func assertXAttr(t *testing.T, dir, xattr, xval string, xerr error) {
4544
}
4645

4746
func TestCopyDirWithXAttrExcludes(t *testing.T) {
48-
src, err := ioutil.TempDir("", "test-copy-src-with-xattr-")
49-
if err != nil {
50-
t.Fatal(err)
51-
}
52-
defer os.RemoveAll(src)
53-
47+
src := t.TempDir()
5448
if err := fstest.Apply(
5549
fstest.SetXAttr(".", "user.test-1", "one"),
5650
fstest.SetXAttr(".", "user.test-2", "two"),
@@ -60,12 +54,8 @@ func TestCopyDirWithXAttrExcludes(t *testing.T) {
6054
}
6155

6256
t.Run("none", func(t *testing.T) {
63-
dst, err := ioutil.TempDir("", "test-copy-dst-with-xattr-exclude-none-")
64-
if err != nil {
65-
t.Fatal(err)
66-
}
67-
defer os.RemoveAll(dst)
68-
err = CopyDir(dst, src, WithXAttrExclude())
57+
dst := t.TempDir()
58+
err := CopyDir(dst, src, WithXAttrExclude())
6959
if err != nil {
7060
t.Fatal(err)
7161
}
@@ -75,12 +65,8 @@ func TestCopyDirWithXAttrExcludes(t *testing.T) {
7565
})
7666

7767
t.Run("some", func(t *testing.T) {
78-
dst, err := ioutil.TempDir("", "test-copy-dst-with-xattr-exclude-some-")
79-
if err != nil {
80-
t.Fatal(err)
81-
}
82-
defer os.RemoveAll(dst)
83-
err = CopyDir(dst, src, WithXAttrExclude("user.test-x"))
68+
dst := t.TempDir()
69+
err := CopyDir(dst, src, WithXAttrExclude("user.test-x"))
8470
if err != nil {
8571
t.Fatal(err)
8672
}
@@ -99,7 +85,7 @@ func TestCopyIrregular(t *testing.T) {
9985
}
10086
prepared++
10187
f1Normal := filepath.Join(src, "f1.normal")
102-
if err := ioutil.WriteFile(f1Normal, []byte("content of f1.normal"), 0600); err != nil {
88+
if err := os.WriteFile(f1Normal, []byte("content of f1.normal"), 0600); err != nil {
10389
t.Fatal(err)
10490
}
10591
prepared++
@@ -117,7 +103,7 @@ func TestCopyIrregular(t *testing.T) {
117103
}
118104

119105
verifyDst := func(dst string) {
120-
entries, err := ioutil.ReadDir(dst)
106+
entries, err := os.ReadDir(dst)
121107
if err != nil {
122108
t.Fatal(err)
123109
}
@@ -136,7 +122,7 @@ func TestCopyIrregular(t *testing.T) {
136122
t.Fatalf("unexpected mode of %s: %v", name, mode)
137123
}
138124
case "f1.normal":
139-
b, err := ioutil.ReadFile(full)
125+
b, err := os.ReadFile(full)
140126
if err != nil {
141127
t.Fatal(err)
142128
}

0 commit comments

Comments
 (0)