Skip to content

Commit

Permalink
Move from deprecated ioutil to os and io packages (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
kallydev committed Sep 28, 2021
1 parent 1ef824c commit 4abf98c
Show file tree
Hide file tree
Showing 17 changed files with 29 additions and 43 deletions.
5 changes: 2 additions & 3 deletions app/dns/dohdns.go
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"sync"
Expand Down Expand Up @@ -315,11 +314,11 @@ func (s *DoHNameServer) dohHTTPSContext(ctx context.Context, b []byte) ([]byte,

defer resp.Body.Close()
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 ioutil.ReadAll(resp.Body)
return io.ReadAll(resp.Body)
}

func (s *DoHNameServer) findIPsForDomain(domain string, option dns_feature.IPOption) ([]net.IP, error) {
Expand Down
3 changes: 1 addition & 2 deletions common/buf/multi_buffer_test.go
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"crypto/rand"
"io"
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -120,7 +119,7 @@ func TestMultiBufferReadAllToByte(t *testing.T) {
common.Must(err)
f.Close()

cnt, err := ioutil.ReadFile(dat)
cnt, err := os.ReadFile(dat)
common.Must(err)

if d := cmp.Diff(buf2, cnt); d != "" {
Expand Down
5 changes: 2 additions & 3 deletions common/common.go
Expand Up @@ -5,7 +5,6 @@ package common
import (
"fmt"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -69,7 +68,7 @@ func GetRuntimeEnv(key string) (string, error) {
}
var data []byte
var runtimeEnv string
data, readErr := ioutil.ReadFile(file)
data, readErr := os.ReadFile(file)
if readErr != nil {
return "", readErr
}
Expand Down Expand Up @@ -131,7 +130,7 @@ func GetModuleName(pathToProjectRoot string) (string, error) {
for {
if idx := strings.LastIndex(loopPath, string(filepath.Separator)); idx >= 0 {
gomodPath := filepath.Join(loopPath, "go.mod")
gomodBytes, err := ioutil.ReadFile(gomodPath)
gomodBytes, err := os.ReadFile(gomodPath)
if err != nil {
loopPath = loopPath[:idx]
continue
Expand Down
3 changes: 1 addition & 2 deletions common/log/logger_test.go
@@ -1,7 +1,6 @@
package log_test

import (
"io/ioutil"
"os"
"strings"
"testing"
Expand All @@ -13,7 +12,7 @@ import (
)

func TestFileLogger(t *testing.T) {
f, err := ioutil.TempFile("", "vtest")
f, err := os.CreateTemp("", "vtest")
common.Must(err)
path := f.Name()
common.Must(f.Close())
Expand Down
6 changes: 3 additions & 3 deletions common/ocsp/ocsp.go
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"io"
"net/http"
"os"

Expand Down Expand Up @@ -74,7 +74,7 @@ func GetOCSPForCert(cert [][]byte) ([]byte, error) {
}
defer resp.Body.Close()

issuerBytes, errC := ioutil.ReadAll(resp.Body)
issuerBytes, errC := io.ReadAll(resp.Body)
if errC != nil {
return nil, newError(errC)
}
Expand All @@ -98,7 +98,7 @@ func GetOCSPForCert(cert [][]byte) ([]byte, error) {
return nil, newError(err)
}
defer req.Body.Close()
ocspResBytes, err := ioutil.ReadAll(req.Body)
ocspResBytes, err := io.ReadAll(req.Body)

if err != nil {
return nil, newError(err)
Expand Down
5 changes: 2 additions & 3 deletions infra/conf/serial/loader.go
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"io"
"io/ioutil"

"github.com/ghodss/yaml"
"github.com/pelletier/go-toml"
Expand Down Expand Up @@ -88,7 +87,7 @@ func LoadJSONConfig(reader io.Reader) (*core.Config, error) {
// DecodeTOMLConfig reads from reader and decode the config into *conf.Config
// using github.com/pelletier/go-toml and map to convert toml to json.
func DecodeTOMLConfig(reader io.Reader) (*conf.Config, error) {
tomlFile, err := ioutil.ReadAll(reader)
tomlFile, err := io.ReadAll(reader)
if err != nil {
return nil, newError("failed to read config file").Base(err)
}
Expand Down Expand Up @@ -123,7 +122,7 @@ func LoadTOMLConfig(reader io.Reader) (*core.Config, error) {
// DecodeYAMLConfig reads from reader and decode the config into *conf.Config
// using github.com/ghodss/yaml to convert yaml to json.
func DecodeYAMLConfig(reader io.Reader) (*conf.Config, error) {
yamlFile, err := ioutil.ReadAll(reader)
yamlFile, err := io.ReadAll(reader)
if err != nil {
return nil, newError("failed to read config file").Base(err)
}
Expand Down
3 changes: 1 addition & 2 deletions infra/vformat/main.go
Expand Up @@ -4,7 +4,6 @@ import (
"flag"
"fmt"
"go/build"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -45,7 +44,7 @@ func GetRuntimeEnv(key string) (string, error) {
}
var data []byte
var runtimeEnv string
data, readErr := ioutil.ReadFile(file)
data, readErr := os.ReadFile(file)
if readErr != nil {
return "", readErr
}
Expand Down
3 changes: 1 addition & 2 deletions infra/vprotogen/main.go
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"go/build"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -49,7 +48,7 @@ func GetRuntimeEnv(key string) (string, error) {
}
var data []byte
var runtimeEnv string
data, readErr := ioutil.ReadFile(file)
data, readErr := os.ReadFile(file)
if readErr != nil {
return "", readErr
}
Expand Down
5 changes: 2 additions & 3 deletions main/commands/all/api/shared.go
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -56,10 +55,10 @@ func loadArg(arg string) (out io.Reader, err error) {
data, err = fetchHTTPContent(arg)

case arg == "stdin:":
data, err = ioutil.ReadAll(os.Stdin)
data, err = io.ReadAll(os.Stdin)

default:
data, err = ioutil.ReadFile(arg)
data, err = os.ReadFile(arg)
}

if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions main/commands/all/convert.go
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -77,10 +76,10 @@ func loadArg(arg string) (out io.Reader, err error) {
data, err = FetchHTTPContent(arg)

case arg == "stdin:":
data, err = ioutil.ReadAll(os.Stdin)
data, err = io.ReadAll(os.Stdin)

default:
data, err = ioutil.ReadFile(arg)
data, err = os.ReadFile(arg)
}

if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions main/confloader/external/external.go
Expand Up @@ -5,7 +5,6 @@ package external
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand All @@ -24,10 +23,10 @@ func ConfigLoader(arg string) (out io.Reader, err error) {
data, err = FetchHTTPContent(arg)

case arg == "stdin:":
data, err = ioutil.ReadAll(os.Stdin)
data, err = io.ReadAll(os.Stdin)

default:
data, err = ioutil.ReadFile(arg)
data, err = os.ReadFile(arg)
}

if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions main/run.go
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
Expand Down Expand Up @@ -126,7 +125,7 @@ func getRegepxByFormat() string {
}

func readConfDir(dirPath string) {
confs, err := ioutil.ReadDir(dirPath)
confs, err := os.ReadDir(dirPath)
if err != nil {
log.Fatalln(err)
}
Expand Down
3 changes: 1 addition & 2 deletions proxy/shadowsocks/protocol.go
Expand Up @@ -7,7 +7,6 @@ import (
"crypto/sha256"
"hash/crc32"
"io"
"io/ioutil"

"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
Expand Down Expand Up @@ -160,7 +159,7 @@ func ReadTCPSession(validator *Validator, reader io.Reader) (*protocol.RequestHe
}

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
}

Expand Down
3 changes: 1 addition & 2 deletions proxy/vmess/encoding/server.go
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/binary"
"hash/fnv"
"io"
"io/ioutil"
"sync"
"time"

Expand Down Expand Up @@ -499,6 +498,6 @@ func (s *ServerSession) EncodeResponseBody(request *protocol.RequestHeader, writ
}

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
}
4 changes: 2 additions & 2 deletions testing/scenarios/common.go
Expand Up @@ -5,7 +5,7 @@ import (
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -102,7 +102,7 @@ func genTestBinaryPath() {
testBinaryPathGen.Do(func() {
var tempDir string
common.Must(retry.Timed(5, 100).On(func() error {
dir, err := ioutil.TempDir("", "xray")
dir, err := os.MkdirTemp("", "xray")
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions testing/scenarios/feature_test.go
Expand Up @@ -2,7 +2,7 @@ package scenarios

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

Expand Down
7 changes: 3 additions & 4 deletions testing/scenarios/http_test.go
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"crypto/rand"
"io"
"io/ioutil"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -74,7 +73,7 @@ func TestHttpConformance(t *testing.T) {
t.Fatal("status: ", resp.StatusCode)
}

content, err := ioutil.ReadAll(resp.Body)
content, err := io.ReadAll(resp.Body)
common.Must(err)
if string(content) != "Home" {
t.Fatal("body: ", string(content))
Expand Down Expand Up @@ -267,7 +266,7 @@ func TestHttpPost(t *testing.T) {
t.Fatal("status: ", resp.StatusCode)
}

content, err := ioutil.ReadAll(resp.Body)
content, err := io.ReadAll(resp.Body)
common.Must(err)
if r := cmp.Diff(content, xor(payload)); r != "" {
t.Fatal(r)
Expand Down Expand Up @@ -361,7 +360,7 @@ func TestHttpBasicAuth(t *testing.T) {
t.Fatal("status: ", resp.StatusCode)
}

content, err := ioutil.ReadAll(resp.Body)
content, err := io.ReadAll(resp.Body)
common.Must(err)
if string(content) != "Home" {
t.Fatal("body: ", string(content))
Expand Down

0 comments on commit 4abf98c

Please sign in to comment.