all: replace io/ioutil with io and os package

For golang/go#45557

Change-Id: Ib1e5d3116f04adaacb4d6b81898db255b3d5bd04
GitHub-Last-Rev: 6a580196cbdc8703799604a59e474f357e2a98e6
GitHub-Pull-Request: golang/benchmarks#3
Reviewed-on: https://go-review.googlesource.com/c/benchmarks/+/430856
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
diff --git a/cmd/bent/bent.go b/cmd/bent/bent.go
index ce7e772..ad75d6c 100644
--- a/cmd/bent/bent.go
+++ b/cmd/bent/bent.go
@@ -15,7 +15,6 @@
 	"flag"
 	"fmt"
 	"io/fs"
-	"io/ioutil"
 	"math/rand"
 	"os"
 	"os/exec"
@@ -226,17 +225,17 @@
 	}
 
 	todo := &Todo{}
-	blobB, err := ioutil.ReadFile(benchFile)
+	blobB, err := os.ReadFile(benchFile)
 	if err != nil {
 		fmt.Printf("There was an error opening or reading file %s: %v\n", benchFile, err)
 		os.Exit(1)
 	}
-	blobC, err := ioutil.ReadFile(confFile)
+	blobC, err := os.ReadFile(confFile)
 	if err != nil {
 		fmt.Printf("There was an error opening or reading file %s: %v\n", confFile, err)
 		os.Exit(1)
 	}
-	blobS, err := ioutil.ReadFile(suiteFile)
+	blobS, err := os.ReadFile(suiteFile)
 	if err != nil {
 		fmt.Printf("There was an error opening or reading file %s: %v\n", suiteFile, err)
 		os.Exit(1)
@@ -1130,7 +1129,7 @@
 			copyAsset(configs, "configs", s)
 		}
 
