golang: Stop using io/ioutils

The package has been deprecated as part of 1.16 and the same
functionality is now provided by either the io or the os package.

Signed-off-by: Fabiano Fidêncio <fabiano.fidencio@intel.com>
(cherry picked from commit 16b8375095)
This commit is contained in:
Fabiano Fidêncio 2022-11-17 13:11:05 +01:00
parent b5661e9882
commit 778ebb6e60
16 changed files with 35 additions and 47 deletions

View File

@ -7,7 +7,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "os"
containerdshim "github.com/kata-containers/kata-containers/src/runtime/pkg/containerd-shim-v2" containerdshim "github.com/kata-containers/kata-containers/src/runtime/pkg/containerd-shim-v2"
"github.com/kata-containers/kata-containers/src/runtime/pkg/katautils" "github.com/kata-containers/kata-containers/src/runtime/pkg/katautils"
@ -103,7 +103,7 @@ var setIPTablesCommand = cli.Command{
} }
// Read file into buffer, and make request to the appropriate shim // Read file into buffer, and make request to the appropriate shim
buf, err := ioutil.ReadFile(iptablesFile) buf, err := os.ReadFile(iptablesFile)
if err != nil { if err != nil {
return err return err
} }

View File

@ -11,7 +11,6 @@ import (
"expvar" "expvar"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/http/pprof" "net/http/pprof"
"net/url" "net/url"
@ -173,7 +172,7 @@ func (s *service) serveVolumeStats(w http.ResponseWriter, r *http.Request) {
} }
func (s *service) serveVolumeResize(w http.ResponseWriter, r *http.Request) { func (s *service) serveVolumeResize(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
shimMgtLog.WithError(err).Error("failed to read request body") shimMgtLog.WithError(err).Error("failed to read request body")
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
@ -212,7 +211,7 @@ func (s *service) genericIPTablesHandler(w http.ResponseWriter, r *http.Request,
switch r.Method { switch r.Method {
case http.MethodPut: case http.MethodPut:
body, err := ioutil.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
logger.WithError(err).Error("failed to read request body") logger.WithError(err).Error("failed to read request body")
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)

View File

@ -10,7 +10,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
) )
@ -75,7 +74,7 @@ func Add(volumePath string, mountInfo string) error {
return err return err
} }
return ioutil.WriteFile(filepath.Join(volumeDir, mountInfoFileName), []byte(mountInfo), 0600) return os.WriteFile(filepath.Join(volumeDir, mountInfoFileName), []byte(mountInfo), 0600)
} }
// Remove deletes the direct volume path including all the files inside it. // Remove deletes the direct volume path including all the files inside it.
@ -89,7 +88,7 @@ func VolumeMountInfo(volumePath string) (*MountInfo, error) {
if _, err := os.Stat(mountInfoFilePath); err != nil { if _, err := os.Stat(mountInfoFilePath); err != nil {
return nil, err return nil, err
} }
buf, err := ioutil.ReadFile(mountInfoFilePath) buf, err := os.ReadFile(mountInfoFilePath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -108,11 +107,11 @@ func RecordSandboxId(sandboxId string, volumePath string) error {
return err return err
} }
return ioutil.WriteFile(filepath.Join(kataDirectVolumeRootPath, encodedPath, sandboxId), []byte(""), 0600) return os.WriteFile(filepath.Join(kataDirectVolumeRootPath, encodedPath, sandboxId), []byte(""), 0600)
} }
func GetSandboxIdForVolume(volumePath string) (string, error) { func GetSandboxIdForVolume(volumePath string) (string, error) {
files, err := ioutil.ReadDir(filepath.Join(kataDirectVolumeRootPath, b64.URLEncoding.EncodeToString([]byte(volumePath)))) files, err := os.ReadDir(filepath.Join(kataDirectVolumeRootPath, b64.URLEncoding.EncodeToString([]byte(volumePath))))
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -8,7 +8,6 @@ package qemu
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path" "path"
@ -44,12 +43,12 @@ func CreateCloudInitISO(ctx context.Context, scratchDir, isoPath string,
dataDirPath, err) dataDirPath, err)
} }
err = ioutil.WriteFile(metaDataPath, metaData, 0644) err = os.WriteFile(metaDataPath, metaData, 0644)
if err != nil { if err != nil {
return fmt.Errorf("unable to create %s : %v", metaDataPath, err) return fmt.Errorf("unable to create %s : %v", metaDataPath, err)
} }
err = ioutil.WriteFile(userDataPath, userData, 0644) err = os.WriteFile(userDataPath, userData, 0644)
if err != nil { if err != nil {
return fmt.Errorf("unable to create %s : %v", userDataPath, err) return fmt.Errorf("unable to create %s : %v", userDataPath, err)
} }

