Skip to content

Commit

Permalink
Remove deprecated io/ioutil (except ioutil.ReadDir)
Browse files Browse the repository at this point in the history
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Mar 30, 2022
1 parent 19ab384 commit 2b9d890
Show file tree
Hide file tree
Showing 21 changed files with 57 additions and 148 deletions.
4 changes: 2 additions & 2 deletions commands/apply.go
Expand Up @@ -17,8 +17,8 @@
package commands

import (
"io/ioutil"
"log"
"os"

"github.com/containerd/continuity"
"github.com/spf13/cobra"
Expand All @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions commands/dump.go
Expand Up @@ -17,7 +17,7 @@
package commands

import (
"io/ioutil"
"io"
"log"
"os"

Expand All @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions commands/main.go
Expand Up @@ -18,7 +18,6 @@ package commands

import (
"io"
"io/ioutil"
"os"
"text/tabwriter"

Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions commands/mount.go
Expand Up @@ -20,7 +20,6 @@
package commands

import (
"io/ioutil"
"log"
"os"
"os/signal"
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions commands/verify.go
Expand Up @@ -17,8 +17,8 @@
package commands

import (
"io/ioutil"
"log"
"os"

"github.com/containerd/continuity"
"github.com/spf13/cobra"
Expand All @@ -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)
}
Expand Down
7 changes: 3 additions & 4 deletions driver/utils.go
Expand Up @@ -18,28 +18,27 @@ 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 {
return nil, err
}
defer f.Close()

data, err := ioutil.ReadAll(f)
data, err := io.ReadAll(f)
if err != nil {
return nil, err
}

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 {
Expand Down
7 changes: 1 addition & 6 deletions fs/copy_linux_test.go
Expand Up @@ -21,7 +21,6 @@ package fs

import (
"io"
"io/ioutil"
"math/rand"
"os"
"os/exec"
Expand All @@ -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 {
Expand Down
23 changes: 6 additions & 17 deletions fs/copy_test.go
Expand Up @@ -19,8 +19,6 @@ package fs
import (
_ "crypto/sha256"
"fmt"
"io/ioutil"
"os"
"testing"
"time"

Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -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)
Expand Down
25 changes: 5 additions & 20 deletions fs/copy_unix_test.go
Expand Up @@ -20,8 +20,6 @@
package fs

import (
"io/ioutil"
"os"
"testing"

"github.com/containerd/continuity/fs/fstest"
Expand All @@ -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"),
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
46 changes: 16 additions & 30 deletions fs/diff_test.go
Expand Up @@ -19,7 +19,6 @@ package fs
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 2b9d890

Please sign in to comment.