Skip to content

Commit

Permalink
refactor(ioutil): ioutil is deprecated after 1.16
Browse files Browse the repository at this point in the history
- [go#40025](golang/go#42025)
- [go#40026](golang/go#42026)
  • Loading branch information
Wabri committed Aug 21, 2024
1 parent 7d0a1cb commit 78f05aa
Show file tree
Hide file tree
Showing 28 changed files with 85 additions and 108 deletions.
3 changes: 1 addition & 2 deletions actions/noteacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/SUSE/saptune/sap/note"
"github.com/SUSE/saptune/system"
"io"
"io/ioutil"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -306,7 +305,7 @@ func NoteActionShow(writer io.Writer, noteID string, tuneApp *app.App) {
system.ErrorExit("%v", err)
}
fileName, _ := getFileName(noteID, NoteTuningSheets, ExtraTuningSheets)
cont, err := ioutil.ReadFile(fileName)
cont, err := os.ReadFile(fileName)
if err != nil {
system.ErrorExit("Failed to read file '%s' - %v", fileName, err)
}
Expand Down
5 changes: 2 additions & 3 deletions actions/solutionacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/SUSE/saptune/sap/solution"
"github.com/SUSE/saptune/system"
"io"
"io/ioutil"
"os"
"regexp"
"sort"
Expand Down Expand Up @@ -420,7 +419,7 @@ func SolutionActionShow(writer io.Writer, solName string) {
}
solFName := fmt.Sprintf("%s.sol", solName)
fileName, _ := getFileName(solFName, SolutionSheets, ExtraTuningSheets)
cont, err := ioutil.ReadFile(fileName)
cont, err := os.ReadFile(fileName)
if err != nil {
system.ErrorExit("Failed to read file '%s' - %v", fileName, err)
}
Expand Down Expand Up @@ -536,7 +535,7 @@ func rewriteSolName(oldName, newName, newFile string) {
}
defer fn.Close()
// create temp file
tmpfn, err := ioutil.TempFile("", "replace-*")
tmpfn, err := os.CreateTemp("", "replace-*")
if err != nil {
system.ErrorExit("Unable to create temporary file - %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions actions/stagingacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/SUSE/saptune/system"
"github.com/SUSE/saptune/txtparser"
"io"
"io/ioutil"
"os"
"sort"
"strconv"
Expand Down Expand Up @@ -521,7 +520,7 @@ func writeStagingToConf(staging string) error {
return err
}
sconf.Set("STAGING", staging)
return ioutil.WriteFile(saptuneSysconfig, []byte(sconf.ToText()), 0644)
return os.WriteFile(saptuneSysconfig, []byte(sconf.ToText()), 0644)
}

// collectStageFileInfo is collecting all needed info about the file
Expand Down
7 changes: 3 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/SUSE/saptune/system"
"github.com/SUSE/saptune/txtparser"
"io"
"io/ioutil"
"os"
"path"
"reflect"
Expand Down Expand Up @@ -99,7 +98,7 @@ func (app *App) SaveConfig() error {
sysconf.SetStrArray(TuneForSolutionsKey, app.TuneForSolutions)
sysconf.SetStrArray(TuneForNotesKey, app.TuneForNotes)
sysconf.SetStrArray(NoteApplyOrderKey, app.NoteApplyOrder)
return ioutil.WriteFile(path.Join(app.SysconfigPrefix, SysconfigSaptuneFile), []byte(sysconf.ToText()), 0644)
return os.WriteFile(path.Join(app.SysconfigPrefix, SysconfigSaptuneFile), []byte(sysconf.ToText()), 0644)
}

// GetSortedSolutionEnabledNotes returns the number of all solution-enabled
Expand Down Expand Up @@ -256,7 +255,7 @@ func (app *App) NoteSanityCheck() error {
// file handling exists
fileName := fmt.Sprintf("/run/saptune/sections/%s.sections", note)
// check, if empty state file exists
if content, err := ioutil.ReadFile(app.State.GetPathToNote(note)); err == nil && len(content) == 0 {
if content, err := os.ReadFile(app.State.GetPathToNote(note)); err == nil && len(content) == 0 {
// remove empty state file
_ = app.State.Remove(note)
if _, err := os.Stat(fileName); err == nil {
Expand Down Expand Up @@ -579,7 +578,7 @@ func (app *App) RevertAll(permanent bool) error {
if err == nil {
for _, otherNoteID := range otherNotes {
// check, if empty state file exists
if content, err := ioutil.ReadFile(app.State.GetPathToNote(otherNoteID)); err == nil && len(content) == 0 {
if content, err := os.ReadFile(app.State.GetPathToNote(otherNoteID)); err == nil && len(content) == 0 {
// remove empty state file
_ = app.State.Remove(otherNoteID)
continue
Expand Down
9 changes: 4 additions & 5 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/SUSE/saptune/sap/param"
"github.com/SUSE/saptune/sap/solution"
"github.com/SUSE/saptune/system"
"io/ioutil"
"os"
"path"
"reflect"
Expand All @@ -30,7 +29,7 @@ func (pa SampleParam) Name() string {
return "Sample parameter"
}
func (pa SampleParam) Inspect() (param.Parameter, error) {
content, _ := ioutil.ReadFile(SampleParamFile)
content, _ := os.ReadFile(SampleParamFile)
pa.Data = string(content)
return pa, nil
}
Expand All @@ -39,7 +38,7 @@ func (pa SampleParam) Optimise(way interface{}) (param.Parameter, error) {
return pa, nil
}
func (pa SampleParam) Apply(way interface{}) error {
return ioutil.WriteFile(SampleParamFile, []byte(pa.Data), 0644)
return os.WriteFile(SampleParamFile, []byte(pa.Data), 0644)
}

type SampleNote1 struct {
Expand Down Expand Up @@ -109,14 +108,14 @@ func VerifyConfig(t *testing.T, app *App, hasNotes []string, hasSolutions []stri
}

func WriteFileOrPanic(filePath, content string) {
if err := ioutil.WriteFile(filePath, []byte(content), 0644); err != nil {
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
panic(err)
}
}

// Verify that the file content is exactly as specified.
func VerifyFileContent(t *testing.T, filePath, content, no string) {
if fileContent, err := ioutil.ReadFile(filePath); err != nil {
if fileContent, err := os.ReadFile(filePath); err != nil {
t.Fatal(err)
} else if string(fileContent) != content {
t.Errorf("%s - file content mismatch\nexpected:%s\nactual:%s", no, content, string(fileContent))
Expand Down
7 changes: 3 additions & 4 deletions app/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"github.com/SUSE/saptune/sap/note"
"io/ioutil"
"os"
"path"
)
Expand Down Expand Up @@ -33,7 +32,7 @@ func (state *State) Store(noteID string, obj note.Note, overwriteExisting bool)
return err
}
if _, err := os.Stat(state.GetPathToNote(noteID)); os.IsNotExist(err) || overwriteExisting {
return ioutil.WriteFile(state.GetPathToNote(noteID), content, 0644)
return os.WriteFile(state.GetPathToNote(noteID), content, 0644)
}
return nil
}
Expand All @@ -44,7 +43,7 @@ func (state *State) List() (ret []string, err error) {
return
}
// List SaptuneStateDir and collect number from file names
dirContent, err := ioutil.ReadDir(path.Join(state.StateDirPrefix, SaptuneStateDir))
dirContent, err := os.ReadDir(path.Join(state.StateDirPrefix, SaptuneStateDir))
if os.IsNotExist(err) {
return []string{}, nil
} else if err != nil {
Expand All @@ -60,7 +59,7 @@ func (state *State) List() (ret []string, err error) {
// Retrieve deserialises a SAP note into the destination pointer.
// The destination must be a pointer.
func (state *State) Retrieve(noteID string, dest interface{}) error {
content, err := ioutil.ReadFile(state.GetPathToNote(noteID))
content, err := os.ReadFile(state.GetPathToNote(noteID))
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions sap/note/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package note
import (
"encoding/json"
"github.com/SUSE/saptune/system"
"io/ioutil"
"os"
"path"
)
Expand Down Expand Up @@ -44,7 +43,7 @@ func ListParams() (ret []string, err error) {
return
}
// List SaptuneParameterStateDir and collect parameter names from file names
dirContent, err := ioutil.ReadDir(SaptuneParameterStateDir)
dirContent, err := os.ReadDir(SaptuneParameterStateDir)
if os.IsNotExist(err) {
return []string{}, nil
} else if err != nil {
Expand Down Expand Up @@ -106,7 +105,7 @@ func GetSavedParameterNotes(param string) ParameterNotes {
pEntries := ParameterNotes{
AllNotes: make([]ParameterNoteEntry, 0, 64),
}
content, err := ioutil.ReadFile(GetPathToParameter(param))
content, err := os.ReadFile(GetPathToParameter(param))
if err != nil {
return pEntries
}
Expand Down Expand Up @@ -145,7 +144,7 @@ func StoreParameter(param string, obj ParameterNotes, overwriteExisting bool) er
return err
}
if _, err := os.Stat(GetPathToParameter(param)); os.IsNotExist(err) || overwriteExisting {
return ioutil.WriteFile(GetPathToParameter(param), content, 0644)
return os.WriteFile(GetPathToParameter(param), content, 0644)
}
return nil
}
Expand Down
5 changes: 2 additions & 3 deletions sap/note/sectLogin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package note
import (
"fmt"
"github.com/SUSE/saptune/system"
"io/ioutil"
"os"
"path"
"regexp"
Expand All @@ -19,7 +18,7 @@ func GetLoginVal(key string) (string, error) {
var utmPat = regexp.MustCompile(`UserTasksMax=(.*)`)
switch key {
case "UserTasksMax":
logindContent, err := ioutil.ReadFile(path.Join(LogindConfDir, LogindSAPConfFile))
logindContent, err := os.ReadFile(path.Join(LogindConfDir, LogindSAPConfFile))
if err != nil && !os.IsNotExist(err) {
return "", err
}
Expand Down Expand Up @@ -97,7 +96,7 @@ func SetLoginVal(key, value string, revert bool) error {
if err := os.MkdirAll(LogindConfDir, 0755); err != nil {
return err
}
if err := ioutil.WriteFile(path.Join(LogindConfDir, LogindSAPConfFile), []byte(LogindSAPConfContent), 0644); err != nil {
if err := os.WriteFile(path.Join(LogindConfDir, LogindSAPConfFile), []byte(LogindSAPConfContent), 0644); err != nil {
return err
}
// reload-or-try-restart systemd-logind.service
Expand Down
12 changes: 6 additions & 6 deletions sap/param/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package param

import (
"github.com/SUSE/saptune/system"
"io/ioutil"
"os"
"path"
"testing"
)
Expand Down Expand Up @@ -35,7 +35,7 @@ func TestIOElevators(t *testing.T) {
t.Logf("oldvals - '%+v'\n", oldvals)

// ANGI TODO - better solution
_, err = ioutil.ReadDir("/sys/block/sda/mq")
_, err = os.ReadDir("/sys/block/sda/mq")
if err != nil {
// single queue scheduler (values: noop deadline cfq)
scheduler = "noop"
Expand Down Expand Up @@ -398,12 +398,12 @@ func TestMaxSectorsKB(t *testing.T) {

func TestIsValidScheduler(t *testing.T) {
scheduler := ""
dirCont, err := ioutil.ReadDir("/sys/block")
dirCont, err := os.ReadDir("/sys/block")
if err != nil {
t.Skip("no block files available. Skip test.")
}
for _, entry := range dirCont {
_, err := ioutil.ReadDir(path.Join("/sys/block/", entry.Name(), "mq"))
_, err := os.ReadDir(path.Join("/sys/block/", entry.Name(), "mq"))
if err != nil {
// single queue scheduler (values: noop deadline cfq)
scheduler = "cfq"
Expand Down Expand Up @@ -431,7 +431,7 @@ func TestIsValidScheduler(t *testing.T) {
}

func TestIsValidforNrRequests(t *testing.T) {
dirCont, err := ioutil.ReadDir("/sys/block")
dirCont, err := os.ReadDir("/sys/block")
if err != nil {
t.Skip("no block files available. Skip test.")
}
Expand Down Expand Up @@ -459,7 +459,7 @@ func TestIsValidforNrRequests(t *testing.T) {
}

func TestIsValidforReadAheadKB(t *testing.T) {
dirCont, err := ioutil.ReadDir("/sys/block")
dirCont, err := os.ReadDir("/sys/block")
if err != nil {
t.Skip("no block files available. Skip test.")
}
Expand Down
11 changes: 5 additions & 6 deletions system/blockdev.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package system
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"regexp"
Expand Down Expand Up @@ -45,7 +44,7 @@ var isNvme = regexp.MustCompile(`^nvme\d+n\d+$`)
// does not work for virtio and nvme block devices, needs workaround
func BlockDeviceIsDisk(dev string) bool {
fname := fmt.Sprintf("/sys/block/%s/device/type", dev)
dtype, err := ioutil.ReadFile(fname)
dtype, err := os.ReadFile(fname)
if err != nil || strings.TrimSpace(string(dtype)) != "0" {
if isVD.FindStringSubmatch(dev) == nil && isNvme.FindStringSubmatch(dev) == nil {
// unsupported device
Expand All @@ -66,7 +65,7 @@ func GetBlockDeviceInfo() (*BlockDev, error) {
BlockAttributes: make(map[string]map[string]string),
}

content, err := ioutil.ReadFile(bdevFileName)
content, err := os.ReadFile(bdevFileName)
if err == nil && len(content) != 0 {
err = json.Unmarshal(content, &bdevConf)
}
Expand All @@ -87,7 +86,7 @@ func getValidBlockDevices() (valDevs []string) {
for _, bdev := range sysDevs {
dmUUID := fmt.Sprintf("/sys/block/%s/dm/uuid", bdev)
if _, err := os.Stat(dmUUID); err == nil {
cont, _ := ioutil.ReadFile(dmUUID)
cont, _ := os.ReadFile(dmUUID)
if isMpath.MatchString(string(cont)) {
candidates = append(candidates, bdev)
_, slaves := ListDir(fmt.Sprintf("/sys/block/%s/slaves", bdev), "dm slaves")
Expand Down Expand Up @@ -148,7 +147,7 @@ func CollectBlockDeviceInfo() []string {
elev, _ = GetSysString(path.Join("block", bdev, "queue", "scheduler"))
}
blockMap["IO_SCHEDULER"] = elev
val, err := ioutil.ReadFile(path.Join("/sys/block/", bdev, "/queue/scheduler"))
val, err := os.ReadFile(path.Join("/sys/block/", bdev, "/queue/scheduler"))
sched := ""
if err == nil {
sched = string(val)
Expand Down Expand Up @@ -222,7 +221,7 @@ func storeBlockDeviceInfo(obj BlockDev) error {
return err
}
if _, err := os.Stat(bdevFileName); os.IsNotExist(err) || overwriteExisting {
return ioutil.WriteFile(bdevFileName, content, 0644)
return os.WriteFile(bdevFileName, content, 0644)
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions system/cmdline.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package system
// Gather information about kernel cmdline

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

// ParseCmdline parse /proc/cmdline into key(string) - value(string) pairs.
// return value for given boot option or 'NA', if not available
func ParseCmdline(fileName, option string) string {
opt := "NA"
cmdLine, err := ioutil.ReadFile(fileName)
cmdLine, err := os.ReadFile(fileName)
if err != nil {
WarningLog("ParseCmdline: failed to read %s: %v", fileName, err)
return opt
Expand Down
Loading

0 comments on commit 78f05aa

Please sign in to comment.