View File

@ -7,7 +7,6 @@ package qemu
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"reflect" "reflect"
"strings" "strings"
@ -186,8 +185,8 @@ func TestAppendDeviceNetwork(t *testing.T) {
} }
func TestAppendDeviceNetworkMq(t *testing.T) { func TestAppendDeviceNetworkMq(t *testing.T) {
foo, _ := ioutil.TempFile(os.TempDir(), "govmm-qemu-test") foo, _ := os.CreateTemp(os.TempDir(), "govmm-qemu-test")
bar, _ := ioutil.TempFile(os.TempDir(), "govmm-qemu-test") bar, _ := os.CreateTemp(os.TempDir(), "govmm-qemu-test")
defer func() { defer func() {
_ = foo.Close() _ = foo.Close()

View File

@ -10,7 +10,6 @@ package katautils
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -1405,7 +1404,7 @@ func decodeDropIns(mainConfigPath string, tomlConf *tomlConfig) error {
configDir := filepath.Dir(mainConfigPath) configDir := filepath.Dir(mainConfigPath)
dropInDir := filepath.Join(configDir, "config.d") dropInDir := filepath.Join(configDir, "config.d")
files, err := ioutil.ReadDir(dropInDir) files, err := os.ReadDir(dropInDir)
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
return fmt.Errorf("error reading %q directory: %s", dropInDir, err) return fmt.Errorf("error reading %q directory: %s", dropInDir, err)

View File

@ -9,7 +9,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"time" "time"
@ -91,7 +90,7 @@ func DoPut(sandboxID string, timeoutInSeconds time.Duration, urlPath, contentTyp
}() }()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
data, _ := ioutil.ReadAll(resp.Body) data, _ := io.ReadAll(resp.Body)
return fmt.Errorf("error sending put: url: %s, status code: %d, response data: %s", urlPath, resp.StatusCode, string(data)) return fmt.Errorf("error sending put: url: %s, status code: %d, response data: %s", urlPath, resp.StatusCode, string(data))
} }
@ -117,7 +116,7 @@ func DoPost(sandboxID string, timeoutInSeconds time.Duration, urlPath, contentTy
}() }()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
data, _ := ioutil.ReadAll(resp.Body) data, _ := io.ReadAll(resp.Body)
return fmt.Errorf("error sending post: url: %s, status code: %d, response data: %s", urlPath, resp.StatusCode, string(data)) return fmt.Errorf("error sending post: url: %s, status code: %d, response data: %s", urlPath, resp.StatusCode, string(data))
} }

View File

