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" "crypto/md5"
"fmt" "fmt"
"hash/fnv" "hash/fnv"
"io/ioutil"
"net/url" "net/url"
"os"
"path/filepath" "path/filepath"
"sort" "sort"
"strings" "strings"
@ -214,7 +214,7 @@ func Parse(pathPatterns []string, validateTplFn ValidateTplFn, validateExpressio
} }
func parseFile(path string) ([]Group, error) { func parseFile(path string) ([]Group, error) {
data, err := ioutil.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
return nil, fmt.Errorf("error reading alert rule file: %w", err) 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) { func writeToFile(t *testing.T, file, b string) {
t.Helper() t.Helper()
err := ioutil.WriteFile(file, []byte(b), 0644) err := os.WriteFile(file, []byte(b), 0644)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -2,7 +2,6 @@ package cgroup
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"runtime" "runtime"
"strconv" "strconv"
@ -80,7 +79,7 @@ func getCPUStat(statName string) (int64, error) {
func getOnlineCPUCount() float64 { func getOnlineCPUCount() float64 {
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/685#issuecomment-674423728 // 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 { if err != nil {
return -1 return -1
} }

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -3,7 +3,6 @@ package mergeset
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
@ -1259,7 +1258,7 @@ func runTransaction(txnLock *sync.RWMutex, pathPrefix, txnPath string) error {
txnLock.RLock() txnLock.RLock()
defer txnLock.RUnlock() defer txnLock.RUnlock()
data, err := ioutil.ReadFile(txnPath) data, err := os.ReadFile(txnPath)
if err != nil { if err != nil {
return fmt.Errorf("cannot read transaction file: %w", err) 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 { if err != nil {
return fmt.Errorf("cannot marshal persistent queue metainfo %#v: %w", mi, err) 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) return fmt.Errorf("cannot write persistent queue metainfo to %q: %w", path, err)
} }
fs.MustSyncPath(path) fs.MustSyncPath(path)
@ -680,7 +680,7 @@ func (mi *metainfo) WriteToFile(path string) error {
func (mi *metainfo) ReadFromFile(path string) error { func (mi *metainfo) ReadFromFile(path string) error {
mi.Reset() mi.Reset()
data, err := ioutil.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return err return err

View file

@ -2,7 +2,6 @@ package persistentqueue
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"strconv" "strconv"
"testing" "testing"
@ -371,7 +370,7 @@ func TestQueueLimitedSize(t *testing.T) {
} }
func mustCreateFile(path, contents string) { 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)) 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 ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/url" "net/url"
"os" "os"
"strconv" "strconv"
@ -145,7 +144,7 @@ func getCloudEnvByName(name string) (*cloudEnvironmentEndpoints, error) {
} }
func readCloudEndpointsFromFile(filePath string) (*cloudEnvironmentEndpoints, error) { func readCloudEndpointsFromFile(filePath string) (*cloudEnvironmentEndpoints, error) {
data, err := ioutil.ReadFile(filePath) data, err := os.ReadFile(filePath)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot file %q: %w", filePath, err) return nil, fmt.Errorf("cannot file %q: %w", filePath, err)
} }

View file

@ -3,7 +3,6 @@ package consul
import ( import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -109,7 +108,7 @@ func getToken(token *promauth.Secret) (string, error) {
return token.String(), nil return token.String(), nil
} }
if tokenFile := os.Getenv("CONSUL_HTTP_TOKEN_FILE"); tokenFile != "" { if tokenFile := os.Getenv("CONSUL_HTTP_TOKEN_FILE"); tokenFile != "" {
data, err := ioutil.ReadFile(tokenFile) data, err := os.ReadFile(tokenFile)
if err != nil { 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) 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" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os"
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
@ -66,7 +67,7 @@ func newAPIWatcher(apiServer string, ac *promauth.Config, sdc *SDConfig, swcFunc
namespaces := sdc.Namespaces.Names namespaces := sdc.Namespaces.Names
if len(namespaces) == 0 { if len(namespaces) == 0 {
if sdc.Namespaces.OwnNamespace { 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 { if err != nil {
logger.Fatalf("cannot determine namespace for the current pod according to `own_namespace: true` option in kubernetes_sd_config: %s", err) 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 ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -131,7 +130,7 @@ func (ph *partHeader) Reset() {
func (ph *partHeader) readMinDedupInterval(partPath string) error { func (ph *partHeader) readMinDedupInterval(partPath string) error {
filePath := partPath + "/min_dedup_interval" filePath := partPath + "/min_dedup_interval"
data, err := ioutil.ReadFile(filePath) data, err := os.ReadFile(filePath)
if err != nil { if err != nil {
if errors.Is(err, os.ErrNotExist) { if errors.Is(err, os.ErrNotExist) {
// The minimum dedup interval may not exist for old parts. // The minimum dedup interval may not exist for old parts.

View file

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

View file

@ -5,7 +5,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"math" "math"
"os" "os"
"path/filepath" "path/filepath"
@ -845,7 +844,7 @@ func (s *Storage) mustLoadNextDayMetricIDs(date uint64) *byDateMetricIDEntry {
logger.Infof("nothing to load from %q", path) logger.Infof("nothing to load from %q", path)
return e return e
} }
src, err := ioutil.ReadFile(path) src, err := os.ReadFile(path)
if err != nil { if err != nil {
logger.Panicf("FATAL: cannot read %s: %s", path, err) 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) logger.Infof("nothing to load from %q", path)
return hm return hm
} }
src, err := ioutil.ReadFile(path) src, err := os.ReadFile(path)
if err != nil { if err != nil {
logger.Panicf("FATAL: cannot read %s: %s", path, err) logger.Panicf("FATAL: cannot read %s: %s", path, err)
} }
@ -938,7 +937,7 @@ func (s *Storage) mustSaveNextDayMetricIDs(e *byDateMetricIDEntry) {
// Marshal e.v // Marshal e.v
dst = marshalUint64Set(dst, &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.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)) 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 // Marshal hm.m
dst = marshalUint64Set(dst, 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.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)) 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) { func loadMinTimestampForCompositeIndex(path string) (int64, error) {
data, err := ioutil.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
return 0, err return 0, err
} }