Skip to content

Commit a469c0f

Browse files
authored
Merge pull request #762 from austinvazquez/remove-ioutil-package
Remove io/ioutil package references.
2 parents a6a0738 + 0f9ac2c commit a469c0f

File tree

22 files changed

+73
-92
lines changed

22 files changed

+73
-92
lines changed

cgroups/cgroups.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cgroups
33
import (
44
"bufio"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"path/filepath"
98
"strings"
@@ -78,7 +77,7 @@ func FindCgroup() (Cgroup, error) {
7877

7978
// GetSubsystemPath gets path of subsystem
8079
func GetSubsystemPath(pid int, subsystem string) (string, error) {
81-
contents, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cgroup", pid))
80+
contents, err := os.ReadFile(fmt.Sprintf("/proc/%d/cgroup", pid))
8281
if err != nil {
8382
return "", err
8483
}

cgroups/cgroups_v1.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cgroups
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76
"path/filepath"
87
"regexp"
@@ -62,7 +61,7 @@ func (cg *CgroupV1) GetBlockIOData(pid int, cgPath string) (*rspec.LinuxBlockIO,
6261
}
6362
filePath = filepath.Join(cg.MountPath, "blkio", subPath, fileName)
6463
}
65-
contents, err := ioutil.ReadFile(filePath)
64+
contents, err := os.ReadFile(filePath)
6665
if err != nil {
6766
if os.IsNotExist(err) {
6867
return nil, specerror.NewError(specerror.CgroupsPathAttach, fmt.Errorf("The runtime MUST consistently attach to the same place in the cgroups hierarchy given the same value of `cgroupsPath`"), rspec.Version)
@@ -237,7 +236,7 @@ func (cg *CgroupV1) GetCPUData(pid int, cgPath string) (*rspec.LinuxCPU, error)
237236
}
238237
filePath = filepath.Join(cg.MountPath, "cpu", subPath, fileName)
239238
}
240-
contents, err := ioutil.ReadFile(filePath)
239+
contents, err := os.ReadFile(filePath)
241240
if err != nil {
242241
if os.IsNotExist(err) {
243242
return nil, specerror.NewError(specerror.CgroupsPathAttach, fmt.Errorf("The runtime MUST consistently attach to the same place in the cgroups hierarchy given the same value of `cgroupsPath`"), rspec.Version)
@@ -271,7 +270,7 @@ func (cg *CgroupV1) GetCPUData(pid int, cgPath string) (*rspec.LinuxCPU, error)
271270
}
272271
// CONFIG_RT_GROUP_SCHED may be not set
273272
// Can always get rt data from /proc
274-
contents, err := ioutil.ReadFile("/proc/sys/kernel/sched_rt_period_us")
273+
contents, err := os.ReadFile("/proc/sys/kernel/sched_rt_period_us")
275274
if err != nil {
276275
return nil, err
277276
}
@@ -280,7 +279,7 @@ func (cg *CgroupV1) GetCPUData(pid int, cgPath string) (*rspec.LinuxCPU, error)
280279
return nil, err
281280
}
282281
lc.RealtimePeriod = &rtPeriod
283-
contents, err = ioutil.ReadFile("/proc/sys/kernel/sched_rt_runtime_us")
282+
contents, err = os.ReadFile("/proc/sys/kernel/sched_rt_runtime_us")
284283
if err != nil {
285284
return nil, err
286285
}
@@ -304,7 +303,7 @@ func (cg *CgroupV1) GetCPUData(pid int, cgPath string) (*rspec.LinuxCPU, error)
304303
}
305304
filePath = filepath.Join(cg.MountPath, "cpuset", subPath, fileName)
306305
}
307-
contents, err := ioutil.ReadFile(filePath)
306+
contents, err := os.ReadFile(filePath)
308307
if err != nil {
309308
return nil, err
310309
}
@@ -334,7 +333,7 @@ func (cg *CgroupV1) GetDevicesData(pid int, cgPath string) ([]rspec.LinuxDeviceC
334333
}
335334
filePath = filepath.Join(cg.MountPath, "devices", subPath, fileName)
336335
}
337-
contents, err := ioutil.ReadFile(filePath)
336+
contents, err := os.ReadFile(filePath)
338337
if err != nil {
339338
return nil, err
340339
}
@@ -403,12 +402,12 @@ func inBytes(size string) (int64, error) {
403402
// eg. ["64KB", "2MB", "1GB"]
404403
func GetHugePageSize() ([]string, error) {
405404
var pageSizes []string
406-
files, err := ioutil.ReadDir("/sys/kernel/mm/hugepages")
405+
entries, err := os.ReadDir("/sys/kernel/mm/hugepages")
407406
if err != nil {
408407
return pageSizes, err
409408
}
410-
for _, st := range files {
411-
nameArray := strings.Split(st.Name(), "-")
409+
for _, entry := range entries {
410+
nameArray := strings.Split(entry.Name(), "-")
412411
pageSize, err := inBytes(nameArray[1])
413412
if err != nil {
414413
return []string{}, err
@@ -457,7 +456,7 @@ func (cg *CgroupV1) GetHugepageLimitData(pid int, cgPath string) ([]rspec.LinuxH
457456
}
458457
filePath = filepath.Join(cg.MountPath, "hugetlb", subPath, maxUsage)
459458
}
460-
contents, err := ioutil.ReadFile(filePath)
459+
contents, err := os.ReadFile(filePath)
461460
if err != nil {
462461
if os.IsNotExist(err) {
463462
return nil, specerror.NewError(specerror.CgroupsPathAttach, fmt.Errorf("The runtime MUST consistently attach to the same place in the cgroups hierarchy given the same value of `cgroupsPath`"), rspec.Version)
@@ -504,7 +503,7 @@ func (cg *CgroupV1) GetMemoryData(pid int, cgPath string) (*rspec.LinuxMemory, e
504503
}
505504
filePath = filepath.Join(cg.MountPath, "memory", subPath, fileName)
506505
}
507-
contents, err := ioutil.ReadFile(filePath)
506+
contents, err := os.ReadFile(filePath)
508507
if err != nil {
509508
if os.IsNotExist(err) {
510509
return nil, specerror.NewError(specerror.CgroupsPathAttach, fmt.Errorf("The runtime MUST consistently attach to the same place in the cgroups hierarchy given the same value of `cgroupsPath`"), rspec.Version)
@@ -597,7 +596,7 @@ func (cg *CgroupV1) GetNetworkData(pid int, cgPath string) (*rspec.LinuxNetwork,
597596
}
598597
filePath = filepath.Join(cg.MountPath, "net_cls", subPath, fileName)
599598
}
600-
contents, err := ioutil.ReadFile(filePath)
599+
contents, err := os.ReadFile(filePath)
601600
if err != nil {
602601
if os.IsNotExist(err) {
603602
return nil, specerror.NewError(specerror.CgroupsPathAttach, fmt.Errorf("The runtime MUST consistently attach to the same place in the cgroups hierarchy given the same value of `cgroupsPath`"), rspec.Version)
@@ -624,7 +623,7 @@ func (cg *CgroupV1) GetNetworkData(pid int, cgPath string) (*rspec.LinuxNetwork,
624623
}
625624
filePath = filepath.Join(cg.MountPath, "net_prio", subPath, fileName)
626625
}
627-
contents, err = ioutil.ReadFile(filePath)
626+
contents, err = os.ReadFile(filePath)
628627
if err != nil {
629628
return nil, err
630629
}
@@ -668,7 +667,7 @@ func (cg *CgroupV1) GetPidsData(pid int, cgPath string) (*rspec.LinuxPids, error
668667
}
669668
filePath = filepath.Join(cg.MountPath, "pids", subPath, fileName)
670669
}
671-
contents, err := ioutil.ReadFile(filePath)
670+
contents, err := os.ReadFile(filePath)
672671
if err != nil {
673672
return nil, err
674673
}

cmd/runtimetest/main.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"errors"
88
"fmt"
99
"io"
10-
"io/ioutil"
1110
"os"
1211
"path/filepath"
1312
"runtime"
@@ -230,7 +229,7 @@ func (c *complianceTester) validateLinuxProcess(spec *rspec.Spec) error {
230229
return nil
231230
}
232231

233-
cmdlineBytes, err := ioutil.ReadFile("/proc/self/cmdline")
232+
cmdlineBytes, err := os.ReadFile("/proc/self/cmdline")
234233
if err != nil {
235234
return err
236235
}
@@ -399,7 +398,7 @@ func (c *complianceTester) validateSysctls(spec *rspec.Spec) error {
399398

400399
for k, v := range spec.Linux.Sysctl {
401400
keyPath := filepath.Join("/proc/sys", strings.Replace(k, ".", "/", -1))
402-
vBytes, err := ioutil.ReadFile(keyPath)
401+
vBytes, err := os.ReadFile(keyPath)
403402
if err != nil {
404403
return err
405404
}
@@ -437,8 +436,8 @@ func testReadAccess(path string) (readable bool, err error) {
437436
}
438437

439438
func testDirectoryReadAccess(path string) (readable bool, err error) {
440-
files, err := ioutil.ReadDir(path)
441-
if err == io.EOF || len(files) == 0 {
439+
entries, err := os.ReadDir(path)
440+
if err == io.EOF || len(entries) == 0 {
442441
// Our validation/ tests only use non-empty directories for read-access
443442
// tests. So if we get an EOF on the first read, the runtime did
444443
// successfully block readability. So it should not be considered as test
@@ -493,7 +492,7 @@ func testWriteAccess(path string) (writable bool, err error) {
493492
}
494493

495494
func testDirectoryWriteAccess(path string) (writable bool, err error) {
496-
tmpfile, err := ioutil.TempFile(path, "Test")
495+
tmpfile, err := os.CreateTemp(path, "Test")
497496
if err != nil {
498497
return false, nil
499498
}
@@ -502,7 +501,7 @@ func testDirectoryWriteAccess(path string) (writable bool, err error) {
502501
}
503502

504503
func testFileWriteAccess(path string) (readable bool, err error) {
505-
err = ioutil.WriteFile(path, []byte("a"), 0644)
504+
err = os.WriteFile(path, []byte("a"), 0644)
506505
if err == nil {
507506
return true, nil
508507
}
@@ -542,7 +541,7 @@ func (c *complianceTester) validateRootfsPropagation(spec *rspec.Spec) error {
542541
return nil
543542
}
544543

545-
targetDir, err := ioutil.TempDir("/", "target")
544+
targetDir, err := os.MkdirTemp("/", "target")
546545
if err != nil {
547546
return err
548547
}
@@ -564,19 +563,19 @@ func (c *complianceTester) validateRootfsPropagation(spec *rspec.Spec) error {
564563
if mountErr != nil {
565564
return fmt.Errorf("bind-mount / %s: %w", targetDir, err)
566565
}
567-
mountDir, err := ioutil.TempDir("/", "mount")
566+
mountDir, err := os.MkdirTemp("/", "mount")
568567
if err != nil {
569568
return err
570569
}
571570
defer os.RemoveAll(mountDir)
572571

573-
testDir, err := ioutil.TempDir("/", "test")
572+
testDir, err := os.MkdirTemp("/", "test")
574573
if err != nil {
575574
return err
576575
}
577576
defer os.RemoveAll(testDir)
578577

579-
tmpfile, err := ioutil.TempFile(testDir, "example")
578+
tmpfile, err := os.CreateTemp(testDir, "example")
580579
if err != nil {
581580
return err
582581
}

generate/generate_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package generate_test
22

33
import (
4-
"io/ioutil"
54
"os"
65
"path/filepath"
76
"runtime"
@@ -31,7 +30,7 @@ func TestGenerateValid(t *testing.T) {
3130
continue
3231
}
3332

34-
bundle, err := ioutil.TempDir("", "TestGenerateValid_bundle")
33+
bundle, err := os.MkdirTemp("", "TestGenerateValid_bundle")
3534
if err != nil {
3635
t.Fatal(err)
3736
}

validate/validate.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8-
"io/ioutil"
98
"net"
109
"os"
1110
"path/filepath"
@@ -88,7 +87,7 @@ func NewValidatorFromPath(bundlePath string, hostSpecific bool, platform string)
8887
}
8988

9089
configPath := filepath.Join(bundlePath, specConfig)
91-
content, err := ioutil.ReadFile(configPath)
90+
content, err := os.ReadFile(configPath)
9291
if err != nil {
9392
return Validator{}, specerror.NewError(specerror.ConfigInRootBundleDir, err, rspec.Version)
9493
}

validate/validate_test.go

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

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76
"path/filepath"
87
"runtime"
@@ -263,7 +262,7 @@ func TestJSONSchema(t *testing.T) {
263262
}
264263

265264
func TestCheckRoot(t *testing.T) {
266-
tmpBundle, err := ioutil.TempDir("", "oci-check-rootfspath")
265+
tmpBundle, err := os.MkdirTemp("", "oci-check-rootfspath")
267266
if err != nil {
268267
t.Fatalf("Failed to create a TempDir in 'CheckRoot'")
269268
}

validation/delete_only_create_resources/delete_only_create_resources.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ package main
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76
"path/filepath"
87
"strconv"
98
"time"
109

10+
"github.com/google/uuid"
1111
tap "github.com/mndrix/tap-go"
1212
"github.com/mrunalp/fileutils"
1313
rspec "github.com/opencontainers/runtime-spec/specs-go"
1414
"github.com/opencontainers/runtime-tools/specerror"
1515
"github.com/opencontainers/runtime-tools/validation/util"
16-
"github.com/google/uuid"
1716
)
1817

1918
func main() {
@@ -62,7 +61,7 @@ func main() {
6261
util.Fatal(err)
6362
}
6463
// Add the container to the cgroup
65-
err = ioutil.WriteFile(filepath.Join(testPath, "tasks"), []byte(strconv.Itoa(state.Pid)), 0644)
64+
err = os.WriteFile(filepath.Join(testPath, "tasks"), []byte(strconv.Itoa(state.Pid)), 0644)
6665
if err != nil {
6766
util.Fatal(err)
6867
}

validation/hooks/hooks.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ package main
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"os"
66
"os/exec"
77
"path/filepath"
88
"time"
99

10+
"github.com/google/uuid"
1011
tap "github.com/mndrix/tap-go"
1112
rspec "github.com/opencontainers/runtime-spec/specs-go"
1213
"github.com/opencontainers/runtime-tools/specerror"
1314
"github.com/opencontainers/runtime-tools/validation/util"
14-
"github.com/google/uuid"
1515
)
1616

1717
func main() {
@@ -75,7 +75,7 @@ func main() {
7575
}
7676

7777
err := util.RuntimeLifecycleValidate(config)
78-
outputData, _ := ioutil.ReadFile(output)
78+
outputData, _ := os.ReadFile(output)
7979
if err == nil && string(outputData) != "pre-start1 called\npre-start2 called\npost-start1\npost-start2\npost-stop1\npost-stop2\n" {
8080
err = specerror.NewError(specerror.PosixHooksCalledInOrder, fmt.Errorf("Hooks MUST be called in the listed order"), rspec.Version)
8181
diagnostic := map[string]string{

validation/hooks_stdin/hooks_stdin.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7-
"io/ioutil"
87
"os"
98
"path/filepath"
109
"reflect"
1110
"time"
1211

12+
"github.com/google/uuid"
1313
multierror "github.com/hashicorp/go-multierror"
1414
tap "github.com/mndrix/tap-go"
1515
rspecs "github.com/opencontainers/runtime-spec/specs-go"
1616
"github.com/opencontainers/runtime-tools/specerror"
1717
"github.com/opencontainers/runtime-tools/validation/util"
18-
"github.com/google/uuid"
1918
)
2019

2120
func stdinStateCheck(outputDir, hookName string, expectedState rspecs.State) (errs *multierror.Error) {
2221
var state rspecs.State
23-
data, err := ioutil.ReadFile(filepath.Join(outputDir, hookName))
22+
data, err := os.ReadFile(filepath.Join(outputDir, hookName))
2423
if err != nil {
2524
errs = multierror.Append(errs, err)
2625
return

validation/linux_masked_paths/linux_masked_paths.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76
"path/filepath"
87

@@ -41,7 +40,7 @@ func checkMaskedPaths(t *tap.T) error {
4140
return err
4241
}
4342
// create a temp file to make testDir non-empty
44-
tmpfile, err := ioutil.TempFile(testDir, "tmp")
43+
tmpfile, err := os.CreateTemp(testDir, "tmp")
4544
if err != nil {
4645
return err
4746
}
@@ -50,17 +49,17 @@ func checkMaskedPaths(t *tap.T) error {
5049
// runtimetest cannot check the readability of empty files, so
5150
// write something.
5251
testSubSubFile := filepath.Join(path, maskedFileSubSub)
53-
if err := ioutil.WriteFile(testSubSubFile, []byte("secrets"), 0777); err != nil {
52+
if err := os.WriteFile(testSubSubFile, []byte("secrets"), 0777); err != nil {
5453
return err
5554
}
5655

5756
testSubFile := filepath.Join(path, maskedFileSub)
58-
if err := ioutil.WriteFile(testSubFile, []byte("secrets"), 0777); err != nil {
57+
if err := os.WriteFile(testSubFile, []byte("secrets"), 0777); err != nil {
5958
return err
6059
}
6160

6261
testFile := filepath.Join(path, maskedFile)
63-
return ioutil.WriteFile(testFile, []byte("secrets"), 0777)
62+
return os.WriteFile(testFile, []byte("secrets"), 0777)
6463
})
6564
return err
6665
}

0 commit comments

Comments
 (0)