Skip to content

Commit 4ba3093

Browse files
authored
Replace deprecation ioutil fucntions (#456)
Go 1.16 deprecated several `io/ioutil` functions. * `ioutil.ReadFile` -> `os.ReadFile` * `ioutil.ReadAll` -> `io.ReadAll` * `ioutil.ReadDir` -> `os.ReadDir` Signed-off-by: SuperQ <[email protected]>
1 parent 5ca7f34 commit 4ba3093

24 files changed

+64
-77
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Many of the files are changing continuously and the data being read can in some
9797
reads in the same file. Also, most of the files are relatively small (less than a few KBs), and system calls
9898
to the `stat` function will often return the wrong size. Therefore, for most files it's recommended to read the
9999
full file in a single operation using an internal utility function called `util.ReadFileNoStat`.
100-
This function is similar to `ioutil.ReadFile`, but it avoids the system call to `stat` to get the current size of
100+
This function is similar to `os.ReadFile`, but it avoids the system call to `stat` to get the current size of
101101
the file.
102102

103103
Note that parsing the file's contents can still be performed one line at a time. This is done by first reading
@@ -113,7 +113,7 @@ the full file, and then using a scanner on the `[]byte` or `string` containing t
113113
```
114114

115115
The `/sys` filesystem contains many very small files which contain only a single numeric or text value. These files
116-
can be read using an internal function called `util.SysReadFile` which is similar to `ioutil.ReadFile` but does
116+
can be read using an internal function called `util.SysReadFile` which is similar to `os.ReadFile` but does
117117
not bother to check the size of the file before reading.
118118
```
119119
data, err := util.SysReadFile("/sys/class/power_supply/BAT0/capacity")

arp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ package procfs
1515

1616
import (
1717
"fmt"
18-
"io/ioutil"
1918
"net"
19+
"os"
2020
"strconv"
2121
"strings"
2222
)
@@ -53,7 +53,7 @@ type ARPEntry struct {
5353
// GatherARPEntries retrieves all the ARP entries, parse the relevant columns,
5454
// and then return a slice of ARPEntry's.
5555
func (fs FS) GatherARPEntries() ([]ARPEntry, error) {
56-
data, err := ioutil.ReadFile(fs.proc.Path("net/arp"))
56+
data, err := os.ReadFile(fs.proc.Path("net/arp"))
5757
if err != nil {
5858
return nil, fmt.Errorf("error reading arp %q: %w", fs.proc.Path("net/arp"), err)
5959
}

bcache/get.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package bcache
1616
import (
1717
"bufio"
1818
"fmt"
19-
"io/ioutil"
2019
"os"
2120
"path"
2221
"path/filepath"
@@ -204,7 +203,7 @@ func (p *parser) readValue(fileName string) uint64 {
204203
return 0
205204
}
206205
path := path.Join(p.currentDir, fileName)
207-
byt, err := ioutil.ReadFile(path)
206+
byt, err := os.ReadFile(path)
208207
if err != nil {
209208
if !os.IsNotExist(err) {
210209
p.err = fmt.Errorf("failed to read: %s", path)

blockdevice/stats.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"bufio"
1818
"fmt"
1919
"io"
20-
"io/ioutil"
2120
"os"
2221
"strings"
2322

@@ -293,7 +292,7 @@ func (fs FS) ProcDiskstats() ([]Diskstats, error) {
293292

294293
// SysBlockDevices lists the device names from /sys/block/<dev>.
295294
func (fs FS) SysBlockDevices() ([]string, error) {
296-
deviceDirs, err := ioutil.ReadDir(fs.sys.Path(sysBlockPath))
295+
deviceDirs, err := os.ReadDir(fs.sys.Path(sysBlockPath))
297296
if err != nil {
298297
return nil, err
299298
}
@@ -309,7 +308,7 @@ func (fs FS) SysBlockDevices() ([]string, error) {
309308
// and 11 if they are not available.
310309
func (fs FS) SysBlockDeviceStat(device string) (IOStats, int, error) {
311310
stat := IOStats{}
312-
bytes, err := ioutil.ReadFile(fs.sys.Path(sysBlockPath, device, "stat"))
311+
bytes, err := os.ReadFile(fs.sys.Path(sysBlockPath, device, "stat"))
313312
if err != nil {
314313
return stat, 0, err
315314
}

btrfs/get.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package btrfs
1515

1616
import (
17-
"io/ioutil"
1817
"os"
1918
"path"
2019
"path/filepath"
@@ -122,7 +121,7 @@ func (r *reader) readValue(n string) (v uint64) {
122121

123122
// listFiles returns a list of files for a directory of the reader.
124123
func (r *reader) listFiles(p string) []string {
125-
files, err := ioutil.ReadDir(path.Join(r.path, p))
124+
files, err := os.ReadDir(path.Join(r.path, p))
126125
if err != nil {
127126
r.err = err
128127
return nil
@@ -164,7 +163,7 @@ func (r *reader) readAllocationStats(d string) (a *AllocationStats) {
164163

165164
// readLayouts reads all Btrfs layout statistics for the current path.
166165
func (r *reader) readLayouts() map[string]*LayoutUsage {
167-
files, err := ioutil.ReadDir(r.path)
166+
files, err := os.ReadDir(r.path)
168167
if err != nil {
169168
r.err = err
170169
return nil

internal/util/parse.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
package util
1515

1616
import (
17-
"io/ioutil"
17+
"os"
1818
"strconv"
1919
"strings"
2020
)
@@ -66,7 +66,7 @@ func ParsePInt64s(ss []string) ([]*int64, error) {
6666

6767
// ReadUintFromFile reads a file and attempts to parse a uint64 from it.
6868
func ReadUintFromFile(path string) (uint64, error) {
69-
data, err := ioutil.ReadFile(path)
69+
data, err := os.ReadFile(path)
7070
if err != nil {
7171
return 0, err
7272
}
@@ -75,7 +75,7 @@ func ReadUintFromFile(path string) (uint64, error) {
7575

7676
// ReadIntFromFile reads a file and attempts to parse a int64 from it.
7777
func ReadIntFromFile(path string) (int64, error) {
78-
data, err := ioutil.ReadFile(path)
78+
data, err := os.ReadFile(path)
7979
if err != nil {
8080
return 0, err
8181
}

internal/util/readfile.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ package util
1515

1616
import (
1717
"io"
18-
"io/ioutil"
1918
"os"
2019
)
2120

22-
// ReadFileNoStat uses ioutil.ReadAll to read contents of entire file.
23-
// This is similar to ioutil.ReadFile but without the call to os.Stat, because
21+
// ReadFileNoStat uses io.ReadAll to read contents of entire file.
22+
// This is similar to os.ReadFile but without the call to os.Stat, because
2423
// many files in /proc and /sys report incorrect file sizes (either 0 or 4096).
2524
// Reads a max file size of 512kB. For files larger than this, a scanner
2625
// should be used.
@@ -34,5 +33,5 @@ func ReadFileNoStat(filename string) ([]byte, error) {
3433
defer f.Close()
3534

3635
reader := io.LimitReader(f, maxBufferSize)
37-
return ioutil.ReadAll(reader)
36+
return io.ReadAll(reader)
3837
}

internal/util/sysreadfile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"syscall"
2323
)
2424

25-
// SysReadFile is a simplified ioutil.ReadFile that invokes syscall.Read directly.
25+
// SysReadFile is a simplified os.ReadFile that invokes syscall.Read directly.
2626
// https://github.com/prometheus/node_exporter/pull/728/files
2727
//
2828
// Note that this function will not read files larger than 128 bytes.
@@ -34,7 +34,7 @@ func SysReadFile(file string) (string, error) {
3434
defer f.Close()
3535

3636
// On some machines, hwmon drivers are broken and return EAGAIN. This causes
37-
// Go's ioutil.ReadFile implementation to poll forever.
37+
// Go's os.ReadFile implementation to poll forever.
3838
//
3939
// Since we either want to read data or bail immediately, do the simplest
4040
// possible read using syscall directly.

ipvs.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"errors"
2121
"fmt"
2222
"io"
23-
"io/ioutil"
2423
"net"
2524
"os"
2625
"strconv"
@@ -84,7 +83,7 @@ func parseIPVSStats(r io.Reader) (IPVSStats, error) {
8483
stats IPVSStats
8584
)
8685

87-
statContent, err := ioutil.ReadAll(r)
86+
statContent, err := io.ReadAll(r)
8887
if err != nil {
8988
return IPVSStats{}, err
9089
}

iscsi/get.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package iscsi
1717
import (
1818
"errors"
1919
"fmt"
20-
"io/ioutil"
2120
"os"
2221
"path/filepath"
2322
"strconv"
@@ -60,7 +59,7 @@ func GetStats(iqnPath string) (*Stats, error) {
6059

6160
// isPathEnable checks if the file "enable" contain enable message.
6261
func isPathEnable(path string) (bool, error) {
63-
enableReadout, err := ioutil.ReadFile(filepath.Join(path, "enable"))
62+
enableReadout, err := os.ReadFile(filepath.Join(path, "enable"))
6463
if err != nil {
6564
return false, fmt.Errorf("iscsi: isPathEnable ReadFile error %w", err)
6665
}
@@ -74,7 +73,7 @@ func isPathEnable(path string) (bool, error) {
7473
func getLunLinkTarget(lunPath string) (lunObject LUN, err error) {
7574
lunObject.Name = filepath.Base(lunPath)
7675
lunObject.LunPath = lunPath
77-
files, err := ioutil.ReadDir(lunPath)
76+
files, err := os.ReadDir(lunPath)
7877
if err != nil {
7978
return lunObject, fmt.Errorf("getLunLinkTarget: ReadDir path %q: %w", lunPath, err)
8079
}
@@ -144,7 +143,7 @@ func (fs FS) GetFileioUdev(fileioNumber string, objectName string) (*FILEIO, err
144143
if _, err := os.Stat(udevPath); os.IsNotExist(err) {
145144
return nil, fmt.Errorf("iscsi: GetFileioUdev: fileio_%s is missing file name", fileio.Fnumber)
146145
}
147-
filename, err := ioutil.ReadFile(udevPath)
146+
filename, err := os.ReadFile(udevPath)
148147
if err != nil {
149148
return nil, fmt.Errorf("iscsi: GetFileioUdev: Cannot read filename from udev link %q", udevPath)
150149
}
@@ -166,7 +165,7 @@ func (fs FS) GetIblockUdev(iblockNumber string, objectName string) (*IBLOCK, err
166165
if _, err := os.Stat(udevPath); os.IsNotExist(err) {
167166
return nil, fmt.Errorf("iscsi: GetIBlockUdev: iblock_%s is missing file name", iblock.Bnumber)
168167
}
169-
filename, err := ioutil.ReadFile(udevPath)
168+
filename, err := os.ReadFile(udevPath)
170169
if err != nil {
171170
return nil, fmt.Errorf("iscsi: GetIBlockUdev: Cannot read iblock from udev link %q", udevPath)
172171
}
@@ -193,7 +192,7 @@ func (fs FS) GetRBDMatch(rbdNumber string, poolImage string) (*RBD, error) {
193192
if _, err := os.Stat(systemPoolPath); os.IsNotExist(err) {
194193
continue
195194
}
196-
bSystemPool, err := ioutil.ReadFile(systemPoolPath)
195+
bSystemPool, err := os.ReadFile(systemPoolPath)
197196
if err != nil {
198197
continue
199198
} else {
@@ -204,7 +203,7 @@ func (fs FS) GetRBDMatch(rbdNumber string, poolImage string) (*RBD, error) {
204203
if _, err := os.Stat(systemImagePath); os.IsNotExist(err) {
205204
continue
206205
}
207-
bSystemImage, err := ioutil.ReadFile(systemImagePath)
206+
bSystemImage, err := os.ReadFile(systemImagePath)
208207
if err != nil {
209208
continue
210209
} else {

mdstat.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ package procfs
1515

1616
import (
1717
"fmt"
18-
"io/ioutil"
18+
"os"
1919
"regexp"
2020
"strconv"
2121
"strings"
@@ -64,7 +64,7 @@ type MDStat struct {
6464
// structs containing the relevant info. More information available here:
6565
// https://raid.wiki.kernel.org/index.php/Mdstat
6666
func (fs FS) MDStat() ([]MDStat, error) {
67-
data, err := ioutil.ReadFile(fs.proc.Path("mdstat"))
67+
data, err := os.ReadFile(fs.proc.Path("mdstat"))
6868
if err != nil {
6969
return nil, err
7070
}

proc.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package procfs
1616
import (
1717
"bytes"
1818
"fmt"
19-
"io/ioutil"
19+
"io"
2020
"os"
2121
"strconv"
2222
"strings"
@@ -142,7 +142,7 @@ func (p Proc) Wchan() (string, error) {
142142
}
143143
defer f.Close()
144144

145-
data, err := ioutil.ReadAll(f)
145+
data, err := io.ReadAll(f)
146146
if err != nil {
147147
return "", err
148148
}
@@ -311,7 +311,7 @@ func (p Proc) FileDescriptorsInfo() (ProcFDInfos, error) {
311311

312312
// Schedstat returns task scheduling information for the process.
313313
func (p Proc) Schedstat() (ProcSchedstat, error) {
314-
contents, err := ioutil.ReadFile(p.path("schedstat"))
314+
contents, err := os.ReadFile(p.path("schedstat"))
315315
if err != nil {
316316
return ProcSchedstat{}, err
317317
}

sysfs/class_dmi.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package sysfs
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
2221
"os"
2322
"path/filepath"
2423

@@ -56,14 +55,14 @@ type DMIClass struct {
5655
func (fs FS) DMIClass() (*DMIClass, error) {
5756
path := fs.sys.Path(dmiClassPath)
5857

59-
files, err := ioutil.ReadDir(path)
58+
files, err := os.ReadDir(path)
6059
if err != nil {
6160
return nil, fmt.Errorf("failed to read directory %q: %w", path, err)
6261
}
6362

6463
var dmi DMIClass
6564
for _, f := range files {
66-
if !f.Mode().IsRegular() {
65+
if !f.Type().IsRegular() {
6766
continue
6867
}
6968

sysfs/class_fibrechannel.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package sysfs
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
2221
"os"
2322
"path/filepath"
2423

@@ -66,7 +65,7 @@ type FibreChannelClass map[string]FibreChannelHost
6665
func (fs FS) FibreChannelClass() (FibreChannelClass, error) {
6766
path := fs.sys.Path(fibrechannelClassPath)
6867

69-
dirs, err := ioutil.ReadDir(path)
68+
dirs, err := os.ReadDir(path)
7069
if err != nil {
7170
return nil, err
7271
}
@@ -148,13 +147,13 @@ func parseFibreChannelStatistics(hostPath string) (*FibreChannelCounters, error)
148147
var counters FibreChannelCounters
149148

150149
path := filepath.Join(hostPath, "statistics")
151-
files, err := ioutil.ReadDir(path)
150+
files, err := os.ReadDir(path)
152151
if err != nil {
153152
return nil, err
154153
}
155154

156155
for _, f := range files {
157-
if !f.Mode().IsRegular() || f.Name() == "reset_statistics" {
156+
if !f.Type().IsRegular() || f.Name() == "reset_statistics" {
158157
continue
159158
}
160159

0 commit comments

Comments
 (0)