-		err := ioutil.WriteFile("Dockerfile",
+		err := os.WriteFile("Dockerfile",
 			[]byte(`
 FROM ubuntu
 ADD . /
@@ -1172,7 +1171,7 @@
 		fmt.Printf("Error reading asset %s\n", file)
 		os.Exit(1)
 	}
-	err = ioutil.WriteFile(file, bytes, 0664)
+	err = os.WriteFile(file, bytes, 0664)
 	if err != nil {
 		fmt.Printf("Error writing %s\n", file)
 		os.Exit(1)
diff --git a/driver/driver_linux.go b/driver/driver_linux.go
index 4012aeb..31395e8 100644
--- a/driver/driver_linux.go
+++ b/driver/driver_linux.go
@@ -11,7 +11,6 @@
 	"bufio"
 	"bytes"
 	"io"
-	"io/ioutil"
 	"log"
 	"os"
 	"os/exec"
@@ -105,7 +104,7 @@
 }
 
 func getVMPeak() uint64 {
-	data, err := ioutil.ReadFile("/proc/self/status")
+	data, err := os.ReadFile("/proc/self/status")
 	if err != nil {
 		log.Printf("Failed to read /proc/self/status: %v", err)
 		return 0
diff --git a/driver/driver_plan9.go b/driver/driver_plan9.go
index 2b58d8a..6f00b14 100644
--- a/driver/driver_plan9.go
+++ b/driver/driver_plan9.go
@@ -7,8 +7,8 @@
 import (
 	"bytes"
 	"fmt"
-	"io/ioutil"
 	"log"
+	"os"
 	"os/exec"
 	"strconv"
 	"strings"
@@ -72,7 +72,7 @@
 }
 
 func procCPUTime() (uint64, error) {
-	b, err := ioutil.ReadFile("/dev/cputime")
+	b, err := os.ReadFile("/dev/cputime")
 	if err != nil {
 		return 0, err
 	}
diff --git a/garbage/nethttp.go b/garbage/nethttp.go
index ba7e806..89c50e9 100644
--- a/garbage/nethttp.go
+++ b/garbage/nethttp.go
@@ -22,7 +22,7 @@
 // 		// handle error
 // 	}
 // 	defer resp.Body.Close()
-// 	body, err := ioutil.ReadAll(resp.Body)
+// 	body, err := io.ReadAll(resp.Body)
 // 	// ...
 //
 // For control over HTTP client headers, redirect policy, and other
@@ -90,7 +90,6 @@
 	"errors"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"log"
 	"mime"
 	"mime/multipart"
@@ -469,7 +468,7 @@
 			// No need to check for errors: if it fails, Transport won't reuse it anyway.
 			const maxBodySlurpSize = 2 << 10
 			if resp.ContentLength == -1 || resp.ContentLength <= maxBodySlurpSize {
-				io.CopyN(ioutil.Discard, resp.Body, maxBodySlurpSize)
+				io.CopyN(io.Discard, resp.Body, maxBodySlurpSize)
 			}
 			resp.Body.Close()
 			if urlStr = resp.Header.Get("Location"); urlStr == "" {
@@ -2495,7 +2494,7 @@
 	}
 	rc, ok := body.(io.ReadCloser)
 	if !ok && body != nil {
-		rc = ioutil.NopCloser(body)
+		rc = io.NopCloser(body)
 	}
 	req := &Request{
 		Method:     method,
@@ -2744,7 +2743,7 @@
 			maxFormSize = int64(10 << 20)
 			reader = io.LimitReader(r.Body, maxFormSize+1)
 		}
-		b, e := ioutil.ReadAll(reader)
+		b, e := io.ReadAll(reader)
 		if e != nil {
 			if err == nil {
 				err = e
@@ -4011,7 +4010,7 @@
 		}
 
 		if discard {
-			_, err := io.CopyN(ioutil.Discard, w.req.Body, maxPostHandlerReadBytes+1)
+			_, err := io.CopyN(io.Discard, w.req.Body, maxPostHandlerReadBytes+1)
 			switch err {
 			case nil:
 
@@ -5190,7 +5189,7 @@
 	if r.ContentLength != 0 {
 
 		mb := MaxBytesReader(w, r.Body, 4<<10)
-		io.Copy(ioutil.Discard, mb)
+		io.Copy(io.Discard, mb)
 	}
 }
 
@@ -5207,7 +5206,7 @@
 	io.Closer
 }{
 	eofReaderWithWriteTo{},
-	ioutil.NopCloser(nil),
+	io.NopCloser(nil),
 }
 
 // Verify that an io.Copy from an eofReader won't require a buffer.
@@ -5813,7 +5812,7 @@
 				return err
 			}
 			var nextra int64
-			nextra, err = io.Copy(ioutil.Discard, t.Body)
+			nextra, err = io.Copy(io.Discard, t.Body)
 			ncopy += nextra
 		}
 		if err != nil {
@@ -6301,7 +6300,7 @@
 		} else {
 			var n int64
 
-			n, err = io.CopyN(ioutil.Discard, bodyLocked{b}, maxPostHandlerReadBytes)
+			n, err = io.CopyN(io.Discard, bodyLocked{b}, maxPostHandlerReadBytes)
 			if err == io.EOF {
 				err = nil
 			}
@@ -6311,7 +6310,7 @@
 		}
 	default:
 
-		_, err = io.Copy(ioutil.Discard, bodyLocked{b})
+		_, err = io.Copy(io.Discard, bodyLocked{b})
 	}
 	b.closed = true
 	return err
diff --git a/http/http.go b/http/http.go
index 1549281..898d400 100644
--- a/http/http.go
+++ b/http/http.go
@@ -7,7 +7,7 @@
 
 import (
 	"fmt"
-	"io/ioutil"
+	"io"
 	"log"
 	"net"
 	"net/http"
@@ -51,7 +51,7 @@
 		return false
 	}
 	defer res.Body.Close()
-	b, err := ioutil.ReadAll(res.Body)
+	b, err := io.ReadAll(res.Body)
 	if err != nil {
 		log.Fatalf("ReadAll: %v", err)
 	}
diff --git a/json/json.go b/json/json.go
index 58269bd..4fc5da1 100644
--- a/json/json.go
+++ b/json/json.go
@@ -12,7 +12,6 @@
 	"encoding/base64"
 	"encoding/json"
 	"io"
-	"io/ioutil"
 
 	"golang.org/x/benchmarks/driver"
 )
@@ -47,7 +46,7 @@
 	r = bytes.NewReader(bytes.Replace(jsonbz2_base64, []byte{'\n'}, nil, -1))
 	r = base64.NewDecoder(base64.StdEncoding, r)
 	r = bzip2.NewReader(r)
-	b, err := ioutil.ReadAll(r)
+	b, err := io.ReadAll(r)
 	if err != nil {
 		panic(err)
 	}
diff --git a/sweet/benchmarks/biogo-igor/igor.go b/sweet/benchmarks/biogo-igor/igor.go
index 1e787f1..8f1a270 100644
--- a/sweet/benchmarks/biogo-igor/igor.go
+++ b/sweet/benchmarks/biogo-igor/igor.go
@@ -10,8 +10,8 @@
 	"bytes"
 	"flag"
 	"fmt"
-	"io/ioutil"
 	"log"
+	"os"
 	"runtime"
 
 	"golang.org/x/benchmarks/sweet/benchmarks/internal/driver"
@@ -41,7 +41,7 @@
 		log.Fatal("error: input GFF file required")
 	}
 
-	data, err := ioutil.ReadFile(flag.Arg(0))
+	data, err := os.ReadFile(flag.Arg(0))
 	if err != nil {
 		log.Fatalf("error: %v", err)
 	}
diff --git a/sweet/benchmarks/internal/driver/mem_linux.go b/sweet/benchmarks/internal/driver/mem_linux.go
index 9243aa0..3c3f71f 100644
--- a/sweet/benchmarks/internal/driver/mem_linux.go
+++ b/sweet/benchmarks/internal/driver/mem_linux.go
@@ -6,7 +6,6 @@
 
 import (
 	"fmt"
-	"io/ioutil"
 	"os"
 	"regexp"
 	"strconv"
@@ -20,7 +19,7 @@
 )
 
 func readStat(pid int, r *regexp.Regexp) (uint64, error) {
-	b, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/status", pid))
+	b, err := os.ReadFile(fmt.Sprintf("/proc/%d/status", pid))
 	if err != nil {
 		return 0, err
 	}
diff --git a/sweet/benchmarks/markdown/main.go b/sweet/benchmarks/markdown/main.go
index 67bfc73..76ffedb 100644
--- a/sweet/benchmarks/markdown/main.go
+++ b/sweet/benchmarks/markdown/main.go
@@ -8,7 +8,6 @@
 	"bytes"
 	"flag"
 	"fmt"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 
@@ -18,7 +17,7 @@
 )
 
 func run(mddir string) error {
-	files, err := ioutil.ReadDir(mddir)
+	files, err := os.ReadDir(mddir)
 	if err != nil {
 		return err
 	}
@@ -26,7 +25,7 @@
 	contents := make([][]byte, 0, len(files))
 	for _, file := range files {
 		if !file.IsDir() && filepath.Ext(file.Name()) == ".md" {
-			content, err := ioutil.ReadFile(filepath.Join(mddir, file.Name()))
+			content, err := os.ReadFile(filepath.Join(mddir, file.Name()))
 			if err != nil {
 				return err
 			}
diff --git a/sweet/cmd/sweet/benchmark.go b/sweet/cmd/sweet/benchmark.go
index a2977a4..a39d2c2 100644
--- a/sweet/cmd/sweet/benchmark.go
+++ b/sweet/cmd/sweet/benchmark.go
@@ -8,7 +8,6 @@
 	"errors"
 	"fmt"
 	"io/fs"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"runtime"
@@ -157,7 +156,7 @@
 
 func rmDirContents(dir string) error {
 	log.CommandPrintf("rm -rf %s/*", dir)
-	fs, err := ioutil.ReadDir(dir)
+	fs, err := os.ReadDir(dir)
 	if err != nil {
 		return err
 	}
diff --git a/sweet/cmd/sweet/run.go b/sweet/cmd/sweet/run.go
index 111d498..75f75aa 100644
--- a/sweet/cmd/sweet/run.go
+++ b/sweet/cmd/sweet/run.go
@@ -10,7 +10,6 @@
 	"fmt"
 	"io"
 	"io/fs"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"sort"
@@ -157,7 +156,7 @@
 	var err error
 	if c.workDir == "" {
 		// Create a temporary work tree for running the benchmarks.
-		c.workDir, err = ioutil.TempDir("", "gosweet")
+		c.workDir, err = os.MkdirTemp("", "gosweet")
 		if err != nil {
 			return fmt.Errorf("creating work root: %w", err)
 		}
@@ -238,7 +237,7 @@
 		configDir := filepath.Dir(configFile)
 
 		// Read and parse the configuration file.
-		b, err := ioutil.ReadFile(configFile)
+		b, err := os.ReadFile(configFile)
 		if err != nil {
 			return fmt.Errorf("failed to read %q: %v", configFile, err)
 		}
diff --git a/sweet/generators/gvisor.go b/sweet/generators/gvisor.go
index ed0b7db..e378151 100644
--- a/sweet/generators/gvisor.go
+++ b/sweet/generators/gvisor.go
@@ -9,7 +9,6 @@
 	"encoding/json"
 	"errors"
 	"fmt"
-	"io/ioutil"
 	"os"
 	"os/exec"
 	"path/filepath"
@@ -67,7 +66,7 @@
 	// copy of runsc. Get and build it from the harness.
 	//
 	// Create a temporary directory where we can put the gVisor source.
-	tmpDir, err := ioutil.TempDir("", "gvisor-gen")
+	tmpDir, err := os.MkdirTemp("", "gvisor-gen")
 	if err != nil {
 		return err
 	}
diff --git a/sweet/generators/tile38.go b/sweet/generators/tile38.go
index 21a0e14..b91c4f7 100644
--- a/sweet/generators/tile38.go
+++ b/sweet/generators/tile38.go
@@ -9,7 +9,6 @@
 	"bytes"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"os"
 	"os/exec"
 	"path/filepath"
@@ -68,7 +67,7 @@
 
 	// Create a temporary directory where we can put the Tile38
 	// source and build it.
-	tmpDir, err := ioutil.TempDir("", "tile38-gen")
+	tmpDir, err := os.MkdirTemp("", "tile38-gen")
 	if err != nil {
 		return err
 	}
@@ -292,7 +291,7 @@
 // storeGeoJSON writes an entire GeoJSON object (which may contain many polygons)
 // to a Tile38 database.
 func storeGeoJSON(c redis.Conn, jsonFile string) error {
-	b, err := ioutil.ReadFile(jsonFile)
+	b, err := os.ReadFile(jsonFile)
 	if err != nil {
 		return err
 	}