Mostly get rid of ioutil

This commit is contained in:
Frank Denis 2023-02-02 19:38:24 +01:00
parent 33c8027e0a
commit 3f23ff5c08
5 changed files with 11 additions and 14 deletions

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"errors" "errors"
"io/ioutil"
"net" "net"
"os" "os"
"strconv" "strconv"
@ -161,7 +160,7 @@ func ExtractHostAndPort(str string, defaultPort int) (host string, port int) {
} }
func ReadTextFile(filename string) (string, error) { func ReadTextFile(filename string) (string, error) {
bin, err := ioutil.ReadFile(filename) bin, err := os.ReadFile(filename)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -4,7 +4,6 @@ import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"strings" "strings"
@ -36,7 +35,7 @@ func (handler localDoHHandler) ServeHTTP(writer http.ResponseWriter, request *ht
start := time.Now() start := time.Now()
if request.Method == "POST" && if request.Method == "POST" &&
request.Header.Get("Content-Type") == dataType { request.Header.Get("Content-Type") == dataType {
packet, err = ioutil.ReadAll(io.LimitReader(request.Body, int64(MaxDNSPacketSize))) packet, err = io.ReadAll(io.LimitReader(request.Body, int64(MaxDNSPacketSize)))
if err != nil { if err != nil {
dlog.Warnf("No body in a local DoH query") dlog.Warnf("No body in a local DoH query")
return return

View File

@ -3,7 +3,6 @@ package main
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil"
"math/rand" "math/rand"
"net/url" "net/url"
"os" "os"
@ -54,10 +53,10 @@ var timeNow = time.Now
func (source *Source) fetchFromCache(now time.Time) (delay time.Duration, err error) { func (source *Source) fetchFromCache(now time.Time) (delay time.Duration, err error) {
var bin, sig []byte var bin, sig []byte
if bin, err = ioutil.ReadFile(source.cacheFile); err != nil { if bin, err = os.ReadFile(source.cacheFile); err != nil {
return return
} }
if sig, err = ioutil.ReadFile(source.cacheFile + ".minisig"); err != nil { if sig, err = os.ReadFile(source.cacheFile + ".minisig"); err != nil {
return return
} }
if err = source.checkSignature(bin, sig); err != nil { if err = source.checkSignature(bin, sig); err != nil {

View File

@ -69,7 +69,7 @@ type SourceTestExpect struct {
} }
func readFixture(t *testing.T, name string) []byte { func readFixture(t *testing.T, name string) []byte {
bin, err := ioutil.ReadFile(filepath.Join("testdata", name)) bin, err := os.ReadFile(filepath.Join("testdata", name))
if err != nil { if err != nil {
t.Fatalf("Unable to read test fixture %s: %v", name, err) t.Fatalf("Unable to read test fixture %s: %v", name, err)
} }
@ -86,7 +86,7 @@ func writeSourceCache(t *testing.T, e *SourceTestExpect) {
if perms == 0 { if perms == 0 {
perms = 0644 perms = 0644
} }
if err := ioutil.WriteFile(path, f.content, perms); err != nil { if err := os.WriteFile(path, f.content, perms); err != nil {
t.Fatalf("Unable to write cache file %s: %v", path, err) t.Fatalf("Unable to write cache file %s: %v", path, err)
} }
if err := acl.Chmod(path, perms); err != nil { if err := acl.Chmod(path, perms); err != nil {
@ -109,7 +109,7 @@ func checkSourceCache(c *check.C, e *SourceTestExpect) {
for _, f := range e.cache { for _, f := range e.cache {
path := e.cachePath + f.suffix path := e.cachePath + f.suffix
_ = acl.Chmod(path, 0644) // don't worry if this fails, reading it will catch the same problem _ = acl.Chmod(path, 0644) // don't worry if this fails, reading it will catch the same problem
got, err := ioutil.ReadFile(path) got, err := os.ReadFile(path)
c.DeepEqual(got, f.content, "Unexpected content for cache file '%s', err %v", path, err) c.DeepEqual(got, f.content, "Unexpected content for cache file '%s', err %v", path, err)
if f.suffix != "" { if f.suffix != "" {
continue continue

View File

@ -10,11 +10,11 @@ import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"io" "io"
"io/ioutil"
"math/rand" "math/rand"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
"os"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -179,7 +179,7 @@ func (xTransport *XTransport) rebuildTransport() {
if certPool == nil { if certPool == nil {
dlog.Fatalf("Additional CAs not supported on this platform: %v", certPoolErr) dlog.Fatalf("Additional CAs not supported on this platform: %v", certPoolErr)
} }
additionalCaCert, err := ioutil.ReadFile(clientCreds.rootCA) additionalCaCert, err := os.ReadFile(clientCreds.rootCA)
if err != nil { if err != nil {
dlog.Fatal(err) dlog.Fatal(err)
} }
@ -470,7 +470,7 @@ func (xTransport *XTransport) Fetch(
} }
if body != nil { if body != nil {
req.ContentLength = int64(len(*body)) req.ContentLength = int64(len(*body))
req.Body = ioutil.NopCloser(bytes.NewReader(*body)) req.Body = io.NopCloser(bytes.NewReader(*body))
} }
start := time.Now() start := time.Now()
resp, err := client.Do(req) resp, err := client.Do(req)
@ -528,7 +528,7 @@ func (xTransport *XTransport) Fetch(
} }
} }
tls := resp.TLS tls := resp.TLS
bin, err := ioutil.ReadAll(io.LimitReader(resp.Body, MaxHTTPBodyLength)) bin, err := io.ReadAll(io.LimitReader(resp.Body, MaxHTTPBodyLength))
if err != nil { if err != nil {
return nil, statusCode, tls, rtt, err return nil, statusCode, tls, rtt, err
} }