refactor: move from io/ioutil to io and os package

The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun
2021-09-10 20:28:11 +08:00
parent c16be1a5e2
commit 50da673592
126 changed files with 291 additions and 399 deletions

View File

@@ -17,7 +17,6 @@
package apparmor
import (
"io/ioutil"
"os"
"sync"
)
@@ -36,7 +35,7 @@ func hostSupports() bool {
checkAppArmor.Do(func() {
// see https://github.com/opencontainers/runc/blob/0d49470392206f40eaab3b2190a57fe7bb3df458/libcontainer/apparmor/apparmor_linux.go
if _, err := os.Stat("/sys/kernel/security/apparmor"); err == nil && os.Getenv("container") == "" {
buf, err := ioutil.ReadFile("/sys/module/apparmor/parameters/enabled")
buf, err := os.ReadFile("/sys/module/apparmor/parameters/enabled")
appArmorSupported = err == nil && len(buf) > 1 && buf[0] == 'Y'
}
})

View File

@@ -21,7 +21,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"time"
"github.com/sirupsen/logrus"
@@ -43,7 +42,7 @@ const (
// NewDiscardLogger creates logger which discards all the input.
func NewDiscardLogger() io.WriteCloser {
return cioutil.NewNopWriteCloser(ioutil.Discard)
return cioutil.NewNopWriteCloser(io.Discard)
}
// NewCRILogger returns a write closer which redirect container log into

View File

@@ -18,7 +18,7 @@ package io
import (
"bytes"
"io/ioutil"
"io"
"strings"
"testing"
"time"
@@ -237,7 +237,7 @@ func TestRedirectLogs(t *testing.T) {
},
} {
t.Logf("TestCase %q", desc)
rc := ioutil.NopCloser(strings.NewReader(test.input))
rc := io.NopCloser(strings.NewReader(test.input))
buf := bytes.NewBuffer(nil)
wc := cioutil.NewNopWriteCloser(buf)
redirectLogs("test-path", rc, wc, test.stream, test.maxLen)

View File

@@ -18,7 +18,6 @@ package opts
import (
"context"
"io/ioutil"
"os"
"path/filepath"
@@ -67,7 +66,7 @@ func WithVolumes(volumeMounts map[string]string) containerd.NewContainerOpts {
if err != nil {
return err
}
root, err := ioutil.TempDir("", "ctd-volume")
root, err := os.MkdirTemp("", "ctd-volume")
if err != nil {
return err
}
@@ -108,7 +107,7 @@ func WithVolumes(volumeMounts map[string]string) containerd.NewContainerOpts {
// copyExistingContents copies from the source to the destination and
// ensures the ownership is appropriately set.
func copyExistingContents(source, destination string) error {
dstList, err := ioutil.ReadDir(destination)
dstList, err := os.ReadDir(destination)
if err != nil {
return err
}

View File

@@ -19,7 +19,6 @@ package opts
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
@@ -533,7 +532,7 @@ var (
// cgroup v1.
func cgroupv1HasHugetlb() (bool, error) {
_cgroupv1HasHugetlbOnce.Do(func() {
if _, err := ioutil.ReadDir("/sys/fs/cgroup/hugetlb"); err != nil {
if _, err := os.ReadDir("/sys/fs/cgroup/hugetlb"); err != nil {
_cgroupv1HasHugetlbErr = errors.Wrap(err, "readdir /sys/fs/cgroup/hugetlb")
_cgroupv1HasHugetlb = false
} else {
@@ -548,7 +547,7 @@ func cgroupv1HasHugetlb() (bool, error) {
// cgroup v2.
func cgroupv2HasHugetlb() (bool, error) {
_cgroupv2HasHugetlbOnce.Do(func() {
controllers, err := ioutil.ReadFile("/sys/fs/cgroup/cgroup.controllers")
controllers, err := os.ReadFile("/sys/fs/cgroup/cgroup.controllers")
if err != nil {
_cgroupv2HasHugetlbErr = errors.Wrap(err, "read /sys/fs/cgroup/cgroup.controllers")
return
@@ -696,7 +695,7 @@ func nullOpt(_ context.Context, _ oci.Client, _ *containers.Container, _ *runtim
}
func getCurrentOOMScoreAdj() (int, error) {
b, err := ioutil.ReadFile("/proc/self/oom_score_adj")
b, err := os.ReadFile("/proc/self/oom_score_adj")
if err != nil {
return 0, errors.Wrap(err, "could not get the daemon oom_score_adj")
}

View File

@@ -17,7 +17,6 @@
package server
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -66,11 +65,11 @@ func TestEnsureRemoveAllWithMount(t *testing.T) {
t.Skip("skipping test that requires root")
}
dir1, err := ioutil.TempDir("", "test-ensure-removeall-with-dir1")
dir1, err := os.MkdirTemp("", "test-ensure-removeall-with-dir1")
if err != nil {
t.Fatal(err)
}
dir2, err := ioutil.TempDir("", "test-ensure-removeall-with-dir2")
dir2, err := os.MkdirTemp("", "test-ensure-removeall-with-dir2")
if err != nil {
t.Fatal(err)
}

View File

@@ -18,7 +18,7 @@ package server
import (
"context"
"io/ioutil"
"os"
"testing"
"time"
@@ -489,7 +489,7 @@ func TestEnsureRemoveAllNotExist(t *testing.T) {
}
func TestEnsureRemoveAllWithDir(t *testing.T) {
dir, err := ioutil.TempDir("", "test-ensure-removeall-with-dir")
dir, err := os.MkdirTemp("", "test-ensure-removeall-with-dir")
if err != nil {
t.Fatal(err)
}
@@ -499,7 +499,7 @@ func TestEnsureRemoveAllWithDir(t *testing.T) {
}
func TestEnsureRemoveAllWithFile(t *testing.T) {
tmp, err := ioutil.TempFile("", "test-ensure-removeall-with-dir")
tmp, err := os.CreateTemp("", "test-ensure-removeall-with-dir")
if err != nil {
t.Fatal(err)
}

View File

@@ -21,10 +21,10 @@ import (
"crypto/x509"
"encoding/base64"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
@@ -301,7 +301,7 @@ func (c *criService) getTLSConfig(registryTLSConfig criconfig.TLSConfig) (*tls.C
if err != nil {
return nil, errors.Wrap(err, "failed to get system cert pool")
}
caCert, err := ioutil.ReadFile(registryTLSConfig.CAFile)
caCert, err := os.ReadFile(registryTLSConfig.CAFile)
if err != nil {
return nil, errors.Wrap(err, "failed to load CA file")
}

View File

@@ -17,7 +17,6 @@
package server
import (
"io/ioutil"
"os"
"path/filepath"
goruntime "runtime"
@@ -458,7 +457,7 @@ func (c *criService) loadImages(ctx context.Context, cImages []containerd.Image)
func cleanupOrphanedIDDirs(ctx context.Context, cntrs []containerd.Container, base string) error {
// Cleanup orphaned id directories.
dirs, err := ioutil.ReadDir(base)
dirs, err := os.ReadDir(base)
if err != nil && !os.IsNotExist(err) {
return errors.Wrap(err, "failed to read base directory")
}

View File

@@ -18,7 +18,6 @@ package server
import (
"encoding/json"
"io/ioutil"
"os"
"testing"
@@ -73,7 +72,7 @@ func newTestCRIService() *criService {
func TestLoadBaseOCISpec(t *testing.T) {
spec := oci.Spec{Version: "1.0.2", Hostname: "default"}
file, err := ioutil.TempFile("", "spec-test-")
file, err := os.CreateTemp("", "spec-test-")
require.NoError(t, err)
defer func() {

View File

@@ -17,7 +17,6 @@
package server
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -94,11 +93,11 @@ func TestUpdateRuntimeConfig(t *testing.T) {
},
} {
t.Run(name, func(t *testing.T) {
testDir, err := ioutil.TempDir(os.TempDir(), "test-runtime-config")
testDir, err := os.MkdirTemp(os.TempDir(), "test-runtime-config")
require.NoError(t, err)
defer os.RemoveAll(testDir)
templateName := filepath.Join(testDir, "template")
err = ioutil.WriteFile(templateName, []byte(testTemplate), 0666)
err = os.WriteFile(templateName, []byte(testTemplate), 0666)
require.NoError(t, err)
confDir := filepath.Join(testDir, "net.d")
confName := filepath.Join(confDir, cniConfigFileName)
@@ -131,7 +130,7 @@ func TestUpdateRuntimeConfig(t *testing.T) {
_, err := os.Stat(confName)
assert.Error(t, err)
} else {
got, err := ioutil.ReadFile(confName)
got, err := os.ReadFile(confName)
assert.NoError(t, err)
assert.Equal(t, expected, string(got))
}

View File

@@ -18,7 +18,6 @@ package container
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"sync"
@@ -182,7 +181,7 @@ func StoreStatus(root, id string, status Status) (StatusStorage, error) {
// writing to the file during loading.
func LoadStatus(root, id string) (Status, error) {
path := filepath.Join(root, "status")
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return Status{}, errors.Wrapf(err, "failed to read status from %q", path)
}

View File

@@ -19,7 +19,6 @@ package container
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -115,7 +114,7 @@ func TestStatus(t *testing.T) {
assert := assertlib.New(t)
require := requirelib.New(t)
tempDir, err := ioutil.TempDir(os.TempDir(), "status-test")
tempDir, err := os.MkdirTemp(os.TempDir(), "status-test")
require.NoError(err)
defer os.RemoveAll(tempDir)
statusFile := filepath.Join(tempDir, "status")

View File

@@ -18,7 +18,6 @@ package image
import (
"io"
"io/ioutil"
"math/rand"
"testing"
@@ -27,7 +26,7 @@ import (
func TestReferenceSorting(t *testing.T) {
digested := func(seed int64) string {
b, err := ioutil.ReadAll(io.LimitReader(rand.New(rand.NewSource(seed)), 64))
b, err := io.ReadAll(io.LimitReader(rand.New(rand.NewSource(seed)), 64))
if err != nil {
panic(err)
}

View File

@@ -17,7 +17,6 @@
package ioutil
import (
"io/ioutil"
"os"
"sort"
"strconv"
@@ -69,7 +68,7 @@ func TestSerialWriteCloser(t *testing.T) {
testData[i] = []byte(repeatNumber(i, dataLen) + "\n")
}
f, err := ioutil.TempFile("", "serial-write-closer")
f, err := os.CreateTemp("", "serial-write-closer")
require.NoError(t, err)
defer os.RemoveAll(f.Name())
defer f.Close()
@@ -91,7 +90,7 @@ func TestSerialWriteCloser(t *testing.T) {
wc.Close()
// Check test result
content, err := ioutil.ReadFile(f.Name())
content, err := os.ReadFile(f.Name())
require.NoError(t, err)
resultData := strings.Split(strings.TrimSpace(string(content)), "\n")
require.Len(t, resultData, goroutine)

View File

@@ -18,7 +18,6 @@ package os
import (
"io"
"io/ioutil"
"os"
"github.com/moby/sys/symlink"
@@ -78,9 +77,9 @@ func (RealOS) CopyFile(src, dest string, perm os.FileMode) error {
return err
}
// WriteFile will call ioutil.WriteFile to write data into a file.
// WriteFile will call os.WriteFile to write data into a file.
func (RealOS) WriteFile(filename string, data []byte, perm os.FileMode) error {
return ioutil.WriteFile(filename, data, perm)
return os.WriteFile(filename, data, perm)
}
// Hostname will call os.Hostname to get the hostname of the host.

View File

@@ -19,7 +19,6 @@ package os
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
@@ -162,7 +161,7 @@ func setupVHDVolume(t *testing.T, vhdPath string) string {
}
func writeFile(t *testing.T, path string, content []byte) {
if err := ioutil.WriteFile(path, content, 0644); err != nil {
if err := os.WriteFile(path, content, 0644); err != nil {
t.Fatal(err)
}
}

View File

@@ -21,8 +21,8 @@ package process
import (
"context"
"io/ioutil"
"net/url"
"os"
"testing"
"github.com/containerd/containerd/namespaces"
@@ -68,6 +68,6 @@ func TestNewBinaryIOCleanup(t *testing.T) {
func descriptorCount(t *testing.T) int {
t.Helper()
files, _ := ioutil.ReadDir("/proc/self/fd")
files, _ := os.ReadDir("/proc/self/fd")
return len(files)
}

View File

@@ -19,7 +19,6 @@ package testutil
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
@@ -53,7 +52,7 @@ func DumpDir(t *testing.T, root string) {
}
t.Log(fi.Mode(), fmt.Sprintf("%10s", ""), path, "->", target)
} else if fi.Mode().IsRegular() {
p, err := ioutil.ReadFile(path)
p, err := os.ReadFile(path)
if err != nil {
t.Logf("error reading file: %v", err)
return nil