Skip to content

Commit

Permalink
Remove io/ioutil use
Browse files Browse the repository at this point in the history
See https://golang.org/doc/go1.16#ioutil

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Oct 14, 2021
1 parent 6a4f4a6 commit 5516294
Show file tree
Hide file tree
Showing 28 changed files with 108 additions and 76 deletions.
5 changes: 2 additions & 3 deletions contrib/cmd/recvtty/recvtty.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"strings"
Expand Down Expand Up @@ -177,7 +176,7 @@ func handleNull(path string) error {
return
}

_, _ = io.Copy(ioutil.Discard, master)
_, _ = io.Copy(io.Discard, master)
}(conn)
}
}
Expand Down Expand Up @@ -225,7 +224,7 @@ func main() {
pidPath := ctx.String("pid-file")
if pidPath != "" {
pid := fmt.Sprintf("%d\n", os.Getpid())
if err := ioutil.WriteFile(pidPath, []byte(pid), 0o644); err != nil {
if err := os.WriteFile(pidPath, []byte(pid), 0o644); err != nil {
return err
}
}
Expand Down
3 changes: 1 addition & 2 deletions contrib/cmd/seccompagent/seccompagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"errors"
"flag"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -246,7 +245,7 @@ func main() {

if pidFile != "" {
pid := fmt.Sprintf("%d", os.Getpid())
if err := ioutil.WriteFile(pidFile, []byte(pid), 0o644); err != nil {
if err := os.WriteFile(pidFile, []byte(pid), 0o644); err != nil {
logrus.Fatalf("Cannot write pid file: %v", err)
}
}
Expand Down
3 changes: 1 addition & 2 deletions libcontainer/apparmor/apparmor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package apparmor
import (
"errors"
"fmt"
"io/ioutil"
"os"
"sync"

Expand All @@ -19,7 +18,7 @@ var (
func isEnabled() bool {
checkAppArmor.Do(func() {
if _, err := os.Stat("/sys/kernel/security/apparmor"); err == nil {
buf, err := ioutil.ReadFile("/sys/module/apparmor/parameters/enabled")
buf, err := os.ReadFile("/sys/module/apparmor/parameters/enabled")
appArmorEnabled = err == nil && len(buf) > 1 && buf[0] == 'Y'
}
})
Expand Down
4 changes: 2 additions & 2 deletions libcontainer/capabilities/capabilities_linux_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package capabilities

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

Expand All @@ -24,7 +24,7 @@ func TestNew(t *testing.T) {
hook := test.NewGlobal()
defer hook.Reset()

logrus.SetOutput(ioutil.Discard)
logrus.SetOutput(io.Discard)
caps, err := New(&conf)
logrus.SetOutput(os.Stderr)

Expand Down
2 changes: 1 addition & 1 deletion libcontainer/cgroups/fs/blkio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func TestBlkioSetMultipleWeightDevice(t *testing.T) {

wd1 := configs.NewWeightDevice(8, 0, 500, 0)
wd2 := configs.NewWeightDevice(8, 16, 500, 0)
// we cannot actually set and check both because normal ioutil.WriteFile
// we cannot actually set and check both because normal os.WriteFile
// when writing to cgroup file will overwrite the whole file content instead
// of updating it as the kernel is doing. Just check the second device
// is present will suffice for the test to ensure multiple writes are done.
Expand Down
4 changes: 2 additions & 2 deletions libcontainer/cgroups/fs2/io_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fs2

import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestStatIo(t *testing.T) {
fakeCgroupDir := t.TempDir()
statPath := filepath.Join(fakeCgroupDir, "io.stat")

if err := ioutil.WriteFile(statPath, []byte(exampleIoStatData), 0o644); err != nil {
if err := os.WriteFile(statPath, []byte(exampleIoStatData), 0o644); err != nil {
t.Fatal(err)
}

Expand Down
11 changes: 5 additions & 6 deletions libcontainer/cgroups/fscommon/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fscommon

import (
"io/ioutil"
"math"
"os"
"path/filepath"
Expand All @@ -27,7 +26,7 @@ func TestGetCgroupParamsInt(t *testing.T) {
tempFile := filepath.Join(tempDir, cgroupFile)

// Success.
if err := ioutil.WriteFile(tempFile, []byte(floatString), 0o755); err != nil {
if err := os.WriteFile(tempFile, []byte(floatString), 0o755); err != nil {
t.Fatal(err)
}
value, err := GetCgroupParamUint(tempDir, cgroupFile)
Expand All @@ -38,7 +37,7 @@ func TestGetCgroupParamsInt(t *testing.T) {
}

// Success with new line.
err = ioutil.WriteFile(tempFile, []byte(floatString+"\n"), 0o755)
err = os.WriteFile(tempFile, []byte(floatString+"\n"), 0o755)
if err != nil {
t.Fatal(err)
}
Expand All @@ -50,7 +49,7 @@ func TestGetCgroupParamsInt(t *testing.T) {
}

// Success with negative values
err = ioutil.WriteFile(tempFile, []byte("-12345"), 0o755)
err = os.WriteFile(tempFile, []byte("-12345"), 0o755)
if err != nil {
t.Fatal(err)
}
Expand All @@ -63,7 +62,7 @@ func TestGetCgroupParamsInt(t *testing.T) {

// Success with negative values lesser than min int64
s := strconv.FormatFloat(math.MinInt64, 'f', -1, 64)
err = ioutil.WriteFile(tempFile, []byte(s), 0o755)
err = os.WriteFile(tempFile, []byte(s), 0o755)
if err != nil {
t.Fatal(err)
}
Expand All @@ -75,7 +74,7 @@ func TestGetCgroupParamsInt(t *testing.T) {
}

// Not a float.
err = ioutil.WriteFile(tempFile, []byte("not-a-float"), 0o755)
err = os.WriteFile(tempFile, []byte("not-a-float"), 0o755)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions libcontainer/cgroups/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -243,7 +242,7 @@ func RemovePath(path string) error {
return nil
}

infos, err := ioutil.ReadDir(path)
infos, err := os.ReadDir(path)
if err != nil {
if os.IsNotExist(err) {
err = nil
Expand Down
3 changes: 1 addition & 2 deletions libcontainer/configs/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package configs_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -190,7 +189,7 @@ exit 0
verifyCommand := fmt.Sprintf(verifyCommandTemplate, stateJson)
filename := "/tmp/runc-hooktest.sh"
os.Remove(filename)
if err := ioutil.WriteFile(filename, []byte(verifyCommand), 0o700); err != nil {
if err := os.WriteFile(filename, []byte(verifyCommand), 0o700); err != nil {
t.Fatalf("Failed to create tmp file: %v", err)
}
defer os.Remove(filename)
Expand Down
9 changes: 4 additions & 5 deletions libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
Expand Down Expand Up @@ -286,7 +285,7 @@ func (c *linuxContainer) exec() error {
}

func readFromExecFifo(execFifo io.Reader) error {
data, err := ioutil.ReadAll(execFifo)
data, err := io.ReadAll(execFifo)
if err != nil {
return err
}
Expand Down Expand Up @@ -1151,7 +1150,7 @@ func (c *linuxContainer) Checkpoint(criuOpts *CriuOpts) error {
return err
}

err = ioutil.WriteFile(filepath.Join(criuOpts.ImagesDirectory, descriptorsFilename), fdsJSON, 0o600)
err = os.WriteFile(filepath.Join(criuOpts.ImagesDirectory, descriptorsFilename), fdsJSON, 0o600)
if err != nil {
return err
}
Expand Down Expand Up @@ -1455,7 +1454,7 @@ func (c *linuxContainer) Restore(process *Process, criuOpts *CriuOpts) error {
fds []string
fdJSON []byte
)
if fdJSON, err = ioutil.ReadFile(filepath.Join(criuOpts.ImagesDirectory, descriptorsFilename)); err != nil {
if fdJSON, err = os.ReadFile(filepath.Join(criuOpts.ImagesDirectory, descriptorsFilename)); err != nil {
return err
}

Expand Down Expand Up @@ -1850,7 +1849,7 @@ func (c *linuxContainer) updateState(process parentProcess) (*State, error) {
}

func (c *linuxContainer) saveState(s *State) (retErr error) {
tmpFile, err := ioutil.TempFile(c.root, "state-")
tmpFile, err := os.CreateTemp(c.root, "state-")
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions libcontainer/devices/device_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package devices

import (
"errors"
"io/ioutil"
"os"
"path/filepath"

Expand All @@ -17,8 +16,8 @@ var ErrNotADevice = errors.New("not a device node")

// Testing dependencies
var (
unixLstat = unix.Lstat
ioutilReadDir = ioutil.ReadDir
unixLstat = unix.Lstat
osReadDir = os.ReadDir
)

func mkDev(d *Rule) (uint64, error) {
Expand Down Expand Up @@ -77,7 +76,7 @@ func HostDevices() ([]*Device, error) {
// GetDevices recursively traverses a directory specified by path
// and returns all devices found there.
func GetDevices(path string) ([]*Device, error) {
files, err := ioutilReadDir(path)
files, err := osReadDir(path)
if err != nil {
return nil, err
}
Expand Down
39 changes: 39 additions & 0 deletions libcontainer/devices/device_unix_go116_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//go:build !go1.17
// +build !go1.17

package devices

import "io/fs"

// The following code is adapted from go1.17.1/src/io/fs/readdir.go
// to compensate for the lack of fs.FileInfoToDirEntry in Go 1.16.

// dirInfo is a DirEntry based on a FileInfo.
type dirInfo struct {
fileInfo fs.FileInfo
}

func (di dirInfo) IsDir() bool {
return di.fileInfo.IsDir()
}

func (di dirInfo) Type() fs.FileMode {
return di.fileInfo.Mode().Type()
}

func (di dirInfo) Info() (fs.FileInfo, error) {
return di.fileInfo, nil
}

func (di dirInfo) Name() string {
return di.fileInfo.Name()
}

// fileInfoToDirEntry returns a DirEntry that returns information from info.
// If info is nil, FileInfoToDirEntry returns nil.
func fileInfoToDirEntry(info fs.FileInfo) fs.DirEntry {
if info == nil {
return nil
}
return dirInfo{fileInfo: info}
}
8 changes: 8 additions & 0 deletions libcontainer/devices/device_unix_go117_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build go1.17
// +build go1.17

package devices

import "io/fs"

var fileInfoToDirEntry = fs.FileInfoToDirEntry
14 changes: 7 additions & 7 deletions libcontainer/devices/device_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package devices

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

Expand All @@ -14,7 +14,7 @@ import (

func cleanupTest() {
unixLstat = unix.Lstat
ioutilReadDir = ioutil.ReadDir
osReadDir = os.ReadDir
}

func TestDeviceFromPathLstatFailure(t *testing.T) {
Expand All @@ -35,8 +35,8 @@ func TestDeviceFromPathLstatFailure(t *testing.T) {
func TestHostDevicesIoutilReadDirFailure(t *testing.T) {
testError := errors.New("test error")

// Override ioutil.ReadDir to inject error.
ioutilReadDir = func(dirname string) ([]os.FileInfo, error) {
// Override os.ReadDir to inject error.
osReadDir = func(dirname string) ([]fs.DirEntry, error) {
return nil, testError
}
defer cleanupTest()
Expand All @@ -51,8 +51,8 @@ func TestHostDevicesIoutilReadDirDeepFailure(t *testing.T) {
testError := errors.New("test error")
called := false

// Override ioutil.ReadDir to inject error after the first call.
ioutilReadDir = func(dirname string) ([]os.FileInfo, error) {
// Override os.ReadDir to inject error after the first call.
osReadDir = func(dirname string) ([]fs.DirEntry, error) {
if called {
return nil, testError
}
Expand All @@ -64,7 +64,7 @@ func TestHostDevicesIoutilReadDirDeepFailure(t *testing.T) {
t.Fatalf("Unexpected error %v", err)
}

return []os.FileInfo{fi}, nil
return []fs.DirEntry{fileInfoToDirEntry(fi)}, nil
}
defer cleanupTest()

Expand Down
3 changes: 1 addition & 2 deletions libcontainer/init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"strings"
Expand Down Expand Up @@ -358,7 +357,7 @@ func setupUser(config *initConfig) error {
return err
}

setgroups, err := ioutil.ReadFile("/proc/self/setgroups")
setgroups, err := os.ReadFile("/proc/self/setgroups")
if err != nil && !os.IsNotExist(err) {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions libcontainer/integration/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -879,7 +878,7 @@ func TestMountCmds(t *testing.T) {
// Wait for process
waitProcess(&pconfig, t)

entries, err := ioutil.ReadDir(tmpDir)
entries, err := os.ReadDir(tmpDir)
ok(t, err)
expected := []string{"hello", "hello-backup", "world", "world-backup"}
for i, e := range entries {
Expand Down
Loading

0 comments on commit 5516294

Please sign in to comment.