all: use os.{Read|Write}File instead of ioutil.{Read|Write}File

The ioutil.{Read|Write}File is deprecated since Go1.16 -
see https://tip.golang.org/doc/go1.16#ioutil

VictoriaMetrics needs at least Go1.18, so it is safe to remove ioutil usage
from source code.

This is a follow-up for 02ca2342ab
This commit is contained in:
Aliaksandr Valialkin 2022-08-21 23:51:13 +03:00
parent 02ca2342ab
commit 9f94c295ab
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
23 changed files with 39 additions and 47 deletions

View file

@ -4,8 +4,8 @@ import (
"crypto/md5"
"fmt"
"hash/fnv"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"sort"
"strings"
@ -214,7 +214,7 @@ func Parse(pathPatterns []string, validateTplFn ValidateTplFn, validateExpressio
}
func parseFile(path string) ([]Group, error) {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("error reading alert rule file: %w", err)
}

View file

@ -154,7 +154,7 @@ groups:
func writeToFile(t *testing.T, file, b string) {
t.Helper()
err := ioutil.WriteFile(file, []byte(b), 0644)
err := os.WriteFile(file, []byte(b), 0644)
if err != nil {
t.Fatal(err)
}

View file

@ -4,8 +4,8 @@ import (
"crypto/md5"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"net/url"
"os"
"path"
"path/filepath"
"strings"
@ -104,7 +104,7 @@ func (cfg *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
}
func parseConfig(path string) (*Config, error) {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("error reading config file: %w", err)
}

View file

@ -175,7 +175,7 @@ consul_sd_configs:
func writeToFile(t *testing.T, file, b string) {
t.Helper()
checkErr(t, ioutil.WriteFile(file, []byte(b), 0644))
checkErr(t, os.WriteFile(file, []byte(b), 0644))
}
func checkErr(t *testing.T, err error) {

View file

@ -4,8 +4,8 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
@ -38,7 +38,7 @@ func TLSConfig(certFile, keyFile, CAFile, serverName string, insecureSkipVerify
var rootCAs *x509.CertPool
if CAFile != "" {
pem, err := ioutil.ReadFile(CAFile)
pem, err := os.ReadFile(CAFile)
if err != nil {
return nil, fmt.Errorf("cannot read `ca_file` %q: %w", CAFile, err)
}

View file

@ -4,7 +4,7 @@ import (
"crypto/rand"
"flag"
"fmt"
"io/ioutil"
"os"
"sync"
"sync/atomic"
"time"
@ -413,7 +413,7 @@ func mustLoadRollupResultCacheKeyPrefix(path string) {
rollupResultCacheKeyPrefix = newRollupResultCacheKeyPrefix()
return
}
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
logger.Errorf("cannot load %s: %s; reset rollupResult cache", path, err)
rollupResultCacheKeyPrefix = newRollupResultCacheKeyPrefix()

View file

@ -196,7 +196,7 @@ func (cfg *Config) getAPICredentials() (*credentials, error) {
SecretAccessKey: cfg.defaultSecretKey,
}
if len(cfg.webTokenPath) > 0 {
token, err := ioutil.ReadFile(cfg.webTokenPath)
token, err := os.ReadFile(cfg.webTokenPath)
if err != nil {
return nil, fmt.Errorf("cannot read webToken from path: %q, err: %w", cfg.webTokenPath, err)
}

View file

@ -3,7 +3,6 @@ package fsremote
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -218,7 +217,7 @@ func (fs *FS) CreateFile(filePath string, data []byte) error {
if err := fs.mkdirAll(path); err != nil {
return err
}
if err := ioutil.WriteFile(path, data, 0600); err != nil {
if err := os.WriteFile(path, data, 0600); err != nil {
return fmt.Errorf("cannot write %d bytes to %q: %w", len(data), path, err)
}
return nil

View file

@ -2,7 +2,6 @@ package cgroup
import (
"fmt"
"io/ioutil"
"os"
"runtime"
"strconv"
@ -80,7 +79,7 @@ func getCPUStat(statName string) (int64, error) {
func getOnlineCPUCount() float64 {
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/685#issuecomment-674423728
data, err := ioutil.ReadFile("/sys/devices/system/cpu/online")
data, err := os.ReadFile("/sys/devices/system/cpu/online")
if err != nil {
return -1
}

View file

@ -2,7 +2,7 @@ package cgroup
import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"strings"
@ -23,11 +23,11 @@ func getStatGeneric(statName, sysfsPrefix, cgroupPath, cgroupGrepLine string) (i
func getFileContents(statName, sysfsPrefix, cgroupPath, cgroupGrepLine string) (string, error) {
filepath := path.Join(sysfsPrefix, statName)
data, err := ioutil.ReadFile(filepath)
data, err := os.ReadFile(filepath)
if err == nil {
return string(data), nil
}
cgroupData, err := ioutil.ReadFile(cgroupPath)
cgroupData, err := os.ReadFile(cgroupPath)
if err != nil {
return "", err
}
@ -36,7 +36,7 @@ func getFileContents(statName, sysfsPrefix, cgroupPath, cgroupGrepLine string) (
return "", fmt.Errorf("cannot find cgroup path for %q in %q: %w", cgroupGrepLine, cgroupPath, err)
}
filepath = path.Join(sysfsPrefix, subPath, statName)
data, err = ioutil.ReadFile(filepath)
data, err = os.ReadFile(filepath)
if err != nil {
return "", err
}

View file

@ -390,7 +390,7 @@ func ReadFileOrHTTP(path string) ([]byte, error) {
}
return data, nil
}
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("cannot read %q: %w", path, err)
}

View file

@ -2,7 +2,7 @@ package fs
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
@ -18,7 +18,7 @@ func testReaderAt(t *testing.T, bufSize int) {
path := "TestReaderAt"
const fileSize = 8 * 1024 * 1024
data := make([]byte, fileSize)
if err := ioutil.WriteFile(path, data, 0600); err != nil {
if err := os.WriteFile(path, data, 0600); err != nil {
t.Fatalf("cannot create %q: %s", path, err)
}
defer MustRemoveAll(path)

View file

@ -2,7 +2,7 @@ package fs
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
@ -25,7 +25,7 @@ func benchmarkReaderAtMustReadAt(b *testing.B, isMmap bool) {
path := "BenchmarkReaderAtMustReadAt"
const fileSize = 8 * 1024 * 1024
data := make([]byte, fileSize)
if err := ioutil.WriteFile(path, data, 0600); err != nil {
if err := os.WriteFile(path, data, 0600); err != nil {
b.Fatalf("cannot create %q: %s", path, err)
}
defer MustRemoveAll(path)

View file

@ -4,7 +4,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
@ -124,7 +124,7 @@ func (ph *partHeader) ParseFromPath(partPath string) error {
// Read other ph fields from metadata.
metadataPath := partPath + "/metadata.json"
metadata, err := ioutil.ReadFile(metadataPath)
metadata, err := os.ReadFile(metadataPath)
if err != nil {
return fmt.Errorf("cannot read %q: %w", metadataPath, err)
}

View file

@ -3,7 +3,6 @@ package mergeset
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
@ -1259,7 +1258,7 @@ func runTransaction(txnLock *sync.RWMutex, pathPrefix, txnPath string) error {
txnLock.RLock()
defer txnLock.RUnlock()
data, err := ioutil.ReadFile(txnPath)
data, err := os.ReadFile(txnPath)
if err != nil {
return fmt.Errorf("cannot read transaction file: %w", err)
}

View file

@ -671,7 +671,7 @@ func (mi *metainfo) WriteToFile(path string) error {
if err != nil {
return fmt.Errorf("cannot marshal persistent queue metainfo %#v: %w", mi, err)
}
if err := ioutil.WriteFile(path, data, 0600); err != nil {
if err := os.WriteFile(path, data, 0600); err != nil {
return fmt.Errorf("cannot write persistent queue metainfo to %q: %w", path, err)
}
fs.MustSyncPath(path)
@ -680,7 +680,7 @@ func (mi *metainfo) WriteToFile(path string) error {
func (mi *metainfo) ReadFromFile(path string) error {
mi.Reset()
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return err

View file

@ -2,7 +2,6 @@ package persistentqueue
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"testing"
@ -371,7 +370,7 @@ func TestQueueLimitedSize(t *testing.T) {
}
func mustCreateFile(path, contents string) {
if err := ioutil.WriteFile(path, []byte(contents), 0600); err != nil {
if err := os.WriteFile(path, []byte(contents), 0600); err != nil {
panic(fmt.Errorf("cannot create file %q with %d bytes contents: %w", path, len(contents), err))
}
}

View file

@ -3,7 +3,6 @@ package azure
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"strconv"
@ -145,7 +144,7 @@ func getCloudEnvByName(name string) (*cloudEnvironmentEndpoints, error) {
}
func readCloudEndpointsFromFile(filePath string) (*cloudEnvironmentEndpoints, error) {
data, err := ioutil.ReadFile(filePath)
data, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("cannot file %q: %w", filePath, err)
}

View file

@ -3,7 +3,6 @@ package consul
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
@ -109,7 +108,7 @@ func getToken(token *promauth.Secret) (string, error) {
return token.String(), nil
}
if tokenFile := os.Getenv("CONSUL_HTTP_TOKEN_FILE"); tokenFile != "" {
data, err := ioutil.ReadFile(tokenFile)
data, err := os.ReadFile(tokenFile)
if err != nil {
return "", fmt.Errorf("cannot read consul token file %q; probably, `token` arg is missing in `consul_sd_config`? error: %w", tokenFile, err)
}

View file

@ -9,6 +9,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"os"
"reflect"
"strconv"
"strings"
@ -66,7 +67,7 @@ func newAPIWatcher(apiServer string, ac *promauth.Config, sdc *SDConfig, swcFunc
namespaces := sdc.Namespaces.Names
if len(namespaces) == 0 {
if sdc.Namespaces.OwnNamespace {
namespace, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
namespace, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil {
logger.Fatalf("cannot determine namespace for the current pod according to `own_namespace: true` option in kubernetes_sd_config: %s", err)
}

View file

@ -3,7 +3,6 @@ package storage
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
@ -131,7 +130,7 @@ func (ph *partHeader) Reset() {
func (ph *partHeader) readMinDedupInterval(partPath string) error {
filePath := partPath + "/min_dedup_interval"
data, err := ioutil.ReadFile(filePath)
data, err := os.ReadFile(filePath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
// The minimum dedup interval may not exist for old parts.

View file

@ -3,7 +3,6 @@ package storage
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
@ -1743,7 +1742,7 @@ func runTransaction(txnLock *sync.RWMutex, pathPrefix1, pathPrefix2, txnPath str
txnLock.RLock()
defer txnLock.RUnlock()
data, err := ioutil.ReadFile(txnPath)
data, err := os.ReadFile(txnPath)
if err != nil {
return fmt.Errorf("cannot read transaction file: %w", err)
}

View file

@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"path/filepath"
@ -845,7 +844,7 @@ func (s *Storage) mustLoadNextDayMetricIDs(date uint64) *byDateMetricIDEntry {
logger.Infof("nothing to load from %q", path)
return e
}
src, err := ioutil.ReadFile(path)
src, err := os.ReadFile(path)
if err != nil {
logger.Panicf("FATAL: cannot read %s: %s", path, err)
}
@ -889,7 +888,7 @@ func (s *Storage) mustLoadHourMetricIDs(hour uint64, name string) *hourMetricIDs
logger.Infof("nothing to load from %q", path)
return hm
}
src, err := ioutil.ReadFile(path)
src, err := os.ReadFile(path)
if err != nil {
logger.Panicf("FATAL: cannot read %s: %s", path, err)
}
@ -938,7 +937,7 @@ func (s *Storage) mustSaveNextDayMetricIDs(e *byDateMetricIDEntry) {
// Marshal e.v
dst = marshalUint64Set(dst, &e.v)
if err := ioutil.WriteFile(path, dst, 0644); err != nil {
if err := os.WriteFile(path, dst, 0644); err != nil {
logger.Panicf("FATAL: cannot write %d bytes to %q: %s", len(dst), path, err)
}
logger.Infof("saved %s to %q in %.3f seconds; entriesCount: %d; sizeBytes: %d", name, path, time.Since(startTime).Seconds(), e.v.Len(), len(dst))
@ -961,7 +960,7 @@ func (s *Storage) mustSaveHourMetricIDs(hm *hourMetricIDs, name string) {
// Marshal hm.m
dst = marshalUint64Set(dst, hm.m)
if err := ioutil.WriteFile(path, dst, 0644); err != nil {
if err := os.WriteFile(path, dst, 0644); err != nil {
logger.Panicf("FATAL: cannot write %d bytes to %q: %s", len(dst), path, err)
}
logger.Infof("saved %s to %q in %.3f seconds; entriesCount: %d; sizeBytes: %d", name, path, time.Since(startTime).Seconds(), hm.m.Len(), len(dst))
@ -1022,7 +1021,7 @@ func mustGetMinTimestampForCompositeIndex(metadataDir string, isEmptyDB bool) in
}
func loadMinTimestampForCompositeIndex(path string) (int64, error) {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return 0, err
}