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

View File

@ -4,7 +4,6 @@ import (
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"strings"
@ -36,7 +35,7 @@ func (handler localDoHHandler) ServeHTTP(writer http.ResponseWriter, request *ht
start := time.Now()
if request.Method == "POST" &&
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 {
dlog.Warnf("No body in a local DoH query")
return

View File

@ -3,7 +3,6 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"net/url"
"os"
@ -54,10 +53,10 @@ var timeNow = time.Now
func (source *Source) fetchFromCache(now time.Time) (delay time.Duration, err error) {
var bin, sig []byte
if bin, err = ioutil.ReadFile(source.cacheFile); err != nil {
if bin, err = os.ReadFile(source.cacheFile); err != nil {
return
}
if sig, err = ioutil.ReadFile(source.cacheFile + ".minisig"); err != nil {
if sig, err = os.ReadFile(source.cacheFile + ".minisig"); err != nil {
return
}
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 {
bin, err := ioutil.ReadFile(filepath.Join("testdata", name))
bin, err := os.ReadFile(filepath.Join("testdata", name))
if err != nil {
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 {
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)
}
if err := acl.Chmod(path, perms); err != nil {
@ -109,7 +109,7 @@ func checkSourceCache(c *check.C, e *SourceTestExpect) {
for _, f := range e.cache {
path := e.cachePath + f.suffix
_ = 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)
if f.suffix != "" {
continue

View File

@ -10,11 +10,11 @@ import (
"encoding/hex"
"errors"
"io"
"io/ioutil"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
@ -179,7 +179,7 @@ func (xTransport *XTransport) rebuildTransport() {
if certPool == nil {
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 {
dlog.Fatal(err)
}
@ -470,7 +470,7 @@ func (xTransport *XTransport) Fetch(
}
if body != nil {
req.ContentLength = int64(len(*body))
req.Body = ioutil.NopCloser(bytes.NewReader(*body))
req.Body = io.NopCloser(bytes.NewReader(*body))
}
start := time.Now()
resp, err := client.Do(req)
@ -528,7 +528,7 @@ func (xTransport *XTransport) Fetch(
}
}
tls := resp.TLS
bin, err := ioutil.ReadAll(io.LimitReader(resp.Body, MaxHTTPBodyLength))
bin, err := io.ReadAll(io.LimitReader(resp.Body, MaxHTTPBodyLength))
if err != nil {
return nil, statusCode, tls, rtt, err
}