Move from deprecated ioutil to os and io packages (#744)

This commit is contained in:
KallyDev 2021-09-29 02:49:34 +08:00 committed by GitHub
parent 1ef824c0b4
commit 4abf98c1be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 29 additions and 43 deletions

View File

@ -5,7 +5,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"sync" "sync"
@ -315,11 +314,11 @@ func (s *DoHNameServer) dohHTTPSContext(ctx context.Context, b []byte) ([]byte,
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
io.Copy(ioutil.Discard, resp.Body) // flush resp.Body so that the conn is reusable io.Copy(io.Discard, resp.Body) // flush resp.Body so that the conn is reusable
return nil, fmt.Errorf("DOH server returned code %d", resp.StatusCode) return nil, fmt.Errorf("DOH server returned code %d", resp.StatusCode)
} }
return ioutil.ReadAll(resp.Body) return io.ReadAll(resp.Body)
} }
func (s *DoHNameServer) findIPsForDomain(domain string, option dns_feature.IPOption) ([]net.IP, error) { func (s *DoHNameServer) findIPsForDomain(domain string, option dns_feature.IPOption) ([]net.IP, error) {

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"crypto/rand" "crypto/rand"
"io" "io"
"io/ioutil"
"os" "os"
"testing" "testing"
@ -120,7 +119,7 @@ func TestMultiBufferReadAllToByte(t *testing.T) {
common.Must(err) common.Must(err)
f.Close() f.Close()
cnt, err := ioutil.ReadFile(dat) cnt, err := os.ReadFile(dat)
common.Must(err) common.Must(err)
if d := cmp.Diff(buf2, cnt); d != "" { if d := cmp.Diff(buf2, cnt); d != "" {

View File

@ -5,7 +5,6 @@ package common
import ( import (
"fmt" "fmt"
"go/build" "go/build"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -69,7 +68,7 @@ func GetRuntimeEnv(key string) (string, error) {
} }
var data []byte var data []byte
var runtimeEnv string var runtimeEnv string
data, readErr := ioutil.ReadFile(file) data, readErr := os.ReadFile(file)
if readErr != nil { if readErr != nil {
return "", readErr return "", readErr
} }
@ -131,7 +130,7 @@ func GetModuleName(pathToProjectRoot string) (string, error) {
for { for {
if idx := strings.LastIndex(loopPath, string(filepath.Separator)); idx >= 0 { if idx := strings.LastIndex(loopPath, string(filepath.Separator)); idx >= 0 {
gomodPath := filepath.Join(loopPath, "go.mod") gomodPath := filepath.Join(loopPath, "go.mod")
gomodBytes, err := ioutil.ReadFile(gomodPath) gomodBytes, err := os.ReadFile(gomodPath)
if err != nil { if err != nil {
loopPath = loopPath[:idx] loopPath = loopPath[:idx]
continue continue

View File

@ -1,7 +1,6 @@
package log_test package log_test
import ( import (
"io/ioutil"
"os" "os"
"strings" "strings"
"testing" "testing"
@ -13,7 +12,7 @@ import (
) )
func TestFileLogger(t *testing.T) { func TestFileLogger(t *testing.T) {
f, err := ioutil.TempFile("", "vtest") f, err := os.CreateTemp("", "vtest")
common.Must(err) common.Must(err)
path := f.Name() path := f.Name()
common.Must(f.Close()) common.Must(f.Close())

View File

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"io/ioutil" "io"
"net/http" "net/http"
"os" "os"
@ -74,7 +74,7 @@ func GetOCSPForCert(cert [][]byte) ([]byte, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
issuerBytes, errC := ioutil.ReadAll(resp.Body) issuerBytes, errC := io.ReadAll(resp.Body)
if errC != nil { if errC != nil {
return nil, newError(errC) return nil, newError(errC)
} }
@ -98,7 +98,7 @@ func GetOCSPForCert(cert [][]byte) ([]byte, error) {
return nil, newError(err) return nil, newError(err)
} }
defer req.Body.Close() defer req.Body.Close()
ocspResBytes, err := ioutil.ReadAll(req.Body) ocspResBytes, err := io.ReadAll(req.Body)
if err != nil { if err != nil {
return nil, newError(err) return nil, newError(err)

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"io" "io"
"io/ioutil"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"github.com/pelletier/go-toml" "github.com/pelletier/go-toml"
@ -88,7 +87,7 @@ func LoadJSONConfig(reader io.Reader) (*core.Config, error) {
// DecodeTOMLConfig reads from reader and decode the config into *conf.Config // DecodeTOMLConfig reads from reader and decode the config into *conf.Config
// using github.com/pelletier/go-toml and map to convert toml to json. // using github.com/pelletier/go-toml and map to convert toml to json.
func DecodeTOMLConfig(reader io.Reader) (*conf.Config, error) { func DecodeTOMLConfig(reader io.Reader) (*conf.Config, error) {
tomlFile, err := ioutil.ReadAll(reader) tomlFile, err := io.ReadAll(reader)
if err != nil { if err != nil {
return nil, newError("failed to read config file").Base(err) return nil, newError("failed to read config file").Base(err)
} }
@ -123,7 +122,7 @@ func LoadTOMLConfig(reader io.Reader) (*core.Config, error) {
// DecodeYAMLConfig reads from reader and decode the config into *conf.Config // DecodeYAMLConfig reads from reader and decode the config into *conf.Config
// using github.com/ghodss/yaml to convert yaml to json. // using github.com/ghodss/yaml to convert yaml to json.
func DecodeYAMLConfig(reader io.Reader) (*conf.Config, error) { func DecodeYAMLConfig(reader io.Reader) (*conf.Config, error) {
yamlFile, err := ioutil.ReadAll(reader) yamlFile, err := io.ReadAll(reader)
if err != nil { if err != nil {
return nil, newError("failed to read config file").Base(err) return nil, newError("failed to read config file").Base(err)
} }

View File

@ -4,7 +4,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"go/build" "go/build"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -45,7 +44,7 @@ func GetRuntimeEnv(key string) (string, error) {
} }
var data []byte var data []byte
var runtimeEnv string var runtimeEnv string
data, readErr := ioutil.ReadFile(file) data, readErr := os.ReadFile(file)
if readErr != nil { if readErr != nil {
return "", readErr return "", readErr
} }

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"go/build" "go/build"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
@ -49,7 +48,7 @@ func GetRuntimeEnv(key string) (string, error) {
} }
var data []byte var data []byte
var runtimeEnv string var runtimeEnv string
data, readErr := ioutil.ReadFile(file) data, readErr := os.ReadFile(file)
if readErr != nil { if readErr != nil {
return "", readErr return "", readErr
} }

View File

@ -6,7 +6,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -56,10 +55,10 @@ func loadArg(arg string) (out io.Reader, err error) {
data, err = fetchHTTPContent(arg) data, err = fetchHTTPContent(arg)
case arg == "stdin:": case arg == "stdin:":
data, err = ioutil.ReadAll(os.Stdin) data, err = io.ReadAll(os.Stdin)
default: default:
data, err = ioutil.ReadFile(arg) data, err = os.ReadFile(arg)
} }
if err != nil { if err != nil {

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -77,10 +76,10 @@ func loadArg(arg string) (out io.Reader, err error) {
data, err = FetchHTTPContent(arg) data, err = FetchHTTPContent(arg)
case arg == "stdin:": case arg == "stdin:":
data, err = ioutil.ReadAll(os.Stdin) data, err = io.ReadAll(os.Stdin)
default: default:
data, err = ioutil.ReadFile(arg) data, err = os.ReadFile(arg)
} }
if err != nil { if err != nil {

View File

@ -5,7 +5,6 @@ package external
import ( import (
"bytes" "bytes"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -24,10 +23,10 @@ func ConfigLoader(arg string) (out io.Reader, err error) {
data, err = FetchHTTPContent(arg) data, err = FetchHTTPContent(arg)
case arg == "stdin:": case arg == "stdin:":
data, err = ioutil.ReadAll(os.Stdin) data, err = io.ReadAll(os.Stdin)
default: default:
data, err = ioutil.ReadFile(arg) data, err = os.ReadFile(arg)
} }
if err != nil { if err != nil {

View File

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"os/signal" "os/signal"
@ -126,7 +125,7 @@ func getRegepxByFormat() string {
} }
func readConfDir(dirPath string) { func readConfDir(dirPath string) {
confs, err := ioutil.ReadDir(dirPath) confs, err := os.ReadDir(dirPath)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }

View File

@ -7,7 +7,6 @@ import (
"crypto/sha256" "crypto/sha256"
"hash/crc32" "hash/crc32"
"io" "io"
"io/ioutil"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
@ -160,7 +159,7 @@ func ReadTCPSession(validator *Validator, reader io.Reader) (*protocol.RequestHe
} }
func DrainConnN(reader io.Reader, n int) error { func DrainConnN(reader io.Reader, n int) error {
_, err := io.CopyN(ioutil.Discard, reader, int64(n)) _, err := io.CopyN(io.Discard, reader, int64(n))
return err return err
} }

View File

@ -9,7 +9,6 @@ import (
"encoding/binary" "encoding/binary"
"hash/fnv" "hash/fnv"
"io" "io"
"io/ioutil"
"sync" "sync"
"time" "time"
@ -499,6 +498,6 @@ func (s *ServerSession) EncodeResponseBody(request *protocol.RequestHeader, writ
} }
func (s *ServerSession) DrainConnN(reader io.Reader, n int) error { func (s *ServerSession) DrainConnN(reader io.Reader, n int) error {
_, err := io.CopyN(ioutil.Discard, reader, int64(n)) _, err := io.CopyN(io.Discard, reader, int64(n))
return err return err
} }

View File

@ -5,7 +5,7 @@ import (
"crypto/rand" "crypto/rand"
"fmt" "fmt"
"io" "io"
"io/ioutil" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -102,7 +102,7 @@ func genTestBinaryPath() {
testBinaryPathGen.Do(func() { testBinaryPathGen.Do(func() {
var tempDir string var tempDir string
common.Must(retry.Timed(5, 100).On(func() error { common.Must(retry.Timed(5, 100).On(func() error {
dir, err := ioutil.TempDir("", "xray") dir, err := os.MkdirTemp("", "xray")
if err != nil { if err != nil {
return err return err
} }

View File

@ -2,7 +2,7 @@ package scenarios
import ( import (
"context" "context"
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"testing" "testing"
@ -642,7 +642,7 @@ func TestDomainSniffing(t *testing.T) {
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
t.Error("unexpected status code: ", resp.StatusCode) t.Error("unexpected status code: ", resp.StatusCode)
} }
common.Must(resp.Write(ioutil.Discard)) common.Must(resp.Write(io.Discard))
} }
} }

View File

@ -5,7 +5,6 @@ import (
"context" "context"
"crypto/rand" "crypto/rand"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"testing" "testing"
@ -74,7 +73,7 @@ func TestHttpConformance(t *testing.T) {
t.Fatal("status: ", resp.StatusCode) t.Fatal("status: ", resp.StatusCode)
} }
content, err := ioutil.ReadAll(resp.Body) content, err := io.ReadAll(resp.Body)
common.Must(err) common.Must(err)
if string(content) != "Home" { if string(content) != "Home" {
t.Fatal("body: ", string(content)) t.Fatal("body: ", string(content))
@ -267,7 +266,7 @@ func TestHttpPost(t *testing.T) {
t.Fatal("status: ", resp.StatusCode) t.Fatal("status: ", resp.StatusCode)
} }
content, err := ioutil.ReadAll(resp.Body) content, err := io.ReadAll(resp.Body)
common.Must(err) common.Must(err)
if r := cmp.Diff(content, xor(payload)); r != "" { if r := cmp.Diff(content, xor(payload)); r != "" {
t.Fatal(r) t.Fatal(r)
@ -361,7 +360,7 @@ func TestHttpBasicAuth(t *testing.T) {
t.Fatal("status: ", resp.StatusCode) t.Fatal("status: ", resp.StatusCode)
} }
content, err := ioutil.ReadAll(resp.Body) content, err := io.ReadAll(resp.Body)
common.Must(err) common.Must(err)
if string(content) != "Home" { if string(content) != "Home" {
t.Fatal("body: ", string(content)) t.Fatal("body: ", string(content))