Skip to content

Commit

Permalink
many: remove usages of deprecated io/ioutil package (#13768)
Browse files Browse the repository at this point in the history
* many: remove usages of deprecated io/ioutil package

Signed-off-by: Miguel Pires <[email protected]>

* .golangci.yml: remove errcheck ignore rule for io/ioutil

Signed-off-by: Miguel Pires <[email protected]>

* run-checks: prevent new usages of io/ioutil

Signed-off-by: Miguel Pires <[email protected]>

---------

Signed-off-by: Miguel Pires <[email protected]>
  • Loading branch information
miguelpires authored Apr 3, 2024
1 parent 3307fea commit 79c5ac1
Show file tree
Hide file tree
Showing 199 changed files with 730 additions and 771 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ linters-settings:
# [deprecated] comma-separated list of pairs of the form pkg:regex
# the regex is used to ignore names within pkg. (default "fmt:.*").
# see https://github.com/kisielk/errcheck#the-deprecated-method for details
ignore: fmt:.*,io/ioutil:^Read.*
ignore: fmt:.*

# path to a file containing a list of functions to exclude from checking
# see https://github.com/kisielk/errcheck#excluding-functions for details
Expand Down
3 changes: 1 addition & 2 deletions asserts/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -137,7 +136,7 @@ func (dbs *databaseSuite) TestImportKey(c *C) {
c.Check(info.Mode().Perm(), Equals, os.FileMode(0600)) // secret
// too much "clear box" testing? ok at least until we have
// more functionality
privKey, err := ioutil.ReadFile(keyPath)
privKey, err := os.ReadFile(keyPath)
c.Assert(err, IsNil)

privKeyFromDisk, err := asserts.DecodePrivateKeyInTest(privKey)
Expand Down
3 changes: 1 addition & 2 deletions asserts/fsentryutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package asserts

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

Expand Down Expand Up @@ -66,7 +65,7 @@ func entryExists(top string, subpath ...string) bool {

func readEntry(top string, subpath ...string) ([]byte, error) {
fpath := filepath.Join(top, filepath.Join(subpath...))
return ioutil.ReadFile(fpath)
return os.ReadFile(fpath)
}

func removeEntry(top string, subpath ...string) error {
Expand Down
5 changes: 2 additions & 3 deletions boot/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package boot
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -224,7 +223,7 @@ func BootFlags(dev snap.Device) ([]string, error) {
// bootenv are for this boot or the next one, but the initramfs will always
// copy the flags that were set into /run, so we always know the current
// boot's flags are written in /run
b, err := ioutil.ReadFile(snapBootFlagsFile)
b, err := os.ReadFile(snapBootFlagsFile)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -314,7 +313,7 @@ func HostUbuntuDataForMode(mode string, mod gadget.Model) ([]string, error) {
// host mount is snap-bootstrap's /run/snapd/snap-bootstrap/degraded.json, so
// we have to go parse that
degradedJSONFile := filepath.Join(dirs.SnapBootstrapRunDir, "degraded.json")
b, err := ioutil.ReadFile(degradedJSONFile)
b, err := os.ReadFile(degradedJSONFile)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions boot/modeenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -209,7 +208,7 @@ base_status=try
err = dupDiskModeenv.Write()
c.Assert(err, IsNil)
c.Assert(dirs.SnapModeenvFileUnder(s.tmpdir), testutil.FilePresent)
origBytes, err := ioutil.ReadFile(dirs.SnapModeenvFileUnder(s.tmpdir) + ".orig")
origBytes, err := os.ReadFile(dirs.SnapModeenvFileUnder(s.tmpdir) + ".orig")
c.Assert(err, IsNil)
// the files should be the same
c.Assert(dirs.SnapModeenvFileUnder(s.tmpdir), testutil.FileEquals, string(origBytes))
Expand Down
3 changes: 1 addition & 2 deletions bootloader/assets/genasset/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package main_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -84,7 +83,7 @@ func (s *generateAssetsTestSuite) TestSimpleAsset(c *C) {
c.Assert(err, IsNil)
err = generate.Run("asset-name", filepath.Join(d, "in"), filepath.Join(d, "out"))
c.Assert(err, IsNil)
data, err := ioutil.ReadFile(filepath.Join(d, "out"))
data, err := os.ReadFile(filepath.Join(d, "out"))
c.Assert(err, IsNil)

const exp = `// -*- Mode: Go; indent-tabs-mode: t -*-
Expand Down
4 changes: 2 additions & 2 deletions bootloader/assets/grub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package assets_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"

. "gopkg.in/check.v1"
Expand Down Expand Up @@ -160,7 +160,7 @@ func (s *grubAssetsTestSuite) TestGrubAssetsWereRegenerated(c *C) {
} {
assetData := assets.Internal(tc.asset)
c.Assert(assetData, NotNil)
data, err := ioutil.ReadFile(tc.file)
data, err := os.ReadFile(tc.file)
c.Assert(err, IsNil)
c.Check(assetData, DeepEquals, data, Commentf("asset %q has not been updated", tc.asset))
}
Expand Down
5 changes: 2 additions & 3 deletions bootloader/efi/efi.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"unicode/utf16"
Expand Down Expand Up @@ -113,7 +112,7 @@ func ReadVarBytes(name string) ([]byte, VariableAttr, error) {
return nil, 0, cannotReadError(name, err)
}
defer varf.Close()
b, err := ioutil.ReadAll(varf)
b, err := io.ReadAll(varf)
if err != nil {
return nil, 0, cannotReadError(name, err)
}
Expand Down Expand Up @@ -173,7 +172,7 @@ func MockVars(vars map[string][]byte, attrs map[string]VariableAttr) (restore fu
if !ok {
attr = VariableRuntimeAccess | VariableBootServiceAccess
}
return ioutil.NopCloser(bytes.NewBuffer(val)), attr, int64(len(val)), nil
return io.NopCloser(bytes.NewBuffer(val)), attr, int64(len(val)), nil
}
return nil, 0, 0, fmt.Errorf("EFI variable %s not mocked", name)
}
Expand Down
3 changes: 1 addition & 2 deletions bootloader/grubenv/grubenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package grubenv
import (
"bytes"
"fmt"
"io/ioutil"
"os"

"github.com/snapcore/snapd/strutil"
Expand Down Expand Up @@ -56,7 +55,7 @@ func (g *Env) Set(key, value string) {
}

func (g *Env) Load() error {
buf, err := ioutil.ReadFile(g.path)
buf, err := os.ReadFile(g.path)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions bootloader/lk.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package bootloader
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -444,7 +443,7 @@ func (l *lk) ExtractKernelAssets(s snap.PlaceInfo, snapf snap.Container) error {
// this is live system, extracted bootimg needs to be flashed to
// free bootimg partition and env has to be updated with
// new kernel snap to bootimg partition mapping
tmpdir, err := ioutil.TempDir("", "bootimg")
tmpdir, err := os.MkdirTemp("", "bootimg")
if err != nil {
return fmt.Errorf("cannot create temp directory: %v", err)
}
Expand Down
11 changes: 5 additions & 6 deletions bootloader/lk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package bootloader_test

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -259,7 +258,7 @@ func (s *lkTestSuite) TestExtractKernelAssetsUnpacksBootimgImageBuilding(c *C) {
c.Assert(err, IsNil)

// just boot.img and snapbootsel.bin are there, no kernel.img
infos, err := ioutil.ReadDir(filepath.Join(s.rootdir, "boot", "lk", ""))
infos, err := os.ReadDir(filepath.Join(s.rootdir, "boot", "lk", ""))
c.Assert(err, IsNil)
var fnames []string
for _, info := range infos {
Expand Down Expand Up @@ -367,13 +366,13 @@ func (s *lkTestSuite) TestExtractKernelAssetsUnpacksAndRemoveInRuntimeMode(c *C)

// and validate it went to the "boot_a" partition
bootA := filepath.Join(s.rootdir, "/dev/disk/by-partlabel/boot_a")
content, err := ioutil.ReadFile(bootA)
content, err := os.ReadFile(bootA)
c.Assert(err, IsNil)
c.Assert(string(content), Equals, "I'm the default boot image name")

// also validate that bootB is empty
bootB := filepath.Join(s.rootdir, "/dev/disk/by-partlabel/boot_b")
content, err = ioutil.ReadFile(bootB)
content, err = os.ReadFile(bootB)
c.Assert(err, IsNil)
c.Assert(content, HasLen, 0)

Expand Down Expand Up @@ -451,15 +450,15 @@ func (s *lkTestSuite) TestExtractKernelAssetsUnpacksAndRemoveInRuntimeModeUC20(c
bootAPartUUID, err := disk.FindMatchingPartitionUUIDWithPartLabel("boot_a")
c.Assert(err, IsNil)
bootA := filepath.Join(s.rootdir, "/dev/disk/by-partuuid", bootAPartUUID)
content, err := ioutil.ReadFile(bootA)
content, err := os.ReadFile(bootA)
c.Assert(err, IsNil)
c.Assert(string(content), Equals, "I'm the default boot image name")

// also validate that bootB is empty
bootBPartUUID, err := disk.FindMatchingPartitionUUIDWithPartLabel("boot_b")
c.Assert(err, IsNil)
bootB := filepath.Join(s.rootdir, "/dev/disk/by-partuuid", bootBPartUUID)
content, err = ioutil.ReadFile(bootB)
content, err = os.ReadFile(bootB)
c.Assert(err, IsNil)
c.Assert(content, HasLen, 0)

Expand Down
9 changes: 4 additions & 5 deletions bootloader/piboot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package bootloader
import (
"encoding/binary"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -238,7 +237,7 @@ func (p *piboot) loadAndApplyConfig(env *ubootenv.Env) error {

// Writes os_prefix in RPi config.txt or tryboot.txt
func (p *piboot) writeRPiCfgWithOsPrefix(prefix, inFile, outFile string) error {
buf, err := ioutil.ReadFile(inFile)
buf, err := os.ReadFile(inFile)
if err != nil {
return err
}
Expand Down Expand Up @@ -268,7 +267,7 @@ func (p *piboot) writeRPiCfgWithOsPrefix(prefix, inFile, outFile string) error {
}

func (p *piboot) writeCmdline(env *ubootenv.Env, defaultsFile, outFile string) error {
buf, err := ioutil.ReadFile(defaultsFile)
buf, err := os.ReadFile(defaultsFile)
if err != nil {
return err
}
Expand Down Expand Up @@ -390,7 +389,7 @@ func (p *piboot) eepromVersionSupportsTryboot() (bool, error) {
// To find out the EEPROM version we do the same as the
// rpi-eeprom-update script (see
// https://github.com/raspberrypi/rpi-eeprom/blob/master/rpi-eeprom-update)
buf, err := ioutil.ReadFile(rpi4EepromTimeStampPath)
buf, err := os.ReadFile(rpi4EepromTimeStampPath)
if err != nil {
return false, err
}
Expand All @@ -408,7 +407,7 @@ func (p *piboot) eepromVersionSupportsTryboot() (bool, error) {
func (p *piboot) isRaspberryPi4() bool {
// For RPi4 detection we do the same as the rpi-eeprom-update script (see
// https://github.com/raspberrypi/rpi-eeprom/blob/master/rpi-eeprom-update)
buf, err := ioutil.ReadFile(rpi4RevisionCodesPath)
buf, err := os.ReadFile(rpi4RevisionCodesPath)
if err != nil {
return false
}
Expand Down
15 changes: 7 additions & 8 deletions bootloader/piboot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package bootloader_test

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -155,7 +154,7 @@ func (s *pibootTestSuite) testExtractKernelAssets(c *C, dtbDir string) {
snapf, err := snapfile.Open(fn)
c.Assert(err, IsNil)

assetsDir, err := ioutil.TempDir("", "kernel-assets")
assetsDir, err := os.MkdirTemp("", "kernel-assets")
c.Assert(err, IsNil)
defer os.RemoveAll(assetsDir)

Expand Down Expand Up @@ -313,7 +312,7 @@ func (s *pibootTestSuite) TestCreateConfig(c *C) {
},
}
for _, fInfo := range files {
readData, err := ioutil.ReadFile(fInfo.path)
readData, err := os.ReadFile(fInfo.path)
c.Assert(err, IsNil)
c.Assert(string(readData), Equals, fInfo.data)
}
Expand Down Expand Up @@ -347,7 +346,7 @@ func (s *pibootTestSuite) TestCreateTrybootCfg(c *C) {
},
}
for _, fInfo := range files {
readData, err := ioutil.ReadFile(fInfo.path)
readData, err := os.ReadFile(fInfo.path)
c.Assert(err, IsNil)
c.Assert(string(readData), Equals, fInfo.data)
}
Expand Down Expand Up @@ -376,7 +375,7 @@ func (s *pibootTestSuite) TestCreateTrybootCfg(c *C) {
},
}
for _, fInfo := range files {
readData, err := ioutil.ReadFile(fInfo.path)
readData, err := os.ReadFile(fInfo.path)
c.Assert(err, IsNil)
c.Assert(string(readData), Equals, fInfo.data)
}
Expand Down Expand Up @@ -418,7 +417,7 @@ func (s *pibootTestSuite) TestCreateConfigCurrentNotEmpty(c *C) {
},
}
for _, fInfo := range files {
readData, err := ioutil.ReadFile(fInfo.path)
readData, err := os.ReadFile(fInfo.path)
c.Assert(err, IsNil)
c.Assert(string(readData), Equals, fInfo.data)
}
Expand Down Expand Up @@ -449,7 +448,7 @@ func (s *pibootTestSuite) TestCreateConfigCurrentNotEmpty(c *C) {
},
}
for _, fInfo := range files {
readData, err := ioutil.ReadFile(fInfo.path)
readData, err := os.ReadFile(fInfo.path)
c.Assert(err, IsNil)
c.Assert(string(readData), Equals, fInfo.data)
}
Expand Down Expand Up @@ -487,7 +486,7 @@ func (s *pibootTestSuite) TestOnlyOneOsPrefix(c *C) {
},
}
for _, fInfo := range files {
readData, err := ioutil.ReadFile(fInfo.path)
readData, err := os.ReadFile(fInfo.path)
c.Assert(err, IsNil)
c.Assert(string(readData), Equals, fInfo.data)
}
Expand Down
3 changes: 1 addition & 2 deletions bootloader/ubootenv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"fmt"
"hash/crc32"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -110,7 +109,7 @@ func OpenWithFlags(fname string, flags OpenFlags) (*Env, error) {
}
defer f.Close()

contentWithHeader, err := ioutil.ReadAll(f)
contentWithHeader, err := io.ReadAll(f)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 79c5ac1

Please sign in to comment.