From 970fddfe138377be47707aeadce1b539b62dcb49 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Sun, 28 Nov 2021 14:49:36 +0800 Subject: [PATCH] refactor: move from io/ioutil to io and os packages 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. This commit also uses `testing#T.TempDir` (https://pkg.go.dev/testing#T.TempDir) to create temporary directory in tests instead of `ioutil.TempDir`. Signed-off-by: Eng Zer Jun --- cmds/boot/fbnetboot/main.go | 8 ++-- cmds/boot/localboot/grub.go | 3 +- cmds/contrib/flash/flash.go | 3 +- cmds/contrib/spidev/spidev.go | 3 +- cmds/core/cat/cat_test.go | 9 +--- cmds/core/chmod/chmod_test.go | 36 +++----------- cmds/core/comm/comm_test.go | 9 +--- cmds/core/cp/cp_test.go | 52 +++++---------------- cmds/core/cpio/cpio_test.go | 23 +++------ cmds/core/dd/dd.go | 3 +- cmds/core/dd/dd_test.go | 21 +++------ cmds/core/df/df.go | 4 +- cmds/core/free/free_linux.go | 4 +- cmds/core/fusermount/fusermount_test.go | 13 +----- cmds/core/grep/grep_test.go | 8 ---- cmds/core/hostname/hostname_plan9.go | 4 +- cmds/core/id/id_plan9.go | 3 +- cmds/core/init/init_linux.go | 3 +- cmds/core/kill/kill_test.go | 14 ------ cmds/core/ln/ln_test.go | 25 ++-------- cmds/core/ls/ls_linux_test.go | 7 +-- cmds/core/ls/ls_test.go | 7 +-- cmds/core/lsdrivers/lsdrivers_linux_test.go | 13 ++---- cmds/core/lsdrivers/main_linux.go | 9 ++-- cmds/core/md5sum/md5sum.go | 3 +- cmds/core/mkdir/mkdir_test.go | 32 ++++++------- cmds/core/mkfifo/mkfifo_test.go | 7 +-- cmds/core/mknod/mknod_linux_test.go | 13 ++---- cmds/core/mknod/mknod_test.go | 13 +----- cmds/core/mktemp/mktemp.go | 5 +- cmds/core/mount/mount.go | 5 +- cmds/core/mount/mount_plan9.go | 3 +- cmds/core/mv/mv_test.go | 28 +++++------ cmds/core/pci/pci.go | 3 +- cmds/core/ps/ps_linux.go | 3 +- cmds/core/ps/ps_test.go | 9 +--- cmds/core/readlink/readlink_test.go | 20 ++------ cmds/core/rm/rm_test.go | 9 +--- cmds/core/scp/scp_test.go | 5 +- cmds/core/shasum/shasum.go | 6 +-- cmds/core/sort/sort.go | 4 +- cmds/core/sort/sort_test.go | 29 ++---------- cmds/core/sshd/sshd.go | 5 +- cmds/core/strings/strings_test.go | 8 ---- cmds/core/stty/stty.go | 3 +- cmds/core/truncate/truncate.go | 3 +- cmds/core/truncate/truncate_test.go | 11 ++--- cmds/core/uniq/uniq_test.go | 8 ---- cmds/core/uptime/uptime.go | 6 +-- cmds/core/wc/wc_test.go | 8 ---- cmds/core/wget/wget_test.go | 9 +--- cmds/exp/ash/parse.go | 4 +- cmds/exp/ash/rush.go | 3 +- cmds/exp/ash/rush_test.go | 9 ---- cmds/exp/bootvars/main.go | 3 +- cmds/exp/builtin/builtin.go | 9 ++-- cmds/exp/bzimage/bzimage.go | 11 ++--- cmds/exp/bzimage/bzimage_test.go | 10 +--- cmds/exp/cbmem/cbmem_linux_test.go | 17 ++++--- cmds/exp/cbmem/gen/gen.go | 3 +- cmds/exp/cksum/cksum.go | 6 +-- cmds/exp/dmidecode/data_linux.go | 8 ++-- cmds/exp/dmidecode/dmidecode_test.go | 9 ++-- cmds/exp/kconf/kconf.go | 3 +- cmds/exp/madeye/madeye.go | 3 +- cmds/exp/netbootxyz/netbootxyz.go | 4 +- cmds/exp/newsshd/newsshd.go | 3 +- cmds/exp/pflask/pflask.go | 5 +- cmds/exp/pox/pox.go | 5 +- cmds/exp/pox/pox_test.go | 8 +--- cmds/exp/rush/parse.go | 4 +- cmds/exp/rush/rush.go | 3 +- cmds/exp/rush/rush_test.go | 9 ---- cmds/exp/smbios_transfer/smbios_transfer.go | 4 +- cmds/exp/tcz/tcz.go | 7 ++- cmds/exp/validate/validate.go | 16 +++---- cmds/exp/validate/validate_test.go | 16 +++---- cmds/exp/vboot/vboot.go | 3 +- integration/generic-tests/dhclient_test.go | 9 +--- pkg/acpi/raw.go | 3 +- pkg/bb/bb.go | 3 +- pkg/bb/bb_test.go | 10 +--- pkg/boot/bls/bls_test.go | 3 +- pkg/boot/boottest/json.go | 3 +- pkg/boot/bzimage/bzimage.go | 14 +++--- pkg/boot/bzimage/bzimage_test.go | 18 +++---- pkg/boot/esxi/esxi.go | 5 +- pkg/boot/fit/fit_test.go | 5 +- pkg/boot/grub/config_test.go | 8 ++-- pkg/boot/grub/echo_test.go | 7 ++- pkg/boot/kexec/memory_linux.go | 3 +- pkg/boot/kexec/memory_linux_test.go | 13 ++---- pkg/boot/linux.go | 5 +- pkg/boot/linux_test.go | 10 ++-- pkg/boot/multiboot_test.go | 7 +-- pkg/boot/syslinux/config_test.go | 4 +- pkg/boot/uefi/fv_test.go | 8 ++-- pkg/boot/uefi/uefi.go | 5 +- pkg/boot/util/reader.go | 3 +- pkg/cmdline/cmdline.go | 3 +- pkg/complete/complete_test.go | 21 +++------ pkg/cp/cmp/cmp.go | 5 +- pkg/cp/cp_test.go | 9 +--- pkg/cpio/fs_unix.go | 3 +- pkg/cpio/newc.go | 3 +- pkg/cpio/newc_test.go | 9 ++-- pkg/crypto/ed25519.go | 9 ++-- pkg/crypto/ed25519_test.go | 25 ++++------ pkg/crypto/measure.go | 4 +- pkg/curl/schemes_test.go | 15 +++--- pkg/dhclient/dhclient.go | 4 +- pkg/dt/fdt_test.go | 3 +- pkg/fb/fb.go | 5 +- pkg/find/find_test.go | 11 ++--- pkg/gpio/gpio_linux.go | 7 ++- pkg/gzip/file_test.go | 14 ++---- pkg/kmodule/kmodule_linux.go | 3 +- pkg/lockfile/lockfile.go | 5 +- pkg/lockfile/lockfile_test.go | 19 ++------ pkg/memio/io_test.go | 3 +- pkg/mount/fs_linux.go | 6 +-- pkg/mount/loop/losetup_linux_test.go | 8 +--- pkg/mount/mount_linux.go | 3 +- pkg/mount/mount_linux_test.go | 12 +---- pkg/msr/msr_linux.go | 3 +- pkg/namespace/builder_test.go | 3 +- pkg/pci/pci.go | 3 +- pkg/pci/pci_linux.go | 4 +- pkg/pogosh/shell.go | 4 +- pkg/rng/entropy.go | 9 ++-- pkg/securelaunch/helpers.go | 5 +- pkg/securelaunch/measurement/files.go | 4 +- pkg/securelaunch/policy/policy.go | 5 +- pkg/smbios/info_linux.go | 6 +-- pkg/spidev/spidev_linux_test.go | 7 ++- pkg/tarutil/tar_test.go | 29 +++--------- pkg/testutil/testutil.go | 3 +- pkg/ts/ts_test.go | 3 +- pkg/tss/tpm_linux.go | 7 ++- pkg/uefivars/boot/efiDppResolver.go | 3 +- pkg/uefivars/boot/fuzz_test.go | 4 +- pkg/uefivars/vars.go | 3 +- pkg/uefivars/vartest/test.go | 3 +- pkg/uio/archivereader_test.go | 5 +- pkg/uio/progress_test.go | 6 +-- pkg/uio/reader.go | 3 +- pkg/uio/reader_test.go | 11 ++--- pkg/upath/symlink_test.go | 8 +--- pkg/uroot/builder/bb_test.go | 8 +--- pkg/uroot/initramfs/dir.go | 3 +- pkg/uroot/initramfs/files_test.go | 38 +++------------ pkg/uroot/uroot.go | 3 +- pkg/uroot/uroot_test.go | 7 +-- pkg/uzip/uzip_test.go | 34 ++++++-------- pkg/vfile/vfile.go | 5 +- pkg/vfile/vfile_test.go | 26 ++++------- pkg/vmtest/gotest.go | 3 +- pkg/vmtest/integration.go | 9 ++-- pkg/vpd/flashromvpd.go | 5 +- pkg/vpd/vpd.go | 5 +- tools/build_perf/build_perf.go | 3 +- tools/checklicenses/checklicenses.go | 6 +-- tools/testramfs/testramfs.go | 3 +- tools/vpdbootmanager/add_test.go | 20 +++----- tools/vpdbootmanager/main_test.go | 20 +++----- u-root.go | 7 ++- uroot_test.go | 24 +++------- 167 files changed, 449 insertions(+), 1003 deletions(-) diff --git a/cmds/boot/fbnetboot/main.go b/cmds/boot/fbnetboot/main.go index b1488b708d..dfabc2ddca 100644 --- a/cmds/boot/fbnetboot/main.go +++ b/cmds/boot/fbnetboot/main.go @@ -10,7 +10,7 @@ import ( "errors" "flag" "fmt" - "io/ioutil" + "io" "log" "net" "net/http" @@ -253,7 +253,7 @@ func boot(ifname string, dhcp dhcpFunc) error { if resp.StatusCode != 200 { return fmt.Errorf("status code is not 200 OK: %d", resp.StatusCode) } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("DHCP: cannot read boot file from the network: %v", err) } @@ -270,7 +270,7 @@ func boot(ifname string, dhcp dhcpFunc) error { if filename == "." || filename == "" { return fmt.Errorf("invalid empty file name extracted from file path %s", u.Path) } - if err = ioutil.WriteFile(filename, body, 0o400); err != nil { + if err = os.WriteFile(filename, body, 0o400); err != nil { return fmt.Errorf("DHCP: cannot write to file %s: %v", filename, err) } debug("DHCP: saved boot file to %s", filename) @@ -315,7 +315,7 @@ func loadCaCerts() (*x509.CertPool, error) { debug("certs: rootCAs == nil") rootCAs = x509.NewCertPool() } - caCerts, err := ioutil.ReadFile(*caCertFile) + caCerts, err := os.ReadFile(*caCertFile) if err != nil { return nil, fmt.Errorf("could not find cert file '%v' - %v", *caCertFile, err) } diff --git a/cmds/boot/localboot/grub.go b/cmds/boot/localboot/grub.go index f0036256c7..cf427949a2 100644 --- a/cmds/boot/localboot/grub.go +++ b/cmds/boot/localboot/grub.go @@ -6,7 +6,6 @@ package main import ( "encoding/hex" - "io/ioutil" "log" "os" "path" @@ -230,7 +229,7 @@ func ScanGrubConfigs(devices block.BlockDevices, basedir string) []jsonboot.Boot return nil } log.Printf("Parsing %s", currentPath) - data, err := ioutil.ReadFile(currentPath) + data, err := os.ReadFile(currentPath) if err != nil { return err } diff --git a/cmds/contrib/flash/flash.go b/cmds/contrib/flash/flash.go index ef8e76f867..b99c156e52 100644 --- a/cmds/contrib/flash/flash.go +++ b/cmds/contrib/flash/flash.go @@ -38,7 +38,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "log" "os" "sort" @@ -160,7 +159,7 @@ func run(args []string, supportedProgrammers map[string]programmerInit) (reterr if _, err := io.ReadFull(f, buf); err != nil { return err } - if leftover, err := io.Copy(ioutil.Discard, f); err != nil { + if leftover, err := io.Copy(io.Discard, f); err != nil { return err } else if leftover != 0 { return fmt.Errorf("flash size (%#x) unequal to file size (%#x)", len(buf), int64(len(buf))+leftover) diff --git a/cmds/contrib/spidev/spidev.go b/cmds/contrib/spidev/spidev.go index 0df7eef43d..ffd78ef69b 100644 --- a/cmds/contrib/spidev/spidev.go +++ b/cmds/contrib/spidev/spidev.go @@ -21,7 +21,6 @@ package main import ( "errors" "io" - "io/ioutil" "log" "os" @@ -71,7 +70,7 @@ func run(args []string, spiOpen spiOpenFunc, input io.Reader, output io.Writer) switch fs.Args()[0] { case "raw": // Create transfer from stdin. - tx, err := ioutil.ReadAll(input) + tx, err := io.ReadAll(input) if err != nil { return err } diff --git a/cmds/core/cat/cat_test.go b/cmds/core/cat/cat_test.go index ee5b656866..a69c2921bb 100644 --- a/cmds/core/cat/cat_test.go +++ b/cmds/core/cat/cat_test.go @@ -9,7 +9,6 @@ package main import ( "bytes" "fmt" - "io/ioutil" "os" "path/filepath" "reflect" @@ -19,14 +18,11 @@ import ( // setup writes a set of files, putting 1 byte in each file. func setup(t *testing.T, data []byte) (string, error) { t.Logf(":: Creating simulation data. ") - dir, err := ioutil.TempDir("", "cat.dir") - if err != nil { - return "", err - } + dir := t.TempDir() for i, d := range data { n := fmt.Sprintf("%v%d", filepath.Join(dir, "file"), i) - if err := ioutil.WriteFile(n, []byte{d}, 0o666); err != nil { + if err := os.WriteFile(n, []byte{d}, 0o666); err != nil { return "", err } } @@ -45,7 +41,6 @@ func TestCat(t *testing.T) { if err != nil { t.Fatalf("setup has failed, %v", err) } - defer os.RemoveAll(dir) for i := range someData { files = append(files, fmt.Sprintf("%v%d", filepath.Join(dir, "file"), i)) diff --git a/cmds/core/chmod/chmod_test.go b/cmds/core/chmod/chmod_test.go index 4d0977bf14..3674b41e7a 100644 --- a/cmds/core/chmod/chmod_test.go +++ b/cmds/core/chmod/chmod_test.go @@ -6,7 +6,6 @@ package main import ( "bytes" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -29,13 +28,7 @@ func run(c *exec.Cmd) (string, string, error) { func TestChmodSimple(t *testing.T) { // Temporary directories. - tempDir, err := ioutil.TempDir("", "TestChmodSimple") - if err != nil { - t.Fatalf("cannot create temporary directory: %v", err) - } - defer os.RemoveAll(tempDir) - - f, err := ioutil.TempFile(tempDir, "BLAH1") + f, err := os.CreateTemp(t.TempDir(), "BLAH1") if err != nil { t.Fatalf("cannot create temporary file: %v", err) } @@ -100,11 +93,7 @@ func checkPath(t *testing.T, path string, instruction string, v fileModeTrans) { func TestChmodRecursive(t *testing.T) { // Temporary directories. - tempDir, err := ioutil.TempDir("", "TestChmodRecursive") - if err != nil { - t.Fatalf("cannot create temporary directory: %v", err) - } - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() var targetDirectories []string for _, dir := range []string{ @@ -148,8 +137,7 @@ func TestChmodRecursive(t *testing.T) { // Set permissions using chmod. c := testutil.Command(t, "-R", k, tempDir) - err = c.Run() - if err != nil { + if err := c.Run(); err != nil { t.Fatalf("setting permissions failed: %v", err) } @@ -162,19 +150,15 @@ func TestChmodRecursive(t *testing.T) { func TestChmodReference(t *testing.T) { // Temporary directories. - tempDir, err := ioutil.TempDir("", "TestChmodReference") - if err != nil { - t.Fatalf("cannot create temporary directory: %v", err) - } - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() - sourceFile, err := ioutil.TempFile(tempDir, "BLAH1") + sourceFile, err := os.CreateTemp(tempDir, "BLAH1") if err != nil { t.Fatalf("cannot create temporary file: %v", err) } defer sourceFile.Close() - targetFile, err := ioutil.TempFile(tempDir, "BLAH2") + targetFile, err := os.CreateTemp(tempDir, "BLAH2") if err != nil { t.Fatalf("cannot create temporary file: %v", err) } @@ -209,13 +193,7 @@ func TestChmodReference(t *testing.T) { } func TestInvocationErrors(t *testing.T) { - tempDir, err := ioutil.TempDir("", "TestInvocationErrors") - if err != nil { - t.Fatalf("cannot create temporary directory: %v", err) - } - defer os.RemoveAll(tempDir) - - f, err := ioutil.TempFile(tempDir, "BLAH1") + f, err := os.CreateTemp(t.TempDir(), "BLAH1") if err != nil { t.Fatalf("cannot create temporary file: %v", err) } diff --git a/cmds/core/comm/comm_test.go b/cmds/core/comm/comm_test.go index 9bda650fc8..50e83c2de4 100644 --- a/cmds/core/comm/comm_test.go +++ b/cmds/core/comm/comm_test.go @@ -6,7 +6,6 @@ package main import ( "fmt" - "io/ioutil" "os" "path/filepath" "testing" @@ -55,18 +54,14 @@ var commTests = []struct { // TestComm implements a table-drivent test. func TestComm(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "comm") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() for _, test := range commTests { // Write inputs into the two files var files [2]string for i, contents := range []string{test.in1, test.in2} { files[i] = filepath.Join(tmpDir, fmt.Sprintf("txt%d", i)) - if err := ioutil.WriteFile(files[i], []byte(contents), 0o600); err != nil { + if err := os.WriteFile(files[i], []byte(contents), 0o600); err != nil { t.Fatalf("Failed to create test file %d: %v", i, err) } } diff --git a/cmds/core/cp/cp_test.go b/cmds/core/cp/cp_test.go index 57740d9011..da5d1d082a 100644 --- a/cmds/core/cp/cp_test.go +++ b/cmds/core/cp/cp_test.go @@ -9,7 +9,6 @@ package main import ( "bytes" "fmt" - "io/ioutil" "log" "math/rand" "os" @@ -38,7 +37,7 @@ func resetFlags() { // randomFile create a random file with random content func randomFile(fpath, prefix string) (*os.File, error) { - f, err := ioutil.TempFile(fpath, prefix) + f, err := os.CreateTemp(fpath, prefix) if err != nil { return nil, err } @@ -56,7 +55,7 @@ func randomFile(fpath, prefix string) (*os.File, error) { func createFilesTree(root string, maxDepth, depth int) error { // create more one dir if don't achieve the maxDepth if depth < maxDepth { - newDir, err := ioutil.TempDir(root, fmt.Sprintf("cpdir_%d_", depth)) + newDir, err := os.MkdirTemp(root, fmt.Sprintf("cpdir_%d_", depth)) if err != nil { return err } @@ -80,11 +79,7 @@ func createFilesTree(root string, maxDepth, depth int) error { // TestCpsSimple make a simple test for copy file-to-file // cmd-line equivalent: cp file file-copy func TestCpSimple(t *testing.T) { - tempDir, err := ioutil.TempDir("", "TestCpSimple") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() f, err := randomFile(tempDir, "src-") if err != nil { @@ -109,17 +104,8 @@ func TestCpSrcDirectory(t *testing.T) { flags.recursive = false defer resetFlags() - tempDir, err := ioutil.TempDir("", "TestCpSrcDirectory") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tempDir) - - tempDirTwo, err := ioutil.TempDir("", "TestCpSrcDirectoryTwo") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tempDirTwo) + tempDir := t.TempDir() + tempDirTwo := t.TempDir() // capture log output to verify var logBytes bytes.Buffer @@ -144,11 +130,7 @@ func TestCpRecursive(t *testing.T) { flags.recursive = true defer resetFlags() - tempDir, err := ioutil.TempDir("", "TestCpRecursive") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() srcDir := filepath.Join(tempDir, "src") if err := os.Mkdir(srcDir, 0o755); err != nil { @@ -159,7 +141,7 @@ func TestCpRecursive(t *testing.T) { t.Fatal(err) } - if err = createFilesTree(srcDir, maxDirDepth, 0); err != nil { + if err := createFilesTree(srcDir, maxDirDepth, 0); err != nil { t.Fatalf("cannot create files tree on directory %q: %v", srcDir, err) } @@ -198,11 +180,7 @@ func TestCpRecursive(t *testing.T) { func TestCpRecursiveMultiple(t *testing.T) { flags.recursive = true defer resetFlags() - tempDir, err := ioutil.TempDir("", "TestCpRecursiveMultiple") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() dstTest := filepath.Join(tempDir, "destination") if err := os.Mkdir(dstTest, 0o755); err != nil { @@ -212,11 +190,9 @@ func TestCpRecursiveMultiple(t *testing.T) { // create multiple random directories sources srcDirs := []string{} for i := 0; i < maxDirDepth; i++ { - srcTest, err := ioutil.TempDir(tempDir, "src-") - if err != nil { - t.Fatalf("Failed on build directory %v: %v\n", srcTest, err) - } - if err = createFilesTree(srcTest, maxDirDepth, 0); err != nil { + srcTest := t.TempDir() + + if err := createFilesTree(srcTest, maxDirDepth, 0); err != nil { t.Fatalf("cannot create files tree on directory %v: %v", srcTest, err) } @@ -247,11 +223,7 @@ func TestCpRecursiveMultiple(t *testing.T) { // using -P don't follow symlinks, create other symlink // cmd-line equivalent: $ cp -P symlink symlink-copy func TestCpSymlink(t *testing.T) { - tempDir, err := ioutil.TempDir("", "TestCpSymlink") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() f, err := randomFile(tempDir, "src-") if err != nil { diff --git a/cmds/core/cpio/cpio_test.go b/cmds/core/cpio/cpio_test.go index 75f85f6cb8..259aac1e3a 100644 --- a/cmds/core/cpio/cpio_test.go +++ b/cmds/core/cpio/cpio_test.go @@ -6,7 +6,6 @@ package main import ( "bytes" - "io/ioutil" "os" "path/filepath" "runtime" @@ -26,11 +25,7 @@ type dirEnt struct { func TestCpio(t *testing.T) { debug = t.Logf // Create a temporary directory - tempDir, err := ioutil.TempDir("", "TestCpio") - if err != nil { - t.Fatalf("cannot create temporary directory: %v", err) - } - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() targets := []dirEnt{ {Name: "file1", Type: "file", Content: "Hello World"}, @@ -76,6 +71,7 @@ func TestCpio(t *testing.T) { // the FileInfo for each target. This needs to happen in a second // pass because of the link count. for key, ent := range targets { + var err error name := filepath.Join(tempDir, ent.Name) targets[key].FileInfo, err = os.Stat(name) if err != nil { @@ -99,7 +95,7 @@ func TestCpio(t *testing.T) { // Cpio can't read from a non-seekable input (e.g. a pipe) in input mode. // Write the archive to a file instead. - archiveFile, err := ioutil.TempFile("", "archive.cpio") + archiveFile, err := os.CreateTemp("", "archive.cpio") if err != nil { t.Fatal(err) } @@ -111,10 +107,7 @@ func TestCpio(t *testing.T) { } // Extract to a new directory - tempExtractDir, err := ioutil.TempDir(tempDir, "extract") - if err != nil { - t.Fatalf("cannot create temporary directory: %v", err) - } + tempExtractDir := t.TempDir() c = testutil.Command(t, "-v", "i") c.Dir = tempExtractDir @@ -135,7 +128,7 @@ func TestCpio(t *testing.T) { checkFileInfo(t, &ent, newFileInfo) if ent.Type != "dir" { - content, err := ioutil.ReadFile(name) + content, err := os.ReadFile(name) if err != nil { t.Error(err) } @@ -147,11 +140,7 @@ func TestCpio(t *testing.T) { } func TestDirectoryHardLink(t *testing.T) { - tempDir, err := ioutil.TempDir("", "TestCpio") - if err != nil { - t.Fatalf("cannot create temporary directory: %v", err) - } - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() // Open an archive containing two directories with the same inode (0). // We're trying to test if having the same inode will trigger a hard link. diff --git a/cmds/core/dd/dd.go b/cmds/core/dd/dd.go index 11dc21e494..d9a8cb6ab8 100644 --- a/cmds/core/dd/dd.go +++ b/cmds/core/dd/dd.go @@ -36,7 +36,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "log" "math" "os" @@ -299,7 +298,7 @@ func newStreamSectionReader(r io.Reader, offset int64, n int64) io.Reader { // Read implements io.Reader. func (s *sectionReader) Read(p []byte) (int, error) { if s.offset == 0 && s.base != 0 { - if n, err := io.CopyN(ioutil.Discard, s.Reader, s.base); err != nil { + if n, err := io.CopyN(io.Discard, s.Reader, s.base); err != nil { return 0, err } else if n != s.base { // Can't happen. diff --git a/cmds/core/dd/dd_test.go b/cmds/core/dd/dd_test.go index fa0a1b8599..3c9fbc3c5f 100644 --- a/cmds/core/dd/dd_test.go +++ b/cmds/core/dd/dd_test.go @@ -9,7 +9,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" "path/filepath" "reflect" @@ -325,14 +324,10 @@ func TestInFile(t *testing.T) { func setupDatafile(t *testing.T, name string) (string, func()) { t.Helper() - testDir, err := ioutil.TempDir("", "dd") - if err != nil { - t.Fatalf("could not create test dd directory %v", err) - } + testDir := t.TempDir() dataFilePath := filepath.Join(testDir, name) - err = ioutil.WriteFile(dataFilePath, []byte("ABCDEFG"), 0o644) - if err != nil { + if err := os.WriteFile(dataFilePath, []byte("ABCDEFG"), 0o644); err != nil { t.Errorf("unable to mockup file: %v", err) } @@ -658,17 +653,13 @@ func TestFiles(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Write in and out file to temporary dir. - tmpDir, err := ioutil.TempDir("", "dd-test") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() inFile := filepath.Join(tmpDir, "inFile") outFile := filepath.Join(tmpDir, "outFile") - if err := ioutil.WriteFile(inFile, tt.inFile, 0o666); err != nil { + if err := os.WriteFile(inFile, tt.inFile, 0o666); err != nil { t.Error(err) } - if err := ioutil.WriteFile(outFile, tt.outFile, 0o666); err != nil { + if err := os.WriteFile(outFile, tt.outFile, 0o666); err != nil { t.Error(err) } @@ -676,7 +667,7 @@ func TestFiles(t *testing.T) { if err := testutil.Command(t, args...).Run(); err != nil { t.Error(err) } - got, err := ioutil.ReadFile(filepath.Join(tmpDir, "outFile")) + got, err := os.ReadFile(filepath.Join(tmpDir, "outFile")) if err != nil { t.Error(err) } diff --git a/cmds/core/df/df.go b/cmds/core/df/df.go index 8ce2056103..f4d1a59de4 100644 --- a/cmds/core/df/df.go +++ b/cmds/core/df/df.go @@ -24,9 +24,9 @@ import ( "bytes" "flag" "fmt" - "io/ioutil" "log" "math" + "os" "syscall" ) @@ -66,7 +66,7 @@ type mountinfomap map[string]Mount // mountinfo returns a map of mounts representing // the data in /proc/mounts func mountinfo() (mountinfomap, error) { - buf, err := ioutil.ReadFile(procmountsFile) + buf, err := os.ReadFile(procmountsFile) if err != nil { return nil, err } diff --git a/cmds/core/free/free_linux.go b/cmds/core/free/free_linux.go index 3e8f1bc6b7..146182a16b 100644 --- a/cmds/core/free/free_linux.go +++ b/cmds/core/free/free_linux.go @@ -7,7 +7,7 @@ package main import ( "bytes" "fmt" - "io/ioutil" + "os" "strconv" ) @@ -16,7 +16,7 @@ const meminfoFile = "/proc/meminfo" // meminfo returns a mapping that represents the fields contained in // /proc/meminfo func meminfo() (meminfomap, error) { - buf, err := ioutil.ReadFile(meminfoFile) + buf, err := os.ReadFile(meminfoFile) if err != nil { return nil, err } diff --git a/cmds/core/fusermount/fusermount_test.go b/cmds/core/fusermount/fusermount_test.go index e693431529..a6ee53061d 100644 --- a/cmds/core/fusermount/fusermount_test.go +++ b/cmds/core/fusermount/fusermount_test.go @@ -10,7 +10,6 @@ package main import ( "bytes" "fmt" - "io/ioutil" "net" "os" "path/filepath" @@ -25,11 +24,7 @@ import ( // I give up. We're breaking something in circleci and I'm not sure // what. This is fine when run from the commandline. func testArgs(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "TestFusermount") - if err != nil { - t.Fatal("TempDir failed: ", err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() tdir := filepath.Join(tmpDir, "a/b/c") if err := os.MkdirAll(tdir, 0o777); err != nil { t.Fatal(err) @@ -103,11 +98,7 @@ func TestMount(t *testing.T) { readFile := os.NewFile(uintptr(fds[1]), "fusermount-parent-reads") defer readFile.Close() - tmpDir, err := ioutil.TempDir("", "TestFusermount") - if err != nil { - t.Fatal("TempDir failed: ", err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() fc, err := net.FileConn(readFile) if err != nil { diff --git a/cmds/core/grep/grep_test.go b/cmds/core/grep/grep_test.go index 90e51d4b8c..dd872bc74a 100644 --- a/cmds/core/grep/grep_test.go +++ b/cmds/core/grep/grep_test.go @@ -6,8 +6,6 @@ package main import ( "bytes" - "io/ioutil" - "os" "testing" "github.com/u-root/u-root/pkg/testutil" @@ -31,12 +29,6 @@ func TestGrep(t *testing.T) { {"HiX\n", "HiX\n", 0, []string{"-i", "hix"}}, } - tmpDir, err := ioutil.TempDir("", "TestGrep") - if err != nil { - t.Fatal("TempDir failed: ", err) - } - defer os.RemoveAll(tmpDir) - for _, v := range tab { c := testutil.Command(t, v.a...) c.Stdin = bytes.NewReader([]byte(v.i)) diff --git a/cmds/core/hostname/hostname_plan9.go b/cmds/core/hostname/hostname_plan9.go index 6a79c52a0b..f6faee1d54 100644 --- a/cmds/core/hostname/hostname_plan9.go +++ b/cmds/core/hostname/hostname_plan9.go @@ -7,8 +7,8 @@ package main -import "io/ioutil" +import "os" func Sethostname(n string) error { - return ioutil.WriteFile("#c/sysname", []byte(n), 0o644) + return os.WriteFile("#c/sysname", []byte(n), 0o644) } diff --git a/cmds/core/id/id_plan9.go b/cmds/core/id/id_plan9.go index be64e8ce25..ebd2d2ed92 100644 --- a/cmds/core/id/id_plan9.go +++ b/cmds/core/id/id_plan9.go @@ -9,7 +9,6 @@ package main import ( "fmt" - "io/ioutil" "log" "os" ) @@ -18,7 +17,7 @@ func main() { if len(os.Args) != 1 { log.Fatalf("Usage: id") } - id, err := ioutil.ReadFile("/env/user") + id, err := os.ReadFile("/env/user") if err != nil { log.Fatal(err) } diff --git a/cmds/core/init/init_linux.go b/cmds/core/init/init_linux.go index 942d321a54..7f5dbc886d 100644 --- a/cmds/core/init/init_linux.go +++ b/cmds/core/init/init_linux.go @@ -5,7 +5,6 @@ package main import ( - "io/ioutil" "log" "os" "os/exec" @@ -63,7 +62,7 @@ func osInitGo() *initCmds { // We also allow passing args to uinit via a flags file in // /etc/uinit.flags. args := cmdline.GetUinitArgs() - if contents, err := ioutil.ReadFile("/etc/uinit.flags"); err == nil { + if contents, err := os.ReadFile("/etc/uinit.flags"); err == nil { args = append(args, uflag.FileToArgv(string(contents))...) } uinitArgs := libinit.WithArguments(args...) diff --git a/cmds/core/kill/kill_test.go b/cmds/core/kill/kill_test.go index b55b72758f..babf65264a 100644 --- a/cmds/core/kill/kill_test.go +++ b/cmds/core/kill/kill_test.go @@ -7,8 +7,6 @@ package main import ( "bytes" "fmt" - "io/ioutil" - "os" "os/exec" "testing" "time" @@ -26,12 +24,6 @@ func run(c *exec.Cmd) (string, string, error) { } func TestKillProcess(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "KillTest") - if err != nil { - t.Fatal("TempDir failed: ", err) - } - defer os.RemoveAll(tmpDir) - cmd := exec.Command("sleep", "10") if err := cmd.Start(); err != nil { t.Fatalf("Failed to start test process: %v", err) @@ -72,12 +64,6 @@ func TestBadInvocations(t *testing.T) { {a: []string{"-1", "a"}, err: "Some processes could not be killed: [a: arguments must be process or job IDS]\n"}, } - tmpDir, err := ioutil.TempDir("", "KillTest") - if err != nil { - t.Fatal("TempDir failed: ", err) - } - defer os.RemoveAll(tmpDir) - for _, v := range tab { _, e, err := run(testutil.Command(t, v.a...)) if e != v.err { diff --git a/cmds/core/ln/ln_test.go b/cmds/core/ln/ln_test.go index d9285d0fdd..2e1f774890 100644 --- a/cmds/core/ln/ln_test.go +++ b/cmds/core/ln/ln_test.go @@ -7,10 +7,8 @@ package main import ( - "io/ioutil" "os" "path/filepath" - "strings" "testing" ) @@ -32,13 +30,6 @@ type test struct { cmdline string // cmdline ln equivalent } -// prepareTestName prepares a custom testname for creating file/folder -// using a cmdline, for that remove chars whose conflicts with unix paths -func prepareTestName(cmdline string) string { - d := strings.Replace(cmdline, " ", "_", -1) - return strings.Replace(d, "/", "|", -1) -} - // loadTests loads the main table driven tests // for ln command tests func loadTests() []test { @@ -143,15 +134,6 @@ func loadTests() []test { } } -// newDir create a temp dir -func newDir(testName string, t *testing.T) (name string) { - name, err := ioutil.TempDir("", "Go_"+testName) - if err != nil { - t.Fatalf("TempDir %s: %s", testName, err) - } - return -} - // testHardLink test if hardlink creation was successful // 'target' and 'linkName' must exists // linkName -> target @@ -223,8 +205,7 @@ func testSymlink(linkName, linksTo string, t *testing.T) { // tabDriven tests (see loadTests()) func TestLn(t *testing.T) { tabDriven := loadTests() - testDir := newDir("TestLnGeneric", t) - defer os.RemoveAll(testDir) + testDir := t.TempDir() // executing ln on isolated testDir if err := os.Chdir(testDir); err != nil { @@ -233,7 +214,7 @@ func TestLn(t *testing.T) { defer os.Chdir("..") // after defer to go back to the original root for caseNum, testCase := range tabDriven { - d := newDir(prepareTestName(testCase.cmdline), t) + d := t.TempDir() if err := os.Chdir(d); err != nil { t.Fatalf("Changing directory for %q fails: %v", d, err) } @@ -246,7 +227,7 @@ func TestLn(t *testing.T) { t.Skipf("Creation of dir %q fails: %v", p, err) } } else { - if err := ioutil.WriteFile(p, []byte{'.'}, 0o640); err != nil { + if err := os.WriteFile(p, []byte{'.'}, 0o640); err != nil { t.Fatal(err) } } diff --git a/cmds/core/ls/ls_linux_test.go b/cmds/core/ls/ls_linux_test.go index faa144f60e..b286b60dd2 100644 --- a/cmds/core/ls/ls_linux_test.go +++ b/cmds/core/ls/ls_linux_test.go @@ -5,7 +5,6 @@ package main import ( - "io/ioutil" "os" "path/filepath" "regexp" @@ -30,11 +29,7 @@ func TestLargeDevNumber(t *testing.T) { } // Make the node. - tmpDir, err := ioutil.TempDir("", "ls") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() file := filepath.Join(tmpDir, "large_node") if err := unix.Mknod(file, 0o660|unix.S_IFBLK, 0x12345678); err != nil { t.Fatal(err) diff --git a/cmds/core/ls/ls_test.go b/cmds/core/ls/ls_test.go index b8bdc51f22..9dcdc39a39 100644 --- a/cmds/core/ls/ls_test.go +++ b/cmds/core/ls/ls_test.go @@ -5,7 +5,6 @@ package main import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -80,11 +79,7 @@ f2 } func TestLs(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "ls") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() // Create an empty directory. testDir := filepath.Join(tmpDir, "testDir") diff --git a/cmds/core/lsdrivers/lsdrivers_linux_test.go b/cmds/core/lsdrivers/lsdrivers_linux_test.go index a97819290b..a083902e98 100644 --- a/cmds/core/lsdrivers/lsdrivers_linux_test.go +++ b/cmds/core/lsdrivers/lsdrivers_linux_test.go @@ -5,7 +5,6 @@ package main import ( - "io/ioutil" "os" "path/filepath" "strings" @@ -51,10 +50,8 @@ func allDone(dir string, t *testing.T) { // This tests one of the unused components, the alarm timer func TestUnusedAlarmTimer(t *testing.T) { - dir, err := ioutil.TempDir("", "lsdrivers") - if err != nil { - t.Fatal(err) - } + dir := t.TempDir() + // generated by: tar cf - /sys/bus | tar tvf - | awk '{printf("{t:\"%s\", n: \"%s\", l: \"%s\"},\n", substr($1,1,1), $6, $8)}' // Then filter what you need. alarmtimer := []dfl{ @@ -88,10 +85,8 @@ func TestUnusedAlarmTimer(t *testing.T) { // This tests one of the unused components, acpi.button func TestUsedPWRButton(t *testing.T) { - dir, err := ioutil.TempDir("", "lsdrivers") - if err != nil { - t.Fatal(err) - } + dir := t.TempDir() + // generated by: tar cf - /sys/bus | tar tvf - | awk '{printf("{t:\"%s\", n: \"%s\", l: \"%s\"},\n", substr($1,1,1), $6, $8)}' alarmtimer := []dfl{ {t: "d", n: filepath.Join(dir, "sys/bus/acpi/drivers/button/"), l: ""}, diff --git a/cmds/core/lsdrivers/main_linux.go b/cmds/core/lsdrivers/main_linux.go index e7e9db6b30..7895719e8b 100644 --- a/cmds/core/lsdrivers/main_linux.go +++ b/cmds/core/lsdrivers/main_linux.go @@ -17,7 +17,6 @@ package main import ( "flag" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -30,12 +29,12 @@ var unused = flag.Bool("u", false, "Show unused drivers") // file in driverpath. This indicates that it is real hardware. // If the path has any sort of error, return false. func hasDevices(driverpath string) bool { - files, err := ioutil.ReadDir(driverpath) + files, err := os.ReadDir(driverpath) if err != nil { return false } for _, file := range files { - if file.Mode()&os.ModeSymlink != 0 { + if file.Type()&os.ModeSymlink != 0 { return true } } @@ -44,14 +43,14 @@ func hasDevices(driverpath string) bool { func lsdrivers(bus string, unused bool) ([]string, error) { var d []string - files, err := ioutil.ReadDir(bus) + files, err := os.ReadDir(bus) if err != nil { return nil, err } for _, f := range files { n := filepath.Join(bus, f.Name(), "drivers") - drivers, err := ioutil.ReadDir(n) + drivers, err := os.ReadDir(n) // In some cases the directory does not exist. if err != nil { continue diff --git a/cmds/core/md5sum/md5sum.go b/cmds/core/md5sum/md5sum.go index 481987b5f1..b3f406db5e 100644 --- a/cmds/core/md5sum/md5sum.go +++ b/cmds/core/md5sum/md5sum.go @@ -9,7 +9,6 @@ import ( "crypto/md5" "fmt" "io" - "io/ioutil" "log" "os" @@ -17,7 +16,7 @@ import ( ) func getInput() (input []byte, err error) { - return ioutil.ReadAll(os.Stdin) + return io.ReadAll(os.Stdin) } func helpPrinter() { diff --git a/cmds/core/mkdir/mkdir_test.go b/cmds/core/mkdir/mkdir_test.go index a294947837..8600e501c9 100644 --- a/cmds/core/mkdir/mkdir_test.go +++ b/cmds/core/mkdir/mkdir_test.go @@ -7,7 +7,6 @@ package main import ( "bytes" "fmt" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -135,14 +134,21 @@ func printError(t *testing.T, testname string, execStmt string, out interface{}, } func findFile(dir string, filename string) (os.FileInfo, error) { - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { return nil, err } for _, file := range files { - if file.Name() == filename { - return file, nil + if file.Name() != filename { + continue + } + + fi, err := file.Info() + if err != nil { + return nil, err } + + return fi, nil } return nil, nil } @@ -155,11 +161,7 @@ func removeCreatedFiles(tmpDir string) { func TestMkdirErrors(t *testing.T) { // Set Up - tmpDir, err := ioutil.TempDir("", "ls") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() syscall.Umask(umaskDefault) // Error Tests @@ -186,11 +188,7 @@ func TestMkdirErrors(t *testing.T) { func TestMkdirRegular(t *testing.T) { // Set Up - tmpDir, err := ioutil.TempDir("", "ls") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() syscall.Umask(umaskDefault) // Regular Tests @@ -220,11 +218,7 @@ func TestMkdirRegular(t *testing.T) { func TestMkdirPermission(t *testing.T) { // Set Up - tmpDir, err := ioutil.TempDir("", "ls") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() syscall.Umask(umaskDefault) // Permission Tests diff --git a/cmds/core/mkfifo/mkfifo_test.go b/cmds/core/mkfifo/mkfifo_test.go index 25211bc08b..cfb5358ba2 100644 --- a/cmds/core/mkfifo/mkfifo_test.go +++ b/cmds/core/mkfifo/mkfifo_test.go @@ -6,7 +6,6 @@ package main import ( "bytes" - "io/ioutil" "os" "path/filepath" "strings" @@ -22,11 +21,7 @@ type test struct { } func TestMkfifo(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "ls") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() // used later in testing testDir := filepath.Join(tmpDir, "mkfifoDir") diff --git a/cmds/core/mknod/mknod_linux_test.go b/cmds/core/mknod/mknod_linux_test.go index a4bc9b558f..99f1537ed9 100644 --- a/cmds/core/mknod/mknod_linux_test.go +++ b/cmds/core/mknod/mknod_linux_test.go @@ -5,7 +5,6 @@ package main import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -29,23 +28,17 @@ func TestLargeDevNumber(t *testing.T) { } // Make a temporary directory. - tmpDir, err := ioutil.TempDir("", "ls") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() file := filepath.Join(tmpDir, "large_node") // Run "mknod large_node b 1110 74616". - err = testutil.Command(t, file, "b", "1110", "74616").Run() - if err != nil { + if err := testutil.Command(t, file, "b", "1110", "74616").Run(); err != nil { t.Fatal(err) } // Check the device number. var s unix.Stat_t - err = unix.Stat(file, &s) - if err != nil { + if err := unix.Stat(file, &s); err != nil { t.Fatal(err) } if s.Rdev != 0x12345678 { diff --git a/cmds/core/mknod/mknod_test.go b/cmds/core/mknod/mknod_test.go index ccd0b6adcb..acdb65f914 100644 --- a/cmds/core/mknod/mknod_test.go +++ b/cmds/core/mknod/mknod_test.go @@ -6,7 +6,6 @@ package main import ( "bytes" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -28,11 +27,7 @@ func run(c *exec.Cmd) (string, string, error) { } func TestMknodFifo(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "ls") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() // Delete a preexisting pipe if it exists, thought it shouldn't pipepath := filepath.Join(tmpDir, "testpipe") @@ -51,11 +46,7 @@ func TestMknodFifo(t *testing.T) { } func TestInvocationErrors(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "ls") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() devpath := filepath.Join(tmpDir, "testdev") tests := []test{ diff --git a/cmds/core/mktemp/mktemp.go b/cmds/core/mktemp/mktemp.go index 194d666ec4..82acdf5761 100644 --- a/cmds/core/mktemp/mktemp.go +++ b/cmds/core/mktemp/mktemp.go @@ -29,7 +29,6 @@ package main import ( "fmt" - "io/ioutil" "log" "os" "strings" @@ -75,10 +74,10 @@ func mktemp() (string, error) { } if flags.d { - d, err := ioutil.TempDir(flags.dir, flags.prefix) + d, err := os.MkdirTemp(flags.dir, flags.prefix) return d, err } - f, err := ioutil.TempFile(flags.dir, flags.prefix) + f, err := os.CreateTemp(flags.dir, flags.prefix) return f.Name(), err } diff --git a/cmds/core/mount/mount.go b/cmds/core/mount/mount.go index e6f5f86a4e..fc845d9053 100644 --- a/cmds/core/mount/mount.go +++ b/cmds/core/mount/mount.go @@ -17,7 +17,6 @@ package main import ( "flag" "fmt" - "io/ioutil" "log" "os" "strings" @@ -65,7 +64,7 @@ func loopSetup(filename string) (loopDevice string, err error) { func getSupportedFilesystem(originFS string) ([]string, bool, error) { var known bool var err error - fs, err := ioutil.ReadFile("/proc/filesystems") + fs, err := os.ReadFile("/proc/filesystems") if err != nil { return nil, known, err } @@ -99,7 +98,7 @@ func main() { if len(os.Args) == 1 { n := []string{"/proc/self/mounts", "/proc/mounts", "/etc/mtab"} for _, p := range n { - if b, err := ioutil.ReadFile(p); err == nil { + if b, err := os.ReadFile(p); err == nil { fmt.Print(string(b)) os.Exit(0) } diff --git a/cmds/core/mount/mount_plan9.go b/cmds/core/mount/mount_plan9.go index 561bd4fc64..9966516662 100644 --- a/cmds/core/mount/mount_plan9.go +++ b/cmds/core/mount/mount_plan9.go @@ -28,7 +28,6 @@ package main import ( "flag" "fmt" - "io/ioutil" "log" "os" @@ -38,7 +37,7 @@ import ( func main() { if len(os.Args) == 1 { n := fmt.Sprintf("/proc/%d/ns", os.Getpid()) - if b, err := ioutil.ReadFile(n); err == nil { + if b, err := os.ReadFile(n); err == nil { fmt.Print(string(b)) os.Exit(0) } diff --git a/cmds/core/mv/mv_test.go b/cmds/core/mv/mv_test.go index 5206768fb0..745676758c 100644 --- a/cmds/core/mv/mv_test.go +++ b/cmds/core/mv/mv_test.go @@ -6,7 +6,6 @@ package main import ( "bytes" - "io/ioutil" "os" "path/filepath" "syscall" @@ -51,11 +50,8 @@ var tests = []makeIt{ new, } -func setup() (string, error) { - d, err := ioutil.TempDir(os.TempDir(), "hi.dir") - if err != nil { - return "", err - } +func setup(t *testing.T) (string, error) { + d := t.TempDir() tmpdir := filepath.Join(d, "hi.sub.dir") if err := os.Mkdir(tmpdir, 0o777); err != nil { @@ -63,7 +59,7 @@ func setup() (string, error) { } for _, tt := range tests { - if err := ioutil.WriteFile(filepath.Join(d, tt.n), tt.c, tt.m); err != nil { + if err := os.WriteFile(filepath.Join(d, tt.n), tt.c, tt.m); err != nil { return "", err } } @@ -80,7 +76,7 @@ func getInode(file string) (uint64, error) { } func TestMv(t *testing.T) { - d, err := setup() + d, err := setup(t) if err != nil { t.Fatal("err") } @@ -102,7 +98,7 @@ func TestMv(t *testing.T) { t.Logf("Verify renamed file integrity...") { - content, err := ioutil.ReadFile(filepath.Join(d, "hi4.txt")) + content, err := os.ReadFile(filepath.Join(d, "hi4.txt")) if err != nil { t.Error(err) } @@ -145,7 +141,7 @@ func TestMv(t *testing.T) { t.Logf("Verify moved files into directory file integrity...") { - content, err := ioutil.ReadFile(filepath.Join(dsub, "hi4.txt")) + content, err := os.ReadFile(filepath.Join(dsub, "hi4.txt")) if err != nil { t.Error(err) } @@ -177,7 +173,7 @@ func TestMv(t *testing.T) { func TestMvUpdate(t *testing.T) { *update = true - d, err := setup() + d, err := setup(t) if err != nil { t.Error(err) } @@ -204,7 +200,7 @@ func TestMvUpdate(t *testing.T) { if err = testutil.IsExitCode(err, 0); err != nil { t.Error(err) } - newContent, err := ioutil.ReadFile(filepath.Join(d, new.n)) + newContent, err := os.ReadFile(filepath.Join(d, new.n)) if err != nil { t.Error(err) } @@ -221,7 +217,7 @@ func TestMvUpdate(t *testing.T) { if err = testutil.IsExitCode(err, 0); err != nil { t.Error(err) } - newContent, err := ioutil.ReadFile(filepath.Join(d, old.n)) + newContent, err := os.ReadFile(filepath.Join(d, old.n)) if err != nil { t.Error(err) } @@ -236,7 +232,7 @@ func TestMvUpdate(t *testing.T) { func TestMvNoClobber(t *testing.T) { *noClobber = true - d, err := setup() + d, err := setup(t) if err != nil { t.Error(err) } @@ -251,7 +247,7 @@ func TestMvNoClobber(t *testing.T) { if err = testutil.IsExitCode(err, 0); err != nil { t.Error(err) } - newContent, err := ioutil.ReadFile(filepath.Join(d, new.n)) + newContent, err := os.ReadFile(filepath.Join(d, new.n)) if err != nil { t.Error(err) } @@ -268,7 +264,7 @@ func TestMvNoClobber(t *testing.T) { if err = testutil.IsExitCode(err, 0); err != nil { t.Error(err) } - newContent, err := ioutil.ReadFile(filepath.Join(d, "hi3.txt")) + newContent, err := os.ReadFile(filepath.Join(d, "hi3.txt")) if err != nil { t.Error(err) } diff --git a/cmds/core/pci/pci.go b/cmds/core/pci/pci.go index 0f6ad48d10..704a87dbfe 100644 --- a/cmds/core/pci/pci.go +++ b/cmds/core/pci/pci.go @@ -19,7 +19,6 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" "log" "os" "strconv" @@ -135,7 +134,7 @@ func main() { var d pci.Devices if len(*readJSON) != 0 { - b, err := ioutil.ReadFile(*readJSON) + b, err := os.ReadFile(*readJSON) if err != nil { log.Fatal(err) } diff --git a/cmds/core/ps/ps_linux.go b/cmds/core/ps/ps_linux.go index 5adc110242..2cc8f1631f 100644 --- a/cmds/core/ps/ps_linux.go +++ b/cmds/core/ps/ps_linux.go @@ -6,7 +6,6 @@ package main import ( "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -217,7 +216,7 @@ func getAllStatNames(globs []string) ([]string, error) { } func file(s string) (string, error) { - b, err := ioutil.ReadFile(s) + b, err := os.ReadFile(s) return string(b), err } diff --git a/cmds/core/ps/ps_test.go b/cmds/core/ps/ps_test.go index fc25eba0fb..310e3cccd6 100644 --- a/cmds/core/ps/ps_test.go +++ b/cmds/core/ps/ps_test.go @@ -9,7 +9,6 @@ package main import ( "bytes" "fmt" - "io/ioutil" "os" "path/filepath" "reflect" @@ -22,11 +21,7 @@ import ( // If no errors returns, it's okay func TestPsExecution(t *testing.T) { t.Logf("TestPsExecution") - d, err := ioutil.TempDir("", "ps") - if err != nil { - t.Fatal(err) - } - // defer os.Rmdirall(d) + d := t.TempDir() tests := []struct { n string pid string @@ -97,7 +92,7 @@ Uid: 110 110 110 110 for n, f := range tt.files { procf := filepath.Join(pd, n) t.Logf("Write %v", procf) - if err := ioutil.WriteFile(procf, []byte(f), 0o666); err != nil { + if err := os.WriteFile(procf, []byte(f), 0o666); err != nil { t.Fatal(err) } } diff --git a/cmds/core/readlink/readlink_test.go b/cmds/core/readlink/readlink_test.go index a7d29af3b5..65b175d3f5 100644 --- a/cmds/core/readlink/readlink_test.go +++ b/cmds/core/readlink/readlink_test.go @@ -7,7 +7,6 @@ package main import ( "bytes" "fmt" - "io/ioutil" "os" "path/filepath" "testing" @@ -23,11 +22,7 @@ type test struct { } func TestReadlink(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "ls") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() // Creating here to utilize path in tests testDir := filepath.Join(tmpDir, "readLinkDir") @@ -90,28 +85,23 @@ func TestReadlink(t *testing.T) { }, } // Createfiles. - _, err = os.Create("f1") - if err != nil { + if _, err := os.Create("f1"); err != nil { t.Error(err) } - _, err = os.Create("f2") - if err != nil { + if _, err := os.Create("f2"); err != nil { t.Error(err) } // Create symlinks f1Symlink := filepath.Join(testDir, "f1symlink") - err = os.Symlink("f1", f1Symlink) - if err != nil { + if err := os.Symlink("f1", f1Symlink); err != nil { t.Error(err) } // Multiple links multiLinks := filepath.Join(testDir, "multilinks") - err = os.Symlink(f1Symlink, multiLinks) - - if err != nil { + if err := os.Symlink(f1Symlink, multiLinks); err != nil { t.Error(err) } diff --git a/cmds/core/rm/rm_test.go b/cmds/core/rm/rm_test.go index 9bbad1a91b..122f1da191 100644 --- a/cmds/core/rm/rm_test.go +++ b/cmds/core/rm/rm_test.go @@ -7,7 +7,6 @@ package main import ( - "io/ioutil" "os" "path" "syscall" @@ -157,11 +156,7 @@ func TestRemove(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - d, err := ioutil.TempDir(os.TempDir(), "u-root.cmds.rm") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(d) + d := t.TempDir() for _, f := range tmpFiles { var ( @@ -171,7 +166,7 @@ func TestRemove(t *testing.T) { if f.isdir { err = os.Mkdir(filepath, f.mode) } else { - err = ioutil.WriteFile(filepath, fbody, f.mode) + err = os.WriteFile(filepath, fbody, f.mode) } if err != nil { t.Fatal(err) diff --git a/cmds/core/scp/scp_test.go b/cmds/core/scp/scp_test.go index e8840232fa..38abba375a 100644 --- a/cmds/core/scp/scp_test.go +++ b/cmds/core/scp/scp_test.go @@ -8,7 +8,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" "path" "testing" @@ -18,7 +17,7 @@ func TestScpSource(t *testing.T) { var w bytes.Buffer var r bytes.Buffer - tf, err := ioutil.TempFile("", "TestScpSource") + tf, err := os.CreateTemp("", "TestScpSource") if err != nil { t.Fatalf("creating temp file: %v", err) } @@ -41,7 +40,7 @@ func TestScpSink(t *testing.T) { var w bytes.Buffer var r bytes.Buffer - tf, err := ioutil.TempFile("", "TestScpSink") + tf, err := os.CreateTemp("", "TestScpSink") if err != nil { t.Fatalf("creating temp file: %v", err) } diff --git a/cmds/core/shasum/shasum.go b/cmds/core/shasum/shasum.go index 260f515763..caaef0c929 100644 --- a/cmds/core/shasum/shasum.go +++ b/cmds/core/shasum/shasum.go @@ -8,7 +8,7 @@ import ( "crypto/sha1" "crypto/sha256" "fmt" - "io/ioutil" + "io" "os" "github.com/spf13/pflag" @@ -27,9 +27,9 @@ func versionPrinter() { func getInput(fileName string) (input []byte, err error) { if fileName != "" { - return ioutil.ReadFile(fileName) + return os.ReadFile(fileName) } - return ioutil.ReadAll(os.Stdin) + return io.ReadAll(os.Stdin) } // diff --git a/cmds/core/sort/sort.go b/cmds/core/sort/sort.go index 9b95efc681..56e9d14066 100644 --- a/cmds/core/sort/sort.go +++ b/cmds/core/sort/sort.go @@ -19,7 +19,7 @@ package main import ( "flag" - "io/ioutil" + "io" "log" "os" "sort" @@ -50,7 +50,7 @@ func readInput() string { // Read unicode string from input fileContents := []string{} for _, f := range from { - bytes, err := ioutil.ReadAll(f) + bytes, err := io.ReadAll(f) if err != nil { log.Fatal(err) } diff --git a/cmds/core/sort/sort_test.go b/cmds/core/sort/sort_test.go index 4ebb8f3f96..5a2a042f8c 100644 --- a/cmds/core/sort/sort_test.go +++ b/cmds/core/sort/sort_test.go @@ -6,7 +6,6 @@ package main import ( "bytes" - "io/ioutil" "os" "path/filepath" "strings" @@ -42,12 +41,6 @@ var sortTests = []test{ // sort < in > out func TestSortWithPipes(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "ls") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) - // Table-driven testing for _, tt := range sortTests { cmd := testutil.Command(t, tt.flags...) @@ -71,7 +64,7 @@ func sortWithFiles(t *testing.T, tt test, tmpDir string, inPaths := make([]string, len(inFiles)) for i, inFile := range inFiles { inPaths[i] = filepath.Join(tmpDir, inFile) - if err := ioutil.WriteFile(inPaths[i], []byte(tt.in), 0o600); err != nil { + if err := os.WriteFile(inPaths[i], []byte(tt.in), 0o600); err != nil { t.Error(err) return } @@ -85,7 +78,7 @@ func sortWithFiles(t *testing.T, tt test, tmpDir string, return } - out, err = ioutil.ReadFile(outPath) + out, err = os.ReadFile(outPath) if err != nil { t.Errorf("Cannot open out file: %v", err) return @@ -98,11 +91,7 @@ func sortWithFiles(t *testing.T, tt test, tmpDir string, // sort -o in out func TestSortWithFiles(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "ls") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() // Table-driven testing for _, tt := range sortTests { @@ -112,11 +101,7 @@ func TestSortWithFiles(t *testing.T) { // sort -o file file func TestInplaceSort(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "ls") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() // Table-driven testing for _, tt := range sortTests { @@ -126,11 +111,7 @@ func TestInplaceSort(t *testing.T) { // sort -o out in1 in2 in3 in4 func TestMultipleFileInputs(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "ls") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() tt := test{ []string{}, diff --git a/cmds/core/sshd/sshd.go b/cmds/core/sshd/sshd.go index bac30304ba..437eebde70 100644 --- a/cmds/core/sshd/sshd.go +++ b/cmds/core/sshd/sshd.go @@ -8,7 +8,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "log" "net" "os" @@ -183,7 +182,7 @@ func main() { // Public key authentication is done by comparing // the public key of a received connection // with the entries in the authorized_keys file. - authorizedKeysBytes, err := ioutil.ReadFile(*keys) + authorizedKeysBytes, err := os.ReadFile(*keys) if err != nil { log.Fatal(err) } @@ -216,7 +215,7 @@ func main() { }, } - privateBytes, err := ioutil.ReadFile(*privkey) + privateBytes, err := os.ReadFile(*privkey) if err != nil { log.Fatal(err) } diff --git a/cmds/core/strings/strings_test.go b/cmds/core/strings/strings_test.go index 31c10a58c7..fac18825e2 100644 --- a/cmds/core/strings/strings_test.go +++ b/cmds/core/strings/strings_test.go @@ -7,8 +7,6 @@ package main import ( "bytes" "fmt" - "io/ioutil" - "os" "testing" "github.com/u-root/u-root/pkg/testutil" @@ -76,12 +74,6 @@ var stringsTests = []test{ // strings < in > out func TestSortWithPipes(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "strings") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) - // Table-driven testing for _, tt := range stringsTests { t.Run(fmt.Sprintf("%v", tt.name), func(t *testing.T) { diff --git a/cmds/core/stty/stty.go b/cmds/core/stty/stty.go index d56985ec0b..484a7c5d91 100644 --- a/cmds/core/stty/stty.go +++ b/cmds/core/stty/stty.go @@ -58,7 +58,6 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" "log" "os" @@ -88,7 +87,7 @@ func main() { if len(os.Args) != 3 { log.Fatalf("arg count") } - b, err := ioutil.ReadFile(os.Args[2]) + b, err := os.ReadFile(os.Args[2]) if err != nil { log.Fatalf("stty load: %v", err) } diff --git a/cmds/core/truncate/truncate.go b/cmds/core/truncate/truncate.go index 06b14914f5..65111b8129 100644 --- a/cmds/core/truncate/truncate.go +++ b/cmds/core/truncate/truncate.go @@ -18,7 +18,6 @@ package main import ( "flag" - "io/ioutil" "log" "os" @@ -69,7 +68,7 @@ func main() { var final int64 st, err := os.Stat(fname) if os.IsNotExist(err) && !*create { - if err = ioutil.WriteFile(fname, []byte{}, 0o644); err != nil { + if err = os.WriteFile(fname, []byte{}, 0o644); err != nil { log.Fatalf("truncate: ERROR: %v\n", err) } if st, err = os.Stat(fname); err != nil { diff --git a/cmds/core/truncate/truncate_test.go b/cmds/core/truncate/truncate_test.go index 512b410b16..4bfe80f0d7 100644 --- a/cmds/core/truncate/truncate_test.go +++ b/cmds/core/truncate/truncate_test.go @@ -6,7 +6,6 @@ package main import ( "fmt" - "io/ioutil" "os" "path/filepath" "testing" @@ -144,25 +143,21 @@ var truncateTests = []struct { // TestTruncate implements a table-driven test. func TestTruncate(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "truncate") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() for i, test := range truncateTests { targetFile := filepath.Join(tmpDir, fmt.Sprintf("target%d", i)) refFile := filepath.Join(tmpDir, fmt.Sprintf("ref%d", i)) if test.genTargetFile { data := make([]byte, test.initTargetSize) - if err := ioutil.WriteFile(targetFile, data, 0o600); err != nil { + if err := os.WriteFile(targetFile, data, 0o600); err != nil { t.Errorf("Failed to create test file %s: %v", targetFile, err) continue } } if test.genRefFile { data := make([]byte, test.initRefSize) - if err := ioutil.WriteFile(refFile, data, 0o600); err != nil { + if err := os.WriteFile(refFile, data, 0o600); err != nil { t.Errorf("Failed to create test file %s: %v", targetFile, err) continue } diff --git a/cmds/core/uniq/uniq_test.go b/cmds/core/uniq/uniq_test.go index 25649f3c3e..6207c3b2d0 100644 --- a/cmds/core/uniq/uniq_test.go +++ b/cmds/core/uniq/uniq_test.go @@ -6,8 +6,6 @@ package main import ( "bytes" - "io/ioutil" - "os" "testing" "github.com/u-root/u-root/pkg/testutil" @@ -34,12 +32,6 @@ func TestUniq(t *testing.T) { } ) - tmpDir, err := ioutil.TempDir("", "UniqTest") - if err != nil { - t.Fatal("TempDir failed: ", err) - } - defer os.RemoveAll(tmpDir) - for _, v := range tab { c := testutil.Command(t, v.a...) c.Stdin = bytes.NewReader([]byte(v.i)) diff --git a/cmds/core/uptime/uptime.go b/cmds/core/uptime/uptime.go index c240890d55..081d92b79a 100644 --- a/cmds/core/uptime/uptime.go +++ b/cmds/core/uptime/uptime.go @@ -10,8 +10,8 @@ package main import ( "errors" "fmt" - "io/ioutil" "log" + "os" "strings" "time" ) @@ -41,7 +41,7 @@ func uptime(contents string) (*time.Time, error) { } func main() { - procUptimeOutput, err := ioutil.ReadFile("/proc/uptime") + procUptimeOutput, err := os.ReadFile("/proc/uptime") if err != nil { log.Fatalf("error reading /proc/uptime: %v \n", err) } @@ -49,7 +49,7 @@ func main() { if err != nil { log.Fatal(err) } - procLoadAvgOutput, err := ioutil.ReadFile("/proc/loadavg") + procLoadAvgOutput, err := os.ReadFile("/proc/loadavg") if err != nil { log.Fatalf("error reading /proc/loadavg: %v \n", err) } diff --git a/cmds/core/wc/wc_test.go b/cmds/core/wc/wc_test.go index 1ebcaa2700..f269219ac0 100644 --- a/cmds/core/wc/wc_test.go +++ b/cmds/core/wc/wc_test.go @@ -6,8 +6,6 @@ package main import ( "bytes" - "io/ioutil" - "os" "testing" "github.com/u-root/u-root/pkg/testutil" @@ -25,12 +23,6 @@ func TestWc(t *testing.T) { {"count chars\n", "12\n", 0, []string{"-c"}}, } - tmpDir, err := ioutil.TempDir("", "TestWc") - if err != nil { - t.Fatal("TempDir failed: ", err) - } - defer os.RemoveAll(tmpDir) - for _, v := range tab { c := testutil.Command(t, v.a...) c.Stdin = bytes.NewReader([]byte(v.i)) diff --git a/cmds/core/wget/wget_test.go b/cmds/core/wget/wget_test.go index b55a38fdbf..02205a9a73 100644 --- a/cmds/core/wget/wget_test.go +++ b/cmds/core/wget/wget_test.go @@ -9,7 +9,6 @@ package main import ( "fmt" - "io/ioutil" "log" "net" "net/http" @@ -128,11 +127,7 @@ func TestWget(t *testing.T) { t.Run(tt.name, func(t *testing.T) { // Change the working directory to a temporary directory, so we can // delete the temporary files after the test runs. - tmpDir, err := ioutil.TempDir("", "uroot-wget") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() fileName := filepath.Base(tt.url) @@ -148,7 +143,7 @@ func TestWget(t *testing.T) { } if tt.content != "" { - content, err := ioutil.ReadFile(filepath.Join(tmpDir, fileName)) + content, err := os.ReadFile(filepath.Join(tmpDir, fileName)) if err != nil { t.Errorf("File %s was not created: %v", fileName, err) } diff --git a/cmds/exp/ash/parse.go b/cmds/exp/ash/parse.go index 625412d21f..bc19b3158a 100644 --- a/cmds/exp/ash/parse.go +++ b/cmds/exp/ash/parse.go @@ -13,7 +13,7 @@ import ( "errors" "fmt" "io" - "io/ioutil" + "os" "os/exec" "path" "path/filepath" @@ -175,7 +175,7 @@ func parsestring(b *bufio.Reader, c *Command) (*Command, string) { if !path.IsAbs(s) { s = filepath.Join(envDir, s) } - b, err := ioutil.ReadFile(s) + b, err := os.ReadFile(s) if err != nil { panic(fmt.Errorf("%s: %v", s, err)) } diff --git a/cmds/exp/ash/rush.go b/cmds/exp/ash/rush.go index 969010e629..27e36de91c 100644 --- a/cmds/exp/ash/rush.go +++ b/cmds/exp/ash/rush.go @@ -12,7 +12,6 @@ import ( "bufio" "fmt" "io" - "io/ioutil" "os" "os/exec" "path" @@ -137,7 +136,7 @@ func doArgs(cmds []*Command) error { if !path.IsAbs(v.val) { e = filepath.Join(envDir, e) } - b, err := ioutil.ReadFile(e) + b, err := os.ReadFile(e) if err != nil { return err } diff --git a/cmds/exp/ash/rush_test.go b/cmds/exp/ash/rush_test.go index f4ba15cefa..f74dec1f4a 100644 --- a/cmds/exp/ash/rush_test.go +++ b/cmds/exp/ash/rush_test.go @@ -7,8 +7,6 @@ package main import ( "bytes" "fmt" - "io/ioutil" - "os" "regexp" "strings" "testing" @@ -33,13 +31,6 @@ var tests = []struct { } func testRush(t *testing.T) { - // Create temp directory - tmpDir, err := ioutil.TempDir("", "TestExit") - if err != nil { - t.Fatal("TempDir failed: ", err) - } - defer os.RemoveAll(tmpDir) - // Table-driven testing for i, tt := range tests { t.Run(fmt.Sprintf("test%d", i), func(t *testing.T) { diff --git a/cmds/exp/bootvars/main.go b/cmds/exp/bootvars/main.go index fd338e8828..25c1735993 100644 --- a/cmds/exp/bootvars/main.go +++ b/cmds/exp/bootvars/main.go @@ -15,7 +15,6 @@ package main import ( "flag" - "io/ioutil" "log" "os" fp "path/filepath" @@ -38,7 +37,7 @@ func main() { if *m { // actually mount FS, locate file exe := fp.Base(os.Args[0]) - tmp, err := ioutil.TempDir("", exe) + tmp, err := os.MkdirTemp("", exe) if err != nil { log.Fatalf("creating temp dir: %s", err) } diff --git a/cmds/exp/builtin/builtin.go b/cmds/exp/builtin/builtin.go index 56292014f3..a54ff5b097 100644 --- a/cmds/exp/builtin/builtin.go +++ b/cmds/exp/builtin/builtin.go @@ -7,7 +7,6 @@ package main import ( "flag" "fmt" - "io/ioutil" "log" "os" "os/exec" @@ -95,7 +94,7 @@ func main() { log.Fatal(err) } for _, i := range globs { - if b, err := ioutil.ReadFile(i); err != nil { + if b, err := os.ReadFile(i); err != nil { log.Fatal(err) } else { if _, ok := filemap[i]; ok { @@ -105,7 +104,7 @@ func main() { } } - if b, err := ioutil.ReadFile("/proc/mounts"); err == nil && false { + if b, err := os.ReadFile("/proc/mounts"); err == nil && false { log.Printf("m %v\n", string(b)) } // we'd like to do this here, but it seems it doesn't end @@ -120,7 +119,7 @@ func main() { } } if *debug { - if b, err := ioutil.ReadFile("/proc/mounts"); err == nil { + if b, err := os.ReadFile("/proc/mounts"); err == nil { log.Printf("Reading /proc/mount:m %v\n", b) } } @@ -138,7 +137,7 @@ func main() { } // write the new elvishPath for i, v := range filemap { - if err = ioutil.WriteFile(i, v, 0o600); err != nil { + if err = os.WriteFile(i, v, 0o600); err != nil { log.Fatal(err) } } diff --git a/cmds/exp/bzimage/bzimage.go b/cmds/exp/bzimage/bzimage.go index 2226848cda..1aab8e9b59 100644 --- a/cmds/exp/bzimage/bzimage.go +++ b/cmds/exp/bzimage/bzimage.go @@ -15,7 +15,6 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" "log" "os" "strings" @@ -88,7 +87,7 @@ func main() { fallthrough case "copy", "initramfs", "extract", "cfg": var err error - image, err = ioutil.ReadFile(a[1]) + image, err = os.ReadFile(a[1]) if err != nil { log.Fatal(err) } @@ -114,11 +113,11 @@ func main() { log.Printf("%s", br.Header.Diff(&br2.Header)) log.Fatalf("there is no hope") } - if err := ioutil.WriteFile(a[2], o, 0o666); err != nil { + if err := os.WriteFile(a[2], o, 0o666); err != nil { log.Fatalf("Writing %v: %v", a[2], err) } case "diff": - b2, err := ioutil.ReadFile(a[2]) + b2, err := os.ReadFile(a[2]) if err != nil { log.Fatal(err) } @@ -155,7 +154,7 @@ func main() { fmt.Printf("Warning: %s is nil", v.n) continue } - if err := ioutil.WriteFile(v.n, v.b, 0o666); err != nil { + if err := os.WriteFile(v.n, v.b, 0o666); err != nil { log.Fatalf("Writing %v: %v", v, err) } } @@ -169,7 +168,7 @@ func main() { log.Fatal(err) } - if err := ioutil.WriteFile(a[3], b, 0o644); err != nil { + if err := os.WriteFile(a[3], b, 0o644); err != nil { log.Fatal(err) } case "ver": diff --git a/cmds/exp/bzimage/bzimage_test.go b/cmds/exp/bzimage/bzimage_test.go index 1b05e1f36d..c2c842711f 100644 --- a/cmds/exp/bzimage/bzimage_test.go +++ b/cmds/exp/bzimage/bzimage_test.go @@ -5,8 +5,6 @@ package main import ( - "io/ioutil" - "os" "path/filepath" "strings" "syscall" @@ -109,12 +107,6 @@ var ( ) func TestSimple(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "bzImage") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) - tests := []struct { args []string name string @@ -138,7 +130,7 @@ func TestSimple(t *testing.T) { skip: uskip, }, { - args: []string{"initramfs", "bzImage", "/dev/null", filepath.Join(tmpDir, "zz")}, + args: []string{"initramfs", "bzImage", "/dev/null", filepath.Join(t.TempDir(), "zz")}, name: "correct initramfs test", status: 0, out: "", diff --git a/cmds/exp/cbmem/cbmem_linux_test.go b/cmds/exp/cbmem/cbmem_linux_test.go index b1cac989a7..97a7f78a62 100644 --- a/cmds/exp/cbmem/cbmem_linux_test.go +++ b/cmds/exp/cbmem/cbmem_linux_test.go @@ -9,14 +9,13 @@ import ( "encoding/hex" "encoding/json" "io" - "io/ioutil" "os" "testing" ) func TestNotFound(t *testing.T) { var err error - f, err := ioutil.TempFile("", "cbmemNotFound") + f, err := os.CreateTemp("", "cbmemNotFound") if err != nil { t.Fatal(err) } @@ -54,7 +53,7 @@ func genFile(f *os.File, p func(string, ...interface{}), s []seg) error { func TestAPU2(t *testing.T) { var err error - f, err := ioutil.TempFile("", "cbmemAPU2") + f, err := os.CreateTemp("", "cbmemAPU2") if err != nil { t.Fatal(err) } @@ -95,7 +94,7 @@ func TestAPU2(t *testing.T) { } // You can use this to generate new test data. It's a timesaver. if false { - ioutil.WriteFile("json", j, 0o666) + os.WriteFile("json", j, 0o666) } if string(j) != apu2JSON { t.Errorf("APU2 JSON: got %s, want %s", j, apu2JSON) @@ -104,7 +103,7 @@ func TestAPU2(t *testing.T) { func TestAPU2CBMemWrap(t *testing.T) { var err error - f, err := ioutil.TempFile("", "cbmemWRAPAPU2") + f, err := os.CreateTemp("", "cbmemWRAPAPU2") if err != nil { t.Fatal(err) } @@ -140,7 +139,7 @@ func TestAPU2CBMemWrap(t *testing.T) { func TestAPU2CBBadCursor(t *testing.T) { var err error - f, err := ioutil.TempFile("", "cbmemWRAPAPU2") + f, err := os.CreateTemp("", "cbmemWRAPAPU2") if err != nil { t.Fatal(err) } @@ -175,7 +174,7 @@ func TestAPU2CBBadCursor(t *testing.T) { func TestAPU2CBBadPtr(t *testing.T) { var err error - f, err := ioutil.TempFile("", "cbmemWRAPAPU2") + f, err := os.CreateTemp("", "cbmemWRAPAPU2") if err != nil { t.Fatal(err) } @@ -213,7 +212,7 @@ func TestIO(t *testing.T) { } func TestOffsetReader(t *testing.T) { - memFile, err := ioutil.TempFile("", "cbmemAPU2") + memFile, err := os.CreateTemp("", "cbmemAPU2") if err != nil { t.Fatal(err) } @@ -263,7 +262,7 @@ func TestOffsetReader(t *testing.T) { } func TestTimeStampsAPU2(t *testing.T) { - f, err := ioutil.TempFile("", "cbmemAPU2") + f, err := os.CreateTemp("", "cbmemAPU2") if err != nil { t.Fatal(err) } diff --git a/cmds/exp/cbmem/gen/gen.go b/cmds/exp/cbmem/gen/gen.go index 20e60f7e56..4fa3ecdfdf 100644 --- a/cmds/exp/cbmem/gen/gen.go +++ b/cmds/exp/cbmem/gen/gen.go @@ -8,7 +8,6 @@ import ( "bytes" "flag" "fmt" - "io/ioutil" "log" "os" "syscall" @@ -72,7 +71,7 @@ func main() { if err != nil { log.Fatal(err) } - if err := ioutil.WriteFile("apu2.go", []byte(s), 0o644); err != nil { + if err := os.WriteFile("apu2.go", []byte(s), 0o644); err != nil { log.Fatal(err) } os.Exit(0) diff --git a/cmds/exp/cksum/cksum.go b/cmds/exp/cksum/cksum.go index 9b148567bb..a189838873 100644 --- a/cmds/exp/cksum/cksum.go +++ b/cmds/exp/cksum/cksum.go @@ -7,7 +7,7 @@ package main import ( "fmt" "hash/crc32" - "io/ioutil" + "io" "os" "github.com/spf13/pflag" @@ -48,9 +48,9 @@ func versionPrinter() { func getInput(fileName string) (input []byte, err error) { if fileName != "" { - return ioutil.ReadFile(fileName) + return os.ReadFile(fileName) } - return ioutil.ReadAll(os.Stdin) + return io.ReadAll(os.Stdin) } func appendLengthToData(data []byte) []byte { diff --git a/cmds/exp/dmidecode/data_linux.go b/cmds/exp/dmidecode/data_linux.go index 1fa7d2641d..cb6c24ea7e 100644 --- a/cmds/exp/dmidecode/data_linux.go +++ b/cmds/exp/dmidecode/data_linux.go @@ -8,7 +8,7 @@ import ( "errors" "fmt" "io" - "io/ioutil" + "os" "path/filepath" ) @@ -20,7 +20,7 @@ func getData(textOut io.Writer, dumpFile, sysfsPath string) ([]byte, []byte, err var entry, data []byte if dumpFile != "" { fmt.Fprintf(textOut, "Reading SMBIOS/DMI data from file %s.\n", dumpFile) - data, err = ioutil.ReadFile(dumpFile) + data, err = os.ReadFile(dumpFile) if err != nil { return nil, nil, fmt.Errorf("error reading dump: %v", err) } @@ -31,11 +31,11 @@ func getData(textOut io.Writer, dumpFile, sysfsPath string) ([]byte, []byte, err data = data[32:] } else { fmt.Fprintf(textOut, "Reading SMBIOS/DMI data from sysfs.\n") - entry, err = ioutil.ReadFile(filepath.Join(sysfsPath, "smbios_entry_point")) + entry, err = os.ReadFile(filepath.Join(sysfsPath, "smbios_entry_point")) if err != nil { return nil, nil, fmt.Errorf("error reading DMI data: %v", err) } - data, err = ioutil.ReadFile(filepath.Join(sysfsPath, "DMI")) + data, err = os.ReadFile(filepath.Join(sysfsPath, "DMI")) if err != nil { return nil, nil, fmt.Errorf("error reading DMI data: %v", err) } diff --git a/cmds/exp/dmidecode/dmidecode_test.go b/cmds/exp/dmidecode/dmidecode_test.go index cae0bf8261..5e7085b82f 100644 --- a/cmds/exp/dmidecode/dmidecode_test.go +++ b/cmds/exp/dmidecode/dmidecode_test.go @@ -8,7 +8,6 @@ import ( "bytes" "encoding/hex" "fmt" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -40,13 +39,13 @@ func testOutput(t *testing.T, dumpFile string, args []string, expectedOutFile st return } actualOut := out.Bytes() - expectedOut, err := ioutil.ReadFile(expectedOutFile) + expectedOut, err := os.ReadFile(expectedOutFile) if err != nil { t.Errorf("%+v %+v %+v: failed to load %s: %v", dumpFile, args, expectedOutFile, expectedOutFile, err) return } if !bytes.Equal(actualOut, expectedOut) { - ioutil.WriteFile(actualOutFile, actualOut, 0o644) + os.WriteFile(actualOutFile, actualOut, 0o644) t.Errorf("%+v %+v %+v: output mismatch, see %s", dumpFile, args, expectedOutFile, actualOutFile) diffOut, _ := exec.Command("diff", "-u", expectedOutFile, actualOutFile).CombinedOutput() t.Errorf("%+v %+v %+v: diff:\n%s", dumpFile, args, expectedOutFile, string(diffOut)) @@ -70,7 +69,7 @@ func TestDMIDecodeTypeFilters(t *testing.T) { } func testDumpBin(t *testing.T, entryData, expectedOutData []byte) { - tmpfile, err := ioutil.TempFile("", "dmidecode") + tmpfile, err := os.CreateTemp("", "dmidecode") if err != nil { t.Fatalf("error creating temp file: %v", err) } @@ -85,7 +84,7 @@ func testDumpBin(t *testing.T, entryData, expectedOutData []byte) { ); err != nil { t.Fatalf("failed to dump bin: %v", err) } - outData, err := ioutil.ReadFile(tmpfile.Name()) + outData, err := os.ReadFile(tmpfile.Name()) if err != nil { t.Fatalf("failed to read output: %v", err) } diff --git a/cmds/exp/kconf/kconf.go b/cmds/exp/kconf/kconf.go index 4a1209e79b..ad53023dab 100644 --- a/cmds/exp/kconf/kconf.go +++ b/cmds/exp/kconf/kconf.go @@ -13,7 +13,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "log" "os" "strings" @@ -42,7 +41,7 @@ func main() { var cfgIn io.Reader if len(*k) > 0 { - image, err := ioutil.ReadFile(*k) + image, err := os.ReadFile(*k) if err != nil { log.Fatal(err) } diff --git a/cmds/exp/madeye/madeye.go b/cmds/exp/madeye/madeye.go index 85ccd90d42..f2797424c2 100644 --- a/cmds/exp/madeye/madeye.go +++ b/cmds/exp/madeye/madeye.go @@ -33,7 +33,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "log" "os" "path/filepath" @@ -90,7 +89,7 @@ func file(archiver cpio.RecordFormat, n string, f io.ReaderAt) ([]cpio.Record, e // TODO: make this use os constants, not unix constants. switch rec.Mode & unix.S_IFMT { case unix.S_IFLNK: - content, err := ioutil.ReadAll(uio.Reader(rec)) + content, err := io.ReadAll(uio.Reader(rec)) if err != nil { return nil, err } diff --git a/cmds/exp/netbootxyz/netbootxyz.go b/cmds/exp/netbootxyz/netbootxyz.go index 2e12838eaa..7e4f31601e 100644 --- a/cmds/exp/netbootxyz/netbootxyz.go +++ b/cmds/exp/netbootxyz/netbootxyz.go @@ -8,7 +8,7 @@ import ( "crypto/tls" "flag" "fmt" - "io/ioutil" + "io" "net/http" "os" "os/exec" @@ -242,7 +242,7 @@ func main() { if err != nil { fmt.Printf("Error : %v\n", err) } - content, err := ioutil.ReadAll(response.Body) + content, err := io.ReadAll(response.Body) if err != nil { fmt.Println(err) return diff --git a/cmds/exp/newsshd/newsshd.go b/cmds/exp/newsshd/newsshd.go index 963a0f2bfd..2afd365215 100644 --- a/cmds/exp/newsshd/newsshd.go +++ b/cmds/exp/newsshd/newsshd.go @@ -7,7 +7,6 @@ package main import ( "fmt" "io" - "io/ioutil" "log" "os" "os/exec" @@ -69,7 +68,7 @@ func main() { publicKeyOption := func(ctx ssh.Context, key ssh.PublicKey) bool { // Glob the users's home directory for all the // possible keys? - data, err := ioutil.ReadFile(*pubKeyFile) + data, err := os.ReadFile(*pubKeyFile) if err != nil { fmt.Print(err) return false diff --git a/cmds/exp/pflask/pflask.go b/cmds/exp/pflask/pflask.go index 7fea5f87fe..91aec948aa 100644 --- a/cmds/exp/pflask/pflask.go +++ b/cmds/exp/pflask/pflask.go @@ -8,7 +8,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "log" "os" "os/exec" @@ -100,7 +99,7 @@ func (c cgroupname) Create(s, name string) { func (c cgroupname) Attach(s, name string, pid int) { t := filepath.Join(string(c), s, name, "tasks") b := []byte(fmt.Sprintf("%v", pid)) - if err := ioutil.WriteFile(t, b, 0o600); err != nil { + if err := os.WriteFile(t, b, 0o600); err != nil { log.Fatal(err) } } @@ -195,7 +194,7 @@ func makeConsole(base, console string, unprivileged bool) { // In unprivileged uses, we can't mknod /dev/console, however, // we can just create a file /dev/console and use bind mount on file. if _, err := os.Stat(nn); err != nil { - ioutil.WriteFile(nn, []byte{}, 0o600) // best effort, ignore error + os.WriteFile(nn, []byte{}, 0o600) // best effort, ignore error } } else { if err := syscall.Mknod(nn, mode, dev); err != nil { diff --git a/cmds/exp/pox/pox.go b/cmds/exp/pox/pox.go index 893285ea1d..7d7399a603 100644 --- a/cmds/exp/pox/pox.go +++ b/cmds/exp/pox/pox.go @@ -88,7 +88,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "log" "os" "os/exec" @@ -161,7 +160,7 @@ func poxCreate(bin ...string) error { sort.Strings(names) // Now we need to make a template file hierarchy and put // the stuff we want in there. - dir, err := ioutil.TempDir("", "pox") + dir, err := os.MkdirTemp("", "pox") if err != nil { return err } @@ -247,7 +246,7 @@ func poxRun(args ...string) error { if len(args) == 0 { return fmt.Errorf(usage) } - dir, err := ioutil.TempDir("", "pox") + dir, err := os.MkdirTemp("", "pox") if err != nil { return err } diff --git a/cmds/exp/pox/pox_test.go b/cmds/exp/pox/pox_test.go index 28a825263f..75471effa6 100644 --- a/cmds/exp/pox/pox_test.go +++ b/cmds/exp/pox/pox_test.go @@ -5,7 +5,6 @@ package main import ( - "io/ioutil" "os" "path/filepath" "syscall" @@ -21,12 +20,7 @@ func TestSimple(t *testing.T) { t.Skip("Must be root for this test") } - tmpDir, err := ioutil.TempDir("", "pox") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) - f := filepath.Join(tmpDir, "x.tcz") + f := filepath.Join(t.TempDir(), "x.tcz") tests := []struct { args []string name string diff --git a/cmds/exp/rush/parse.go b/cmds/exp/rush/parse.go index 9991cc0a3b..12137cd556 100644 --- a/cmds/exp/rush/parse.go +++ b/cmds/exp/rush/parse.go @@ -13,7 +13,7 @@ import ( "errors" "fmt" "io" - "io/ioutil" + "os" "os/exec" "path" "path/filepath" @@ -175,7 +175,7 @@ func parsestring(b *bufio.Reader, c *Command) (*Command, string) { if !path.IsAbs(s) { s = filepath.Join(envDir, s) } - b, err := ioutil.ReadFile(s) + b, err := os.ReadFile(s) if err != nil { panic(fmt.Errorf("%s: %v", s, err)) } diff --git a/cmds/exp/rush/rush.go b/cmds/exp/rush/rush.go index 07d5d95d29..6f9bd695b8 100644 --- a/cmds/exp/rush/rush.go +++ b/cmds/exp/rush/rush.go @@ -12,7 +12,6 @@ import ( "bufio" "fmt" "io" - "io/ioutil" "os" "os/exec" "path" @@ -134,7 +133,7 @@ func doArgs(cmds []*Command) error { if !path.IsAbs(v.val) { e = filepath.Join(envDir, e) } - b, err := ioutil.ReadFile(e) + b, err := os.ReadFile(e) if err != nil { return err } diff --git a/cmds/exp/rush/rush_test.go b/cmds/exp/rush/rush_test.go index 17a32612e1..286f164544 100644 --- a/cmds/exp/rush/rush_test.go +++ b/cmds/exp/rush/rush_test.go @@ -7,8 +7,6 @@ package main import ( "bytes" "fmt" - "io/ioutil" - "os" "regexp" "strings" "testing" @@ -33,13 +31,6 @@ var tests = []struct { } func TestRush(t *testing.T) { - // Create temp directory - tmpDir, err := ioutil.TempDir("", "TestExit") - if err != nil { - t.Fatal("TempDir failed: ", err) - } - defer os.RemoveAll(tmpDir) - // Table-driven testing for i, tt := range tests { t.Run(fmt.Sprintf("test%d", i), func(t *testing.T) { diff --git a/cmds/exp/smbios_transfer/smbios_transfer.go b/cmds/exp/smbios_transfer/smbios_transfer.go index 726800ca36..f221bc831e 100644 --- a/cmds/exp/smbios_transfer/smbios_transfer.go +++ b/cmds/exp/smbios_transfer/smbios_transfer.go @@ -14,8 +14,8 @@ package main import ( "flag" "fmt" - "io/ioutil" "log" + "os" "path/filepath" "github.com/u-root/u-root/pkg/ipmi" @@ -74,7 +74,7 @@ func writeCommitSmbiosBlob(id string, data []uint8, h *blobs.BlobHandler) (rerr } func getSmbiosData() ([]uint8, error) { - tables, err := ioutil.ReadFile(filepath.Join(sysfsPath, "DMI")) + tables, err := os.ReadFile(filepath.Join(sysfsPath, "DMI")) if err != nil { return nil, fmt.Errorf("error reading DMI data: %v", err) } diff --git a/cmds/exp/tcz/tcz.go b/cmds/exp/tcz/tcz.go index 71820f140b..391d4b22c3 100644 --- a/cmds/exp/tcz/tcz.go +++ b/cmds/exp/tcz/tcz.go @@ -8,7 +8,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "log" "net/http" "os" @@ -185,14 +184,14 @@ func installPackage(tczName string, deps map[string]bool) error { debug("Fetched dep ok!\n") } else { debug("No dep file found\n") - if err := ioutil.WriteFile(filepath.Join(tczLocalPackageDir, depName), []byte{}, os.FileMode(0o444)); err != nil { + if err := os.WriteFile(filepath.Join(tczLocalPackageDir, depName), []byte{}, os.FileMode(0o444)); err != nil { debug("Tried to write Blank file %v, failed %v\n", depName, err) } return nil } // read deps file depFullPath := filepath.Join(tczLocalPackageDir, depName) - deplist, err := ioutil.ReadFile(depFullPath) + deplist, err := os.ReadFile(depFullPath) if err != nil { l.Fatalf("Fetched dep file %v but can't read it? %v", depName, err) } @@ -220,7 +219,7 @@ func installPackage(tczName string, deps map[string]bool) error { if string(deplist) == realDepList { return nil } - if err := ioutil.WriteFile(depFullPath, []byte(realDepList), os.FileMode(0o444)); err != nil { + if err := os.WriteFile(depFullPath, []byte(realDepList), os.FileMode(0o444)); err != nil { debug("Tried to write deplist file %v, failed %v\n", depName, err) return err } diff --git a/cmds/exp/validate/validate.go b/cmds/exp/validate/validate.go index 79ea516994..77106e5726 100644 --- a/cmds/exp/validate/validate.go +++ b/cmds/exp/validate/validate.go @@ -19,18 +19,16 @@ package main import ( "crypto" + _ "crypto/md5" + _ "crypto/sha1" + _ "crypto/sha256" + _ "crypto/sha512" "flag" "fmt" - "io/ioutil" "log" "os" "strings" - _ "crypto/md5" - _ "crypto/sha1" - _ "crypto/sha256" - _ "crypto/sha512" - _ "golang.org/x/crypto/md4" _ "golang.org/x/crypto/openpgp" _ "golang.org/x/crypto/ripemd160" @@ -112,7 +110,7 @@ func main() { v, f := flag.Args()[0], flag.Args()[1] - sigData, err := ioutil.ReadFile(v) + sigData, err := os.ReadFile(v) if err != nil { log.Fatalf("%v", err) } @@ -127,13 +125,13 @@ func main() { debug("Signature is %v len %v", sig[0], len(sig[0])) - b, err := ioutil.ReadFile(f) + b, err := os.ReadFile(f) if err != nil { log.Fatalf("%s: %v", f, err) } if *sumfile != "" { - sum, err := ioutil.ReadFile(*sumfile) + sum, err := os.ReadFile(*sumfile) if err != nil { log.Fatalf("Can't read sumfile: %v", *sumfile) } diff --git a/cmds/exp/validate/validate_test.go b/cmds/exp/validate/validate_test.go index 1b83d32a66..2ba654301d 100644 --- a/cmds/exp/validate/validate_test.go +++ b/cmds/exp/validate/validate_test.go @@ -5,7 +5,7 @@ package main import ( - "io/ioutil" + "io" "os" "path/filepath" "syscall" @@ -38,17 +38,13 @@ ff02::2 ip6-allrouters {name: "hosts.sha1", val: []byte("3f397a3b3a7450075da91b078afa35b794cf6088 hosts"), o: "SHA1\n"}, } - tmpDir, err := ioutil.TempDir("", "validatetest") - if err != nil { - t.Fatal("TempDir failed: ", err) - } - defer os.RemoveAll(tmpDir) - if err := ioutil.WriteFile(filepath.Join(tmpDir, "hosts"), data, 0o444); err != nil { + tmpDir := t.TempDir() + if err := os.WriteFile(filepath.Join(tmpDir, "hosts"), data, 0o444); err != nil { t.Fatalf("Can't set up data file: %v", err) } for _, v := range tests { - if err := ioutil.WriteFile(filepath.Join(tmpDir, v.name), v.val, 0o444); err != nil { + if err := os.WriteFile(filepath.Join(tmpDir, v.name), v.val, 0o444); err != nil { t.Fatalf("Can't set up hash file: %v", err) } @@ -65,11 +61,11 @@ ff02::2 ip6-allrouters if err := c.Start(); err != nil { t.Fatalf("Can't start %v: %v", c, err) } - e, err := ioutil.ReadAll(ep) + e, err := io.ReadAll(ep) if err != nil { t.Fatalf("Can't get stderr of %v: %v", c, err) } - o, err := ioutil.ReadAll(op) + o, err := io.ReadAll(op) if err != nil { t.Fatalf("Can't get sdout of %v: %v", c, err) } diff --git a/cmds/exp/vboot/vboot.go b/cmds/exp/vboot/vboot.go index 764ff55020..f3c029b4ef 100644 --- a/cmds/exp/vboot/vboot.go +++ b/cmds/exp/vboot/vboot.go @@ -8,7 +8,6 @@ import ( "crypto/sha1" "crypto/sha256" "flag" - "io/ioutil" "log" "os" "os/exec" @@ -60,7 +59,7 @@ func main() { files := make(map[string][]byte) for _, element := range paths { - data, err := ioutil.ReadFile(element) + data, err := os.ReadFile(element) if err != nil { die(err) } else { diff --git a/integration/generic-tests/dhclient_test.go b/integration/generic-tests/dhclient_test.go index c9ba7e4299..454b3d4050 100644 --- a/integration/generic-tests/dhclient_test.go +++ b/integration/generic-tests/dhclient_test.go @@ -9,7 +9,6 @@ package integration import ( "fmt" - "io/ioutil" "net" "net/http" "os" @@ -31,15 +30,11 @@ func TestDhclientQEMU4(t *testing.T) { } // Create the file to download - dir, err := ioutil.TempDir("", "dhclient-") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() want := "conteeent" foobarFile := filepath.Join(dir, "foobar") - if err := ioutil.WriteFile(foobarFile, []byte(want), 0o644); err != nil { + if err := os.WriteFile(foobarFile, []byte(want), 0o644); err != nil { t.Fatal(err) } diff --git a/pkg/acpi/raw.go b/pkg/acpi/raw.go index 29a9403297..9929d0a668 100644 --- a/pkg/acpi/raw.go +++ b/pkg/acpi/raw.go @@ -8,7 +8,6 @@ import ( "encoding/binary" "fmt" "io" - "io/ioutil" "os" "github.com/u-root/u-root/pkg/memio" @@ -51,7 +50,7 @@ func NewRaw(b []byte) ([]Table, error) { // RawFromFile reads from an io.Reader and returns a []Table and error if any. func RawFromFile(r io.Reader) ([]Table, error) { - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { return nil, err } diff --git a/pkg/bb/bb.go b/pkg/bb/bb.go index 7f321dcda6..e042115015 100644 --- a/pkg/bb/bb.go +++ b/pkg/bb/bb.go @@ -34,7 +34,6 @@ import ( "go/parser" "go/token" "go/types" - "io/ioutil" "os" "path" "path/filepath" @@ -599,7 +598,7 @@ func writeGoFile(path string, code []byte) error { return fmt.Errorf("bad parse while processing imports %q: %v", path, err) } - if err := ioutil.WriteFile(path, code, 0o644); err != nil { + if err := os.WriteFile(path, code, 0o644); err != nil { return fmt.Errorf("error writing Go file to %q: %v", path, err) } return nil diff --git a/pkg/bb/bb_test.go b/pkg/bb/bb_test.go index 08cd6141da..8c264cf21b 100644 --- a/pkg/bb/bb_test.go +++ b/pkg/bb/bb_test.go @@ -5,8 +5,6 @@ package bb import ( - "io/ioutil" - "os" "os/exec" "path/filepath" "testing" @@ -15,13 +13,7 @@ import ( ) func TestPackageRewriteFile(t *testing.T) { - dir, err := ioutil.TempDir("", "u-root") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) - - bin := filepath.Join(dir, "foo") + bin := filepath.Join(t.TempDir(), "foo") if err := BuildBusybox(golang.Default(), []string{"github.com/u-root/u-root/pkg/uroot/test/foo"}, false, bin); err != nil { t.Fatal(err) } diff --git a/pkg/boot/bls/bls_test.go b/pkg/boot/bls/bls_test.go index a45f07bf4e..020bc39f80 100644 --- a/pkg/boot/bls/bls_test.go +++ b/pkg/boot/bls/bls_test.go @@ -5,7 +5,6 @@ package bls import ( - "io/ioutil" "os" "path/filepath" "strconv" @@ -85,7 +84,7 @@ func TestScanBLSEntries(t *testing.T) { for _, test := range tests { configPath := strings.TrimRight(test, ".json") t.Run(configPath, func(t *testing.T) { - want, err := ioutil.ReadFile(test) + want, err := os.ReadFile(test) if err != nil { t.Errorf("Failed to read test json '%v':%v", test, err) } diff --git a/pkg/boot/boottest/json.go b/pkg/boot/boottest/json.go index 83d243de3f..24b49991da 100644 --- a/pkg/boot/boottest/json.go +++ b/pkg/boot/boottest/json.go @@ -20,7 +20,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/url" "os" "strconv" @@ -99,7 +98,7 @@ func ToJSONFile(imgs []boot.OSImage, filename string) error { if err != nil { return err } - return ioutil.WriteFile(filename, enc, 0o644) + return os.WriteFile(filename, enc, 0o644) } // ImagesToJSONLike spits out a json-convertible reproducible representation of diff --git a/pkg/boot/bzimage/bzimage.go b/pkg/boot/bzimage/bzimage.go index 46f5f6fade..c28f0fada1 100644 --- a/pkg/boot/bzimage/bzimage.go +++ b/pkg/boot/bzimage/bzimage.go @@ -18,7 +18,7 @@ import ( "errors" "fmt" "io" - "io/ioutil" + "os" "os/exec" "reflect" "strings" @@ -238,7 +238,7 @@ func unpack(d []byte, c exec.Cmd) ([]byte, error) { return nil, err } - dat, err := ioutil.ReadAll(stdout) + dat, err := io.ReadAll(stdout) if err != nil { return nil, err } @@ -246,7 +246,7 @@ func unpack(d []byte, c exec.Cmd) ([]byte, error) { // You can enable this if you have a nasty bug from xz. // Just be aware that xz ALWAYS errors out even when nothing is wrong. if false { - if e, err := ioutil.ReadAll(stderr); err != nil || len(e) > 0 { + if e, err := io.ReadAll(stderr); err != nil || len(e) > 0 { Debug("xz stderr: '%s', %v", string(e), err) } } @@ -267,7 +267,7 @@ func compress(b []byte, dictOps string) ([]byte, error) { return nil, err } - dat, err := ioutil.ReadAll(stdout) + dat, err := io.ReadAll(stdout) if err != nil { return nil, err } @@ -326,7 +326,7 @@ func Equal(a, b []byte) error { // AddInitRAMFS adds an initramfs to the BzImage. func (b *BzImage) AddInitRAMFS(name string) error { - u, err := ioutil.ReadFile(name) + u, err := os.ReadFile(name) if err != nil { return err } @@ -447,7 +447,7 @@ func (b *BzImage) InitRAMFS() (int, int, error) { var prog *elf.Prog for _, p := range f.Progs { if p.Flags&(elf.PF_X|elf.PF_W|elf.PF_R) == elf.PF_X|elf.PF_W|elf.PF_R { - dat, err = ioutil.ReadAll(p.Open()) + dat, err = io.ReadAll(p.Open()) if err != nil { return -1, -1, err } @@ -537,7 +537,7 @@ func (b *BzImage) ReadConfig() (string, error) { } // make it stop at end of stream, since we don't know the actual size gz.Multistream(false) - cfg, err := ioutil.ReadAll(gz) + cfg, err := io.ReadAll(gz) if err != nil { return "", err } diff --git a/pkg/boot/bzimage/bzimage_test.go b/pkg/boot/bzimage/bzimage_test.go index 652f72aad5..c05b96bd34 100644 --- a/pkg/boot/bzimage/bzimage_test.go +++ b/pkg/boot/bzimage/bzimage_test.go @@ -5,7 +5,7 @@ package bzimage import ( - "io/ioutil" + "os" "testing" "github.com/u-root/u-root/pkg/cpio" @@ -15,7 +15,7 @@ var badmagic = []byte("hi there") func TestUnmarshal(t *testing.T) { Debug = t.Logf - image, err := ioutil.ReadFile("testdata/bzImage") + image, err := os.ReadFile("testdata/bzImage") if err != nil { t.Fatal(err) } @@ -27,7 +27,7 @@ func TestUnmarshal(t *testing.T) { func TestMarshal(t *testing.T) { Debug = t.Logf - image, err := ioutil.ReadFile("testdata/bzImage") + image, err := os.ReadFile("testdata/bzImage") if err != nil { t.Fatal(err) } @@ -103,7 +103,7 @@ func TestBadMagic(t *testing.T) { func TestAddInitRAMFS(t *testing.T) { t.Logf("TestAddInitRAMFS") Debug = t.Logf - initramfsimage, err := ioutil.ReadFile("testdata/bzimage-64kurandominitramfs") + initramfsimage, err := os.ReadFile("testdata/bzimage-64kurandominitramfs") if err != nil { t.Fatal(err) } @@ -122,7 +122,7 @@ func TestAddInitRAMFS(t *testing.T) { // qemu-system-x86_64 -serial stdio -kernel /tmp/x // I mainly left this here as a memo. if true { - if err := ioutil.WriteFile("/tmp/x", d, 0o644); err != nil { + if err := os.WriteFile("/tmp/x", d, 0o644); err != nil { t.Fatal(err) } } @@ -150,7 +150,7 @@ func TestAddInitRAMFS(t *testing.T) { func TestHeaderString(t *testing.T) { Debug = t.Logf - initramfsimage, err := ioutil.ReadFile("testdata/bzImage") + initramfsimage, err := os.ReadFile("testdata/bzImage") if err != nil { t.Fatal(err) } @@ -163,7 +163,7 @@ func TestHeaderString(t *testing.T) { func TestExtract(t *testing.T) { Debug = t.Logf - initramfsimage, err := ioutil.ReadFile("testdata/bzImage") + initramfsimage, err := os.ReadFile("testdata/bzImage") if err != nil { t.Fatal(err) } @@ -185,7 +185,7 @@ func TestExtract(t *testing.T) { func TestELF(t *testing.T) { Debug = t.Logf - initramfsimage, err := ioutil.ReadFile("testdata/bzImage") + initramfsimage, err := os.ReadFile("testdata/bzImage") if err != nil { t.Fatal(err) } @@ -205,7 +205,7 @@ func TestInitRAMFS(t *testing.T) { Debug = t.Logf cpio.Debug = t.Logf for _, bz := range []string{"testdata/bzImage", "testdata/bzimage-64kurandominitramfs"} { - initramfsimage, err := ioutil.ReadFile(bz) + initramfsimage, err := os.ReadFile(bz) if err != nil { t.Fatal(err) } diff --git a/pkg/boot/esxi/esxi.go b/pkg/boot/esxi/esxi.go index f38486e894..083dadcab6 100644 --- a/pkg/boot/esxi/esxi.go +++ b/pkg/boot/esxi/esxi.go @@ -30,7 +30,6 @@ import ( "encoding/hex" "fmt" "io" - "io/ioutil" "os" "path/filepath" "strconv" @@ -125,7 +124,7 @@ func getImages(device string, opts5, opts6 *options) ([]*boot.MultibootImage, er // // device will be mounted at mountPoint. func LoadCDROM(device string) (*boot.MultibootImage, *mount.MountPoint, error) { - mountPoint, err := ioutil.TempDir("", "esxi-mount-") + mountPoint, err := os.MkdirTemp("", "esxi-mount-") if err != nil { return nil, nil, err } @@ -165,7 +164,7 @@ func mountPartition(parentdev string, partition int) (*options, *mount.MountPoin return nil, nil, err } base := filepath.Base(dev) - mountPoint, err := ioutil.TempDir("", fmt.Sprintf("%s-", base)) + mountPoint, err := os.MkdirTemp("", fmt.Sprintf("%s-", base)) if err != nil { return nil, nil, err } diff --git a/pkg/boot/fit/fit_test.go b/pkg/boot/fit/fit_test.go index 46a5cbb003..51d84d4fd8 100644 --- a/pkg/boot/fit/fit_test.go +++ b/pkg/boot/fit/fit_test.go @@ -9,7 +9,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "path/filepath" "reflect" @@ -83,7 +82,7 @@ func TestLoad(t *testing.T) { var keys []*openpgp.Entity for _, k := range keyFiles { - b, err := ioutil.ReadFile(filepath.Join("testdata", k)) + b, err := os.ReadFile(filepath.Join("testdata", k)) if err != nil { t.Fatal(err) } @@ -216,7 +215,7 @@ func TestReadSignedImage(t *testing.T) { var keys []*openpgp.Entity for _, k := range keyFiles { - b, err := ioutil.ReadFile(filepath.Join("testdata", k)) + b, err := os.ReadFile(filepath.Join("testdata", k)) if err != nil { t.Fatal(err) } diff --git a/pkg/boot/grub/config_test.go b/pkg/boot/grub/config_test.go index 3a3c3cb01a..f9a8a8ed2a 100644 --- a/pkg/boot/grub/config_test.go +++ b/pkg/boot/grub/config_test.go @@ -6,7 +6,7 @@ package grub import ( "context" - "io/ioutil" + "os" "path/filepath" "strings" "testing" @@ -20,7 +20,7 @@ import ( // The pool is pre-populated so that Mount is never called. func fakeDevices() (block.BlockDevices, *mount.Pool, error) { // For some reason, Glob("testdata_new/*/") does not work here. - files, err := ioutil.ReadDir("testdata_new") + files, err := os.ReadDir("testdata_new") if err != nil { return nil, nil, err } @@ -35,7 +35,7 @@ func fakeDevices() (block.BlockDevices, *mount.Pool, error) { mountPool := &mount.Pool{} for _, dir := range dirs { // TODO: Also add LABEL to BlockDev - fsUUID, _ := ioutil.ReadFile(filepath.Join(dir, "UUID")) + fsUUID, _ := os.ReadFile(filepath.Join(dir, "UUID")) devices = append(devices, &block.BlockDev{ Name: dir, FSType: "test", @@ -86,7 +86,7 @@ func TestConfigs(t *testing.T) { for _, test := range tests { configPath := strings.TrimRight(test, ".json") t.Run(configPath, func(t *testing.T) { - want, err := ioutil.ReadFile(test) + want, err := os.ReadFile(test) if err != nil { t.Errorf("Failed to read test json '%v':%v", test, err) } diff --git a/pkg/boot/grub/echo_test.go b/pkg/boot/grub/echo_test.go index 29cf201865..58fa58a419 100644 --- a/pkg/boot/grub/echo_test.go +++ b/pkg/boot/grub/echo_test.go @@ -9,7 +9,6 @@ import ( "context" "flag" "fmt" - "io/ioutil" "net/url" "os" "os/exec" @@ -100,7 +99,7 @@ func TestGrubTests(t *testing.T) { t.Fatalf("process ran with err %v", err) } } else { - out, err = ioutil.ReadFile(golden) + out, err = os.ReadFile(golden) if err != nil { t.Fatalf("error loading file `%s`, %v", golden, err) } @@ -115,7 +114,7 @@ func TestGrubTests(t *testing.T) { c := newParser(wd, block.BlockDevices{}, mountPool, curl.DefaultSchemes) c.W = &b - script, err := ioutil.ReadFile(file) + script, err := os.ReadFile(file) if err != nil { t.Fatalf("error loading file `%s`, %v", file, err) } @@ -129,7 +128,7 @@ func TestGrubTests(t *testing.T) { } // update/create golden file on success if *update { - err := ioutil.WriteFile(golden, out, 0o644) + err := os.WriteFile(golden, out, 0o644) if err != nil { t.Fatalf("error writing file `%s`, %v", file, err) } diff --git a/pkg/boot/kexec/memory_linux.go b/pkg/boot/kexec/memory_linux.go index 20683034ab..1a1c508a82 100644 --- a/pkg/boot/kexec/memory_linux.go +++ b/pkg/boot/kexec/memory_linux.go @@ -9,7 +9,6 @@ import ( "debug/elf" "fmt" "io" - "io/ioutil" "log" "os" "path" @@ -566,7 +565,7 @@ func internalParseMemoryMap(memoryMapDir string) (MemoryMap, error) { } dir := path.Dir(name) - b, err := ioutil.ReadFile(name) + b, err := os.ReadFile(name) if err != nil { return fmt.Errorf("error reading file %q: %v", name, err) } diff --git a/pkg/boot/kexec/memory_linux_test.go b/pkg/boot/kexec/memory_linux_test.go index 8077adcffa..b4c3ca1575 100644 --- a/pkg/boot/kexec/memory_linux_test.go +++ b/pkg/boot/kexec/memory_linux_test.go @@ -6,7 +6,6 @@ package kexec import ( "fmt" - "io/ioutil" "os" "path" "reflect" @@ -16,24 +15,20 @@ import ( ) func TestParseMemoryMap(t *testing.T) { - root, err := ioutil.TempDir("", "memmap") - if err != nil { - t.Fatalf("Cannot create test dir: %v", err) - } - defer os.RemoveAll(root) + root := t.TempDir() create := func(dir string, start, end uintptr, typ RangeType) error { p := path.Join(root, dir) if err := os.Mkdir(p, 0o755); err != nil { return err } - if err := ioutil.WriteFile(path.Join(p, "start"), []byte(fmt.Sprintf("%#x\n", start)), 0o655); err != nil { + if err := os.WriteFile(path.Join(p, "start"), []byte(fmt.Sprintf("%#x\n", start)), 0o655); err != nil { return err } - if err := ioutil.WriteFile(path.Join(p, "end"), []byte(fmt.Sprintf("%#x\n", end)), 0o655); err != nil { + if err := os.WriteFile(path.Join(p, "end"), []byte(fmt.Sprintf("%#x\n", end)), 0o655); err != nil { return err } - return ioutil.WriteFile(path.Join(p, "type"), append([]byte(typ), '\n'), 0o655) + return os.WriteFile(path.Join(p, "type"), append([]byte(typ), '\n'), 0o655) } if err := create("0", 0, 49, RangeRAM); err != nil { diff --git a/pkg/boot/linux.go b/pkg/boot/linux.go index 6b41b6977d..36a2cb51ed 100644 --- a/pkg/boot/linux.go +++ b/pkg/boot/linux.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "log" "os" @@ -106,7 +105,7 @@ func copyToFileIfNotRegular(r io.ReaderAt, verbose bool) (*os.File, error) { // but it at least proves the files are still downloading. progress := func(r io.Reader) io.Reader { return &uio.ProgressReadCloser{ - RC: ioutil.NopCloser(r), + RC: io.NopCloser(r), Symbol: ".", Interval: 5 * 1024 * 1024, W: os.Stdout, @@ -115,7 +114,7 @@ func copyToFileIfNotRegular(r io.ReaderAt, verbose bool) (*os.File, error) { rdr = progress(rdr) } - f, err := ioutil.TempFile("", "kexec-image") + f, err := os.CreateTemp("", "kexec-image") if err != nil { return nil, err } diff --git a/pkg/boot/linux_test.go b/pkg/boot/linux_test.go index 1bef4d84ef..cc359f8439 100644 --- a/pkg/boot/linux_test.go +++ b/pkg/boot/linux_test.go @@ -7,7 +7,7 @@ package boot import ( "bytes" "fmt" - "io/ioutil" + "io" "net/url" "os" "path/filepath" @@ -19,11 +19,7 @@ import ( ) func TestLinuxLabel(t *testing.T) { - dir, err := ioutil.TempDir("", "foo") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() osKernel, err := os.Create(filepath.Join(dir, "kernel")) if err != nil { @@ -115,7 +111,7 @@ func TestCopyToFile(t *testing.T) { t.Fatal(err) } defer os.RemoveAll(f.Name()) - got, err := ioutil.ReadAll(f) + got, err := io.ReadAll(f) if err != nil { t.Fatal(err) } diff --git a/pkg/boot/multiboot_test.go b/pkg/boot/multiboot_test.go index aef968517b..a213132359 100644 --- a/pkg/boot/multiboot_test.go +++ b/pkg/boot/multiboot_test.go @@ -6,18 +6,13 @@ package boot import ( "fmt" - "io/ioutil" "os" "path/filepath" "testing" ) func TestLabel(t *testing.T) { - dir, err := ioutil.TempDir("", "foo") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() osKernel, err := os.Create(filepath.Join(dir, "kernel")) if err != nil { diff --git a/pkg/boot/syslinux/config_test.go b/pkg/boot/syslinux/config_test.go index 6befb66fe0..e2e76eb279 100644 --- a/pkg/boot/syslinux/config_test.go +++ b/pkg/boot/syslinux/config_test.go @@ -6,7 +6,7 @@ package syslinux import ( "context" - "io/ioutil" + "os" "path/filepath" "strings" "testing" @@ -46,7 +46,7 @@ func TestConfigs(t *testing.T) { for _, test := range tests { configPath := strings.TrimRight(test, ".json") t.Run(configPath, func(t *testing.T) { - want, err := ioutil.ReadFile(test) + want, err := os.ReadFile(test) if err != nil { t.Errorf("Failed to read test json '%v':%v", test, err) } diff --git a/pkg/boot/uefi/fv_test.go b/pkg/boot/uefi/fv_test.go index 036ac6ce60..b11d57e43f 100644 --- a/pkg/boot/uefi/fv_test.go +++ b/pkg/boot/uefi/fv_test.go @@ -5,12 +5,12 @@ package uefi import ( - "io/ioutil" + "os" "testing" ) func TestFindSecurityCorePEEntry(t *testing.T) { - dat, err := ioutil.ReadFile("testdata/fv_with_sec.fd") + dat, err := os.ReadFile("testdata/fv_with_sec.fd") if err != nil { t.Fatalf("fail to read firmware volume: %v", err) } @@ -25,7 +25,7 @@ func TestFindSecurityCorePEEntry(t *testing.T) { } func TestFindSecurityCorePEEntryNestedSec(t *testing.T) { - dat, err := ioutil.ReadFile("testdata/fv_with_nested_sec.fd") + dat, err := os.ReadFile("testdata/fv_with_nested_sec.fd") if err != nil { t.Fatalf("fail to read firmware volume: %v", err) } @@ -40,7 +40,7 @@ func TestFindSecurityCorePEEntryNestedSec(t *testing.T) { } func TestFindSecurityCorePEEntryNotFound(t *testing.T) { - dat, err := ioutil.ReadFile("testdata/fv_without_sec.fd") + dat, err := os.ReadFile("testdata/fv_without_sec.fd") if err != nil { t.Fatalf("fail to read firmware volume: %v", err) } diff --git a/pkg/boot/uefi/uefi.go b/pkg/boot/uefi/uefi.go index 29d749132d..b4883420ba 100644 --- a/pkg/boot/uefi/uefi.go +++ b/pkg/boot/uefi/uefi.go @@ -10,7 +10,6 @@ import ( "encoding/binary" "fmt" "io" - "io/ioutil" "log" "math" "os" @@ -70,7 +69,7 @@ type FVImage struct { func checkFVAndGetEntryPoint(name string) (uintptr, error) { // Parse entire firmware volume to find SEC's PE32. Since payload will // be only few MBs, it should be fine to load entire image for parsing. - dat, err := ioutil.ReadFile(name) + dat, err := os.ReadFile(name) if err != nil { return 0, err } @@ -108,7 +107,7 @@ const uefiPayloadConfigSize = 0x10000 // Load loads fimware volume payload and boot the the payload func (fv *FVImage) Load(verbose bool) error { // Install payload - dat, err := ioutil.ReadFile(fv.name) + dat, err := os.ReadFile(fv.name) if err != nil { return err } diff --git a/pkg/boot/util/reader.go b/pkg/boot/util/reader.go index 597d5a32c3..28f608c2e8 100644 --- a/pkg/boot/util/reader.go +++ b/pkg/boot/util/reader.go @@ -8,7 +8,6 @@ import ( "bytes" "compress/gzip" "io" - "io/ioutil" "github.com/u-root/u-root/pkg/uio" ) @@ -19,7 +18,7 @@ func readGzip(r io.Reader) ([]byte, error) { return nil, err } defer z.Close() - return ioutil.ReadAll(z) + return io.ReadAll(z) } // TryGzipFilter tries to read from an io.ReaderAt to see if it is a Gzip. If it is not, the diff --git a/pkg/cmdline/cmdline.go b/pkg/cmdline/cmdline.go index 4e553ef8b3..571fbe765a 100644 --- a/pkg/cmdline/cmdline.go +++ b/pkg/cmdline/cmdline.go @@ -13,7 +13,6 @@ package cmdline import ( "fmt" "io" - "io/ioutil" "log" "os" "strings" @@ -64,7 +63,7 @@ func FullCmdLine() string { // parse returns the current command line, trimmed func parse(cmdlineReader io.Reader) CmdLine { - raw, err := ioutil.ReadAll(cmdlineReader) + raw, err := io.ReadAll(cmdlineReader) line := CmdLine{} if err != nil { log.Printf("Can't read command line: %v", err) diff --git a/pkg/complete/complete_test.go b/pkg/complete/complete_test.go index d55005a721..3899b5e833 100644 --- a/pkg/complete/complete_test.go +++ b/pkg/complete/complete_test.go @@ -8,7 +8,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" "path/filepath" "reflect" @@ -52,11 +51,7 @@ func TestSimple(t *testing.T) { // TestFile tests the file completer func TestFile(t *testing.T) { - tempDir, err := ioutil.TempDir("", "TestComplete") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() var ( hinames = []string{"hi", "hil", "hit"} @@ -74,7 +69,7 @@ func TestFile(t *testing.T) { ) for _, n := range allnames { - if err := ioutil.WriteFile(filepath.Join(tempDir, n), []byte{}, 0o600); err != nil { + if err := os.WriteFile(filepath.Join(tempDir, n), []byte{}, 0o600); err != nil { t.Fatal(err) } t.Logf("Wrote %v", filepath.Join(tempDir, n)) @@ -118,11 +113,7 @@ func TestFile(t *testing.T) { // of a simple completer and another multicompleter, which in turn has two // file completers. It also tests the Path completer. func TestMulti(t *testing.T) { - tempDir, err := ioutil.TempDir("", "TestComplete") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() var ( hinames = []string{"hi", "hil", "hit"} @@ -146,10 +137,10 @@ func TestMulti(t *testing.T) { } } for _, n := range allnames { - if err := ioutil.WriteFile(filepath.Join(tempDir, "bin", "a"+n), []byte{}, 0o600); err != nil { + if err := os.WriteFile(filepath.Join(tempDir, "bin", "a"+n), []byte{}, 0o600); err != nil { t.Fatal(err) } - if err := ioutil.WriteFile(filepath.Join(tempDir, "sbin", "b"+n), []byte{}, 0o600); err != nil { + if err := os.WriteFile(filepath.Join(tempDir, "sbin", "b"+n), []byte{}, 0o600); err != nil { t.Fatal(err) } } @@ -268,7 +259,7 @@ func TestLineReader(t *testing.T) { var out []byte go func(o string, r io.Reader) { var err error - out, err = ioutil.ReadAll(r) + out, err = io.ReadAll(r) if err != nil { t.Errorf("reading console io.Pipe: got %v, want nil", err) } diff --git a/pkg/cp/cmp/cmp.go b/pkg/cp/cmp/cmp.go index 096f040363..b1ac457f0d 100644 --- a/pkg/cp/cmp/cmp.go +++ b/pkg/cp/cmp/cmp.go @@ -9,7 +9,6 @@ package cmp import ( "fmt" - "io/ioutil" "os" "path/filepath" "reflect" @@ -38,7 +37,7 @@ func isEqualFile(fpath1, fpath2 string) error { } func readDirNames(path string) ([]string, error) { - entries, err := ioutil.ReadDir(path) + entries, err := os.ReadDir(path) if err != nil { return nil, err } @@ -80,7 +79,7 @@ func IsEqualTree(o cp.Options, src, dst string) error { if err != nil { return err } - // ioutil.ReadDir guarantees these are sorted. + // os.ReadDir guarantees these are sorted. if !reflect.DeepEqual(srcEntries, dstEntries) { return fmt.Errorf("directory contents did not match:\n%q had %v\n%q had %v", src, srcEntries, dst, dstEntries) } diff --git a/pkg/cp/cp_test.go b/pkg/cp/cp_test.go index 544180b1d6..e7fedcbac6 100644 --- a/pkg/cp/cp_test.go +++ b/pkg/cp/cp_test.go @@ -5,7 +5,6 @@ package cp_test import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -24,11 +23,7 @@ func copyAndTest(t *testing.T, o cp.Options, src, dst string) { } func TestSimpleCopy(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "u-root-pkg-cp-") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() // Copy a directory. origd := filepath.Join(tmpDir, "directory") @@ -41,7 +36,7 @@ func TestSimpleCopy(t *testing.T) { // Copy a file. origf := filepath.Join(tmpDir, "normal-file") - if err := ioutil.WriteFile(origf, []byte("F is for fire that burns down the whole town"), 0o766); err != nil { + if err := os.WriteFile(origf, []byte("F is for fire that burns down the whole town"), 0o766); err != nil { t.Fatal(err) } diff --git a/pkg/cpio/fs_unix.go b/pkg/cpio/fs_unix.go index 8494c45031..3a71e76f41 100644 --- a/pkg/cpio/fs_unix.go +++ b/pkg/cpio/fs_unix.go @@ -10,7 +10,6 @@ package cpio import ( "fmt" "io" - "io/ioutil" "os" "path/filepath" "syscall" @@ -126,7 +125,7 @@ func CreateFileInRoot(f Record, rootDir string, forcePriv bool) error { return fmt.Errorf("%q: type %v: cannot create IPC endpoints", f.Name, m) case os.ModeSymlink: - content, err := ioutil.ReadAll(uio.Reader(f)) + content, err := io.ReadAll(uio.Reader(f)) if err != nil { return err } diff --git a/pkg/cpio/newc.go b/pkg/cpio/newc.go index 3d9a28ec78..f74e29fc89 100644 --- a/pkg/cpio/newc.go +++ b/pkg/cpio/newc.go @@ -10,7 +10,6 @@ import ( "encoding/hex" "fmt" "io" - "io/ioutil" "os" "github.com/u-root/u-root/pkg/uio" @@ -199,7 +198,7 @@ func (r *discarder) ReadAt(p []byte, off int64) (int, error) { return 0, fmt.Errorf("negative seek on discarder not allowed") } if off != r.pos { - i, err := io.Copy(ioutil.Discard, io.LimitReader(r.r, off-r.pos)) + i, err := io.Copy(io.Discard, io.LimitReader(r.r, off-r.pos)) if err != nil || i != off-r.pos { return 0, err } diff --git a/pkg/cpio/newc_test.go b/pkg/cpio/newc_test.go index 28874e8f48..37146eb95b 100644 --- a/pkg/cpio/newc_test.go +++ b/pkg/cpio/newc_test.go @@ -7,7 +7,6 @@ package cpio import ( "bytes" "io" - "io/ioutil" "os" "reflect" "syscall" @@ -73,7 +72,7 @@ func TestWriteRead(t *testing.T) { t.Errorf("Records not equal:\n%#v\n%#v", rec.Info, rec2.Info) } - contents2, err := ioutil.ReadAll(uio.Reader(rec2)) + contents2, err := io.ReadAll(uio.Reader(rec2)) if err != nil { t.Errorf("Could not read %q: %v", rec2.Name, err) } @@ -156,7 +155,7 @@ func TestPipeWriteRead(t *testing.T) { } t.Logf("Check Data") - dat, err := ioutil.ReadAll(uio.Reader(rec)) + dat, err := io.ReadAll(uio.Reader(rec)) if err != nil { t.Errorf("Could not read %q: %v", rec.Name, err) } @@ -211,11 +210,11 @@ func TestReadWrite(t *testing.T) { t.Errorf("index %d: testCPIO Info\n%v\ngenerated Info\n%v\n", i, f1.Info, f2.Info) } - contents1, err := ioutil.ReadAll(uio.Reader(f1)) + contents1, err := io.ReadAll(uio.Reader(f1)) if err != nil { t.Errorf("index %d(%q): can't read from the source: %v", i, f1.Name, err) } - contents2, err := ioutil.ReadAll(uio.Reader(f2)) + contents2, err := io.ReadAll(uio.Reader(f2)) if err != nil { t.Errorf("index %d(%q): can't read from the dest: %v", i, f2.Name, err) } diff --git a/pkg/crypto/ed25519.go b/pkg/crypto/ed25519.go index 006aded097..384f55f41f 100644 --- a/pkg/crypto/ed25519.go +++ b/pkg/crypto/ed25519.go @@ -9,7 +9,6 @@ import ( "crypto/x509" "encoding/pem" "errors" - "io/ioutil" "os" "golang.org/x/crypto/ed25519" @@ -30,7 +29,7 @@ var ( // LoadPublicKeyFromFile loads PEM formatted ED25519 public key from file. func LoadPublicKeyFromFile(publicKeyPath string) ([]byte, error) { - x509PEM, err := ioutil.ReadFile(publicKeyPath) + x509PEM, err := os.ReadFile(publicKeyPath) if err != nil { return nil, err } @@ -52,7 +51,7 @@ func LoadPublicKeyFromFile(publicKeyPath string) ([]byte, error) { // LoadPrivateKeyFromFile loads PEM formatted ED25519 private key from file. func LoadPrivateKeyFromFile(privateKeyPath string, password []byte) ([]byte, error) { - x509PEM, err := ioutil.ReadFile(privateKeyPath) + x509PEM, err := os.ReadFile(privateKeyPath) if err != nil { return nil, err } @@ -109,9 +108,9 @@ func GeneratED25519Key(password []byte, privateKeyFilePath string, publicKeyFile privateKey = pem.EncodeToMemory(privBlock) } - if err := ioutil.WriteFile(privateKeyFilePath, privateKey, PrivKeyFilePermissions); err != nil { + if err := os.WriteFile(privateKeyFilePath, privateKey, PrivKeyFilePermissions); err != nil { return err } - return ioutil.WriteFile(publicKeyFilePath, pem.EncodeToMemory(pubBlock), PubKeyFilePermissions) + return os.WriteFile(publicKeyFilePath, pem.EncodeToMemory(pubBlock), PubKeyFilePermissions) } diff --git a/pkg/crypto/ed25519_test.go b/pkg/crypto/ed25519_test.go index 772a2b6d75..fc82b14340 100644 --- a/pkg/crypto/ed25519_test.go +++ b/pkg/crypto/ed25519_test.go @@ -5,7 +5,6 @@ package crypto import ( - "io/ioutil" "os" "path" "testing" @@ -59,7 +58,7 @@ func TestSignVerifyData(t *testing.T) { publicKey, err := LoadPublicKeyFromFile(publicKeyPEMFile) require.NoError(t, err) - testData, err := ioutil.ReadFile(testDataFile) + testData, err := os.ReadFile(testDataFile) require.NoError(t, err) signature := ed25519.Sign(privateKey, testData) @@ -71,10 +70,10 @@ func TestGoodSignature(t *testing.T) { publicKey, err := LoadPublicKeyFromFile(publicKeyPEMFile) require.NoError(t, err) - testData, err := ioutil.ReadFile(testDataFile) + testData, err := os.ReadFile(testDataFile) require.NoError(t, err) - signatureGood, err := ioutil.ReadFile(signatureGoodFile) + signatureGood, err := os.ReadFile(signatureGoodFile) require.NoError(t, err) verified := ed25519.Verify(publicKey, testData, signatureGood) @@ -85,10 +84,10 @@ func TestBadSignature(t *testing.T) { publicKey, err := LoadPublicKeyFromFile(publicKeyPEMFile) require.NoError(t, err) - testData, err := ioutil.ReadFile(testDataFile) + testData, err := os.ReadFile(testDataFile) require.NoError(t, err) - signatureBad, err := ioutil.ReadFile(signatureBadFile) + signatureBad, err := os.ReadFile(signatureBadFile) require.NoError(t, err) verified := ed25519.Verify(publicKey, testData, signatureBad) @@ -96,21 +95,15 @@ func TestBadSignature(t *testing.T) { } func TestGenerateKeys(t *testing.T) { - // FIXME: move this to testing.TempDir once we require >= Go 1.15 - tmpdir, err := ioutil.TempDir("", "generate-keys") - require.NoError(t, err) - defer os.RemoveAll(tmpdir) + tmpdir := t.TempDir() - err = GeneratED25519Key(password, path.Join(tmpdir, "private_key.pem"), path.Join(tmpdir, "public_key.pem")) + err := GeneratED25519Key(password, path.Join(tmpdir, "private_key.pem"), path.Join(tmpdir, "public_key.pem")) require.NoError(t, err) } func TestGenerateUnprotectedKeys(t *testing.T) { - // FIXME: move this to testing.TempDir once we require >= Go 1.15 - tmpdir, err := ioutil.TempDir("", "generate-keys") - require.NoError(t, err) - defer os.RemoveAll(tmpdir) + tmpdir := t.TempDir() - err = GeneratED25519Key(nil, path.Join(tmpdir, "private_key.pem"), path.Join(tmpdir, "public_key.pem")) + err := GeneratED25519Key(nil, path.Join(tmpdir, "private_key.pem"), path.Join(tmpdir, "public_key.pem")) require.NoError(t, err) } diff --git a/pkg/crypto/measure.go b/pkg/crypto/measure.go index 98a1cdaefc..df5fea6ea5 100644 --- a/pkg/crypto/measure.go +++ b/pkg/crypto/measure.go @@ -5,8 +5,8 @@ package crypto import ( - "io/ioutil" "log" + "os" tss "github.com/u-root/u-root/pkg/tss" ) @@ -45,7 +45,7 @@ func TryMeasureFiles(files ...string) error { } for _, file := range files { log.Printf("Measuring file: %v", file) - data, err := ioutil.ReadFile(file) + data, err := os.ReadFile(file) if err != nil { continue } diff --git a/pkg/curl/schemes_test.go b/pkg/curl/schemes_test.go index 0cc2343c41..1e9c3e653e 100644 --- a/pkg/curl/schemes_test.go +++ b/pkg/curl/schemes_test.go @@ -9,7 +9,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "net/http/httptest" "net/url" @@ -203,7 +202,7 @@ func TestFetchWithoutCache(t *testing.T) { } // Read the entire file. - content, err := ioutil.ReadAll(r) + content, err := io.ReadAll(r) if err != nil { t.Errorf("bytes.Buffer read returned an error? %v", err) } @@ -249,7 +248,7 @@ func TestFetch(t *testing.T) { } // Read the entire file. - content, err := ioutil.ReadAll(uio.Reader(r)) + content, err := io.ReadAll(uio.Reader(r)) if err != nil { t.Errorf("bytes.Buffer read returned an error? %v", err) } @@ -297,7 +296,7 @@ func TestLazyFetch(t *testing.T) { } // Read the entire file. - content, err := ioutil.ReadAll(uio.Reader(r)) + content, err := io.ReadAll(uio.Reader(r)) if uErr, ok := err.(*URLError); ok && uErr.Err != tt.err { t.Errorf("ReadAll() = %v, want %v", uErr.Err, tt.err) } else if !ok && err != tt.err { @@ -334,9 +333,9 @@ func TestHttpFetches(t *testing.T) { if err != nil { t.Errorf("Fetch(context.Background(), %s) = %v, want no error", fURL, err) } - got, err := ioutil.ReadAll(io.NewSectionReader(fetchFile, 0, int64(len(c)))) + got, err := io.ReadAll(io.NewSectionReader(fetchFile, 0, int64(len(c)))) if err != nil { - t.Errorf("ioutil.ReadAll(%v) = %v, want no error", fetchFile, err) + t.Errorf("io.ReadAll(%v) = %v, want no error", fetchFile, err) } if string(got) != c { t.Errorf("got %s, want %s", got, c) @@ -347,9 +346,9 @@ func TestHttpFetches(t *testing.T) { if err != nil { t.Errorf("FetchWithoutCache(context.Background(), %s) = %v, want no error", fURL, err) } - got, err = ioutil.ReadAll(fetchFileNoCache) + got, err = io.ReadAll(fetchFileNoCache) if err != nil { - t.Errorf("ioutil.ReadAll(%s) = %v, want no error", fetchFileNoCache, err) + t.Errorf("io.ReadAll(%s) = %v, want no error", fetchFileNoCache, err) } if string(got) != c { t.Errorf("got %s, want %s", got, c) diff --git a/pkg/dhclient/dhclient.go b/pkg/dhclient/dhclient.go index 4fb8f146dc..6ad79d918c 100644 --- a/pkg/dhclient/dhclient.go +++ b/pkg/dhclient/dhclient.go @@ -11,10 +11,10 @@ import ( "context" "errors" "fmt" - "io/ioutil" "log" "net" "net/url" + "os" "strings" "sync" "time" @@ -87,7 +87,7 @@ func WriteDNSSettings(ns []net.IP, sl []string, domain string) error { rc.WriteString(strings.Join(sl, " ")) rc.WriteString("\n") } - return ioutil.WriteFile("/etc/resolv.conf", rc.Bytes(), 0o644) + return os.WriteFile("/etc/resolv.conf", rc.Bytes(), 0o644) } // Lease is a network configuration obtained by DHCP. diff --git a/pkg/dt/fdt_test.go b/pkg/dt/fdt_test.go index 8410ec774f..6afe9f7e26 100644 --- a/pkg/dt/fdt_test.go +++ b/pkg/dt/fdt_test.go @@ -7,7 +7,6 @@ package dt import ( "bytes" "encoding/json" - "io/ioutil" "os" "os/exec" "reflect" @@ -22,7 +21,7 @@ func TestRead(t *testing.T) { } defer f.Close() - jsonData, err := ioutil.ReadFile("testdata/fdt.json") + jsonData, err := os.ReadFile("testdata/fdt.json") if err != nil { t.Fatal(err) } diff --git a/pkg/fb/fb.go b/pkg/fb/fb.go index 17067c3ded..fcde8c7f87 100644 --- a/pkg/fb/fb.go +++ b/pkg/fb/fb.go @@ -7,7 +7,6 @@ package fb import ( "fmt" "image" - "io/ioutil" "os" "github.com/orangecms/go-framebuffer/framebuffer" @@ -61,7 +60,7 @@ func DrawImageAt(img image.Image, posx int, posy int) error { } buf := make([]byte, width*height*bpp) DrawOnBufAt(buf, img, posx, posy, stride, bpp) - err = ioutil.WriteFile(fbdev, buf, 0o600) + err = os.WriteFile(fbdev, buf, 0o600) if err != nil { return fmt.Errorf("Error writing to framebuffer: %v", err) } @@ -102,7 +101,7 @@ func DrawScaledImageAt(img image.Image, posx int, posy int, factor int) error { } buf := make([]byte, width*height*bpp) DrawScaledOnBufAt(buf, img, posx, posy, factor, stride, bpp) - err = ioutil.WriteFile(fbdev, buf, 0o600) + err = os.WriteFile(fbdev, buf, 0o600) if err != nil { return fmt.Errorf("Error writing to framebuffer: %v", err) } diff --git a/pkg/find/find_test.go b/pkg/find/find_test.go index 3086d2b541..bd79f0af02 100644 --- a/pkg/find/find_test.go +++ b/pkg/find/find_test.go @@ -6,7 +6,6 @@ package find import ( "context" - "io/ioutil" "os" "path/filepath" "reflect" @@ -62,21 +61,17 @@ func TestSimple(t *testing.T) { names: []string{"/root/xyz/file"}, }, } - d, err := ioutil.TempDir(os.TempDir(), "u-root.cmds.find") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(d) + d := t.TempDir() // Make sure files are actually created with the permissions we ask for. syscall.Umask(0) if err := os.MkdirAll(filepath.Join(d, "root/xyz"), 0o775); err != nil { t.Fatal(err) } - if err := ioutil.WriteFile(filepath.Join(d, "root/xyz/file"), nil, 0o664); err != nil { + if err := os.WriteFile(filepath.Join(d, "root/xyz/file"), nil, 0o664); err != nil { t.Fatal(err) } - if err := ioutil.WriteFile(filepath.Join(d, "root/xyz/0777"), nil, 0o444); err != nil { + if err := os.WriteFile(filepath.Join(d, "root/xyz/0777"), nil, 0o444); err != nil { t.Fatal(err) } diff --git a/pkg/gpio/gpio_linux.go b/pkg/gpio/gpio_linux.go index 595520d295..309da956a0 100644 --- a/pkg/gpio/gpio_linux.go +++ b/pkg/gpio/gpio_linux.go @@ -8,7 +8,6 @@ package gpio import ( "fmt" - "io/ioutil" "os" "path/filepath" "strconv" @@ -43,7 +42,7 @@ func (v Value) String() string { func readInt(filename string) (int, error) { // Get base offset (the first GPIO managed by this chip) - buf, err := ioutil.ReadFile(filename) + buf, err := os.ReadFile(filename) if err != nil { return 0, fmt.Errorf("failed to read integer out of %s: %v", filename, err) } @@ -67,7 +66,7 @@ func GetPinID(controller string, pin uint) (int, error) { for _, c := range controllers { // Get label (name of the controller) - buf, err := ioutil.ReadFile(filepath.Join(c, "label")) + buf, err := os.ReadFile(filepath.Join(c, "label")) if err != nil { return 0, fmt.Errorf("failed to read label of %s: %v", c, err) } @@ -118,7 +117,7 @@ func SetOutputValue(pin int, val Value) error { // unsuccessful, it returns a value of Low and the associated error. func ReadValue(pin int) (Value, error) { path := filepath.Join(gpioPath, fmt.Sprintf("gpio%d", pin), "value") - buf, err := ioutil.ReadFile(path) + buf, err := os.ReadFile(path) if err != nil { return Low, fmt.Errorf("failed to read value of gpio %d: %v", pin, err) } diff --git a/pkg/gzip/file_test.go b/pkg/gzip/file_test.go index c0b3b4fc25..2f5b1d65ad 100644 --- a/pkg/gzip/file_test.go +++ b/pkg/gzip/file_test.go @@ -5,7 +5,6 @@ package gzip import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -89,8 +88,7 @@ func TestFile_CheckPath(t *testing.T) { }, } - tempDir := os.TempDir() - defer os.Remove(tempDir) + tempDir := t.TempDir() for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -250,12 +248,11 @@ func TestFile_Cleanup(t *testing.T) { }, } - tempDir := os.TempDir() - defer os.Remove(tempDir) + tempDir := t.TempDir() for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - path, err := ioutil.TempFile(tempDir, "cleanup-test") + path, err := os.CreateTemp(tempDir, "cleanup-test") if err != nil { t.Errorf("File.Cleanup() error can't create temp file: %v", err) } @@ -279,9 +276,8 @@ func TestFile_Cleanup(t *testing.T) { } func TestFile_Process(t *testing.T) { - tempDir := os.TempDir() - defer os.Remove(tempDir) - path, err := ioutil.TempFile(tempDir, "process-test") + tempDir := t.TempDir() + path, err := os.CreateTemp(tempDir, "process-test") if err != nil { t.Fatalf("File.Process() error can't create temp file: %v", err) } diff --git a/pkg/kmodule/kmodule_linux.go b/pkg/kmodule/kmodule_linux.go index 083a6b1335..ddb8d30eb1 100644 --- a/pkg/kmodule/kmodule_linux.go +++ b/pkg/kmodule/kmodule_linux.go @@ -13,7 +13,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" "path" "path/filepath" @@ -71,7 +70,7 @@ func FileInit(f *os.File, opts string, flags uintptr) error { } } - img, err := ioutil.ReadAll(r) + img, err := io.ReadAll(r) if err != nil { return err } diff --git a/pkg/lockfile/lockfile.go b/pkg/lockfile/lockfile.go index 55651156b1..2616b09813 100644 --- a/pkg/lockfile/lockfile.go +++ b/pkg/lockfile/lockfile.go @@ -34,7 +34,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "log" "os" "path/filepath" @@ -80,7 +79,7 @@ func New(path string) *Lockfile { func (l *Lockfile) pidfile() (string, error) { dir, base := filepath.Split(l.path) - pidfile, err := ioutil.TempFile(dir, fmt.Sprintf("%s-", base)) + pidfile, err := os.CreateTemp(dir, fmt.Sprintf("%s-", base)) if err != nil { return "", err } @@ -140,7 +139,7 @@ func (l *Lockfile) Lock() error { } func (l *Lockfile) checkLockfile() error { - owningPid, err := ioutil.ReadFile(l.path) + owningPid, err := os.ReadFile(l.path) if os.IsNotExist(err) { return errUnlocked } else if err != nil { diff --git a/pkg/lockfile/lockfile_test.go b/pkg/lockfile/lockfile_test.go index b2ec3eecf6..4fbdf585e5 100644 --- a/pkg/lockfile/lockfile_test.go +++ b/pkg/lockfile/lockfile_test.go @@ -5,7 +5,6 @@ package lockfile import ( - "io/ioutil" "os" "os/exec" "path/filepath" @@ -28,11 +27,7 @@ func TestTryLock(t *testing.T) { p2 := testProcess(t) defer p2.Kill() - dir, err := ioutil.TempDir("", "lockfile-") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() l := &Lockfile{ path: filepath.Join(dir, "test1"), @@ -67,11 +62,7 @@ func TestLockFileRemoval(t *testing.T) { p := testProcess(t) defer p.Kill() - dir, err := ioutil.TempDir("", "lockfile-") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() l := &Lockfile{ path: filepath.Join(dir, "test2"), @@ -102,11 +93,7 @@ func testDeadProcess(t *testing.T) { p2 := testProcess(t) defer p2.Kill() - dir, err := ioutil.TempDir("", "lockfile-") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() l := &Lockfile{ path: filepath.Join(dir, "test3"), diff --git a/pkg/memio/io_test.go b/pkg/memio/io_test.go index c4747db90b..e8bdeefe7f 100644 --- a/pkg/memio/io_test.go +++ b/pkg/memio/io_test.go @@ -6,7 +6,6 @@ package memio import ( "fmt" - "io/ioutil" "log" "os" "reflect" @@ -54,7 +53,7 @@ var tests = []struct { func TestIO(t *testing.T) { for _, tt := range tests { t.Run(fmt.Sprintf(tt.name), func(t *testing.T) { - tmpFile, err := ioutil.TempFile("", "io_test") + tmpFile, err := os.CreateTemp("", "io_test") if err != nil { t.Fatal(err) } diff --git a/pkg/mount/fs_linux.go b/pkg/mount/fs_linux.go index f8d0eb7422..d581c82dcc 100644 --- a/pkg/mount/fs_linux.go +++ b/pkg/mount/fs_linux.go @@ -6,7 +6,7 @@ package mount import ( "fmt" - "io/ioutil" + "os" "strings" ) @@ -15,7 +15,7 @@ import ( // It rereads /proc/filesystems each time as the supported file systems can change // as modules are added and removed. func FindFileSystem(fstype string) error { - b, err := ioutil.ReadFile("/proc/filesystems") + b, err := os.ReadFile("/proc/filesystems") if err != nil { return err } @@ -39,7 +39,7 @@ func GetBlockFilesystems() (fstypes []string, err error) { func internalGetFilesystems(file string) (fstypes []string, err error) { var bytes []byte - if bytes, err = ioutil.ReadFile(file); err != nil { + if bytes, err = os.ReadFile(file); err != nil { return nil, fmt.Errorf("failed to read supported file systems: %v", err) } for _, line := range strings.Split(string(bytes), "\n") { diff --git a/pkg/mount/loop/losetup_linux_test.go b/pkg/mount/loop/losetup_linux_test.go index 2702981e07..369e64c74d 100644 --- a/pkg/mount/loop/losetup_linux_test.go +++ b/pkg/mount/loop/losetup_linux_test.go @@ -5,7 +5,6 @@ package loop import ( - "io/ioutil" "os" "path/filepath" "syscall" @@ -47,10 +46,7 @@ func TestFindDevice(t *testing.T) { func TestSetFile(t *testing.T) { skipIfNotRoot(t) - tmpDir, err := ioutil.TempDir("", "u-root-losetup-") - if err != nil { - t.Fatal(err) - } + tmpDir := t.TempDir() testdisk := filepath.Join(tmpDir, "testdisk") if err := cp.Copy("./testdata/pristine-vfat-disk", testdisk); err != nil { t.Fatal(err) @@ -71,7 +67,7 @@ func TestSetFile(t *testing.T) { } defer mp.Unmount(0) //nolint:errcheck - if err := ioutil.WriteFile("/tmp/disk/foobar", []byte("Are you feeling it now Mr Krabs"), 0o755); err != nil { + if err := os.WriteFile("/tmp/disk/foobar", []byte("Are you feeling it now Mr Krabs"), 0o755); err != nil { t.Fatal(err) } } diff --git a/pkg/mount/mount_linux.go b/pkg/mount/mount_linux.go index 901a50606f..f3382f0714 100644 --- a/pkg/mount/mount_linux.go +++ b/pkg/mount/mount_linux.go @@ -8,7 +8,6 @@ package mount import ( "errors" "fmt" - "io/ioutil" "os" "path/filepath" @@ -159,7 +158,7 @@ func (p *Pool) Mount(mounter Mounter, flags uintptr) (*MountPoint, error) { // Create temporary directory if one does not already exist. if p.tmpDir == "" { - tmpDir, err := ioutil.TempDir("", "u-root-mounts") + tmpDir, err := os.MkdirTemp("", "u-root-mounts") if err != nil { return nil, fmt.Errorf("cannot create tmpdir: %v", err) } diff --git a/pkg/mount/mount_linux_test.go b/pkg/mount/mount_linux_test.go index 2af3233a00..c28dc0586f 100644 --- a/pkg/mount/mount_linux_test.go +++ b/pkg/mount/mount_linux_test.go @@ -6,7 +6,6 @@ package mount_test import ( "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -394,11 +393,7 @@ func TestTryMount(t *testing.T) { prefix := getDevicePrefix() - d, err := ioutil.TempDir("", "test-") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(d) + d := t.TempDir() sda1 := filepath.Join(d, prefix+"a1") deva1 := fmt.Sprintf("/dev/%sa1", prefix) @@ -507,10 +502,7 @@ func TestMountPool(t *testing.T) { func TestIsTmpRamfs(t *testing.T) { testutil.SkipIfNotRoot(t) - testRoot, err := ioutil.TempDir("", "testtmpfs") - if err != nil { - t.Fatalf("Failed to create tmp dir: %v", err) - } + testRoot := t.TempDir() // Is a tmpfs. tmpfsMount := filepath.Join(testRoot, "tmpfs") diff --git a/pkg/msr/msr_linux.go b/pkg/msr/msr_linux.go index ab030d6e1e..a8cde67458 100644 --- a/pkg/msr/msr_linux.go +++ b/pkg/msr/msr_linux.go @@ -8,7 +8,6 @@ package msr import ( "encoding/binary" "fmt" - "io/ioutil" "os" "path/filepath" "sort" @@ -76,7 +75,7 @@ func parseCPUs(s string) (CPUs, error) { // AllCPUs searches for actual present CPUs instead of relying on the glob. // This is more accurate than what's presented in /dev/cpu/*/msr func AllCPUs() (CPUs, error) { - v, err := ioutil.ReadFile("/sys/devices/system/cpu/present") + v, err := os.ReadFile("/sys/devices/system/cpu/present") if err != nil { return nil, err } diff --git a/pkg/namespace/builder_test.go b/pkg/namespace/builder_test.go index 879eca2784..062b84e558 100644 --- a/pkg/namespace/builder_test.go +++ b/pkg/namespace/builder_test.go @@ -7,7 +7,6 @@ package namespace import ( "bytes" "io" - "io/ioutil" "log" "os" "path" @@ -58,7 +57,7 @@ func TestBuilder_buildNS(t *testing.T) { wantErr bool inspectErr func(err error, t *testing.T) // use for more precise error evaluation }{} - files, err := ioutil.ReadDir("testdata") + files, err := os.ReadDir("testdata") if err != nil { log.Fatal(err) } diff --git a/pkg/pci/pci.go b/pkg/pci/pci.go index b890ea0f0f..c86a39b0a2 100644 --- a/pkg/pci/pci.go +++ b/pkg/pci/pci.go @@ -7,7 +7,6 @@ package pci import ( "encoding/binary" "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -65,7 +64,7 @@ func (p *PCI) SetVendorDeviceName() { // ReadConfig reads the config space. func (p *PCI) ReadConfig() error { dev := filepath.Join(p.FullPath, "config") - c, err := ioutil.ReadFile(dev) + c, err := os.ReadFile(dev) if err != nil { return err } diff --git a/pkg/pci/pci_linux.go b/pkg/pci/pci_linux.go index 9b84468c3f..8d9b76b651 100644 --- a/pkg/pci/pci_linux.go +++ b/pkg/pci/pci_linux.go @@ -8,7 +8,7 @@ package pci import ( "fmt" - "io/ioutil" + "os" "path/filepath" "sort" "strconv" @@ -24,7 +24,7 @@ type bus struct { } func readString(dir, file string) (string, error) { - s, err := ioutil.ReadFile(filepath.Join(dir, file)) + s, err := os.ReadFile(filepath.Join(dir, file)) if err != nil { return "", err } diff --git a/pkg/pogosh/shell.go b/pkg/pogosh/shell.go index cef4e201cf..cc9cb12d20 100644 --- a/pkg/pogosh/shell.go +++ b/pkg/pogosh/shell.go @@ -7,7 +7,7 @@ package pogosh import ( "fmt" - "io/ioutil" + "os" ) // Run executes the given fragment of shell. @@ -64,7 +64,7 @@ func (s *State) Run(script string) (exitCode int, err error) { // RunFile is a convenient wrapper around Run. func (s *State) RunFile(filename string) (int, error) { - script, err := ioutil.ReadFile(filename) + script, err := os.ReadFile(filename) if err != nil { return 0, err } diff --git a/pkg/rng/entropy.go b/pkg/rng/entropy.go index b3db24016d..dbd86091e8 100644 --- a/pkg/rng/entropy.go +++ b/pkg/rng/entropy.go @@ -6,7 +6,6 @@ package rng import ( "errors" - "io/ioutil" "os" "strconv" "strings" @@ -70,7 +69,7 @@ func setAvailableTRNG() error { selectedRNG string ) - availableFileData, err := ioutil.ReadFile(HwRandomAvailableFile) + availableFileData, err := os.ReadFile(HwRandomAvailableFile) if err != nil { return err } @@ -89,12 +88,12 @@ func setAvailableTRNG() error { return errors.New("no TRNG found on platform") } - if err = ioutil.WriteFile(HwRandomCurrentFile, []byte(selectedRNG), 0o644); err != nil { + if err = os.WriteFile(HwRandomCurrentFile, []byte(selectedRNG), 0o644); err != nil { return err } // Check if the correct TRNG was successful written - currentFileData, err := ioutil.ReadFile(HwRandomCurrentFile) + currentFileData, err := os.ReadFile(HwRandomCurrentFile) if err != nil { return err } @@ -132,7 +131,7 @@ func UpdateLinuxRandomness(recoverer recovery.Recoverer) error { for { time.Sleep(EntropyFeedTime) - randomEntropyAvailableData, err := ioutil.ReadFile(RandomEntropyAvailableFile) + randomEntropyAvailableData, err := os.ReadFile(RandomEntropyAvailableFile) if err != nil { recoverer.Recover("Can't read entropy pool size") } diff --git a/pkg/securelaunch/helpers.go b/pkg/securelaunch/helpers.go index c6442fd485..345531690a 100644 --- a/pkg/securelaunch/helpers.go +++ b/pkg/securelaunch/helpers.go @@ -7,7 +7,6 @@ package securelaunch import ( "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -67,7 +66,7 @@ func WriteToFile(data []byte, dst, defFileName string) (string, error) { } Debug("WriteToFile: target=%s", target) - err = ioutil.WriteFile(target, data, 0o644) + err = os.WriteFile(target, data, 0o644) if err != nil { return "", fmt.Errorf("failed to write date to file =%s, err=%v", target, err) } @@ -209,7 +208,7 @@ func MountDevice(device *block.BlockDev, flags uintptr) (string, error) { Debug("MountDevice: cache lookup failed for %s", devName) Debug("MountDevice: Attempting to mount %s with flags %d", devName, flags) - mountPath, err := ioutil.TempDir("/tmp", "slaunch-") + mountPath, err := os.MkdirTemp("/tmp", "slaunch-") if err != nil { return "", fmt.Errorf("failed to create tmp mount directory: %v", err) } diff --git a/pkg/securelaunch/measurement/files.go b/pkg/securelaunch/measurement/files.go index 41df8da055..22d0d96f66 100644 --- a/pkg/securelaunch/measurement/files.go +++ b/pkg/securelaunch/measurement/files.go @@ -8,8 +8,8 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" "log" + "os" "github.com/u-root/u-root/pkg/mount" slaunch "github.com/u-root/u-root/pkg/securelaunch" @@ -64,7 +64,7 @@ func HashFile(inputVal string) error { slaunch.Debug("File Collector: Reading file=%s", mntFilePath) slaunch.Debug("File Collector: fileP=%s\n", mntFilePath) - d, err := ioutil.ReadFile(mntFilePath) + d, err := os.ReadFile(mntFilePath) if err != nil { return fmt.Errorf("failed to read target file: filePath=%s, inputVal=%s, err=%v", mntFilePath, inputVal, err) diff --git a/pkg/securelaunch/policy/policy.go b/pkg/securelaunch/policy/policy.go index 2f7d0ca9f3..b88245128a 100644 --- a/pkg/securelaunch/policy/policy.go +++ b/pkg/securelaunch/policy/policy.go @@ -9,7 +9,6 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -61,7 +60,7 @@ func scanKernelCmdLine() []byte { } slaunch.Debug("scanKernelCmdLine: Reading file=%s", mntFilePath) - d, err := ioutil.ReadFile(mntFilePath) + d, err := os.ReadFile(mntFilePath) if err != nil { log.Printf("Error reading policy file:mountPath=%s, passed=%s", mntFilePath, val) return nil @@ -90,7 +89,7 @@ func scanBlockDevice(mountPath string) []byte { continue } - d, err := ioutil.ReadFile(searchPath) + d, err := os.ReadFile(searchPath) if err != nil { // Policy File not found. Moving on to next search root... log.Printf("Error reading policy file %s, continuing", searchPath) diff --git a/pkg/smbios/info_linux.go b/pkg/smbios/info_linux.go index 8ca9e898eb..7483251904 100644 --- a/pkg/smbios/info_linux.go +++ b/pkg/smbios/info_linux.go @@ -6,7 +6,7 @@ package smbios import ( "fmt" - "io/ioutil" + "os" "path/filepath" ) @@ -16,11 +16,11 @@ func FromSysfs() (*Info, error) { } func fromSysfs(sysfsPath string) (*Info, error) { - entry, err := ioutil.ReadFile(filepath.Join(sysfsPath, "smbios_entry_point")) + entry, err := os.ReadFile(filepath.Join(sysfsPath, "smbios_entry_point")) if err != nil { return nil, fmt.Errorf("error reading SMBIOS entry data: %v", err) } - data, err := ioutil.ReadFile(filepath.Join(sysfsPath, "DMI")) + data, err := os.ReadFile(filepath.Join(sysfsPath, "DMI")) if err != nil { return nil, fmt.Errorf("error reading DMI data: %v", err) } diff --git a/pkg/spidev/spidev_linux_test.go b/pkg/spidev/spidev_linux_test.go index 6878d8205b..71cf68e49b 100644 --- a/pkg/spidev/spidev_linux_test.go +++ b/pkg/spidev/spidev_linux_test.go @@ -7,7 +7,6 @@ package spidev import ( "encoding/binary" "errors" - "io/ioutil" "os" "reflect" "runtime" @@ -103,7 +102,7 @@ func TestOpenError(t *testing.T) { // TestGetters tests the functions which return values like Mode, SpeedHz, ... func TestGetters(t *testing.T) { - tmpFile, err := ioutil.TempFile("", "") + tmpFile, err := os.CreateTemp("", "") if err != nil { t.Fatalf("Could not create temporary file: %v", err) } @@ -181,7 +180,7 @@ func TestGetters(t *testing.T) { // TestSetters tests the functions which set values like SetMode, SetSpeedHz, ... func TestSetters(t *testing.T) { - tmpFile, err := ioutil.TempFile("", "") + tmpFile, err := os.CreateTemp("", "") if err != nil { t.Fatalf("Could not create temporary file: %v", err) } @@ -393,7 +392,7 @@ func TestTransfer(t *testing.T) { }, } { t.Run(tt.name, func(t *testing.T) { - tmpFile, err := ioutil.TempFile("", "") + tmpFile, err := os.CreateTemp("", "") if err != nil { t.Fatalf("Could not create temporary file: %v", err) } diff --git a/pkg/tarutil/tar_test.go b/pkg/tarutil/tar_test.go index 4da67a6cdf..f5f96e6754 100644 --- a/pkg/tarutil/tar_test.go +++ b/pkg/tarutil/tar_test.go @@ -5,7 +5,6 @@ package tarutil import ( - "io/ioutil" "os" "os/exec" "path/filepath" @@ -14,11 +13,7 @@ import ( ) func extractAndCompare(t *testing.T, tarFile string, files []struct{ name, body string }) { - tmpDir, err := ioutil.TempDir("", "tartest") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() // Extract tar to tmpDir. f, err := os.Open(tarFile) @@ -31,7 +26,7 @@ func extractAndCompare(t *testing.T, tarFile string, files []struct{ name, body } for _, f := range files { - body, err := ioutil.ReadFile(filepath.Join(tmpDir, f.name)) + body, err := os.ReadFile(filepath.Join(tmpDir, f.name)) if err != nil { t.Errorf("could not read %s: %v", f.name, err) continue @@ -54,11 +49,7 @@ func TestExtractDir(t *testing.T) { } func TestCreateTarSingleFile(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "tartest") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() // Create the tar file. filename := filepath.Join(tmpDir, "test.tar") @@ -89,11 +80,7 @@ test0/dir/b.txt } func TestCreateTarMultFiles(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "tartest") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() // Create the tar file. filename := filepath.Join(tmpDir, "test.tar") @@ -135,16 +122,12 @@ func TestCreateTarProcfsFile(t *testing.T) { t.Skipf("/proc/version is only on linux, but GOOS=%s", runtime.GOOS) } - tmpDir, err := ioutil.TempDir("", "tartest") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() // /proc/version won't change during the test. The size according to // stat should also be 0. procfsFile := "/proc/version" - contents, err := ioutil.ReadFile(procfsFile) + contents, err := os.ReadFile(procfsFile) if err != nil { t.Fatal(err) } diff --git a/pkg/testutil/testutil.go b/pkg/testutil/testutil.go index 7cc342782e..ec905dd45e 100644 --- a/pkg/testutil/testutil.go +++ b/pkg/testutil/testutil.go @@ -6,7 +6,6 @@ package testutil import ( "fmt" - "io/ioutil" "log" "os" "os/exec" @@ -124,7 +123,7 @@ func run(m *testing.M, mainFn func()) int { // build the command. // // This is NOT build-system-independent, and hence the fallback. - tmpDir, err := ioutil.TempDir("", "uroot-build") + tmpDir, err := os.MkdirTemp("", "uroot-build") if err != nil { log.Fatal(err) } diff --git a/pkg/ts/ts_test.go b/pkg/ts/ts_test.go index 3aa2f356da..dfd73029f8 100644 --- a/pkg/ts/ts_test.go +++ b/pkg/ts/ts_test.go @@ -8,7 +8,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "strings" "testing" "time" @@ -147,7 +146,7 @@ func BenchmarkPrependTime(b *testing.B) { data := strings.Repeat(line, (b.N+len(line))/len(line))[:b.N] pt := New(bytes.NewBufferString(data)) b.ResetTimer() - if _, err := io.Copy(ioutil.Discard, pt); err != nil { + if _, err := io.Copy(io.Discard, pt); err != nil { b.Fatal(err) } } diff --git a/pkg/tss/tpm_linux.go b/pkg/tss/tpm_linux.go index 0cb1fa2a8b..e4e21f4d88 100644 --- a/pkg/tss/tpm_linux.go +++ b/pkg/tss/tpm_linux.go @@ -6,7 +6,6 @@ package tss import ( "io" - "io/ioutil" "os" "path/filepath" "strings" @@ -22,7 +21,7 @@ const ( func probeSystemTPMs() ([]probedTPM, error) { var tpms []probedTPM - tpmDevs, err := ioutil.ReadDir(tpmRoot) + tpmDevs, err := os.ReadDir(tpmRoot) if os.IsNotExist(err) { return nil, nil } else if err != nil { @@ -71,7 +70,7 @@ func newTPM(pTPM probedTPM) (*TPM, error) { // If the TPM has a kernel-provided resource manager, we should // use that instead of communicating directly. devPath := filepath.Join("/dev", filepath.Base(pTPM.Path)) - f, err := ioutil.ReadDir(filepath.Join(pTPM.Path, "device", "tpmrm")) + f, err := os.ReadDir(filepath.Join(pTPM.Path, "device", "tpmrm")) if err != nil { if !os.IsNotExist(err) { return nil, err @@ -98,5 +97,5 @@ func newTPM(pTPM probedTPM) (*TPM, error) { // MeasurementLog reads the TCPA eventlog in binary format // from the Linux kernel func (t *TPM) MeasurementLog() ([]byte, error) { - return ioutil.ReadFile("/sys/kernel/security/tpm0/binary_bios_measurements") + return os.ReadFile("/sys/kernel/security/tpm0/binary_bios_measurements") } diff --git a/pkg/uefivars/boot/efiDppResolver.go b/pkg/uefivars/boot/efiDppResolver.go index b7874ea0ff..1fb7f7e314 100644 --- a/pkg/uefivars/boot/efiDppResolver.go +++ b/pkg/uefivars/boot/efiDppResolver.go @@ -8,7 +8,6 @@ package boot import ( - "io/ioutil" "log" "os" fp "path/filepath" @@ -51,7 +50,7 @@ func (r *HddResolver) Resolve(basePath string) (string, error) { } var err error if len(basePath) == 0 { - basePath, err = ioutil.TempDir("", "uefiPath") + basePath, err = os.MkdirTemp("", "uefiPath") if err != nil { return "", err } diff --git a/pkg/uefivars/boot/fuzz_test.go b/pkg/uefivars/boot/fuzz_test.go index 9b1e1d08b6..f9147f251c 100644 --- a/pkg/uefivars/boot/fuzz_test.go +++ b/pkg/uefivars/boot/fuzz_test.go @@ -10,7 +10,7 @@ package boot import ( "archive/zip" "fmt" - "io/ioutil" + "io" "log" "os" "strings" @@ -56,7 +56,7 @@ func TestFuzzInputs(t *testing.T) { if err != nil { t.Error(err) } - data, err := ioutil.ReadAll(f) + data, err := io.ReadAll(f) if err != nil { t.Error(err) } diff --git a/pkg/uefivars/vars.go b/pkg/uefivars/vars.go index ff941f0cd3..cc821977ea 100644 --- a/pkg/uefivars/vars.go +++ b/pkg/uefivars/vars.go @@ -10,7 +10,6 @@ package uefivars import ( "bytes" "fmt" - "io/ioutil" "log" "os" fp "path/filepath" @@ -35,7 +34,7 @@ func ReadVar(uuid, name string) (e EfiVar, err error) { path := fp.Join(EfiVarDir, name+"-"+uuid, "data") e.UUID = uuid e.Name = name - e.Data, err = ioutil.ReadFile(path) + e.Data, err = os.ReadFile(path) return } diff --git a/pkg/uefivars/vartest/test.go b/pkg/uefivars/vartest/test.go index 5252f9d12d..8e9de7960e 100644 --- a/pkg/uefivars/vartest/test.go +++ b/pkg/uefivars/vartest/test.go @@ -12,14 +12,13 @@ package vartest import ( "archive/zip" "io" - "io/ioutil" "os" fp "path/filepath" ) // Extracts testdata zip for use as efivars in tests. Used in uefivars and subpackages. func SetupVarZip(path string) (efiVarDir string, cleanup func(), err error) { - efiVarDir, err = ioutil.TempDir("", "gotest-uefivars") + efiVarDir, err = os.MkdirTemp("", "gotest-uefivars") if err != nil { return } diff --git a/pkg/uio/archivereader_test.go b/pkg/uio/archivereader_test.go index 1442b1d057..798b1d172a 100644 --- a/pkg/uio/archivereader_test.go +++ b/pkg/uio/archivereader_test.go @@ -7,7 +7,6 @@ package uio import ( "bytes" "io" - "io/ioutil" "math/rand" "strings" "testing" @@ -41,7 +40,7 @@ func TestArchiveReaderPreReadShort(t *testing.T) { if err != nil { t.Errorf("newArchiveReader(bytes.NewReader([]byte(%s))) returned err: %v, want nil", dataStr, err) } - got, err := ioutil.ReadAll(ar) + got, err := io.ReadAll(ar) if err != nil { t.Errorf("got error reading archive reader: %v, want nil", err) } @@ -54,7 +53,7 @@ func TestArchiveReaderPreReadShort(t *testing.T) { if err != ErrPreReadError { t.Errorf("newArchiveReader(bytes.NewReader([]byte(%s))) returned err: %v, want %v", dataStr, err, ErrPreReadError) } - got, err = ioutil.ReadAll(ar) + got, err = io.ReadAll(ar) if err != nil { t.Errorf("got error reading archive reader: %v, want nil", err) } diff --git a/pkg/uio/progress_test.go b/pkg/uio/progress_test.go index 868149afd9..6d8de14ecf 100644 --- a/pkg/uio/progress_test.go +++ b/pkg/uio/progress_test.go @@ -6,12 +6,12 @@ package uio import ( "bytes" - "io/ioutil" + "io" "testing" ) func TestProgressReadCloser(t *testing.T) { - input := ioutil.NopCloser(bytes.NewBufferString("01234567890123456789")) + input := io.NopCloser(bytes.NewBufferString("01234567890123456789")) stdout := &bytes.Buffer{} prc := ProgressReadCloser{ RC: input, @@ -44,7 +44,7 @@ func TestProgressReadCloser(t *testing.T) { } // Read until EOF - output, err := ioutil.ReadAll(&prc) + output, err := io.ReadAll(&prc) if err != nil { t.Errorf("got %v, expected nil error", err) } diff --git a/pkg/uio/reader.go b/pkg/uio/reader.go index 15ebc6267d..0ca839a073 100644 --- a/pkg/uio/reader.go +++ b/pkg/uio/reader.go @@ -7,7 +7,6 @@ package uio import ( "bytes" "io" - "io/ioutil" "math" "os" "reflect" @@ -27,7 +26,7 @@ func ReadAll(r io.ReaderAt) ([]byte, error) { if imra, ok := r.(inMemReaderAt); ok { return imra.Bytes(), nil } - return ioutil.ReadAll(Reader(r)) + return io.ReadAll(Reader(r)) } // Reader generates a Reader from a ReaderAt. diff --git a/pkg/uio/reader_test.go b/pkg/uio/reader_test.go index 1740e30f41..c6ebfc70bd 100644 --- a/pkg/uio/reader_test.go +++ b/pkg/uio/reader_test.go @@ -5,7 +5,6 @@ package uio import ( - "io/ioutil" "os" "path/filepath" "strings" @@ -19,9 +18,9 @@ func readAndCheck(t *testing.T, want, tmpfileP string) { t.Errorf("ReadIntoFile(%v, %s) = %v, want no error", r, tmpfileP, err) } - got, err := ioutil.ReadFile(tmpfileP) + got, err := os.ReadFile(tmpfileP) if err != nil { - t.Fatalf("ioutil.ReadFile(%s) = %v, want no error", tmpfileP, err) + t.Fatalf("os.ReadFile(%s) = %v, want no error", tmpfileP, err) } if want != string(got) { t.Errorf("got: %v, want %s", string(got), want) @@ -31,11 +30,7 @@ func readAndCheck(t *testing.T, want, tmpfileP string) { func TestReadIntoFile(t *testing.T) { want := "I am the wanted" - dir, err := ioutil.TempDir("", "uio-reader-test") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() // Write to a file already exist. p := filepath.Join(dir, "uio-out") diff --git a/pkg/upath/symlink_test.go b/pkg/upath/symlink_test.go index 8cb0292150..fec8b3a36b 100644 --- a/pkg/upath/symlink_test.go +++ b/pkg/upath/symlink_test.go @@ -5,7 +5,6 @@ package upath import ( - "io/ioutil" "log" "os" "path/filepath" @@ -13,10 +12,7 @@ import ( ) func TestSymlink(t *testing.T) { - td, err := ioutil.TempDir("", "testsymlink") - if err != nil { - t.Fatal(err) - } + td := t.TempDir() for _, n := range []string{"bin", "buildbin"} { p := filepath.Join(td, n) if err := os.Mkdir(p, 0o777); err != nil { @@ -50,7 +46,7 @@ func TestSymlink(t *testing.T) { } // test to make sure a plain file gives a reasonable result. ic := filepath.Join(td, "x") - if err := ioutil.WriteFile(ic, nil, 0o666); err != nil { + if err := os.WriteFile(ic, nil, 0o666); err != nil { t.Fatalf("WriteFile %v: got %v, want nil", ic, err) } v := ResolveUntilLastSymlink(ic) diff --git a/pkg/uroot/builder/bb_test.go b/pkg/uroot/builder/bb_test.go index 7a74cd0501..4cd59dc7da 100644 --- a/pkg/uroot/builder/bb_test.go +++ b/pkg/uroot/builder/bb_test.go @@ -5,8 +5,6 @@ package builder import ( - "io/ioutil" - "os" "testing" "github.com/u-root/u-root/pkg/golang" @@ -14,11 +12,7 @@ import ( ) func TestBBBuild(t *testing.T) { - dir, err := ioutil.TempDir("", "u-root") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() opts := Opts{ Env: golang.Default(), diff --git a/pkg/uroot/initramfs/dir.go b/pkg/uroot/initramfs/dir.go index 1c2cb88bb8..55b21bffab 100644 --- a/pkg/uroot/initramfs/dir.go +++ b/pkg/uroot/initramfs/dir.go @@ -7,7 +7,6 @@ package initramfs import ( "fmt" "io" - "io/ioutil" "os" "github.com/u-root/u-root/pkg/cpio" @@ -28,7 +27,7 @@ func (da DirArchiver) Reader(io.ReaderAt) Reader { func (da DirArchiver) OpenWriter(l ulog.Logger, path string) (Writer, error) { if len(path) == 0 { var err error - path, err = ioutil.TempDir("", "u-root") + path, err = os.MkdirTemp("", "u-root") if err != nil { return nil, err } diff --git a/pkg/uroot/initramfs/files_test.go b/pkg/uroot/initramfs/files_test.go index 7396e45566..c25099e32b 100644 --- a/pkg/uroot/initramfs/files_test.go +++ b/pkg/uroot/initramfs/files_test.go @@ -7,7 +7,6 @@ package initramfs import ( "fmt" "io" - "io/ioutil" "os" "path/filepath" "reflect" @@ -19,23 +18,14 @@ import ( ) func TestFilesAddFileNoFollow(t *testing.T) { - regularFile, err := ioutil.TempFile("", "archive-files-add-file") + regularFile, err := os.CreateTemp("", "archive-files-add-file") if err != nil { t.Error(err) } defer os.RemoveAll(regularFile.Name()) - dir, err := ioutil.TempDir("", "archive-add-files") - if err != nil { - t.Error(err) - } - defer os.RemoveAll(dir) - - dir2, err := ioutil.TempDir("", "archive-add-files") - if err != nil { - t.Error(err) - } - defer os.RemoveAll(dir2) + dir := t.TempDir() + dir2 := t.TempDir() os.Create(filepath.Join(dir, "foo2")) os.Symlink(filepath.Join(dir, "foo2"), filepath.Join(dir2, "foo3")) @@ -92,29 +82,15 @@ func TestFilesAddFileNoFollow(t *testing.T) { } func TestFilesAddFile(t *testing.T) { - regularFile, err := ioutil.TempFile("", "archive-files-add-file") + regularFile, err := os.CreateTemp("", "archive-files-add-file") if err != nil { t.Error(err) } defer os.RemoveAll(regularFile.Name()) - dir, err := ioutil.TempDir("", "archive-add-files") - if err != nil { - t.Error(err) - } - defer os.RemoveAll(dir) - - dir2, err := ioutil.TempDir("", "archive-add-files") - if err != nil { - t.Error(err) - } - defer os.RemoveAll(dir2) - - dir3, err := ioutil.TempDir("", "archive-add-files") - if err != nil { - t.Error(err) - } - defer os.RemoveAll(dir3) + dir := t.TempDir() + dir2 := t.TempDir() + dir3 := t.TempDir() os.Create(filepath.Join(dir, "foo")) os.Create(filepath.Join(dir, "foo2")) diff --git a/pkg/uroot/uroot.go b/pkg/uroot/uroot.go index b7a81d642e..aac18706f4 100644 --- a/pkg/uroot/uroot.go +++ b/pkg/uroot/uroot.go @@ -11,7 +11,6 @@ package uroot import ( "debug/elf" "fmt" - "io/ioutil" "os" "path" "path/filepath" @@ -242,7 +241,7 @@ func CreateInitramfs(logger ulog.Logger, opts Opts) error { // Add each build mode's commands to the archive. for _, cmds := range opts.Commands { - builderTmpDir, err := ioutil.TempDir(opts.TempDir, "builder") + builderTmpDir, err := os.MkdirTemp(opts.TempDir, "builder") if err != nil { return err } diff --git a/pkg/uroot/uroot_test.go b/pkg/uroot/uroot_test.go index 0e66a65e97..5d320d1464 100644 --- a/pkg/uroot/uroot_test.go +++ b/pkg/uroot/uroot_test.go @@ -6,7 +6,6 @@ package uroot import ( "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -170,11 +169,7 @@ func TestResolvePackagePaths(t *testing.T) { } func TestCreateInitramfs(t *testing.T) { - dir, err := ioutil.TempDir("", "foo") - if err != nil { - t.Error(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() syscall.Umask(0) tmp777 := filepath.Join(dir, "tmp777") diff --git a/pkg/uzip/uzip_test.go b/pkg/uzip/uzip_test.go index f95162a5c9..0947ad4c50 100644 --- a/pkg/uzip/uzip_test.go +++ b/pkg/uzip/uzip_test.go @@ -5,7 +5,6 @@ package uzip import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -14,32 +13,25 @@ import ( ) func TestFromZip(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "ziptest") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() f := filepath.Join(tmpDir, "test.zip") - err = ToZip("testdata/testFolder", f, "") - if err != nil { + if err := ToZip("testdata/testFolder", f, ""); err != nil { t.Fatal(err) } - z, err := ioutil.ReadFile(f) + z, err := os.ReadFile(f) if err != nil { t.Fatal(err) } require.NotEmpty(t, z) out := filepath.Join(tmpDir, "unziped") - err = os.MkdirAll(out, os.ModePerm) - if err != nil { + if err := os.MkdirAll(out, os.ModePerm); err != nil { t.Fatal(err) } - err = FromZip(f, out) - if err != nil { + if err := FromZip(f, out); err != nil { t.Fatal(err) } @@ -48,19 +40,19 @@ func TestFromZip(t *testing.T) { f3 := filepath.Join(out, "subFolder", "file3") f4 := filepath.Join(out, "subFolder", "file4") - f1Expected, err := ioutil.ReadFile("testdata/testFolder/file1") + f1Expected, err := os.ReadFile("testdata/testFolder/file1") if err != nil { t.Fatal(err) } - f2Expected, err := ioutil.ReadFile("testdata/testFolder/file2") + f2Expected, err := os.ReadFile("testdata/testFolder/file2") if err != nil { t.Fatal(err) } - f3Expected, err := ioutil.ReadFile("testdata/testFolder/subFolder/file3") + f3Expected, err := os.ReadFile("testdata/testFolder/subFolder/file3") if err != nil { t.Fatal(err) } - f4Expected, err := ioutil.ReadFile("testdata/testFolder/subFolder/file4") + f4Expected, err := os.ReadFile("testdata/testFolder/subFolder/file4") if err != nil { t.Fatal(err) } @@ -72,19 +64,19 @@ func TestFromZip(t *testing.T) { var x []byte - x, err = ioutil.ReadFile(f1) + x, err = os.ReadFile(f1) require.NoError(t, err) require.Equal(t, x, f1Expected) - x, err = ioutil.ReadFile(f2) + x, err = os.ReadFile(f2) require.NoError(t, err) require.Equal(t, x, f2Expected) - x, err = ioutil.ReadFile(f3) + x, err = os.ReadFile(f3) require.NoError(t, err) require.Equal(t, x, f3Expected) - x, err = ioutil.ReadFile(f4) + x, err = os.ReadFile(f4) require.NoError(t, err) require.Equal(t, x, f4Expected) } diff --git a/pkg/vfile/vfile.go b/pkg/vfile/vfile.go index 0039a98224..e79803e987 100644 --- a/pkg/vfile/vfile.go +++ b/pkg/vfile/vfile.go @@ -17,7 +17,6 @@ import ( "fmt" "hash" "io" - "io/ioutil" "os" "golang.org/x/crypto/openpgp" @@ -136,7 +135,7 @@ func (f *File) Name() string { // If the signature does not exist or does not match the keyring, both the file // and a signature error will be returned. func OpenSignedFile(keyring openpgp.KeyRing, path, pathSig string) (*File, error) { - content, err := ioutil.ReadFile(path) + content, err := os.ReadFile(path) if err != nil { return nil, err } @@ -214,7 +213,7 @@ func OpenHashedFile512(path string, wantSHA512Hash []byte) (*File, error) { } func openHashedFile(path string, wantHash []byte, h hash.Hash) (*File, error) { - content, err := ioutil.ReadFile(path) + content, err := os.ReadFile(path) if err != nil { return nil, err } diff --git a/pkg/vfile/vfile_test.go b/pkg/vfile/vfile_test.go index f377f38fac..910569e990 100644 --- a/pkg/vfile/vfile_test.go +++ b/pkg/vfile/vfile_test.go @@ -8,7 +8,7 @@ import ( "bytes" "crypto/sha256" "fmt" - "io/ioutil" + "io" "math/rand" "os" "path/filepath" @@ -57,12 +57,12 @@ type normalFile struct { } func (n normalFile) write(path string) error { - return ioutil.WriteFile(path, []byte(n.content), 0o600) + return os.WriteFile(path, []byte(n.content), 0o600) } func writeHashedFile(path, content string) ([]byte, error) { c := []byte(content) - if err := ioutil.WriteFile(path, c, 0o600); err != nil { + if err := os.WriteFile(path, c, 0o600); err != nil { return nil, err } hash := sha256.Sum256(c) @@ -115,7 +115,7 @@ func TestOpenSignedFile(t *testing.T) { // This depends on the keys generated by EntityGenerate. var keys []*openpgp.Entity for _, k := range keyFiles { - b, err := ioutil.ReadFile(filepath.Join("testdata", k)) + b, err := os.ReadFile(filepath.Join("testdata", k)) if err != nil { t.Fatal(err) } @@ -128,11 +128,7 @@ func TestOpenSignedFile(t *testing.T) { ring := openpgp.EntityList{keys[0]} - dir, err := ioutil.TempDir("", "opensignedfile") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() signed := signedFile{ signers: openpgp.EntityList{keys[0]}, @@ -162,7 +158,7 @@ func TestOpenSignedFile(t *testing.T) { } normalPath := filepath.Join(dir, "unsigned") - if err := ioutil.WriteFile(normalPath, []byte("foo"), 0o777); err != nil { + if err := os.WriteFile(normalPath, []byte("foo"), 0o777); err != nil { t.Fatal(err) } @@ -262,7 +258,7 @@ func TestOpenSignedFile(t *testing.T) { // Make sure that the file is readable from position 0. if f != nil { - content, err := ioutil.ReadAll(f) + content, err := io.ReadAll(f) if err != nil { t.Errorf("Could not read content: %v", err) } @@ -330,11 +326,7 @@ func TestReadSignedImage(t *testing.T) { } func TestOpenHashedFile(t *testing.T) { - dir, err := ioutil.TempDir("", "openhashedfile") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() hashedPath := filepath.Join(dir, "hashed") hash, err := writeHashedFile(hashedPath, "foo") @@ -421,7 +413,7 @@ func TestOpenHashedFile(t *testing.T) { // Make sure that the file is readable from position 0. if f != nil { - content, err := ioutil.ReadAll(f) + content, err := io.ReadAll(f) if err != nil { t.Errorf("Could not read content: %v", err) } diff --git a/pkg/vmtest/gotest.go b/pkg/vmtest/gotest.go index d58b109ce4..b11cc3c642 100644 --- a/pkg/vmtest/gotest.go +++ b/pkg/vmtest/gotest.go @@ -7,7 +7,6 @@ package vmtest import ( "fmt" "io" - "io/ioutil" "os" "path" "path/filepath" @@ -33,7 +32,7 @@ func GolangTest(t *testing.T, pkgs []string, o *Options) { // Create a temporary directory. if len(o.TmpDir) == 0 { - tmpDir, err := ioutil.TempDir("", "uroot-integration") + tmpDir, err := os.MkdirTemp("", "uroot-integration") if err != nil { t.Fatal(err) } diff --git a/pkg/vmtest/integration.go b/pkg/vmtest/integration.go index 5b593a7f68..d6bb50d364 100644 --- a/pkg/vmtest/integration.go +++ b/pkg/vmtest/integration.go @@ -7,7 +7,6 @@ package vmtest import ( "fmt" "io" - "io/ioutil" "log" "os" "path/filepath" @@ -209,7 +208,7 @@ func QEMUTest(t *testing.T, o *Options) (*qemu.VM, func()) { // Create or reuse a temporary directory. This is exposed to the VM. if o.TmpDir == "" { - tmpDir, err := ioutil.TempDir("", "uroot-integration") + tmpDir, err := os.MkdirTemp("", "uroot-integration") if err != nil { t.Fatalf("Failed to create temp dir: %v", err) } @@ -262,7 +261,7 @@ func QEMU(o *Options) (*qemu.Options, error) { if len(o.TestCmds) > 0 { testFile := filepath.Join(o.TmpDir, "test.elv") - if err := ioutil.WriteFile( + if err := os.WriteFile( testFile, []byte(strings.Join(o.TestCmds, "\n")), 0o777); err != nil { return nil, err } @@ -372,7 +371,7 @@ func CreateTestInitramfs(dontSetEnv bool, o uroot.Opts, uinit, outputFile string o.DefaultShell = "elvish" } if len(o.TempDir) == 0 { - tempDir, err := ioutil.TempDir("", "initramfs-tempdir") + tempDir, err := os.MkdirTemp("", "initramfs-tempdir") if err != nil { return "", fmt.Errorf("Failed to create temp dir: %v", err) } @@ -382,7 +381,7 @@ func CreateTestInitramfs(dontSetEnv bool, o uroot.Opts, uinit, outputFile string // Create an output file if one was not provided. if len(outputFile) == 0 { - f, err := ioutil.TempFile("", "initramfs.cpio") + f, err := os.CreateTemp("", "initramfs.cpio") if err != nil { return "", fmt.Errorf("failed to create output file: %v", err) } diff --git a/pkg/vpd/flashromvpd.go b/pkg/vpd/flashromvpd.go index 1fbcc7462e..ceaa0f5183 100644 --- a/pkg/vpd/flashromvpd.go +++ b/pkg/vpd/flashromvpd.go @@ -5,7 +5,6 @@ package vpd import ( - "io/ioutil" "log" "os" "os/exec" @@ -22,7 +21,7 @@ func handler(c <-chan os.Signal) { // Set RW_VPD key-value via flashrom and vpd executables, delete set to false would set or add the key, // delete set to true would delete an existing key. func FlashromRWVpdSet(key string, value []byte, delete bool) error { - file, err := ioutil.TempFile("/tmp", "rwvpd*.bin") + file, err := os.CreateTemp("/tmp", "rwvpd*.bin") if err != nil { return err } @@ -68,7 +67,7 @@ func FlashromRWVpdSet(key string, value []byte, delete bool) error { // ClearRwVpd re-format RW_VPD via flashrom and vpd executables func ClearRwVpd() error { - file, err := ioutil.TempFile("/tmp", "rwvpd*.bin") + file, err := os.CreateTemp("/tmp", "rwvpd*.bin") if err != nil { return err } diff --git a/pkg/vpd/vpd.go b/pkg/vpd/vpd.go index 1385504467..9cd892343f 100644 --- a/pkg/vpd/vpd.go +++ b/pkg/vpd/vpd.go @@ -5,7 +5,6 @@ package vpd import ( - "io/ioutil" "os" "path" "path/filepath" @@ -57,7 +56,7 @@ type Reader struct { // bytes. The `readOnly` flag specifies whether the variable is read-only or // read-write. func (r *Reader) Get(key string, readOnly bool) ([]byte, error) { - buf, err := ioutil.ReadFile(path.Join(r.getBaseDir(readOnly), key)) + buf, err := os.ReadFile(path.Join(r.getBaseDir(readOnly), key)) if err != nil { return []byte{}, err } @@ -73,7 +72,7 @@ func (r *Reader) Get(key string, readOnly bool) ([]byte, error) { func (r *Reader) Set(key string, value []byte, readOnly bool) error { // NOTE this is not implemented yet in the kernel interface, and will always // return a permission denied error - return ioutil.WriteFile(path.Join(r.getBaseDir(readOnly), key), value, 0o644) + return os.WriteFile(path.Join(r.getBaseDir(readOnly), key), value, 0o644) } // GetAll reads all the VPD variables and returns a map contaiing each diff --git a/tools/build_perf/build_perf.go b/tools/build_perf/build_perf.go index 18473b710d..c3d9f58d55 100644 --- a/tools/build_perf/build_perf.go +++ b/tools/build_perf/build_perf.go @@ -14,7 +14,6 @@ package main import ( "encoding/csv" "fmt" - "io/ioutil" "log" "os" "os/exec" @@ -73,7 +72,7 @@ var wg sync.WaitGroup // Return a list of command names. func getCmdNames() ([]string, error) { - files, err := ioutil.ReadDir(os.ExpandEnv(cmdsPath)) + files, err := os.ReadDir(os.ExpandEnv(cmdsPath)) if err != nil { return nil, err } diff --git a/tools/checklicenses/checklicenses.go b/tools/checklicenses/checklicenses.go index 9600503087..c07d2deb87 100644 --- a/tools/checklicenses/checklicenses.go +++ b/tools/checklicenses/checklicenses.go @@ -11,7 +11,7 @@ import ( "encoding/json" "flag" "fmt" - "io/ioutil" + "io" "log" "os" "os/exec" @@ -92,7 +92,7 @@ func main() { if *configFile == "" { log.Fatal("Config file name cannot be empty") } - buf, err := ioutil.ReadFile(*configFile) + buf, err := os.ReadFile(*configFile) if err != nil { log.Fatalf("Failed to read file %s: %v", *configFile, err) } @@ -151,7 +151,7 @@ func main() { log.Fatalln("cannot open", file, err) } defer r.Close() - contents, err := ioutil.ReadAll(r) + contents, err := io.ReadAll(r) if err != nil { log.Fatalln("cannot read", file, err) } diff --git a/tools/testramfs/testramfs.go b/tools/testramfs/testramfs.go index 7c367d0d0f..3417a3aa65 100644 --- a/tools/testramfs/testramfs.go +++ b/tools/testramfs/testramfs.go @@ -7,7 +7,6 @@ package main import ( "io" - "io/ioutil" "log" "os" "syscall" @@ -66,7 +65,7 @@ func main() { u := unix.Umask(0) defer unix.Umask(u) - tempDir, err := ioutil.TempDir("", "u-root") + tempDir, err := os.MkdirTemp("", "u-root") if err != nil { log.Fatal(err) } diff --git a/tools/vpdbootmanager/add_test.go b/tools/vpdbootmanager/add_test.go index 552dbee960..14fe49e7b5 100644 --- a/tools/vpdbootmanager/add_test.go +++ b/tools/vpdbootmanager/add_test.go @@ -7,8 +7,6 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" - "log" "os" "path" "testing" @@ -129,17 +127,14 @@ func TestFailGracefullyUnknownEntryType(t *testing.T) { } func TestAddBootEntry(t *testing.T) { - vpdDir, err := ioutil.TempDir("", "vpdbootmanager") - if err != nil { - log.Fatal(err) - } + vpdDir := t.TempDir() os.MkdirAll(path.Join(vpdDir, "rw"), 0o700) defer os.RemoveAll(vpdDir) - err = addBootEntry(&systembooter.LocalBooter{ + err := addBootEntry(&systembooter.LocalBooter{ Method: "grub", }, vpdDir) require.NoError(t, err) - file, err := ioutil.ReadFile(path.Join(vpdDir, "rw", "Boot0001")) + file, err := os.ReadFile(path.Join(vpdDir, "rw", "Boot0001")) require.NoError(t, err) var out systembooter.LocalBooter err = json.Unmarshal([]byte(file), &out) @@ -148,18 +143,15 @@ func TestAddBootEntry(t *testing.T) { } func TestAddBootEntryMultiple(t *testing.T) { - vpdDir, err := ioutil.TempDir("", "vpdbootmanager") - if err != nil { - log.Fatal(err) - } + vpdDir := t.TempDir() os.MkdirAll(path.Join(vpdDir, "rw"), 0o700) defer os.RemoveAll(vpdDir) for i := 1; i < 5; i++ { - err = addBootEntry(&systembooter.LocalBooter{ + err := addBootEntry(&systembooter.LocalBooter{ Method: "grub", }, vpdDir) require.NoError(t, err) - file, err := ioutil.ReadFile(path.Join(vpdDir, "rw", fmt.Sprintf("Boot%04d", i))) + file, err := os.ReadFile(path.Join(vpdDir, "rw", fmt.Sprintf("Boot%04d", i))) require.NoError(t, err) var out systembooter.LocalBooter err = json.Unmarshal([]byte(file), &out) diff --git a/tools/vpdbootmanager/main_test.go b/tools/vpdbootmanager/main_test.go index 4e663ccc43..2816e0909f 100644 --- a/tools/vpdbootmanager/main_test.go +++ b/tools/vpdbootmanager/main_test.go @@ -6,8 +6,6 @@ package main import ( "encoding/json" - "io/ioutil" - "log" "os" "path" "testing" @@ -32,13 +30,10 @@ func TestNoAction(t *testing.T) { } func TestAddNetbootEntryFull(t *testing.T) { - dir, err := ioutil.TempDir("", "vpdbootmanager") - if err != nil { - log.Fatal(err) - } + dir := t.TempDir() os.MkdirAll(path.Join(dir, "rw"), 0o700) defer os.RemoveAll(dir) - err = cli([]string{ + err := cli([]string{ "add", "netboot", "dhcpv6", @@ -47,7 +42,7 @@ func TestAddNetbootEntryFull(t *testing.T) { dir, }) require.NoError(t, err) - file, err := ioutil.ReadFile(path.Join(dir, "rw", "Boot0001")) + file, err := os.ReadFile(path.Join(dir, "rw", "Boot0001")) require.NoError(t, err) var out systembooter.NetBooter err = json.Unmarshal([]byte(file), &out) @@ -57,13 +52,10 @@ func TestAddNetbootEntryFull(t *testing.T) { } func TestAddLocalbootEntryFull(t *testing.T) { - dir, err := ioutil.TempDir("", "vpdbootmanager") - if err != nil { - log.Fatal(err) - } + dir := t.TempDir() os.MkdirAll(path.Join(dir, "rw"), 0o700) defer os.RemoveAll(dir) - err = cli([]string{ + err := cli([]string{ "add", "localboot", "grub", @@ -71,7 +63,7 @@ func TestAddLocalbootEntryFull(t *testing.T) { dir, }) require.NoError(t, err) - file, err := ioutil.ReadFile(path.Join(dir, "rw", "Boot0001")) + file, err := os.ReadFile(path.Join(dir, "rw", "Boot0001")) require.NoError(t, err) var out systembooter.NetBooter err = json.Unmarshal([]byte(file), &out) diff --git a/u-root.go b/u-root.go index 3efe7b29d8..57bde42083 100644 --- a/u-root.go +++ b/u-root.go @@ -8,7 +8,6 @@ import ( "encoding/json" "flag" "fmt" - "io/ioutil" "log" "os" "path" @@ -95,7 +94,7 @@ type buildStats struct { func writeBuildStats(stats buildStats, path string) error { var allStats []buildStats - if data, err := ioutil.ReadFile(*statsOutputPath); err == nil { + if data, err := os.ReadFile(*statsOutputPath); err == nil { json.Unmarshal(data, &allStats) } found := false @@ -116,7 +115,7 @@ func writeBuildStats(stats buildStats, path string) error { if err != nil { return err } - if err := ioutil.WriteFile(*statsOutputPath, data, 0o644); err != nil { + if err := os.WriteFile(*statsOutputPath, data, 0o644); err != nil { return err } return nil @@ -242,7 +241,7 @@ func Main() error { tempDir := *tmpDir if tempDir == "" { var err error - tempDir, err = ioutil.TempDir("", "u-root") + tempDir, err = os.MkdirTemp("", "u-root") if err != nil { return err } diff --git a/uroot_test.go b/uroot_test.go index 062a9f22c5..23fe69b9ba 100644 --- a/uroot_test.go +++ b/uroot_test.go @@ -10,7 +10,6 @@ import ( "crypto/sha256" "fmt" "io" - "io/ioutil" "os" "path/filepath" "strings" @@ -78,7 +77,7 @@ func (v noDeadCode) Validate(a *cpio.Archive) error { if !ok { return fmt.Errorf("archive does not contain %s, but should", v.Path) } - tf, err := ioutil.TempFile("", "u-root-temp-bb-") + tf, err := os.CreateTemp("", "u-root-temp-bb-") if err != nil { return err } @@ -126,23 +125,19 @@ func (v noDeadCode) Validate(a *cpio.Archive) error { } func TestUrootCmdline(t *testing.T) { - samplef, err := ioutil.TempFile("", "u-root-test-") + samplef, err := os.CreateTemp("", "u-root-test-") if err != nil { t.Fatal(err) } samplef.Close() defer os.RemoveAll(samplef.Name()) - sampledir, err := ioutil.TempDir("", "u-root-test-dir-") - if err != nil { - t.Fatal(err) - } - if err = ioutil.WriteFile(filepath.Join(sampledir, "foo"), nil, 0o644); err != nil { + sampledir := t.TempDir() + if err = os.WriteFile(filepath.Join(sampledir, "foo"), nil, 0o644); err != nil { t.Fatal(err) } - if err = ioutil.WriteFile(filepath.Join(sampledir, "bar"), nil, 0o644); err != nil { + if err = os.WriteFile(filepath.Join(sampledir, "bar"), nil, 0o644); err != nil { t.Fatal(err) } - defer os.RemoveAll(sampledir) for _, tt := range []struct { name string @@ -307,22 +302,17 @@ func TestUrootCmdline(t *testing.T) { } func buildIt(t *testing.T, args, env []string, want error) (*os.File, []byte) { - f, err := ioutil.TempFile("", "u-root-") + f, err := os.CreateTemp("", "u-root-") if err != nil { t.Fatal(err) } // Use the u-root command outside of the $GOPATH tree to make sure it // still works. - dir, err := ioutil.TempDir("", "build-") - if err != nil { - t.Fatal(err) - } - arg := append([]string{"-o", f.Name()}, args...) c := testutil.Command(t, arg...) t.Logf("Commandline: %v", arg) c.Env = append(c.Env, env...) - c.Dir = dir + c.Dir = t.TempDir() if out, err := c.CombinedOutput(); err != want { t.Fatalf("Error: %v\nOutput:\n%s", err, out) } else if err != nil {