Skip to content

Commit

Permalink
Replace deprecation ioutil fucntions
Browse files Browse the repository at this point in the history
Go 1.16 deprecated several `io/ioutil` functions.
* `ioutil.ReadFile` -> `os.ReadFile`
* `ioutil.ReadAll` -> `io.ReadAll`

Signed-off-by: SuperQ <superq@gmail.com>
  • Loading branch information
SuperQ committed Jun 18, 2022
1 parent 5ca7f34 commit 941c8a8
Show file tree
Hide file tree
Showing 16 changed files with 35 additions and 38 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Expand Up @@ -97,7 +97,7 @@ Many of the files are changing continuously and the data being read can in some
reads in the same file. Also, most of the files are relatively small (less than a few KBs), and system calls
to the `stat` function will often return the wrong size. Therefore, for most files it's recommended to read the
full file in a single operation using an internal utility function called `util.ReadFileNoStat`.
This function is similar to `ioutil.ReadFile`, but it avoids the system call to `stat` to get the current size of
This function is similar to `os.ReadFile`, but it avoids the system call to `stat` to get the current size of
the file.

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

The `/sys` filesystem contains many very small files which contain only a single numeric or text value. These files
can be read using an internal function called `util.SysReadFile` which is similar to `ioutil.ReadFile` but does
can be read using an internal function called `util.SysReadFile` which is similar to `os.ReadFile` but does
not bother to check the size of the file before reading.
```
data, err := util.SysReadFile("/sys/class/power_supply/BAT0/capacity")
Expand Down
4 changes: 2 additions & 2 deletions arp.go
Expand Up @@ -15,8 +15,8 @@ package procfs

import (
"fmt"
"io/ioutil"
"net"
"os"
"strconv"
"strings"
)
Expand Down Expand Up @@ -53,7 +53,7 @@ type ARPEntry struct {
// GatherARPEntries retrieves all the ARP entries, parse the relevant columns,
// and then return a slice of ARPEntry's.
func (fs FS) GatherARPEntries() ([]ARPEntry, error) {
data, err := ioutil.ReadFile(fs.proc.Path("net/arp"))
data, err := os.ReadFile(fs.proc.Path("net/arp"))
if err != nil {
return nil, fmt.Errorf("error reading arp %q: %w", fs.proc.Path("net/arp"), err)
}
Expand Down
3 changes: 1 addition & 2 deletions bcache/get.go
Expand Up @@ -16,7 +16,6 @@ package bcache
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -204,7 +203,7 @@ func (p *parser) readValue(fileName string) uint64 {
return 0
}
path := path.Join(p.currentDir, fileName)
byt, err := ioutil.ReadFile(path)
byt, err := os.ReadFile(path)
if err != nil {
if !os.IsNotExist(err) {
p.err = fmt.Errorf("failed to read: %s", path)
Expand Down
2 changes: 1 addition & 1 deletion blockdevice/stats.go
Expand Up @@ -309,7 +309,7 @@ func (fs FS) SysBlockDevices() ([]string, error) {
// and 11 if they are not available.
func (fs FS) SysBlockDeviceStat(device string) (IOStats, int, error) {
stat := IOStats{}
bytes, err := ioutil.ReadFile(fs.sys.Path(sysBlockPath, device, "stat"))
bytes, err := os.ReadFile(fs.sys.Path(sysBlockPath, device, "stat"))
if err != nil {
return stat, 0, err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/util/parse.go
Expand Up @@ -14,7 +14,7 @@
package util

import (
"io/ioutil"
"os"
"strconv"
"strings"
)
Expand Down Expand Up @@ -66,7 +66,7 @@ func ParsePInt64s(ss []string) ([]*int64, error) {

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

// ReadIntFromFile reads a file and attempts to parse a int64 from it.
func ReadIntFromFile(path string) (int64, error) {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return 0, err
}
Expand Down
7 changes: 3 additions & 4 deletions internal/util/readfile.go
Expand Up @@ -15,12 +15,11 @@ package util

import (
"io"
"io/ioutil"
"os"
)

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

reader := io.LimitReader(f, maxBufferSize)
return ioutil.ReadAll(reader)
return io.ReadAll(reader)
}
4 changes: 2 additions & 2 deletions internal/util/sysreadfile.go
Expand Up @@ -22,7 +22,7 @@ import (
"syscall"
)

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

// On some machines, hwmon drivers are broken and return EAGAIN. This causes
// Go's ioutil.ReadFile implementation to poll forever.
// Go's os.ReadFile implementation to poll forever.
//
// Since we either want to read data or bail immediately, do the simplest
// possible read using syscall directly.
Expand Down
3 changes: 1 addition & 2 deletions ipvs.go
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"strconv"
Expand Down Expand Up @@ -84,7 +83,7 @@ func parseIPVSStats(r io.Reader) (IPVSStats, error) {
stats IPVSStats
)

statContent, err := ioutil.ReadAll(r)
statContent, err := io.ReadAll(r)
if err != nil {
return IPVSStats{}, err
}
Expand Down
10 changes: 5 additions & 5 deletions iscsi/get.go
Expand Up @@ -60,7 +60,7 @@ func GetStats(iqnPath string) (*Stats, error) {

// isPathEnable checks if the file "enable" contain enable message.
func isPathEnable(path string) (bool, error) {
enableReadout, err := ioutil.ReadFile(filepath.Join(path, "enable"))
enableReadout, err := os.ReadFile(filepath.Join(path, "enable"))
if err != nil {
return false, fmt.Errorf("iscsi: isPathEnable ReadFile error %w", err)
}
Expand Down Expand Up @@ -144,7 +144,7 @@ func (fs FS) GetFileioUdev(fileioNumber string, objectName string) (*FILEIO, err
if _, err := os.Stat(udevPath); os.IsNotExist(err) {
return nil, fmt.Errorf("iscsi: GetFileioUdev: fileio_%s is missing file name", fileio.Fnumber)
}
filename, err := ioutil.ReadFile(udevPath)
filename, err := os.ReadFile(udevPath)
if err != nil {
return nil, fmt.Errorf("iscsi: GetFileioUdev: Cannot read filename from udev link %q", udevPath)
}
Expand All @@ -166,7 +166,7 @@ func (fs FS) GetIblockUdev(iblockNumber string, objectName string) (*IBLOCK, err
if _, err := os.Stat(udevPath); os.IsNotExist(err) {
return nil, fmt.Errorf("iscsi: GetIBlockUdev: iblock_%s is missing file name", iblock.Bnumber)
}
filename, err := ioutil.ReadFile(udevPath)
filename, err := os.ReadFile(udevPath)
if err != nil {
return nil, fmt.Errorf("iscsi: GetIBlockUdev: Cannot read iblock from udev link %q", udevPath)
}
Expand All @@ -193,7 +193,7 @@ func (fs FS) GetRBDMatch(rbdNumber string, poolImage string) (*RBD, error) {
if _, err := os.Stat(systemPoolPath); os.IsNotExist(err) {
continue
}
bSystemPool, err := ioutil.ReadFile(systemPoolPath)
bSystemPool, err := os.ReadFile(systemPoolPath)
if err != nil {
continue
} else {
Expand All @@ -204,7 +204,7 @@ func (fs FS) GetRBDMatch(rbdNumber string, poolImage string) (*RBD, error) {
if _, err := os.Stat(systemImagePath); os.IsNotExist(err) {
continue
}
bSystemImage, err := ioutil.ReadFile(systemImagePath)
bSystemImage, err := os.ReadFile(systemImagePath)
if err != nil {
continue
} else {
Expand Down
4 changes: 2 additions & 2 deletions mdstat.go
Expand Up @@ -15,7 +15,7 @@ package procfs

import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -64,7 +64,7 @@ type MDStat struct {
// structs containing the relevant info. More information available here:
// https://raid.wiki.kernel.org/index.php/Mdstat
func (fs FS) MDStat() ([]MDStat, error) {
data, err := ioutil.ReadFile(fs.proc.Path("mdstat"))
data, err := os.ReadFile(fs.proc.Path("mdstat"))
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions proc.go
Expand Up @@ -16,7 +16,7 @@ package procfs
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -142,7 +142,7 @@ func (p Proc) Wchan() (string, error) {
}
defer f.Close()

data, err := ioutil.ReadAll(f)
data, err := io.ReadAll(f)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -311,7 +311,7 @@ func (p Proc) FileDescriptorsInfo() (ProcFDInfos, error) {

// Schedstat returns task scheduling information for the process.
func (p Proc) Schedstat() (ProcSchedstat, error) {
contents, err := ioutil.ReadFile(p.path("schedstat"))
contents, err := os.ReadFile(p.path("schedstat"))
if err != nil {
return ProcSchedstat{}, err
}
Expand Down
6 changes: 3 additions & 3 deletions sysfs/class_infiniband.go
Expand Up @@ -205,7 +205,7 @@ func (fs FS) parseInfiniBandPort(name string, port string) (*InfiniBandPort, err
ibp := InfiniBandPort{Name: name, Port: uint(portNumber)}

portPath := fs.sys.Path(infinibandClassPath, name, "ports", port)
content, err := ioutil.ReadFile(filepath.Join(portPath, "state"))
content, err := os.ReadFile(filepath.Join(portPath, "state"))
if err != nil {
return nil, err
}
Expand All @@ -216,7 +216,7 @@ func (fs FS) parseInfiniBandPort(name string, port string) (*InfiniBandPort, err
ibp.State = name
ibp.StateID = id

content, err = ioutil.ReadFile(filepath.Join(portPath, "phys_state"))
content, err = os.ReadFile(filepath.Join(portPath, "phys_state"))
if err != nil {
return nil, err
}
Expand All @@ -227,7 +227,7 @@ func (fs FS) parseInfiniBandPort(name string, port string) (*InfiniBandPort, err
ibp.PhysState = name
ibp.PhysStateID = id

content, err = ioutil.ReadFile(filepath.Join(portPath, "rate"))
content, err = os.ReadFile(filepath.Join(portPath, "rate"))
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion sysfs/class_powercap.go
Expand Up @@ -19,6 +19,7 @@ package sysfs
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -53,7 +54,7 @@ func GetRaplZones(fs FS) ([]RaplZone, error) {
// Loop through directory files searching for file "name" from subdirs.
for _, f := range files {
nameFile := filepath.Join(raplDir, f.Name(), "/name")
nameBytes, err := ioutil.ReadFile(nameFile)
nameBytes, err := os.ReadFile(nameFile)
if err == nil {
// Add new rapl zone since name file was found.
name := strings.TrimSpace(string(nameBytes))
Expand Down
3 changes: 1 addition & 2 deletions sysfs/system_cpu.go
Expand Up @@ -18,7 +18,6 @@ package sysfs

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -243,7 +242,7 @@ func parseCpufreqCpuinfo(cpuPath string) (*SystemCPUCpufreqStats, error) {
}

func (fs FS) IsolatedCPUs() ([]uint16, error) {
isolcpus, err := ioutil.ReadFile(fs.sys.Path("devices/system/cpu/isolated"))
isolcpus, err := os.ReadFile(fs.sys.Path("devices/system/cpu/isolated"))
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions sysfs/vulnerability.go
Expand Up @@ -18,7 +18,7 @@ package sysfs

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
Expand All @@ -40,7 +40,7 @@ func (fs FS) CPUVulnerabilities() ([]Vulnerability, error) {
for _, match := range matches {
name := filepath.Base(match)

value, err := ioutil.ReadFile(match)
value, err := os.ReadFile(match)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions zoneinfo.go
Expand Up @@ -19,7 +19,7 @@ package procfs
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"

Expand Down Expand Up @@ -73,7 +73,7 @@ var nodeZoneRE = regexp.MustCompile(`(\d+), zone\s+(\w+)`)
// structs containing the relevant info. More information available here:
// https://www.kernel.org/doc/Documentation/sysctl/vm.txt
func (fs FS) Zoneinfo() ([]Zoneinfo, error) {
data, err := ioutil.ReadFile(fs.proc.Path("zoneinfo"))
data, err := os.ReadFile(fs.proc.Path("zoneinfo"))
if err != nil {
return nil, fmt.Errorf("error reading zoneinfo %q: %w", fs.proc.Path("zoneinfo"), err)
}
Expand Down

0 comments on commit 941c8a8

Please sign in to comment.