From 2b9d890ea31d73239c410cc88eff11848166e8eb Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Wed, 30 Mar 2022 19:06:18 +0900 Subject: [PATCH] Remove deprecated io/ioutil (except ioutil.ReadDir) Signed-off-by: Akihiro Suda --- commands/apply.go | 4 +-- commands/dump.go | 6 ++-- commands/main.go | 3 +- commands/mount.go | 3 +- commands/verify.go | 4 +-- driver/utils.go | 7 ++--- fs/copy_linux_test.go | 7 +---- fs/copy_test.go | 23 ++++----------- fs/copy_unix_test.go | 25 ++++------------ fs/diff_test.go | 46 ++++++++++------------------- fs/dtype_linux.go | 2 +- fs/dtype_linux_test.go | 8 +---- fs/du_test.go | 10 ++----- fs/du_unix_test.go | 10 ++----- fs/fstest/compare.go | 3 +- fs/fstest/testsuite.go | 3 +- fs/path_test.go | 15 ++-------- ioutils.go | 3 +- ioutils_test.go | 9 ++---- manifest_test.go | 11 ++----- testutil/loopback/loopback_linux.go | 3 +- 21 files changed, 57 insertions(+), 148 deletions(-) diff --git a/commands/apply.go b/commands/apply.go index fac86ad9..36ff2b3b 100644 --- a/commands/apply.go +++ b/commands/apply.go @@ -17,8 +17,8 @@ package commands import ( - "io/ioutil" "log" + "os" "github.com/containerd/continuity" "github.com/spf13/cobra" @@ -30,7 +30,7 @@ var ApplyCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { root, path := args[0], args[1] - p, err := ioutil.ReadFile(path) + p, err := os.ReadFile(path) if err != nil { log.Fatalf("error reading manifest: %v", err) } diff --git a/commands/dump.go b/commands/dump.go index 4a03db47..4e31542f 100644 --- a/commands/dump.go +++ b/commands/dump.go @@ -17,7 +17,7 @@ package commands import ( - "io/ioutil" + "io" "log" "os" @@ -34,12 +34,12 @@ var DumpCmd = &cobra.Command{ var err error if len(args) < 1 { - p, err = ioutil.ReadAll(os.Stdin) + p, err = io.ReadAll(os.Stdin) if err != nil { log.Fatalf("error reading manifest: %v", err) } } else { - p, err = ioutil.ReadFile(args[0]) + p, err = os.ReadFile(args[0]) if err != nil { log.Fatalf("error reading manifest: %v", err) } diff --git a/commands/main.go b/commands/main.go index 34aedb14..4618000c 100644 --- a/commands/main.go +++ b/commands/main.go @@ -18,7 +18,6 @@ package commands import ( "io" - "io/ioutil" "os" "text/tabwriter" @@ -78,7 +77,7 @@ func init() { // readManifestFile reads the manifest from the given path. This should // probably be provided by the continuity library. func readManifestFile(path string) (*pb.Manifest, error) { - p, err := ioutil.ReadFile(path) + p, err := os.ReadFile(path) if err != nil { return nil, err } diff --git a/commands/mount.go b/commands/mount.go index f2873af2..acda1d44 100644 --- a/commands/mount.go +++ b/commands/mount.go @@ -20,7 +20,6 @@ package commands import ( - "io/ioutil" "log" "os" "os/signal" @@ -50,7 +49,7 @@ var MountCmd = &cobra.Command{ manifestName := filepath.Base(manifest) - p, err := ioutil.ReadFile(manifest) + p, err := os.ReadFile(manifest) if err != nil { log.Fatalf("error reading manifest: %v", err) } diff --git a/commands/verify.go b/commands/verify.go index 49d3537e..5b017f06 100644 --- a/commands/verify.go +++ b/commands/verify.go @@ -17,8 +17,8 @@ package commands import ( - "io/ioutil" "log" + "os" "github.com/containerd/continuity" "github.com/spf13/cobra" @@ -34,7 +34,7 @@ var VerifyCmd = &cobra.Command{ root, path := args[0], args[1] - p, err := ioutil.ReadFile(path) + p, err := os.ReadFile(path) if err != nil { log.Fatalf("error reading manifest: %v", err) } diff --git a/driver/utils.go b/driver/utils.go index 0c688d15..d122a3f7 100644 --- a/driver/utils.go +++ b/driver/utils.go @@ -18,12 +18,11 @@ package driver import ( "io" - "io/ioutil" "os" "sort" ) -// ReadFile works the same as ioutil.ReadFile with the Driver abstraction +// ReadFile works the same as os.ReadFile with the Driver abstraction func ReadFile(r Driver, filename string) ([]byte, error) { f, err := r.Open(filename) if err != nil { @@ -31,7 +30,7 @@ func ReadFile(r Driver, filename string) ([]byte, error) { } defer f.Close() - data, err := ioutil.ReadAll(f) + data, err := io.ReadAll(f) if err != nil { return nil, err } @@ -39,7 +38,7 @@ func ReadFile(r Driver, filename string) ([]byte, error) { return data, nil } -// WriteFile works the same as ioutil.WriteFile with the Driver abstraction +// WriteFile works the same as os.WriteFile with the Driver abstraction func WriteFile(r Driver, filename string, data []byte, perm os.FileMode) error { f, err := r.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) if err != nil { diff --git a/fs/copy_linux_test.go b/fs/copy_linux_test.go index d91a0437..20703dfb 100644 --- a/fs/copy_linux_test.go +++ b/fs/copy_linux_test.go @@ -21,7 +21,6 @@ package fs import ( "io" - "io/ioutil" "math/rand" "os" "os/exec" @@ -34,11 +33,7 @@ import ( func TestCopyReflinkWithXFS(t *testing.T) { testutil.RequiresRoot(t) - mnt, err := ioutil.TempDir("", "containerd-test-copy-reflink-with-xfs") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(mnt) + mnt := t.TempDir() loop, err := loopback.New(1 << 30) // sparse file (max=1GB) if err != nil { diff --git a/fs/copy_test.go b/fs/copy_test.go index e27716ac..7cca9b4c 100644 --- a/fs/copy_test.go +++ b/fs/copy_test.go @@ -19,8 +19,6 @@ package fs import ( _ "crypto/sha256" "fmt" - "io/ioutil" - "os" "testing" "time" @@ -43,7 +41,7 @@ func TestCopyDirectory(t *testing.T) { fstest.CreateDir("/home", 0755), ) - if err := testCopy(apply); err != nil { + if err := testCopy(t, apply); err != nil { t.Fatalf("Copy test failed: %+v", err) } } @@ -57,7 +55,7 @@ func TestCopyDirectoryWithLocalSymlink(t *testing.T) { fstest.Symlink("nothing.txt", "link-no-nothing.txt"), ) - if err := testCopy(apply); err != nil { + if err := testCopy(t, apply); err != nil { t.Fatalf("Copy test failed: %+v", err) } } @@ -72,23 +70,14 @@ func TestCopyWithLargeFile(t *testing.T) { fstest.CreateRandomFile("/banana/split", time.Now().UnixNano(), 3*1024*1024*1024, 0644), ) - if err := testCopy(apply); err != nil { + if err := testCopy(t, apply); err != nil { t.Fatal(err) } } -func testCopy(apply fstest.Applier) error { - t1, err := ioutil.TempDir("", "test-copy-src-") - if err != nil { - return fmt.Errorf("failed to create temporary directory: %w", err) - } - defer os.RemoveAll(t1) - - t2, err := ioutil.TempDir("", "test-copy-dst-") - if err != nil { - return fmt.Errorf("failed to create temporary directory: %w", err) - } - defer os.RemoveAll(t2) +func testCopy(t testing.TB, apply fstest.Applier) error { + t1 := t.TempDir() + t2 := t.TempDir() if err := apply.Apply(t1); err != nil { return fmt.Errorf("failed to apply changes: %w", err) diff --git a/fs/copy_unix_test.go b/fs/copy_unix_test.go index 6f6d2f41..973f1abc 100644 --- a/fs/copy_unix_test.go +++ b/fs/copy_unix_test.go @@ -20,8 +20,6 @@ package fs import ( - "io/ioutil" - "os" "testing" "github.com/containerd/continuity/fs/fstest" @@ -42,12 +40,7 @@ func assertXAttr(t *testing.T, dir, xattr, xval string, xerr error) { } func TestCopyDirWithXAttrExcludes(t *testing.T) { - src, err := ioutil.TempDir("", "test-copy-src-with-xattr-") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(src) - + src := t.TempDir() if err := fstest.Apply( fstest.SetXAttr(".", "user.test-1", "one"), fstest.SetXAttr(".", "user.test-2", "two"), @@ -57,12 +50,8 @@ func TestCopyDirWithXAttrExcludes(t *testing.T) { } t.Run("none", func(t *testing.T) { - dst, err := ioutil.TempDir("", "test-copy-dst-with-xattr-exclude-none-") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dst) - err = CopyDir(dst, src, WithXAttrExclude()) + dst := t.TempDir() + err := CopyDir(dst, src, WithXAttrExclude()) if err != nil { t.Fatal(err) } @@ -72,12 +61,8 @@ func TestCopyDirWithXAttrExcludes(t *testing.T) { }) t.Run("some", func(t *testing.T) { - dst, err := ioutil.TempDir("", "test-copy-dst-with-xattr-exclude-some-") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dst) - err = CopyDir(dst, src, WithXAttrExclude("user.test-x")) + dst := t.TempDir() + err := CopyDir(dst, src, WithXAttrExclude("user.test-x")) if err != nil { t.Fatal(err) } diff --git a/fs/diff_test.go b/fs/diff_test.go index f63be231..6d6ca251 100644 --- a/fs/diff_test.go +++ b/fs/diff_test.go @@ -19,7 +19,6 @@ package fs import ( "context" "fmt" - "io/ioutil" "os" "path/filepath" "runtime" @@ -66,7 +65,7 @@ func TestSimpleDiff(t *testing.T) { Add("/root/.bashrc"), } - if err := testDiffWithBase(l1, l2, diff); err != nil { + if err := testDiffWithBase(t, l1, l2, diff); err != nil { t.Fatalf("Failed diff with base: %+v", err) } } @@ -82,7 +81,7 @@ func TestEmptyFileDiff(t *testing.T) { l2 := fstest.Apply() diff := []TestChange{} - if err := testDiffWithBase(l1, l2, diff); err != nil { + if err := testDiffWithBase(t, l1, l2, diff); err != nil { t.Fatalf("Failed diff with base: %+v", err) } } @@ -104,7 +103,7 @@ func TestNestedDeletion(t *testing.T) { Delete("/d1"), } - if err := testDiffWithBase(l1, l2, diff); err != nil { + if err := testDiffWithBase(t, l1, l2, diff); err != nil { t.Fatalf("Failed diff with base: %+v", err) } } @@ -127,7 +126,7 @@ func TestDirectoryReplace(t *testing.T) { Modify("/dir1/f2"), } - if err := testDiffWithBase(l1, l2, diff); err != nil { + if err := testDiffWithBase(t, l1, l2, diff); err != nil { t.Fatalf("Failed diff with base: %+v", err) } } @@ -145,7 +144,7 @@ func TestRemoveDirectoryTree(t *testing.T) { Delete("/dir1"), } - if err := testDiffWithBase(l1, l2, diff); err != nil { + if err := testDiffWithBase(t, l1, l2, diff); err != nil { t.Fatalf("Failed diff with base: %+v", err) } } @@ -165,7 +164,7 @@ func TestRemoveDirectoryTreeWithDash(t *testing.T) { Delete("/dir1"), } - if err := testDiffWithBase(l1, l2, diff); err != nil { + if err := testDiffWithBase(t, l1, l2, diff); err != nil { t.Fatalf("Failed diff with base: %+v", err) } } @@ -185,7 +184,7 @@ func TestFileReplace(t *testing.T) { Add("/dir1/dir2/f1"), } - if err := testDiffWithBase(l1, l2, diff); err != nil { + if err := testDiffWithBase(t, l1, l2, diff); err != nil { t.Fatalf("Failed diff with base: %+v", err) } } @@ -212,7 +211,7 @@ func TestParentDirectoryPermission(t *testing.T) { Add("/dir3/f"), } - if err := testDiffWithBase(l1, l2, diff); err != nil { + if err := testDiffWithBase(t, l1, l2, diff); err != nil { t.Fatalf("Failed diff with base: %+v", err) } } @@ -262,7 +261,7 @@ func TestUpdateWithSameTime(t *testing.T) { Modify("/file-truncated-time-3"), } - if err := testDiffWithBase(l1, l2, diff); err != nil { + if err := testDiffWithBase(t, l1, l2, diff); err != nil { t.Fatalf("Failed diff with base: %+v", err) } } @@ -283,23 +282,15 @@ func TestLchtimes(t *testing.T) { ) l2 := fstest.Apply() // empty diff := []TestChange{} - if err := testDiffWithBase(l1, l2, diff); err != nil { + if err := testDiffWithBase(t, l1, l2, diff); err != nil { t.Fatalf("Failed diff with base: %+v", err) } } } -func testDiffWithBase(base, diff fstest.Applier, expected []TestChange) error { - t1, err := ioutil.TempDir("", "diff-with-base-lower-") - if err != nil { - return fmt.Errorf("failed to create temp dir: %w", err) - } - defer os.RemoveAll(t1) - t2, err := ioutil.TempDir("", "diff-with-base-upper-") - if err != nil { - return fmt.Errorf("failed to create temp dir: %w", err) - } - defer os.RemoveAll(t2) +func testDiffWithBase(t testing.TB, base, diff fstest.Applier, expected []TestChange) error { + t1 := t.TempDir() + t2 := t.TempDir() if err := base.Apply(t1); err != nil { return fmt.Errorf("failed to apply base filesystem: %w", err) @@ -337,18 +328,13 @@ func TestBaseDirectoryChanges(t *testing.T) { Add("/root/.bashrc"), } - if err := testDiffWithoutBase(apply, changes); err != nil { + if err := testDiffWithoutBase(t, apply, changes); err != nil { t.Fatalf("Failed diff without base: %+v", err) } } -func testDiffWithoutBase(apply fstest.Applier, expected []TestChange) error { - tmp, err := ioutil.TempDir("", "diff-without-base-") - if err != nil { - return fmt.Errorf("failed to create temp dir: %w", err) - } - defer os.RemoveAll(tmp) - +func testDiffWithoutBase(t testing.TB, apply fstest.Applier, expected []TestChange) error { + tmp := t.TempDir() if err := apply.Apply(tmp); err != nil { return fmt.Errorf("failed to apply filesytem changes: %w", err) } diff --git a/fs/dtype_linux.go b/fs/dtype_linux.go index ddd6c793..a8eab1db 100644 --- a/fs/dtype_linux.go +++ b/fs/dtype_linux.go @@ -35,7 +35,7 @@ func locateDummyIfEmpty(path string) (string, error) { if len(children) != 0 { return "", nil } - dummyFile, err := ioutil.TempFile(path, "fsutils-dummy") + dummyFile, err := os.CreateTemp(path, "fsutils-dummy") if err != nil { return "", err } diff --git a/fs/dtype_linux_test.go b/fs/dtype_linux_test.go index 61c3b694..179e5ec0 100644 --- a/fs/dtype_linux_test.go +++ b/fs/dtype_linux_test.go @@ -20,8 +20,6 @@ package fs import ( - "io/ioutil" - "os" "os/exec" "testing" @@ -31,11 +29,7 @@ import ( func testSupportsDType(t *testing.T, expected bool, mkfs ...string) { testutil.RequiresRoot(t) - mnt, err := ioutil.TempDir("", "containerd-fs-test-supports-dtype") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(mnt) + mnt := t.TempDir() loop, err := loopback.New(100 << 20) // 100 MB if err != nil { diff --git a/fs/du_test.go b/fs/du_test.go index 7b1e8c1b..da9a623c 100644 --- a/fs/du_test.go +++ b/fs/du_test.go @@ -20,7 +20,6 @@ import ( "context" "errors" "io" - "io/ioutil" "math/rand" "os" "path/filepath" @@ -33,7 +32,7 @@ import ( var errNotImplemented = errors.New("check not implemented") func TestUsage(t *testing.T) { - align, dirs, err := getTmpAlign() + align, dirs, err := getTmpAlign(t) if err != nil { t.Fatal(err) } @@ -111,12 +110,7 @@ func TestUsage(t *testing.T) { t.Run(tc.name, func(t *testing.T) { t.Parallel() - t1, err := ioutil.TempDir("", "usage-") - if err != nil { - t.Fatal("Failed to create temp dir:", err) - } - defer os.RemoveAll(t1) - + t1 := t.TempDir() if err := tc.fs.Apply(t1); err != nil { t.Fatal("Failed to apply base filesystem:", err) } diff --git a/fs/du_unix_test.go b/fs/du_unix_test.go index e2d293c2..1a2fbd0a 100644 --- a/fs/du_unix_test.go +++ b/fs/du_unix_test.go @@ -22,11 +22,11 @@ package fs import ( "errors" "fmt" - "io/ioutil" "os" "strconv" "strings" "syscall" + "testing" ) func getBsize(root string) (int64, error) { @@ -41,12 +41,8 @@ func getBsize(root string) (int64, error) { // getTmpAlign returns filesystem specific size alignment functions // first: aligns filesize to file usage based on blocks // second: determines directory usage based on directory count (assumes small directories) -func getTmpAlign() (func(int64) int64, func(int64) int64, error) { - t1, err := ioutil.TempDir("", "compute-align-") - if err != nil { - return nil, nil, fmt.Errorf("failed to create temp dir: %w", err) - } - defer os.RemoveAll(t1) +func getTmpAlign(t testing.TB) (func(int64) int64, func(int64) int64, error) { + t1 := t.TempDir() bsize, err := getBsize(t1) if err != nil { diff --git a/fs/fstest/compare.go b/fs/fstest/compare.go index 5ae167e5..98a9abcd 100644 --- a/fs/fstest/compare.go +++ b/fs/fstest/compare.go @@ -18,7 +18,6 @@ package fstest import ( "fmt" - "io/ioutil" "os" "github.com/containerd/continuity" @@ -57,7 +56,7 @@ func CheckDirectoryEqual(d1, d2 string) error { // CheckDirectoryEqualWithApplier compares directory against applier func CheckDirectoryEqualWithApplier(root string, a Applier) error { - applied, err := ioutil.TempDir("", "fstest") + applied, err := os.MkdirTemp("", "fstest") if err != nil { return err } diff --git a/fs/fstest/testsuite.go b/fs/fstest/testsuite.go index 360ef552..420126e6 100644 --- a/fs/fstest/testsuite.go +++ b/fs/fstest/testsuite.go @@ -18,7 +18,6 @@ package fstest import ( "context" - "io/ioutil" "os" "testing" ) @@ -49,7 +48,7 @@ func makeTest(t *testing.T, ta TestApplier, as []Applier) func(t *testing.T) { } defer cleanup() - applyDir, err := ioutil.TempDir("", "test-expected-") + applyDir, err := os.MkdirTemp("", "test-expected-") if err != nil { t.Fatalf("Unable to make temp directory: %+v", err) } diff --git a/fs/path_test.go b/fs/path_test.go index 04ccc84d..24477cc0 100644 --- a/fs/path_test.go +++ b/fs/path_test.go @@ -21,7 +21,6 @@ package fs import ( "errors" - "io/ioutil" "os" "path/filepath" "testing" @@ -230,12 +229,7 @@ func TestDirectoryCompare(t *testing.T) { } func testRootPathSymlinkRootScope(t *testing.T) { - tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkRootScope") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpdir) - + tmpdir := t.TempDir() expected, err := filepath.EvalSymlinks(tmpdir) if err != nil { t.Fatal(err) @@ -265,12 +259,7 @@ func testRootPathSymlinkEmpty(t *testing.T) { func makeRootPathTest(t *testing.T, apply fstest.Applier, checks []RootCheck) func(t *testing.T) { return func(t *testing.T) { - applyDir, err := ioutil.TempDir("", "test-root-path-") - if err != nil { - t.Fatalf("Unable to make temp directory: %+v", err) - } - defer os.RemoveAll(applyDir) - + applyDir := t.TempDir() if apply != nil { if err := apply.Apply(applyDir); err != nil { t.Fatalf("Apply failed: %+v", err) diff --git a/ioutils.go b/ioutils.go index 503640eb..392c407f 100644 --- a/ioutils.go +++ b/ioutils.go @@ -19,7 +19,6 @@ package continuity import ( "bytes" "io" - "io/ioutil" "os" "path/filepath" ) @@ -34,7 +33,7 @@ func AtomicWriteFile(filename string, data []byte, perm os.FileMode) error { // atomicWriteFile writes data to a file by first writing to a temp // file and calling rename. func atomicWriteFile(filename string, r io.Reader, dataSize int64, perm os.FileMode) error { - f, err := ioutil.TempFile(filepath.Dir(filename), ".tmp-"+filepath.Base(filename)) + f, err := os.CreateTemp(filepath.Dir(filename), ".tmp-"+filepath.Base(filename)) if err != nil { return err } diff --git a/ioutils_test.go b/ioutils_test.go index dab8ead1..7004c7bf 100644 --- a/ioutils_test.go +++ b/ioutils_test.go @@ -18,25 +18,20 @@ package continuity import ( "bytes" - "io/ioutil" "os" "path/filepath" "testing" ) func TestAtomicWriteFile(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "atomic-write-test") - if err != nil { - t.Fatalf("Error when creating temporary directory: %s", err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() expected := []byte("barbaz") if err := AtomicWriteFile(filepath.Join(tmpDir, "foo"), expected, 0666); err != nil { t.Fatalf("Error writing to file: %v", err) } - actual, err := ioutil.ReadFile(filepath.Join(tmpDir, "foo")) + actual, err := os.ReadFile(filepath.Join(tmpDir, "foo")) if err != nil { t.Fatalf("Error reading from file: %v", err) } diff --git a/manifest_test.go b/manifest_test.go index 0d7fdbc5..f923f265 100644 --- a/manifest_test.go +++ b/manifest_test.go @@ -24,7 +24,6 @@ import ( _ "crypto/sha256" "errors" "fmt" - "io/ioutil" "math/rand" "os" "path/filepath" @@ -142,13 +141,7 @@ func TestWalkFS(t *testing.T) { // devZeroResource, } - root, err := ioutil.TempDir("", "continuity-test-") - if err != nil { - t.Fatalf("error creating temporary directory: %v", err) - } - - defer os.RemoveAll(root) - + root := t.TempDir() generateTestFiles(t, root, testResources) ctx, err := NewContext(root) @@ -251,7 +244,7 @@ func generateTestFiles(t *testing.T, root string, resources []dresource) { resources[i].size = size // this relies on the proper directory parent being defined. - if err := ioutil.WriteFile(p, d, resource.mode); err != nil { + if err := os.WriteFile(p, d, resource.mode); err != nil { t.Fatalf("error writing %q: %v", p, err) } case rdirectory: diff --git a/testutil/loopback/loopback_linux.go b/testutil/loopback/loopback_linux.go index a43a70e7..ffa46c75 100644 --- a/testutil/loopback/loopback_linux.go +++ b/testutil/loopback/loopback_linux.go @@ -23,7 +23,6 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" "os" "os/exec" "strings" @@ -35,7 +34,7 @@ import ( // New creates a loopback device func New(size int64) (*Loopback, error) { // create temporary file for the disk image - file, err := ioutil.TempFile("", "containerd-test-loopback") + file, err := os.CreateTemp("", "containerd-test-loopback") if err != nil { return nil, fmt.Errorf("could not create temporary file for loopback: %w", err) }