git » chasquid » commit 3ebe5c5

Replace uses of ioutil

author Alberto Bertogli
2022-11-12 20:02:52 UTC
committer Alberto Bertogli
2022-11-12 20:06:35 UTC
parent 008367d3202593f7d474fa5f30b6c2f9f59a81a7

Replace uses of ioutil

ioutil package was deprecated in Go 1.16, replace all uses with their
respective replacements.

This patch was generated with a combination of `gofmt -r`, `eg`, and
manually (for `ioutil.ReadDir`).

chasquid.go +5 -6
cmd/chasquid-util/chasquid-util.go +3 -4
internal/aliases/aliases_test.go +1 -2
internal/config/config.go +1 -2
internal/config/config_test.go +2 -3
internal/courier/fakeserver_test.go +1 -2
internal/courier/mda_test.go +1 -2
internal/expvarom/expvarom_test.go +2 -2
internal/maillog/maillog.go +1 -2
internal/protoio/protoio.go +3 -4
internal/safeio/safeio.go +2 -3
internal/safeio/safeio_test.go +1 -2
internal/smtpsrv/conn.go +1 -2
internal/smtpsrv/fuzz.go +1 -2
internal/smtpsrv/server_test.go +1 -2
internal/sts/sts.go +3 -4
internal/testlib/testlib.go +2 -3
internal/testlib/testlib_test.go +1 -2
internal/userdb/userdb_test.go +3 -4
test/t-03-queue_persistency/addtoqueue.go +2 -2
test/util/coverhtml/coverhtml.go +1 -2
test/util/fexp/fexp.go +4 -4

