diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 943de7615..853eb9d49 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 @@ -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") diff --git a/arp.go b/arp.go index 1f2616a96..68f36e888 100644 --- a/arp.go +++ b/arp.go @@ -15,8 +15,8 @@ package procfs import ( "fmt" - "io/ioutil" "net" + "os" "strconv" "strings" ) @@ -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) } diff --git a/bcache/get.go b/bcache/get.go index 41ac1899c..aa380c2b4 100644 --- a/bcache/get.go +++ b/bcache/get.go @@ -16,7 +16,6 @@ package bcache import ( "bufio" "fmt" - "io/ioutil" "os" "path" "path/filepath" @@ -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) diff --git a/blockdevice/stats.go b/blockdevice/stats.go index 9b8182fab..ae35ca75a 100644 --- a/blockdevice/stats.go +++ b/blockdevice/stats.go @@ -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 } diff --git a/internal/util/parse.go b/internal/util/parse.go index 22cb07a6b..b030951fa 100644 --- a/internal/util/parse.go +++ b/internal/util/parse.go @@ -14,7 +14,7 @@ package util import ( - "io/ioutil" + "os" "strconv" "strings" ) @@ -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 } @@ -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 } diff --git a/internal/util/readfile.go b/internal/util/readfile.go index 8051161b2..1e9c1fbfa 100644 --- a/internal/util/readfile.go +++ b/internal/util/readfile.go @@ -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. @@ -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) } diff --git a/internal/util/sysreadfile.go b/internal/util/sysreadfile.go index 38e76f920..bd6ea1ebd 100644 --- a/internal/util/sysreadfile.go +++ b/internal/util/sysreadfile.go @@ -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. @@ -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. diff --git a/ipvs.go b/ipvs.go index 89e447746..391c07957 100644 --- a/ipvs.go +++ b/ipvs.go @@ -20,7 +20,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net" "os" "strconv" @@ -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 } diff --git a/iscsi/get.go b/iscsi/get.go index 347edd993..8448578d3 100644 --- a/iscsi/get.go +++ b/iscsi/get.go @@ -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) } @@ -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) } @@ -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) } @@ -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 { @@ -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 { diff --git a/mdstat.go b/mdstat.go index 2c7900f35..a95c889cb 100644 --- a/mdstat.go +++ b/mdstat.go @@ -15,7 +15,7 @@ package procfs import ( "fmt" - "io/ioutil" + "os" "regexp" "strconv" "strings" @@ -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 } diff --git a/proc.go b/proc.go index 5b3e1e4a9..c30223af7 100644 --- a/proc.go +++ b/proc.go @@ -16,7 +16,7 @@ package procfs import ( "bytes" "fmt" - "io/ioutil" + "io" "os" "strconv" "strings" @@ -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 } @@ -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 } diff --git a/sysfs/class_infiniband.go b/sysfs/class_infiniband.go index 17b986262..740195127 100644 --- a/sysfs/class_infiniband.go +++ b/sysfs/class_infiniband.go @@ -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 } @@ -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 } @@ -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 } diff --git a/sysfs/class_powercap.go b/sysfs/class_powercap.go index d1434a2eb..2ed3e7f2a 100644 --- a/sysfs/class_powercap.go +++ b/sysfs/class_powercap.go @@ -19,6 +19,7 @@ package sysfs import ( "fmt" "io/ioutil" + "os" "path/filepath" "strconv" "strings" @@ -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)) diff --git a/sysfs/system_cpu.go b/sysfs/system_cpu.go index ed975ba72..b369568a0 100644 --- a/sysfs/system_cpu.go +++ b/sysfs/system_cpu.go @@ -18,7 +18,6 @@ package sysfs import ( "fmt" - "io/ioutil" "os" "path/filepath" "strconv" @@ -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 } diff --git a/sysfs/vulnerability.go b/sysfs/vulnerability.go index 721948faf..87e701602 100644 --- a/sysfs/vulnerability.go +++ b/sysfs/vulnerability.go @@ -18,7 +18,7 @@ package sysfs import ( "fmt" - "io/ioutil" + "os" "path/filepath" "strings" ) @@ -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 } diff --git a/zoneinfo.go b/zoneinfo.go index 33ddf6838..c745a4c04 100644 --- a/zoneinfo.go +++ b/zoneinfo.go @@ -19,7 +19,7 @@ package procfs import ( "bytes" "fmt" - "io/ioutil" + "os" "regexp" "strings" @@ -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) }