Skip to content

Commit

Permalink
Merge pull request #1524 from shirou/feature/replace_ioutil
Browse files Browse the repository at this point in the history
chore: replace deprecated ioutil package to os and io
  • Loading branch information
shirou committed Sep 10, 2023
2 parents 6048441 + 0665caf commit 0e9f133
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 51 deletions.
6 changes: 3 additions & 3 deletions cpu/cpu_solaris_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cpu

import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
Expand Down Expand Up @@ -49,7 +49,7 @@ func TestParseISAInfo(t *testing.T) {
}

for _, tc := range cases {
content, err := ioutil.ReadFile(filepath.Join("testdata", "solaris", tc.filename))
content, err := os.ReadFile(filepath.Join("testdata", "solaris", tc.filename))
if err != nil {
t.Errorf("cannot read test case: %s", err)
}
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestParseProcessorInfo(t *testing.T) {
}

for _, tc := range cases {
content, err := ioutil.ReadFile(filepath.Join("testdata", "solaris", tc.filename))
content, err := os.ReadFile(filepath.Join("testdata", "solaris", tc.filename))
if err != nil {
t.Errorf("cannot read test case: %s", err)
}
Expand Down
9 changes: 4 additions & 5 deletions disk/disk_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -497,7 +496,7 @@ func SerialNumberWithContext(ctx context.Context, name string) (string, error) {

// Try to get the serial from udev data
udevDataPath := common.HostRunWithContext(ctx, fmt.Sprintf("udev/data/b%d:%d", major, minor))
if udevdata, err := ioutil.ReadFile(udevDataPath); err == nil {
if udevdata, err := os.ReadFile(udevDataPath); err == nil {
scanner := bufio.NewScanner(bytes.NewReader(udevdata))
for scanner.Scan() {
values := strings.Split(scanner.Text(), "=")
Expand All @@ -510,8 +509,8 @@ func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
// Try to get the serial from sysfs, look at the disk device (minor 0) directly
// because if it is a partition it is not going to contain any device information
devicePath := common.HostSysWithContext(ctx, fmt.Sprintf("dev/block/%d:0/device", major))
model, _ := ioutil.ReadFile(filepath.Join(devicePath, "model"))
serial, _ := ioutil.ReadFile(filepath.Join(devicePath, "serial"))
model, _ := os.ReadFile(filepath.Join(devicePath, "model"))
serial, _ := os.ReadFile(filepath.Join(devicePath, "serial"))
if len(model) > 0 && len(serial) > 0 {
return fmt.Sprintf("%s_%s", string(model), string(serial)), nil
}
Expand All @@ -526,7 +525,7 @@ func LabelWithContext(ctx context.Context, name string) (string, error) {
return "", nil
}

dmname, err := ioutil.ReadFile(dmname_filename)
dmname, err := os.ReadFile(dmname_filename)
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions host/host_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"context"
"encoding/binary"
"errors"
"io/ioutil"
"io"
"os"
"strings"
"unsafe"
Expand Down Expand Up @@ -59,7 +59,7 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) {
}
defer file.Close()

buf, err := ioutil.ReadAll(file)
buf, err := io.ReadAll(file)
if err != nil {
return ret, err
}
Expand Down
6 changes: 3 additions & 3 deletions host/host_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"bytes"
"context"
"encoding/binary"
"io/ioutil"
"io"
"math"
"os"
"strings"
Expand Down Expand Up @@ -54,7 +54,7 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) {
}
defer file.Close()

buf, err := ioutil.ReadAll(file)
buf, err := io.ReadAll(file)
if err != nil {
return ret, err
}
Expand Down Expand Up @@ -111,7 +111,7 @@ func getUsersFromUtmp(utmpfile string) ([]UserStat, error) {
}
defer file.Close()

buf, err := ioutil.ReadAll(file)
buf, err := io.ReadAll(file)
if err != nil {
return ret, err
}
Expand Down
16 changes: 8 additions & 8 deletions host/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"context"
"encoding/binary"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -91,7 +91,7 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) {
}
defer file.Close()

buf, err := ioutil.ReadAll(file)
buf, err := io.ReadAll(file)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -411,13 +411,13 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err
}
for _, file := range files {
// Get the name of the temperature you are reading
name, err := ioutil.ReadFile(filepath.Join(file, "type"))
name, err := os.ReadFile(filepath.Join(file, "type"))
if err != nil {
warns.Add(err)
continue
}
// Get the temperature reading
current, err := ioutil.ReadFile(filepath.Join(file, "temp"))
current, err := os.ReadFile(filepath.Join(file, "temp"))
if err != nil {
warns.Add(err)
continue
Expand Down Expand Up @@ -461,13 +461,13 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err
// Get the label of the temperature you are reading
label := ""

if raw, _ = ioutil.ReadFile(basepath + "_label"); len(raw) != 0 {
if raw, _ = os.ReadFile(basepath + "_label"); len(raw) != 0 {
// Format the label from "Core 0" to "core_0"
label = strings.Join(strings.Split(strings.TrimSpace(strings.ToLower(string(raw))), " "), "_")
}

// Get the name of the temperature you are reading
if raw, err = ioutil.ReadFile(filepath.Join(directory, "name")); err != nil {
if raw, err = os.ReadFile(filepath.Join(directory, "name")); err != nil {
warns.Add(err)
continue
}
Expand All @@ -479,7 +479,7 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err
}

// Get the temperature reading
if raw, err = ioutil.ReadFile(file); err != nil {
if raw, err = os.ReadFile(file); err != nil {
warns.Add(err)
continue
}
Expand Down Expand Up @@ -513,7 +513,7 @@ func optionalValueReadFromFile(filename string) float64 {
return 0
}

if raw, err = ioutil.ReadFile(filename); err != nil {
if raw, err = os.ReadFile(filename); err != nil {
return 0
}

Expand Down
4 changes: 2 additions & 2 deletions host/host_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"bytes"
"context"
"encoding/binary"
"io/ioutil"
"io"
"os"
"strings"
"unsafe"
Expand Down Expand Up @@ -65,7 +65,7 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) {
}
defer file.Close()

buf, err := ioutil.ReadAll(file)
buf, err := io.ReadAll(file)
if err != nil {
return ret, err
}
Expand Down
2 changes: 1 addition & 1 deletion host/host_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func VirtualizationWithContext(ctx context.Context) (string, string, error) {

// Find distribution name from /etc/release
func parseReleaseFile() (string, error) {
b, err := ioutil.ReadFile("/etc/release")
b, err := os.ReadFile("/etc/release")
if err != nil {
return "", err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"os/exec"
Expand Down Expand Up @@ -87,7 +86,7 @@ func (i FakeInvoke) Command(name string, arg ...string) ([]byte, error) {
fpath += "_" + i.Suffix
}
if PathExists(fpath) {
return ioutil.ReadFile(fpath)
return os.ReadFile(fpath)
}
return []byte{}, fmt.Errorf("could not find testdata: %s", fpath)
}
Expand All @@ -100,7 +99,7 @@ var ErrNotImplementedError = errors.New("not implemented yet")

// ReadFile reads contents from a file
func ReadFile(filename string) (string, error) {
content, err := ioutil.ReadFile(filename)
content, err := os.ReadFile(filename)
if err != nil {
return "", err
}
Expand Down
6 changes: 3 additions & 3 deletions load/load_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package load

import (
"context"
"io/ioutil"
"os"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -76,7 +76,7 @@ func Misc() (*MiscStat, error) {

func MiscWithContext(ctx context.Context) (*MiscStat, error) {
filename := common.HostProcWithContext(ctx, "stat")
out, err := ioutil.ReadFile(filename)
out, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func getProcsTotal(ctx context.Context) (int64, error) {

func readLoadAvgFromFile(ctx context.Context) ([]string, error) {
loadavgFilename := common.HostProcWithContext(ctx, "loadavg")
line, err := ioutil.ReadFile(loadavgFilename)
line, err := os.ReadFile(loadavgFilename)
if err != nil {
return nil, err
}
Expand Down
7 changes: 3 additions & 4 deletions net/net_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"strconv"
Expand Down Expand Up @@ -643,7 +642,7 @@ func (p *process) getUids(ctx context.Context) ([]int32, error) {
func (p *process) fillFromStatus(ctx context.Context) error {
pid := p.Pid
statPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "status")
contents, err := ioutil.ReadFile(statPath)
contents, err := os.ReadFile(statPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -784,7 +783,7 @@ func processInetWithContext(ctx context.Context, file string, kind netConnection
// This minimizes duplicates in the returned connections
// For more info:
// https://github.com/shirou/gopsutil/pull/361
contents, err := ioutil.ReadFile(file)
contents, err := os.ReadFile(file)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -845,7 +844,7 @@ func processUnix(file string, kind netConnectionKindType, inodes map[string][]in
// This minimizes duplicates in the returned connections
// For more info:
// https://github.com/shirou/gopsutil/pull/361
contents, err := ioutil.ReadFile(file)
contents, err := os.ReadFile(file)
if err != nil {
return nil, err
}
Expand Down
21 changes: 10 additions & 11 deletions process/process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -136,7 +135,7 @@ func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
// see https://github.com/shirou/gopsutil/issues/596#issuecomment-432707831 for implementation details
pid := p.Pid
statPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "stat")
contents, err := ioutil.ReadFile(statPath)
contents, err := os.ReadFile(statPath)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -391,7 +390,7 @@ func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]M
smapsPath = smapsRollupPath
}
}
contents, err := ioutil.ReadFile(smapsPath)
contents, err := os.ReadFile(smapsPath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -484,7 +483,7 @@ func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]M
func (p *Process) EnvironWithContext(ctx context.Context) ([]string, error) {
environPath := common.HostProcWithContext(ctx, strconv.Itoa(int(p.Pid)), "environ")

environContent, err := ioutil.ReadFile(environPath)
environContent, err := os.ReadFile(environPath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -668,7 +667,7 @@ func (p *Process) fillFromExeWithContext(ctx context.Context) (string, error) {
func (p *Process) fillFromCmdlineWithContext(ctx context.Context) (string, error) {
pid := p.Pid
cmdPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "cmdline")
cmdline, err := ioutil.ReadFile(cmdPath)
cmdline, err := os.ReadFile(cmdPath)
if err != nil {
return "", err
}
Expand All @@ -682,7 +681,7 @@ func (p *Process) fillFromCmdlineWithContext(ctx context.Context) (string, error
func (p *Process) fillSliceFromCmdlineWithContext(ctx context.Context) ([]string, error) {
pid := p.Pid
cmdPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "cmdline")
cmdline, err := ioutil.ReadFile(cmdPath)
cmdline, err := os.ReadFile(cmdPath)
if err != nil {
return nil, err
}
Expand All @@ -705,7 +704,7 @@ func (p *Process) fillSliceFromCmdlineWithContext(ctx context.Context) ([]string
func (p *Process) fillFromIOWithContext(ctx context.Context) (*IOCountersStat, error) {
pid := p.Pid
ioPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "io")
ioline, err := ioutil.ReadFile(ioPath)
ioline, err := os.ReadFile(ioPath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -741,7 +740,7 @@ func (p *Process) fillFromIOWithContext(ctx context.Context) (*IOCountersStat, e
func (p *Process) fillFromStatmWithContext(ctx context.Context) (*MemoryInfoStat, *MemoryInfoExStat, error) {
pid := p.Pid
memPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "statm")
contents, err := ioutil.ReadFile(memPath)
contents, err := os.ReadFile(memPath)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -802,7 +801,7 @@ func (p *Process) fillNameWithContext(ctx context.Context) error {
func (p *Process) fillFromCommWithContext(ctx context.Context) error {
pid := p.Pid
statPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "comm")
contents, err := ioutil.ReadFile(statPath)
contents, err := os.ReadFile(statPath)
if err != nil {
return err
}
Expand All @@ -819,7 +818,7 @@ func (p *Process) fillFromStatus() error {
func (p *Process) fillFromStatusWithContext(ctx context.Context) error {
pid := p.Pid
statPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "status")
contents, err := ioutil.ReadFile(statPath)
contents, err := os.ReadFile(statPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -1026,7 +1025,7 @@ func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (ui
statPath = common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "task", strconv.Itoa(int(tid)), "stat")
}

contents, err := ioutil.ReadFile(statPath)
contents, err := os.ReadFile(statPath)
if err != nil {
return 0, 0, nil, 0, 0, 0, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion process/process_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Test_Process_splitProcStat_fromFile(t *testing.T) {
if _, err := os.Stat(statFile); err != nil {
continue
}
contents, err := ioutil.ReadFile(statFile)
contents, err := os.ReadFile(statFile)
assert.NoError(t, err)

pidStr := strconv.Itoa(int(pid))
Expand Down
Loading

0 comments on commit 0e9f133

Please sign in to comment.