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 (
"fmt"
"io/ioutil"
"os"
containerdshim "github.com/kata-containers/kata-containers/src/runtime/pkg/containerd-shim-v2"
"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
buf, err := ioutil.ReadFile(iptablesFile)
buf, err := os.ReadFile(iptablesFile)
if err != nil {
return err
}

View File

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

View File

@ -10,7 +10,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
@ -75,7 +74,7 @@ func Add(volumePath string, mountInfo string) error {
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.
@ -89,7 +88,7 @@ func VolumeMountInfo(volumePath string) (*MountInfo, error) {
if _, err := os.Stat(mountInfoFilePath); err != nil {
return nil, err
}
buf, err := ioutil.ReadFile(mountInfoFilePath)
buf, err := os.ReadFile(mountInfoFilePath)
if err != nil {
return nil, err
}
@ -108,11 +107,11 @@ func RecordSandboxId(sandboxId string, volumePath string) error {
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) {
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 {
return "", err
}

View File

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

View File

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

View File

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

View File

@ -9,7 +9,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"time"
@ -91,7 +90,7 @@ func DoPut(sandboxID string, timeoutInSeconds time.Duration, urlPath, contentTyp
}()
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))
}
@ -117,7 +116,7 @@ func DoPost(sandboxID string, timeoutInSeconds time.Duration, urlPath, contentTy
}()
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))
}

View File

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

View File

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

View File

@ -9,7 +9,6 @@ import (
"bufio"
"context"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
@ -1168,7 +1167,7 @@ func TestHandleHugepages(t *testing.T) {
// 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.
if runtime.GOARCH == "s390x" {
dirs, err := ioutil.ReadDir(sysHugepagesDir)
dirs, err := os.ReadDir(sysHugepagesDir)
assert.Nil(err)
for _, dir := range dirs {
formattedSizes = append(formattedSizes, strings.TrimPrefix(dir.Name(), "hugepages-"))

View File

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

View File

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

View File

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

View File

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

View File

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