@ -14,7 +14,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net" "net"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
@ -210,13 +210,13 @@ var vmAddNetPutRequest = func(clh *cloudHypervisor) error {
return err return err
} }
respBody, err := ioutil.ReadAll(resp.Body) respBody, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return err
} }
resp.Body.Close() resp.Body.Close()
resp.Body = ioutil.NopCloser(bytes.NewBuffer(respBody)) resp.Body = io.NopCloser(bytes.NewBuffer(respBody))
if resp.StatusCode != 204 { if resp.StatusCode != 204 {
clh.Logger().Errorf("vmAddNetPut failed with error '%d'. Response: %+v", resp.StatusCode, resp) clh.Logger().Errorf("vmAddNetPut failed with error '%d'. Response: %+v", resp.StatusCode, resp)

View File

@ -10,7 +10,6 @@ package virtcontainers
import ( import (
"context" "context"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -134,7 +133,7 @@ func TestCloudHypervisorAddNetCheckNetConfigListValues(t *testing.T) {
macTest := "00:00:00:00:00" macTest := "00:00:00:00:00"
file, err := ioutil.TempFile("", "netFd") file, err := os.CreateTemp("", "netFd")
assert.Nil(err) assert.Nil(err)
defer os.Remove(file.Name()) defer os.Remove(file.Name())
@ -172,7 +171,7 @@ func TestCloudHypervisorAddNetCheckEnpointTypes(t *testing.T) {
macTest := "00:00:00:00:00" macTest := "00:00:00:00:00"
file, err := ioutil.TempFile("", "netFd") file, err := os.CreateTemp("", "netFd")
assert.Nil(err) assert.Nil(err)
defer os.Remove(file.Name()) defer os.Remove(file.Name())
@ -214,7 +213,7 @@ func TestCloudHypervisorAddNetCheckEnpointTypes(t *testing.T) {
func TestCloudHypervisorNetRateLimiter(t *testing.T) { func TestCloudHypervisorNetRateLimiter(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
file, err := ioutil.TempFile("", "netFd") file, err := os.CreateTemp("", "netFd")
assert.Nil(err) assert.Nil(err)
defer os.Remove(file.Name()) defer os.Remove(file.Name())

View File

@ -9,7 +9,6 @@ import (
"bufio" "bufio"
"context" "context"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -1168,7 +1167,7 @@ func TestHandleHugepages(t *testing.T) {
// are present (default is 1M, can only be changed on LPAR). See // are present (default is 1M, can only be changed on LPAR). See
// https://www.ibm.com/docs/en/linuxonibm/pdf/lku5dd05.pdf, p. 345 for more information. // https://www.ibm.com/docs/en/linuxonibm/pdf/lku5dd05.pdf, p. 345 for more information.
if runtime.GOARCH == "s390x" { if runtime.GOARCH == "s390x" {
dirs, err := ioutil.ReadDir(sysHugepagesDir) dirs, err := os.ReadDir(sysHugepagesDir)
assert.Nil(err) assert.Nil(err)
for _, dir := range dirs { for _, dir := range dirs {
formattedSizes = append(formattedSizes, strings.TrimPrefix(dir.Name(), "hugepages-")) formattedSizes = append(formattedSizes, strings.TrimPrefix(dir.Name(), "hugepages-"))

View File

@ -13,7 +13,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"os" "os"
@ -398,7 +397,7 @@ func (c *NydusClient) CheckStatus() (DaemonInfo, error) {
return DaemonInfo{}, err return DaemonInfo{}, err
} }
defer resp.Body.Close() defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body) b, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return DaemonInfo{}, err return DaemonInfo{}, err
} }
@ -470,7 +469,7 @@ func (c *NydusClient) Umount(mountPoint string) error {
} }
func handleMountError(r io.Reader) error { func handleMountError(r io.Reader) error {
b, err := ioutil.ReadAll(r) b, err := io.ReadAll(r)
if err != nil { if err != nil {
return err return err
} }

View File

@ -9,7 +9,6 @@ package main
import ( import (
"errors" "errors"
"io" "io"
"io/ioutil"
"os" "os"
"strings" "strings"
) )
@ -56,7 +55,7 @@ func (r *HexByteReader) Read(p []byte) (n int, err error) {
} }
// read the entire file // read the entire file
bytes, err := ioutil.ReadAll(r.f) bytes, err := io.ReadAll(r.f)
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -7,7 +7,7 @@
package main package main
import ( import (
"io/ioutil" "io"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -36,7 +36,7 @@ func TestNewHexByteReaderStdin(t *testing.T) {
func TestHexByteReaderRead(t *testing.T) { func TestHexByteReaderRead(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
dir, err := ioutil.TempDir("", "") dir, err := os.MkdirTemp("", "")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -68,7 +68,7 @@ func TestHexByteReaderRead(t *testing.T) {
assert.NoError(err) assert.NoError(err)
reader := NewHexByteReader(file) reader := NewHexByteReader(file)
bytes, err := ioutil.ReadAll(reader) bytes, err := io.ReadAll(reader)
if d.expectError { if d.expectError {
assert.Errorf(err, "test[%d]: %+v", i, d) assert.Errorf(err, "test[%d]: %+v", i, d)

View File

@ -6,12 +6,12 @@
package main package main
import "io/ioutil" import "os"
func createEmptyFile(path string) (err error) { func createEmptyFile(path string) (err error) {
return ioutil.WriteFile(path, []byte(""), testFileMode) return os.WriteFile(path, []byte(""), testFileMode)
} }
func createFile(file, contents string) error { func createFile(file, contents string) error {
return ioutil.WriteFile(file, []byte(contents), testFileMode) return os.WriteFile(file, []byte(contents), testFileMode)
} }

View File

@ -9,7 +9,6 @@ package main
import ( import (
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -52,7 +51,7 @@ func (r *TestReaderData) Read(p []byte) (n int, err error) {
func TestParseLogFile(t *testing.T) { func TestParseLogFile(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
dir, err := ioutil.TempDir("", "") dir, err := os.MkdirTemp("", "")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -201,7 +200,7 @@ func TestParseLogFiles(t *testing.T) {
}, },
} }
dir, err := ioutil.TempDir("", "") dir, err := os.MkdirTemp("", "")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -7,7 +7,6 @@
package main package main
import ( import (
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -32,7 +31,7 @@ func TestUtilsResolvePathEmptyPath(t *testing.T) {
func TestUtilsResolvePathValidPath(t *testing.T) { func TestUtilsResolvePathValidPath(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
dir, err := ioutil.TempDir("", "") dir, err := os.MkdirTemp("", "")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -70,7 +69,7 @@ func TestUtilsResolvePathValidPath(t *testing.T) {
func TestUtilsResolvePathENOENT(t *testing.T) { func TestUtilsResolvePathENOENT(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
dir, err := ioutil.TempDir("", "") dir, err := os.MkdirTemp("", "")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }