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`
* `ioutil.ReadDir` -> `os.ReadDir`

Signed-off-by: SuperQ <superq@gmail.com>
  • Loading branch information
SuperQ committed Jun 18, 2022
1 parent 5ca7f34 commit fc47a74
Show file tree
Hide file tree
Showing 24 changed files with 64 additions and 77 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
5 changes: 2 additions & 3 deletions blockdevice/stats.go
Expand Up @@ -17,7 +17,6 @@ import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"strings"

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

// SysBlockDevices lists the device names from /sys/block/<dev>.
func (fs FS) SysBlockDevices() ([]string, error) {
deviceDirs, err := ioutil.ReadDir(fs.sys.Path(sysBlockPath))
deviceDirs, err := os.ReadDir(fs.sys.Path(sysBlockPath))
if err != nil {
return nil, err
}
Expand All @@ -309,7 +308,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
5 changes: 2 additions & 3 deletions btrfs/get.go
Expand Up @@ -14,7 +14,6 @@
package btrfs

import (
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -122,7 +121,7 @@ func (r *reader) readValue(n string) (v uint64) {

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

// readLayouts reads all Btrfs layout statistics for the current path.
func (r *reader) readLayouts() map[string]*LayoutUsage {
files, err := ioutil.ReadDir(r.path)
files, err := os.ReadDir(r.path)
if err != nil {
r.err = err
return nil
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
13 changes: 6 additions & 7 deletions iscsi/get.go
Expand Up @@ -17,7 +17,6 @@ package iscsi
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -60,7 +59,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 All @@ -74,7 +73,7 @@ func isPathEnable(path string) (bool, error) {
func getLunLinkTarget(lunPath string) (lunObject LUN, err error) {
lunObject.Name = filepath.Base(lunPath)
lunObject.LunPath = lunPath
files, err := ioutil.ReadDir(lunPath)
files, err := os.ReadDir(lunPath)
if err != nil {
return lunObject, fmt.Errorf("getLunLinkTarget: ReadDir path %q: %w", lunPath, err)
}
Expand Down Expand Up @@ -144,7 +143,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 +165,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 +192,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 +203,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
5 changes: 2 additions & 3 deletions sysfs/class_dmi.go
Expand Up @@ -18,7 +18,6 @@ package sysfs

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

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

files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
return nil, fmt.Errorf("failed to read directory %q: %w", path, err)
}

var dmi DMIClass
for _, f := range files {
if !f.Mode().IsRegular() {
if !f.Type().IsRegular() {
continue
}

Expand Down
7 changes: 3 additions & 4 deletions sysfs/class_fibrechannel.go
Expand Up @@ -18,7 +18,6 @@ package sysfs

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

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

dirs, err := ioutil.ReadDir(path)
dirs, err := os.ReadDir(path)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -148,13 +147,13 @@ func parseFibreChannelStatistics(hostPath string) (*FibreChannelCounters, error)
var counters FibreChannelCounters

path := filepath.Join(hostPath, "statistics")
files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
return nil, err
}

for _, f := range files {
if !f.Mode().IsRegular() || f.Name() == "reset_statistics" {
if !f.Type().IsRegular() || f.Name() == "reset_statistics" {
continue
}

Expand Down

0 comments on commit fc47a74

Please sign in to comment.