Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4e1320

Browse files
committed
Remove 99% of deprecated ioutil usage
Signed-off-by: David Gageot <[email protected]>
1 parent 8ef4fa3 commit d4e1320

21 files changed

Lines changed: 78 additions & 89 deletions

pkg/binfmt/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"flag"
66
"fmt"
7-
"io/ioutil"
87
"log"
98
"os"
109
"path/filepath"
@@ -74,13 +73,13 @@ func main() {
7473
}
7574
defer syscall.Unmount(mount, 0)
7675

77-
files, err := ioutil.ReadDir(dir)
76+
files, err := os.ReadDir(dir)
7877
if err != nil {
7978
log.Fatalf("Cannot read directory %s: %s", dir, err)
8079
}
8180

8281
for _, file := range files {
83-
contents, err := ioutil.ReadFile(filepath.Join(dir, file.Name()))
82+
contents, err := os.ReadFile(filepath.Join(dir, file.Name()))
8483
if err != nil {
8584
log.Fatalf("Cannot read file %s: %s", file.Name(), err)
8685
}

pkg/init/cmd/rc.init/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"bufio"
55
"encoding/csv"
6-
"io/ioutil"
76
"log"
87
"os"
98
"os/exec"
@@ -109,15 +108,15 @@ func cgroupList() []string {
109108

110109
// write a file, eg sysfs
111110
func write(path string, value string) {
112-
err := ioutil.WriteFile(path, []byte(value), 0600)
111+
err := os.WriteFile(path, []byte(value), 0600)
113112
if err != nil {
114113
log.Printf("cannot write to %s: %v", path, err)
115114
}
116115
}
117116

118117
// read a file, eg sysfs, strip whitespace, empty string if does not exist
119118
func read(path string) string {
120-
data, err := ioutil.ReadFile(path)
119+
data, err := os.ReadFile(path)
121120
if err != nil {
122121
return ""
123122
}
@@ -127,7 +126,7 @@ func read(path string) string {
127126
// read a directory
128127
func readdir(path string) []string {
129128
names := []string{}
130-
files, err := ioutil.ReadDir(path)
129+
files, err := os.ReadDir(path)
131130
if err != nil {
132131
log.Printf("cannot read directory %s: %v", path, err)
133132
return names

pkg/init/cmd/service/prepare.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"path/filepath"
98
"strings"
@@ -47,7 +46,7 @@ type Interface struct {
4746

4847
func getRuntimeConfig(path string) Runtime {
4948
var runtime Runtime
50-
conf, err := ioutil.ReadFile(filepath.Join(path, "runtime.json"))
49+
conf, err := os.ReadFile(filepath.Join(path, "runtime.json"))
5150
if err != nil {
5251
// if it does not exist it is fine to return an empty runtime, to not do anything
5352
if os.IsNotExist(err) {
@@ -134,7 +133,7 @@ func newCgroup(cgroup string) error {
134133
return nil
135134
}
136135
// a cgroupv1 cgroup is a directory under all directories in /sys/fs/cgroup
137-
dirs, err := ioutil.ReadDir("/sys/fs/cgroup")
136+
dirs, err := os.ReadDir("/sys/fs/cgroup")
138137
if err != nil {
139138
return err
140139
}

pkg/init/cmd/service/runc.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"io"
5-
"io/ioutil"
65
"os"
76
"os/exec"
87
"path"
@@ -38,12 +37,12 @@ func runcInit(rootPath, serviceType string) int {
3837
}
3938

4039
// get files; note ReadDir already sorts them
41-
files, err := ioutil.ReadDir(rootPath)
40+
files, err := os.ReadDir(rootPath)
4241
if err != nil {
4342
log.Fatalf("Cannot read files in %s: %v", rootPath, err)
4443
}
4544

46-
tmpdir, err := ioutil.TempDir("", filepath.Base(rootPath))
45+
tmpdir, err := os.MkdirTemp("", filepath.Base(rootPath))
4746
if err != nil {
4847
log.Fatalf("Cannot create temporary directory: %v", err)
4948
}
@@ -105,7 +104,7 @@ func runcInit(rootPath, serviceType string) int {
105104
// skip cleanup on error for debug
106105
continue
107106
}
108-
pf, err := ioutil.ReadFile(pidfile)
107+
pf, err := os.ReadFile(pidfile)
109108
if err != nil {
110109
log.Printf("Cannot read pidfile: %v", err)
111110
status = 1

pkg/init/cmd/service/system_init.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"flag"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"os"
109
"os/exec"
1110
"path/filepath"
@@ -91,7 +90,7 @@ func systemInitCmd(ctx context.Context, args []string) {
9190
stderr io.Writer = os.Stderr
9291
stdout io.Writer = os.Stdout
9392
)
94-
if b, err := ioutil.ReadFile(containerdOptsFile); err == nil {
93+
if b, err := os.ReadFile(containerdOptsFile); err == nil {
9594
config, err := toml.LoadBytes(b)
9695
if err != nil {
9796
log.Fatalf("error reading toml file %s: %v", containerdOptsFile, err)
@@ -173,7 +172,7 @@ func systemInitCmd(ctx context.Context, args []string) {
173172
}
174173

175174
// Start up containers
176-
files, err := ioutil.ReadDir(*path)
175+
files, err := os.ReadDir(*path)
177176
// just skip if there is an error, eg no such path
178177
if err != nil {
179178
return

pkg/metadata/main.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"encoding/json"
55
"flag"
6-
"io/ioutil"
76
"os"
87
"path"
98
"strconv"
@@ -160,7 +159,7 @@ func main() {
160159
log.Printf("Error during metadata probe: %s", err)
161160
}
162161

163-
err = ioutil.WriteFile(path.Join(ConfigPath, "provider"), []byte(p.String()), 0644)
162+
err = os.WriteFile(path.Join(ConfigPath, "provider"), []byte(p.String()), 0644)
164163
if err != nil {
165164
log.Printf("Error writing metadata provider: %s", err)
166165
}
@@ -173,7 +172,7 @@ func main() {
173172

174173
// Handle setting the hostname as a special case. We want to
175174
// do this early and don't really want another container for it.
176-
hostname, err := ioutil.ReadFile(path.Join(ConfigPath, Hostname))
175+
hostname, err := os.ReadFile(path.Join(ConfigPath, Hostname))
177176
if err == nil {
178177
err := syscall.Sethostname(hostname)
179178
if err != nil {
@@ -196,7 +195,7 @@ func main() {
196195
// Will create foobar/foo with mode 0644 and content "hello"
197196
func processUserData(basePath string, data []byte) error {
198197
// Always write the raw data to a file
199-
err := ioutil.WriteFile(path.Join(basePath, "userdata"), data, 0644)
198+
err := os.WriteFile(path.Join(basePath, "userdata"), data, 0644)
200199
if err != nil {
201200
log.Printf("Could not write userdata: %s", err)
202201
return err
@@ -223,7 +222,7 @@ func writeConfigFiles(target string, current Entry) {
223222
log.Printf("Failed to parse permission %+v: %s", current, err)
224223
return
225224
}
226-
if err := ioutil.WriteFile(target, []byte(*current.Content), filemode); err != nil {
225+
if err := os.WriteFile(target, []byte(*current.Content), filemode); err != nil {
227226
log.Printf("Failed to write %s: %s", target, err)
228227
return
229228
}

pkg/metadata/main_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ package main
33
import (
44
"bytes"
55
"encoding/json"
6-
"io/ioutil"
76
"os"
87
"path"
98
"testing"
109
)
1110

1211
func TestSampleConfig(t *testing.T) {
13-
basePath, err := ioutil.TempDir("", "metadata")
12+
basePath, err := os.MkdirTemp("", "metadata")
1413
if err != nil {
1514
t.Fatalf("can't make a temp rootdir %v", err)
1615
}
@@ -82,7 +81,7 @@ func TestSerialization(t *testing.T) {
8281
}
8382

8483
func TestWriteSingleFile(t *testing.T) {
85-
basePath, err := ioutil.TempDir(os.TempDir(), "metadata")
84+
basePath, err := os.MkdirTemp(os.TempDir(), "metadata")
8685
if err != nil {
8786
t.Fatalf("can't make a temp rootdir %v", err)
8887
}
@@ -98,7 +97,7 @@ func TestWriteSingleFile(t *testing.T) {
9897
}
9998

10099
func TestWriteEmptyFile(t *testing.T) {
101-
basePath, err := ioutil.TempDir(os.TempDir(), "metadata")
100+
basePath, err := os.MkdirTemp(os.TempDir(), "metadata")
102101
if err != nil {
103102
t.Fatalf("can't make a temp rootdir %v", err)
104103
}
@@ -114,7 +113,7 @@ func TestWriteEmptyFile(t *testing.T) {
114113
}
115114

116115
func TestWriteEmptyDirectory(t *testing.T) {
117-
basePath, err := ioutil.TempDir(os.TempDir(), "metadata")
116+
basePath, err := os.MkdirTemp(os.TempDir(), "metadata")
118117
if err != nil {
119118
t.Fatalf("can't make a temp rootdir %v", err)
120119
}
@@ -132,7 +131,7 @@ func TestWriteEmptyDirectory(t *testing.T) {
132131
}
133132

134133
func TestSetPermission(t *testing.T) {
135-
basePath, err := ioutil.TempDir(os.TempDir(), "metadata")
134+
basePath, err := os.MkdirTemp(os.TempDir(), "metadata")
136135
if err != nil {
137136
t.Fatalf("can't make a temp rootdir %v", err)
138137
}
@@ -155,7 +154,7 @@ func TestSetPermission(t *testing.T) {
155154
}
156155

157156
func TestDeepTree(t *testing.T) {
158-
basePath, err := ioutil.TempDir("", "metadata")
157+
basePath, err := os.MkdirTemp("", "metadata")
159158
if err != nil {
160159
t.Fatalf("can't make a temp rootdir %v", err)
161160
}
@@ -207,7 +206,7 @@ func assertPermission(t *testing.T, path string, expected os.FileMode) {
207206
}
208207

209208
func assertContent(t *testing.T, path, expected string) {
210-
file, err := ioutil.ReadFile(path)
209+
file, err := os.ReadFile(path)
211210
if err != nil {
212211
t.Fatalf("can't read %v: %v", path, err)
213212
}

pkg/metadata/provider_aws.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io"
66
"log"
77
"net/http"
88
"os"
@@ -37,7 +37,7 @@ func (p *ProviderAWS) Extract() ([]byte, error) {
3737
if err != nil {
3838
return nil, err
3939
}
40-
err = ioutil.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644)
40+
err = os.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644)
4141
if err != nil {
4242
return nil, fmt.Errorf("AWS: Failed to write hostname: %s", err)
4343
}
@@ -79,7 +79,7 @@ func (p *ProviderAWS) Extract() ([]byte, error) {
7979
func awsMetaGet(lookupName string, fileName string, fileMode os.FileMode) {
8080
if lookupValue, err := awsGet(metaDataURL + lookupName); err == nil {
8181
// we got a value from the metadata server, now save to filesystem
82-
err = ioutil.WriteFile(path.Join(ConfigPath, fileName), lookupValue, fileMode)
82+
err = os.WriteFile(path.Join(ConfigPath, fileName), lookupValue, fileMode)
8383
if err != nil {
8484
// we couldn't save the file for some reason
8585
log.Printf("AWS: Failed to write %s:%s %s", fileName, lookupValue, err)
@@ -108,7 +108,7 @@ func awsGet(url string) ([]byte, error) {
108108
if resp.StatusCode != 200 {
109109
return nil, fmt.Errorf("AWS: Status not ok: %d", resp.StatusCode)
110110
}
111-
body, err := ioutil.ReadAll(resp.Body)
111+
body, err := io.ReadAll(resp.Body)
112112
if err != nil {
113113
return nil, fmt.Errorf("AWS: Failed to read http response: %s", err)
114114
}
@@ -126,7 +126,7 @@ func (p *ProviderAWS) handleSSH() error {
126126
return fmt.Errorf("Failed to create %s: %s", SSH, err)
127127
}
128128

129-
err = ioutil.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), sshKeys, 0600)
129+
err = os.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), sshKeys, 0600)
130130
if err != nil {
131131
return fmt.Errorf("Failed to write ssh keys: %s", err)
132132
}

pkg/metadata/provider_cdrom.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"os"
66
"path"
77
"path/filepath"
88
"strings"
@@ -100,14 +100,14 @@ func FindCIs() []string {
100100

101101
// NewCDROM returns a new ProviderCDROM
102102
func NewCDROM(device string) *ProviderCDROM {
103-
mountPoint, err := ioutil.TempDir("", "cd")
103+
mountPoint, err := os.MkdirTemp("", "cd")
104104
p := ProviderCDROM{device, mountPoint, err, []byte{}, []byte{}}
105105
if err == nil {
106106
if p.err = p.mount(); p.err == nil {
107107
// read the userdata - we read the spec file and the fallback, but eventually
108108
// will remove the fallback
109109
for _, f := range userdataFiles {
110-
userdata, err := ioutil.ReadFile(path.Join(p.mountPoint, f))
110+
userdata, err := os.ReadFile(path.Join(p.mountPoint, f))
111111
// did we find a file?
112112
if err == nil && userdata != nil {
113113
p.userdata = userdata
@@ -118,7 +118,7 @@ func NewCDROM(device string) *ProviderCDROM {
118118
p.err = fmt.Errorf("no userdata file found at any of %v", userdataFiles)
119119
}
120120
// read the metadata
121-
metadata, err := ioutil.ReadFile(path.Join(p.mountPoint, metadataFile))
121+
metadata, err := os.ReadFile(path.Join(p.mountPoint, metadataFile))
122122
// did we find a file?
123123
if err == nil && metadata != nil {
124124
p.metadata = metadata

pkg/metadata/provider_digitalocean.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io"
66
"log"
77
"net/http"
88
"os"
@@ -41,7 +41,7 @@ func (p *ProviderDigitalOcean) Extract() ([]byte, error) {
4141
if err != nil {
4242
return nil, err
4343
}
44-
err = ioutil.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644)
44+
err = os.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644)
4545
if err != nil {
4646
return nil, fmt.Errorf("DigitalOcean: Failed to write hostname: %s", err)
4747
}
@@ -77,7 +77,7 @@ func (p *ProviderDigitalOcean) Extract() ([]byte, error) {
7777
func digitalOceanMetaGet(lookupName string, fileName string, fileMode os.FileMode) {
7878
if lookupValue, err := digitalOceanGet(digitalOceanMetaDataURL + lookupName); err == nil {
7979
// we got a value from the metadata server, now save to filesystem
80-
err = ioutil.WriteFile(path.Join(ConfigPath, fileName), lookupValue, fileMode)
80+
err = os.WriteFile(path.Join(ConfigPath, fileName), lookupValue, fileMode)
8181
if err != nil {
8282
// we couldn't save the file for some reason
8383
log.Printf("DigitalOcean: Failed to write %s:%s %s", fileName, lookupValue, err)
@@ -106,7 +106,7 @@ func digitalOceanGet(url string) ([]byte, error) {
106106
if resp.StatusCode != 200 {
107107
return nil, fmt.Errorf("DigitalOcean: Status not ok: %d", resp.StatusCode)
108108
}
109-
body, err := ioutil.ReadAll(resp.Body)
109+
body, err := io.ReadAll(resp.Body)
110110
if err != nil {
111111
return nil, fmt.Errorf("DigitalOcean: Failed to read http response: %s", err)
112112
}
@@ -124,7 +124,7 @@ func (p *ProviderDigitalOcean) handleSSH() error {
124124
return fmt.Errorf("Failed to create %s: %s", SSH, err)
125125
}
126126

127-
err = ioutil.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), sshKeys, 0600)
127+
err = os.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), sshKeys, 0600)
128128
if err != nil {
129129
return fmt.Errorf("Failed to write ssh keys: %s", err)
130130
}

0 commit comments

Comments
 (0)