Skip to content

Commit

Permalink
refactor: move from io/ioutil to io and os package
Browse files Browse the repository at this point in the history
The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee committed Sep 26, 2021
1 parent b25a9e5 commit b1f6c32
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 35 deletions.
5 changes: 2 additions & 3 deletions app/dns/nameserver_doh.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"sync"
Expand Down Expand Up @@ -283,11 +282,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
Original file line number Diff line number Diff line change
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
6 changes: 3 additions & 3 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package common
import (
"fmt"
"go/build"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -70,7 +70,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 @@ -153,7 +153,7 @@ func FetchHTTPContent(target string) ([]byte, error) {
return nil, newError("unexpected HTTP status code: ", resp.StatusCode)
}

content, err := ioutil.ReadAll(resp.Body)
content, err := io.ReadAll(resp.Body)
if err != nil {
return nil, newError("failed to read HTTP response").Base(err)
}
Expand Down
3 changes: 1 addition & 2 deletions common/drain/drainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package drain

import (
"io"
"io/ioutil"

"github.com/v2fly/v2ray-core/v4/common/dice"
)
Expand Down Expand Up @@ -36,7 +35,7 @@ func (d *BehaviorSeedLimitedDrainer) Drain(reader io.Reader) error {
}

func drainReadN(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 common/log/logger_test.go
Original file line number Diff line number Diff line change
@@ -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 infra/conf/geodata/memconservative/cache.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package memconservative

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

"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -53,7 +53,7 @@ func (g GeoIPCache) Unmarshal(filename, code string) (*router.GeoIP, error) {
case errFailedToReadBytes, errFailedToReadExpectedLenBytes,
errInvalidGeodataFile, errInvalidGeodataVarintLength:
newError("failed to decode geoip file: ", filename, ", fallback to the original ReadFile method")
geoipBytes, err = ioutil.ReadFile(asset)
geoipBytes, err = os.ReadFile(asset)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func (g GeoSiteCache) Unmarshal(filename, code string) (*router.GeoSite, error)
case errFailedToReadBytes, errFailedToReadExpectedLenBytes,
errInvalidGeodataFile, errInvalidGeodataVarintLength:
newError("failed to decode geoip file: ", filename, ", fallback to the original ReadFile method")
geositeBytes, err = ioutil.ReadFile(asset)
geositeBytes, err = os.ReadFile(asset)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions infra/control/certchainhash.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package control
import (
"flag"
"fmt"
"io/ioutil"
"os"

v2tls "github.com/v2fly/v2ray-core/v4/transport/internet/tls"
)
Expand All @@ -30,7 +30,7 @@ func (c CertificateChainHashCommand) Execute(args []string) error {
if err := fs.Parse(args); err != nil {
return err
}
certContent, err := ioutil.ReadFile(*cert)
certContent, err := os.ReadFile(*cert)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions infra/control/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package control
import (
"bytes"
"io"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -73,10 +72,10 @@ func (c *ConfigCommand) 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
3 changes: 1 addition & 2 deletions infra/vprotogen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"go/build"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -46,7 +45,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/confloader/external/external.go
Original file line number Diff line number Diff line change
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/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
Expand Down Expand Up @@ -54,7 +53,7 @@ func dirExists(file string) bool {
}

func readConfDir(dirPath string) {
confs, err := ioutil.ReadDir(dirPath)
confs, err := os.ReadDir(dirPath)
if err != nil {
log.Fatalln(err)
}
Expand Down
4 changes: 2 additions & 2 deletions testing/scenarios/common.go
Original file line number Diff line number Diff line change
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("", "v2ray")
dir, err := os.MkdirTemp("", "v2ray")
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions testing/scenarios/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package scenarios

import (
"context"
"io/ioutil"
"io"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -643,7 +643,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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"crypto/rand"
"io"
"io/ioutil"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -75,7 +74,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 @@ -271,7 +270,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 @@ -368,7 +367,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 b1f6c32

Please sign in to comment.