diff --git a/chasquid.go b/chasquid.go
index 4745f64..18067ce 100644
--- a/chasquid.go
+++ b/chasquid.go
@@ -9,7 +9,6 @@ import (
 	"expvar"
 	"flag"
 	"fmt"
-	"io/ioutil"
 	"math/rand"
 	"net"
 	"os"
@@ -112,13 +111,13 @@ func main() {
 	// The structure matches letsencrypt's, to make it easier for that case.
 	log.Infof("Loading certificates")
 	for _, info := range mustReadDir("certs/") {
-		name := info.Name()
-		dir := filepath.Join("certs/", name)
-		if fi, err := os.Stat(dir); err == nil && !fi.IsDir() {
+		if !info.IsDir() {
 			// Skip non-directories.
 			continue
 		}
 
+		name := info.Name()
+		dir := filepath.Join("certs/", name)
 		log.Infof("  %s", name)
 
 		certPath := filepath.Join(dir, "fullchain.pem")
@@ -291,8 +290,8 @@ func loadDovecot(s *smtpsrv.Server, userdb, client string) {
 }
 
 // Read a directory, which must have at least some entries.
-func mustReadDir(path string) []os.FileInfo {
-	dirs, err := ioutil.ReadDir(path)
+func mustReadDir(path string) []os.DirEntry {
+	dirs, err := os.ReadDir(path)
 	if err != nil {
 		log.Fatalf("Error reading %q directory: %v", path, err)
 	}
diff --git a/cmd/chasquid-util/chasquid-util.go b/cmd/chasquid-util/chasquid-util.go
index 9b4e9b7..d14a61b 100644
--- a/cmd/chasquid-util/chasquid-util.go
+++ b/cmd/chasquid-util/chasquid-util.go
@@ -9,7 +9,6 @@ package main
 import (
 	"bytes"
 	"fmt"
-	"io/ioutil"
 	"net/url"
 	"os"
 	"path/filepath"
@@ -229,7 +228,7 @@ func aliasesResolve() {
 	r.SuffixSep = *conf.SuffixSeparators
 	r.DropChars = *conf.DropCharacters
 
-	domainDirs, err := ioutil.ReadDir("domains/")
+	domainDirs, err := os.ReadDir("domains/")
 	if err != nil {
 		Fatalf("Error reading domains/ directory: %v", err)
 	}
@@ -237,8 +236,8 @@ func aliasesResolve() {
 		Fatalf("No domains found in config")
 	}
 
-	for _, info := range domainDirs {
-		name := info.Name()
+	for _, entry := range domainDirs {
+		name := entry.Name()
 		aliasfile := "domains/" + name + "/aliases"
 		r.AddDomain(name)
 		err := r.AddAliasesFile(name, aliasfile)
diff --git a/internal/aliases/aliases_test.go b/internal/aliases/aliases_test.go
index 9df92aa..70aad4a 100644
--- a/internal/aliases/aliases_test.go
+++ b/internal/aliases/aliases_test.go
@@ -2,7 +2,6 @@ package aliases
 
 import (
 	"errors"
-	"io/ioutil"
 	"os"
 	"os/exec"
 	"reflect"
@@ -276,7 +275,7 @@ func TestTooMuchRecursionOnCatchAll(t *testing.T) {
 }
 
 func mustWriteFile(t *testing.T, content string) string {
-	f, err := ioutil.TempFile("", "aliases_test")
+	f, err := os.CreateTemp("", "aliases_test")
 	if err != nil {
 		t.Fatalf("failed to get temp file: %v", err)
 	}
diff --git a/internal/config/config.go b/internal/config/config.go
index efde353..f994373 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -6,7 +6,6 @@ package config
 
 import (
 	"fmt"
-	"io/ioutil"
 	"os"
 
 	"blitiri.com.ar/go/log"
@@ -39,7 +38,7 @@ func Load(path, overrides string) (*Config, error) {
 	c := proto.Clone(defaultConfig).(*Config)
 
 	// Load from the path.
-	buf, err := ioutil.ReadFile(path)
+	buf, err := os.ReadFile(path)
 	if err != nil {
 		return nil, fmt.Errorf("failed to read config at %q: %v", path, err)
 	}
diff --git a/internal/config/config_test.go b/internal/config/config_test.go
index ad36cf2..29f6100 100644
--- a/internal/config/config_test.go
+++ b/internal/config/config_test.go
@@ -2,7 +2,6 @@ package config
 
 import (
 	"io"
-	"io/ioutil"
 	"os"
 	"testing"
 
@@ -16,7 +15,7 @@ import (
 func mustCreateConfig(t *testing.T, contents string) (string, string) {
 	tmpDir := testlib.MustTempDir(t)
 	confStr := []byte(contents)
-	err := ioutil.WriteFile(tmpDir+"/chasquid.conf", confStr, 0600)
+	err := os.WriteFile(tmpDir+"/chasquid.conf", confStr, 0600)
 	if err != nil {
 		t.Fatalf("Failed to write tmp config: %v", err)
 	}
@@ -138,7 +137,7 @@ func TestBrokenOverride(t *testing.T) {
 // Run LogConfig, overriding the default logger first. This exercises the
 // code, we don't yet validate the output, but it is an useful sanity check.
 func testLogConfig(c *Config) {
-	l := log.New(nopWCloser{ioutil.Discard})
+	l := log.New(nopWCloser{io.Discard})
 	log.Default = l
 	LogConfig(c)
 }
diff --git a/internal/courier/fakeserver_test.go b/internal/courier/fakeserver_test.go
index 8cabe98..677350c 100644
--- a/internal/courier/fakeserver_test.go
+++ b/internal/courier/fakeserver_test.go
@@ -4,7 +4,6 @@ import (
 	"bufio"
 	"crypto/tls"
 	"crypto/x509"
-	"io/ioutil"
 	"net"
 	"net/textproto"
 	"os"
@@ -62,7 +61,7 @@ func (s *FakeServer) rootCA() *x509.CertPool {
 	s.t.Helper()
 	pool := x509.NewCertPool()
 	path := s.tmpDir + "/cert.pem"
-	data, err := ioutil.ReadFile(path)
+	data, err := os.ReadFile(path)
 	if err != nil {
 		s.t.Fatalf("error reading cert %q: %v", path, err)
 	}
diff --git a/internal/courier/mda_test.go b/internal/courier/mda_test.go
index 5239384..6fdb124 100644
--- a/internal/courier/mda_test.go
+++ b/internal/courier/mda_test.go
@@ -2,7 +2,6 @@ package courier
 
 import (
 	"bytes"
-	"io/ioutil"
 	"os"
 	"testing"
 	"time"
@@ -25,7 +24,7 @@ func TestMDA(t *testing.T) {
 		t.Fatalf("Deliver: %v", err)
 	}
 
-	data, err := ioutil.ReadFile(dir + "/to")
+	data, err := os.ReadFile(dir + "/to")
 	if err != nil || !bytes.Equal(data, []byte("data")) {
 		t.Errorf("Invalid data: %q - %v", string(data), err)
 	}
diff --git a/internal/expvarom/expvarom_test.go b/internal/expvarom/expvarom_test.go
index fa43c39..b375ed9 100644
--- a/internal/expvarom/expvarom_test.go
+++ b/internal/expvarom/expvarom_test.go
@@ -2,7 +2,7 @@ package expvarom
 
 import (
 	"expvar"
-	"io/ioutil"
+	"io"
 	"net/http/httptest"
 	"testing"
 
@@ -111,7 +111,7 @@ func TestHandler(t *testing.T) {
 	MetricsHandler(w, req)
 
 	resp := w.Result()
-	body, _ := ioutil.ReadAll(resp.Body)
+	body, _ := io.ReadAll(resp.Body)
 
 	if diff := cmp.Diff(expected, string(body)); diff != "" {
 		t.Errorf("MetricsHandler() mismatch (-want +got):\n%s", diff)
diff --git a/internal/maillog/maillog.go b/internal/maillog/maillog.go
index d2954d4..ce01119 100644
--- a/internal/maillog/maillog.go
+++ b/internal/maillog/maillog.go
@@ -4,7 +4,6 @@ package maillog
 import (
 	"fmt"
 	"io"
-	"io/ioutil"
 	"log/syslog"
 	"net"
 	"sync"
@@ -148,7 +147,7 @@ type nopCloser struct {
 func (nopCloser) Close() error { return nil }
 
 // Default logger, used in the following top-level functions.
-var Default *Logger = New(nopCloser{ioutil.Discard})
+var Default *Logger = New(nopCloser{io.Discard})
 
 // Listening logs that the daemon is listening on the given address.
 func Listening(a string) {
diff --git a/internal/protoio/protoio.go b/internal/protoio/protoio.go
index fb73b87..b6bdd79 100644
--- a/internal/protoio/protoio.go
+++ b/internal/protoio/protoio.go
@@ -2,7 +2,6 @@
 package protoio
 
 import (
-	"io/ioutil"
 	"net/url"
 	"os"
 	"strings"
@@ -16,7 +15,7 @@ import (
 // ReadMessage reads a protocol buffer message from fname, and unmarshalls it
 // into pb.
 func ReadMessage(fname string, pb proto.Message) error {
-	in, err := ioutil.ReadFile(fname)
+	in, err := os.ReadFile(fname)
 	if err != nil {
 		return err
 	}
@@ -26,7 +25,7 @@ func ReadMessage(fname string, pb proto.Message) error {
 // ReadTextMessage reads a text format protocol buffer message from fname, and
 // unmarshalls it into pb.
 func ReadTextMessage(fname string, pb proto.Message) error {
-	in, err := ioutil.ReadFile(fname)
+	in, err := os.ReadFile(fname)
 	if err != nil {
 		return err
 	}
@@ -98,7 +97,7 @@ func (s *Store) Get(id string, m proto.Message) (bool, error) {
 func (s *Store) ListIDs() ([]string, error) {
 	ids := []string{}
 
-	entries, err := ioutil.ReadDir(s.dir)
+	entries, err := os.ReadDir(s.dir)
 	if err != nil {
 		return nil, err
 	}
diff --git a/internal/safeio/safeio.go b/internal/safeio/safeio.go
index dc742f7..22abf73 100644
--- a/internal/safeio/safeio.go
+++ b/internal/safeio/safeio.go
@@ -3,7 +3,6 @@
 package safeio
 
 import (
-	"io/ioutil"
 	"os"
 	"path"
 	"syscall"
@@ -14,7 +13,7 @@ type FileOp func(fname string) error
 
 // WriteFile writes data to a file named by filename, atomically.
 //
-// It's a wrapper to ioutil.WriteFile, but provides atomicity (and increased
+// It's a wrapper to os.WriteFile, but provides atomicity (and increased
 // safety) by writing to a temporary file and renaming it at the end.
 //
 // Before the final rename, the given ops (if any) are called. They can be
@@ -28,7 +27,7 @@ func WriteFile(filename string, data []byte, perm os.FileMode, ops ...FileOp) er
 	// would have no expectation of Rename being atomic.
 	// We make the file names start with "." so there's no confusion with the
 	// originals.
-	tmpf, err := ioutil.TempFile(path.Dir(filename), "."+path.Base(filename))
+	tmpf, err := os.CreateTemp(path.Dir(filename), "."+path.Base(filename))
 	if err != nil {
 		return err
 	}
diff --git a/internal/safeio/safeio_test.go b/internal/safeio/safeio_test.go
index 2d4150c..c460922 100644
--- a/internal/safeio/safeio_test.go
+++ b/internal/safeio/safeio_test.go
@@ -4,7 +4,6 @@ import (
 	"bytes"
 	"errors"
 	"fmt"
-	"io/ioutil"
 	"os"
 	"strings"
 	"testing"
@@ -19,7 +18,7 @@ func testWriteFile(fname string, data []byte, perm os.FileMode, ops ...FileOp) e
 	}
 
 	// Read and compare the contents.
-	c, err := ioutil.ReadFile(fname)
+	c, err := os.ReadFile(fname)
 	if err != nil {
 		return fmt.Errorf("error reading: %v", err)
 	}
diff --git a/internal/smtpsrv/conn.go b/internal/smtpsrv/conn.go
index 66f8c74..8a3008f 100644
--- a/internal/smtpsrv/conn.go
+++ b/internal/smtpsrv/conn.go
@@ -8,7 +8,6 @@ import (
 	"flag"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"math/rand"
 	"net"
 	"net/mail"
@@ -637,7 +636,7 @@ func (c *Conn) DATA(params string) (code int, msg string) {
 	// Create a dot reader, limited to the maximum size.
 	dotr := textproto.NewReader(bufio.NewReader(
 		io.LimitReader(c.reader, c.maxDataSize))).DotReader()
-	c.data, err = ioutil.ReadAll(dotr)
+	c.data, err = io.ReadAll(dotr)
 	if err != nil {
 		if err == io.ErrUnexpectedEOF {
 			// Message is too big already. But we need to keep reading until we see
diff --git a/internal/smtpsrv/fuzz.go b/internal/smtpsrv/fuzz.go
index bb19d43..206ed7b 100644
--- a/internal/smtpsrv/fuzz.go
+++ b/internal/smtpsrv/fuzz.go
@@ -17,7 +17,6 @@ import (
 	"flag"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"math/big"
 	"net"
 	"net/textproto"
@@ -235,7 +234,7 @@ func init() {
 	log.Default.Level = log.Error
 
 	// Generate certificates in a temporary directory.
-	tmpDir, err := ioutil.TempDir("", "chasquid_smtpsrv_fuzz:")
+	tmpDir, err := os.MkdirTemp("", "chasquid_smtpsrv_fuzz:")
 	if err != nil {
 		panic(fmt.Errorf("Failed to create temp dir: %v\n", tmpDir))
 	}
diff --git a/internal/smtpsrv/server_test.go b/internal/smtpsrv/server_test.go
index 9915973..9378b21 100644
--- a/internal/smtpsrv/server_test.go
+++ b/internal/smtpsrv/server_test.go
@@ -4,7 +4,6 @@ import (
 	"crypto/tls"
 	"flag"
 	"fmt"
-	"io/ioutil"
 	"net"
 	"net/smtp"
 	"os"
@@ -568,7 +567,7 @@ func realMain(m *testing.M) int {
 		}
 	} else {
 		// Generate certificates in a temporary directory.
-		tmpDir, err := ioutil.TempDir("", "chasquid_test:")
+		tmpDir, err := os.MkdirTemp("", "chasquid_test:")
 		if err != nil {
 			fmt.Printf("Failed to create temp dir: %v\n", tmpDir)
 			return 1
diff --git a/internal/sts/sts.go b/internal/sts/sts.go
index dd2227d..05bd489 100644
--- a/internal/sts/sts.go
+++ b/internal/sts/sts.go
@@ -13,7 +13,6 @@ import (
 	"errors"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"mime"
 	"net"
 	"net/http"
@@ -266,7 +265,7 @@ func httpGet(ctx context.Context, url string) ([]byte, error) {
 
 	// Read but up to 10k; policies should be way smaller than that, and
 	// having a limit prevents abuse/accidents with very large replies.
-	return ioutil.ReadAll(&io.LimitedReader{R: resp.Body, N: 10 * 1024})
+	return io.ReadAll(&io.LimitedReader{R: resp.Body, N: 10 * 1024})
 }
 
 var errRejectRedirect = errors.New("redirects not allowed in MTA-STS")
@@ -385,7 +384,7 @@ func (c *PolicyCache) load(domain string) (*Policy, error) {
 		return nil, errExpired
 	}
 
-	data, err := ioutil.ReadFile(fname)
+	data, err := os.ReadFile(fname)
 	if err != nil {
 		cacheIOErrors.Add(1)
 		return nil, err
@@ -486,7 +485,7 @@ func (c *PolicyCache) refresh(ctx context.Context) {
 	tr := trace.New("STSCache.Refresh", c.dir)
 	defer tr.Finish()
 
-	entries, err := ioutil.ReadDir(c.dir)
+	entries, err := os.ReadDir(c.dir)
 	if err != nil {
 		tr.Errorf("failed to list directory %q: %v", c.dir, err)
 		return
diff --git a/internal/testlib/testlib.go b/internal/testlib/testlib.go
index 66541d8..ac864d5 100644
--- a/internal/testlib/testlib.go
+++ b/internal/testlib/testlib.go
@@ -8,7 +8,6 @@ import (
 	"crypto/x509"
 	"crypto/x509/pkix"
 	"encoding/pem"
-	"io/ioutil"
 	"math/big"
 	"net"
 	"os"
@@ -20,7 +19,7 @@ import (
 
 // MustTempDir creates a temporary directory, or dies trying.
 func MustTempDir(t *testing.T) string {
-	dir, err := ioutil.TempDir("", "testlib_")
+	dir, err := os.MkdirTemp("", "testlib_")
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -55,7 +54,7 @@ func Rewrite(t *testing.T, path, contents string) error {
 		panic("invalid/dangerous path")
 	}
 
-	err := ioutil.WriteFile(path, []byte(contents), 0600)
+	err := os.WriteFile(path, []byte(contents), 0600)
 	if err != nil {
 		t.Errorf("failed to rewrite file: %v", err)
 	}
diff --git a/internal/testlib/testlib_test.go b/internal/testlib/testlib_test.go
index f514dc4..9059c62 100644
--- a/internal/testlib/testlib_test.go
+++ b/internal/testlib/testlib_test.go
@@ -1,7 +1,6 @@
 package testlib
 
 import (
-	"io/ioutil"
 	"os"
 	"testing"
 	"time"
@@ -9,7 +8,7 @@ import (
 
 func TestBasic(t *testing.T) {
 	dir := MustTempDir(t)
-	if err := ioutil.WriteFile(dir+"/file", nil, 0660); err != nil {
+	if err := os.WriteFile(dir+"/file", nil, 0660); err != nil {
 		t.Fatalf("could not create file in %s: %v", dir, err)
 	}
 
diff --git a/internal/userdb/userdb_test.go b/internal/userdb/userdb_test.go
index b9eec38..2c9f018 100644
--- a/internal/userdb/userdb_test.go
+++ b/internal/userdb/userdb_test.go
@@ -2,7 +2,6 @@ package userdb
 
 import (
 	"fmt"
-	"io/ioutil"
 	"os"
 	"reflect"
 	"strings"
@@ -26,7 +25,7 @@ func removeIfSuccessful(t *testing.T, fname string) {
 // Create a database with the given content on a temporary filename. Return
 // the filename, or an error if there were errors creating it.
 func mustCreateDB(t *testing.T, content string) string {
-	f, err := ioutil.TempFile("", "userdb_test")
+	f, err := os.CreateTemp("", "userdb_test")
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -244,7 +243,7 @@ func TestReload(t *testing.T) {
 
 	// Add a valid line to the file.
 	content += "users:< key: 'u2' value:< plain:< password: 'pass' >>>"
-	ioutil.WriteFile(fname, []byte(content), 0660)
+	os.WriteFile(fname, []byte(content), 0660)
 
 	err := db.Reload()
 	if err != nil {
@@ -256,7 +255,7 @@ func TestReload(t *testing.T) {
 
 	// And now a broken one.
 	content += "users:< invalid >"
-	ioutil.WriteFile(fname, []byte(content), 0660)
+	os.WriteFile(fname, []byte(content), 0660)
 
 	err = db.Reload()
 	if err == nil {
diff --git a/test/t-03-queue_persistency/addtoqueue.go b/test/t-03-queue_persistency/addtoqueue.go
index 9e71ae3..39bdbf6 100644
--- a/test/t-03-queue_persistency/addtoqueue.go
+++ b/test/t-03-queue_persistency/addtoqueue.go
@@ -12,7 +12,7 @@ package main
 import (
 	"flag"
 	"fmt"
-	"io/ioutil"
+	"io"
 	"os"
 	"time"
 
@@ -29,7 +29,7 @@ var (
 func main() {
 	flag.Parse()
 
-	data, err := ioutil.ReadAll(os.Stdin)
+	data, err := io.ReadAll(os.Stdin)
 	if err != nil {
 		fmt.Printf("error reading data: %v\n", err)
 		os.Exit(1)
diff --git a/test/util/coverhtml/coverhtml.go b/test/util/coverhtml/coverhtml.go
index ff57af1..d66b602 100644
--- a/test/util/coverhtml/coverhtml.go
+++ b/test/util/coverhtml/coverhtml.go
@@ -10,7 +10,6 @@ import (
 	"flag"
 	"fmt"
 	"html/template"
-	"io/ioutil"
 	"math"
 	"os"
 	"strings"
@@ -50,7 +49,7 @@ func main() {
 		totals.Add(p)
 
 		fname := strings.Join(strings.Split(p.FileName, "/")[*strip:], "/")
-		src, err := ioutil.ReadFile(fname)
+		src, err := os.ReadFile(fname)
 		if err != nil {
 			errorf("Failed to read %q: %v", fname, err)
 		}
diff --git a/test/util/fexp/fexp.go b/test/util/fexp/fexp.go
index 4aa40ea..02ddbb0 100644
--- a/test/util/fexp/fexp.go
+++ b/test/util/fexp/fexp.go
@@ -11,7 +11,7 @@ import (
 	"crypto/x509"
 	"flag"
 	"fmt"
-	"io/ioutil"
+	"io"
 	"net/http"
 	"os"
 	"regexp"
@@ -70,13 +70,13 @@ func main() {
 		fatalf("error getting %q: %v\n", url, err)
 	}
 	defer resp.Body.Close()
-	rbody, err := ioutil.ReadAll(resp.Body)
+	rbody, err := io.ReadAll(resp.Body)
 	if err != nil {
 		errorf("error reading body: %v\n", err)
 	}
 
 	if *save != "" {
-		err = ioutil.WriteFile(*save, rbody, 0664)
+		err = os.WriteFile(*save, rbody, 0664)
 		if err != nil {
 			errorf("error writing body to file %q: %v\n", *save, err)
 		}
@@ -170,7 +170,7 @@ func mkTransport(caCert string) http.RoundTripper {
 		return nil
 	}
 
-	certs, err := ioutil.ReadFile(caCert)
+	certs, err := os.ReadFile(caCert)
 	if err != nil {
 		fatalf("error reading CA file %q: %v\n", caCert, err)
 	}