Skip to content

Commit 501601b

Browse files
committed
general: remove usage of deprecated ioutil
1 parent 8630f04 commit 501601b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+111
-240
lines changed

app.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
runtime: go119
1+
runtime: go120
22
includes:
33
- gcp_appengine_data/appeng-environment.yaml
44
instance_class: f1

cli/log.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"io/ioutil"
7+
"io"
88
"os"
99
"sync"
1010
"time"
@@ -86,7 +86,7 @@ func InitialiseAWS() {
8686
// Integrate the decorated handler with AWS x-ray. The crucial x-ray daemon program seems to be only capable of running on AWS compute resources.
8787
_ = os.Setenv("AWS_XRAY_CONTEXT_MISSING", "LOG_ERROR")
8888
_ = xray.Configure(xray.Config{ContextMissingStrategy: ctxmissing.NewDefaultIgnoreErrorStrategy()})
89-
xray.SetLogger(xraylog.NewDefaultLogger(ioutil.Discard, xraylog.LogLevelWarn))
89+
xray.SetLogger(xraylog.NewDefaultLogger(io.Discard, xraylog.LogLevelWarn))
9090
go func() {
9191
// These functions of aws lib take their sweet time, don't let them block main's progress. It's OK to miss a couple of traces.
9292
beanstalk.Init()

cli/security.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bufio"
55
"context"
66
"crypto/tls"
7-
"io/ioutil"
87
pseudoRand "math/rand"
98
"os"
109
"runtime"
@@ -46,7 +45,7 @@ func DecryptFile(filePath string) {
4645
lalog.DefaultLogger.Abort("main", err, "failed to decrypt file")
4746
return
4847
}
49-
if err := ioutil.WriteFile(filePath, content, 0600); err != nil {
48+
if err := os.WriteFile(filePath, content, 0600); err != nil {
5049
lalog.DefaultLogger.Abort("main", err, "failed to decrypt file")
5150
return
5251
}

cli/sys.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cli
22

33
import (
44
"context"
5-
"io/ioutil"
65
"net"
76
"net/http"
87
"net/http/pprof"
@@ -86,7 +85,7 @@ func GAEDaemonList(logger *lalog.Logger) string {
8685
}
8786
}
8887
// Read the value of CLI parameter "-daemons" from a text file
89-
daemonListContent, err := ioutil.ReadFile("daemonList")
88+
daemonListContent, err := os.ReadFile("daemonList")
9089
if err != nil {
9190
logger.Abort("", err, "failed to read daemonList")
9291
return ""

daemon/dnsd/proxy.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"net"
109
"sync"
1110
"time"
@@ -269,7 +268,7 @@ func (proxy *Proxy) Receive(in tcpoverdns.Segment) (tcpoverdns.Segment, bool) {
269268
MaxRetransmissions: 300,
270269
// The output transport is not used. Instead, the output segments
271270
// are kept in a backlog.
272-
OutputTransport: ioutil.Discard,
271+
OutputTransport: io.Discard,
273272
// The segment length and sliding window length are set by the
274273
// initiator using InitiatorConfig.
275274
}

daemon/dnsd/proxy_dns_relay.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"io/ioutil"
7+
"io"
88
"math/rand"
99
"net"
1010
"strconv"
@@ -119,7 +119,7 @@ func (relay *DNSRelay) establish(ctx context.Context) (*ProxiedConnection, error
119119
MaxRetransmissions: 300,
120120
// The output transport is not used. Instead, the output segments
121121
// are kept in a backlog.
122-
OutputTransport: ioutil.Discard,
122+
OutputTransport: io.Discard,
123123
}
124124
relay.Config.Config(tc)
125125
conn := &ProxiedConnection{

daemon/dnsd/proxy_http_server.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"errors"
88
"fmt"
99
"io"
10-
"io/ioutil"
1110
"math/rand"
1211
"net"
1312
"net/http"
@@ -164,7 +163,7 @@ func (proxy *HTTPProxyServer) dialContext(ctx context.Context, network, addr str
164163
MaxRetransmissions: 300,
165164
// The output transport is not used. Instead, the output segments
166165
// are kept in a backlog.
167-
OutputTransport: ioutil.Discard,
166+
OutputTransport: io.Discard,
168167
}
169168
proxy.Config.Config(tc)
170169
conn := &ProxiedConnection{

daemon/httpd/handler/file_upload.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/hex"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"net/http"
109
"os"
1110
"path/filepath"
@@ -74,17 +73,20 @@ func (upload *HandleFileUpload) render(w http.ResponseWriter, r *http.Request, m
7473
func (upload *HandleFileUpload) periodicallyDeleteExpiredFiles() {
7574
for {
7675
time.Sleep(FileUploadCleanUpIntervalSec * time.Second)
77-
files, err := ioutil.ReadDir(fileUploadStorage)
76+
files, err := os.ReadDir(fileUploadStorage)
7877
if err != nil {
7978
upload.logger.Warning("", err, "failed to read file upload directory")
8079
continue
8180
}
8281
var anyFileExpired bool
8382
for _, fileEntry := range files {
84-
// fileInfo, err := fileEntry.Info()
85-
if fileEntry.ModTime().Before(time.Now().Add(-(FileUploadExpireInSec * time.Second))) {
83+
fileInfo, err := fileEntry.Info()
84+
if err != nil {
85+
continue
86+
}
87+
if fileInfo.ModTime().Before(time.Now().Add(-(FileUploadExpireInSec * time.Second))) {
8688
anyFileExpired = true
87-
upload.logger.Info("", os.Remove(fileEntry.Name()), "delete expired file")
89+
upload.logger.Info("", os.Remove(fileInfo.Name()), "delete expired file")
8890
}
8991
}
9092
if !anyFileExpired {

daemon/httpd/handler/html_doc.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package handler
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"net/http"
6+
"os"
77
"strings"
88
"time"
99

@@ -40,7 +40,7 @@ func (doc *HandleHTMLDocument) Initialise(*lalog.Logger, *toolbox.CommandProcess
4040
var err error
4141
doc.contentString = doc.HTMLContent
4242
if doc.HTMLFilePath != "" {
43-
if doc.contentBytes, err = ioutil.ReadFile(doc.HTMLFilePath); err != nil {
43+
if doc.contentBytes, err = os.ReadFile(doc.HTMLFilePath); err != nil {
4444
return fmt.Errorf("HandleHTMLDocument.Initialise: failed to open HTML file at %s - %v", doc.HTMLFilePath, err)
4545
}
4646
doc.contentString = string(doc.contentBytes)

daemon/httpd/handler/html_doc_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package handler
22

33
import (
4-
"io/ioutil"
4+
"io"
55
"net/http"
66
"net/http/httptest"
77
"os"
@@ -27,7 +27,7 @@ func TestHandleHTMLDocument_Empty(t *testing.T) {
2727
}
2828
w := httptest.NewRecorder()
2929
handler.Handle(w, req)
30-
body, err := ioutil.ReadAll(w.Result().Body)
30+
body, err := io.ReadAll(w.Result().Body)
3131
if err != nil {
3232
t.Fatal(err)
3333
}
@@ -50,7 +50,7 @@ func TestHandleHTMLDocument_HTMLContent(t *testing.T) {
5050
}
5151
w := httptest.NewRecorder()
5252
handler.Handle(w, req)
53-
body, err := ioutil.ReadAll(w.Result().Body)
53+
body, err := io.ReadAll(w.Result().Body)
5454
if err != nil {
5555
t.Fatal(err)
5656
}
@@ -84,7 +84,7 @@ func TestHandleHTMLDocument_HTMLFile(t *testing.T) {
8484
}
8585
w := httptest.NewRecorder()
8686
handler.Handle(w, req)
87-
body, err := ioutil.ReadAll(w.Result().Body)
87+
body, err := io.ReadAll(w.Result().Body)
8888
if err != nil {
8989
t.Fatal(err)
9090
}

daemon/httpd/handler/latest_requests_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package handler
22

33
import (
4-
"io/ioutil"
4+
"io"
55
"net/http"
66
"net/http/httptest"
77
"testing"
@@ -25,7 +25,7 @@ func TestHandleLatestRequestsInspector(t *testing.T) {
2525
}
2626
w := httptest.NewRecorder()
2727
handler.Handle(w, req)
28-
body, _ := ioutil.ReadAll(w.Result().Body)
28+
body, _ := io.ReadAll(w.Result().Body)
2929
bodyStr := string(body)
3030
if bodyStr != "Start memorising latest requests." {
3131
t.Fatal(bodyStr)
@@ -37,7 +37,7 @@ func TestHandleLatestRequestsInspector(t *testing.T) {
3737
}
3838
w = httptest.NewRecorder()
3939
handler.Handle(w, req)
40-
body, _ = ioutil.ReadAll(w.Result().Body)
40+
body, _ = io.ReadAll(w.Result().Body)
4141
bodyStr = string(body)
4242
if bodyStr != "Stop memorising latest requests." {
4343
t.Fatal(bodyStr)
@@ -52,7 +52,7 @@ func TestHandleLatestRequestsInspector(t *testing.T) {
5252
}
5353
w = httptest.NewRecorder()
5454
handler.Handle(w, req)
55-
body, _ = ioutil.ReadAll(w.Result().Body)
55+
body, _ = io.ReadAll(w.Result().Body)
5656
bodyStr = string(body)
5757
if bodyStr != "<pre>req1</pre><hr>\n<pre>req2</pre><hr>\n<pre>req3</pre><hr>\n" {
5858
t.Fatal(bodyStr)

daemon/httpd/handler/lorawan_webhook.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8-
"io/ioutil"
8+
"io"
99
"net/http"
1010
"strings"
1111
"time"
@@ -156,7 +156,7 @@ func (msg Downlinks) JSONString() string {
156156
}
157157

158158
func (hand *HandleLoraWANWebhook) Handle(w http.ResponseWriter, r *http.Request) {
159-
body, err := ioutil.ReadAll(r.Body)
159+
body, err := io.ReadAll(r.Body)
160160
if err != nil {
161161
return
162162
}

daemon/httpd/handler/procexp_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package handler
22

33
import (
44
"encoding/json"
5-
"io/ioutil"
5+
"io"
66
"net/http"
77
"net/http/httptest"
88
"os"
@@ -37,7 +37,7 @@ func TestHandleProcessExplorer_Handle(t *testing.T) {
3737
}
3838
w := httptest.NewRecorder()
3939
handler.Handle(w, req)
40-
body, _ := ioutil.ReadAll(w.Result().Body)
40+
body, _ := io.ReadAll(w.Result().Body)
4141
if w.Result().StatusCode != http.StatusOK {
4242
t.Fatalf("%+v", w.Result())
4343
}
@@ -59,7 +59,7 @@ func TestHandleProcessExplorer_Handle(t *testing.T) {
5959
}
6060
w := httptest.NewRecorder()
6161
handler.Handle(w, req)
62-
body, _ := ioutil.ReadAll(w.Result().Body)
62+
body, _ := io.ReadAll(w.Result().Body)
6363
if w.Result().StatusCode != http.StatusOK {
6464
t.Fatalf("%+v", w.Result())
6565
}
@@ -78,7 +78,7 @@ func TestHandleProcessExplorer_Handle(t *testing.T) {
7878
}
7979
w := httptest.NewRecorder()
8080
handler.Handle(w, req)
81-
body, _ := ioutil.ReadAll(w.Result().Body)
81+
body, _ := io.ReadAll(w.Result().Body)
8282
if w.Result().StatusCode != http.StatusOK {
8383
t.Fatalf("%+v", w.Result())
8484
}

daemon/httpd/handler/prometheus_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package handler
22

33
import (
4-
"io/ioutil"
4+
"io"
55
"net/http"
66
"net/http/httptest"
77
"strings"
@@ -54,7 +54,7 @@ func TestHandlePrometheus_HandleWithPromIntegEnabled(t *testing.T) {
5454
if w.Result().StatusCode != http.StatusOK {
5555
t.Fatalf("%+v", w.Result())
5656
}
57-
body, _ := ioutil.ReadAll(w.Result().Body)
57+
body, _ := io.ReadAll(w.Result().Body)
5858
if !strings.Contains(string(body), "go_memstats_heap_objects") {
5959
t.Fatalf("missing metrics readings from response body: %s", string(body))
6060
}

daemon/httpd/handler/remotevm.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package handler
33
import (
44
"errors"
55
"fmt"
6-
"io/ioutil"
76
"net/http"
87
"os"
98
"path"
@@ -280,7 +279,7 @@ func (_ *HandleVirtualMachineScreenshot) Initialise(*lalog.Logger, *toolbox.Comm
280279
func (handler *HandleVirtualMachineScreenshot) Handle(w http.ResponseWriter, r *http.Request) {
281280
NoCache(w)
282281
// Store screenshot picture in a temporary file
283-
screenshot, err := ioutil.TempFile("", "laitos-handle-vm-screenshot")
282+
screenshot, err := os.CreateTemp("", "laitos-handle-vm-screenshot")
284283
if err != nil {
285284
http.Error(w, "Failed to create temporary file: "+err.Error(), http.StatusInternalServerError)
286285
return
@@ -291,7 +290,7 @@ func (handler *HandleVirtualMachineScreenshot) Handle(w http.ResponseWriter, r *
291290
http.Error(w, "Failed to create temporary file: "+err.Error(), http.StatusInternalServerError)
292291
return
293292
}
294-
jpegContent, err := ioutil.ReadFile(screenshot.Name())
293+
jpegContent, err := os.ReadFile(screenshot.Name())
295294
if err != nil {
296295
http.Error(w, "Failed to read screenshot file: "+err.Error(), http.StatusInternalServerError)
297296
return

daemon/httpd/handler/req_inspector_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package handler
22

33
import (
4-
"io/ioutil"
4+
"io"
55
"net/http"
66
"net/http/httptest"
77
"strings"
@@ -27,7 +27,7 @@ func TestRequestInspector(t *testing.T) {
2727
}
2828
w := httptest.NewRecorder()
2929
handler.Handle(w, req)
30-
body, _ := ioutil.ReadAll(w.Result().Body)
30+
body, _ := io.ReadAll(w.Result().Body)
3131
bodyStr := string(body)
3232
t.Log(bodyStr)
3333

daemon/httpd/httpd.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11-
"io/ioutil"
1211
"mime/multipart"
1312
"net"
1413
"net/http"
@@ -724,14 +723,14 @@ func PrepareForTestHTTPD(t testingstub.T) {
724723
// Create a temporary file for index
725724
_ = os.MkdirAll("/tmp", 1777)
726725
indexFile := "/tmp/test-laitos-index.html"
727-
if err := ioutil.WriteFile(indexFile, []byte(TestLaitosIndexHTMLContent), 0644); err != nil {
726+
if err := os.WriteFile(indexFile, []byte(TestLaitosIndexHTMLContent), 0644); err != nil {
728727
panic(err)
729728
}
730729
htmlDir := "/tmp/test-laitos-dir"
731730
if err := os.MkdirAll(htmlDir, 0755); err != nil {
732731
t.Fatal(err)
733732
}
734-
if err := ioutil.WriteFile(htmlDir+"/a.html", []byte("a html"), 0644); err != nil {
733+
if err := os.WriteFile(htmlDir+"/a.html", []byte("a html"), 0644); err != nil {
735734
t.Fatal(err)
736735
}
737736
/*

daemon/httpd/middleware/middleware.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"net/http"
98
"net/http/httputil"
109
"strings"
@@ -231,7 +230,7 @@ func RecordLatestRequests(logger *lalog.Logger, next http.HandlerFunc) http.Hand
231230
return func(w http.ResponseWriter, r *http.Request) {
232231
if r.Body != nil && EnableLatestRequestsRecording {
233232
// Read the entire request into memory.
234-
requestBody, err := ioutil.ReadAll(r.Body)
233+
requestBody, err := io.ReadAll(r.Body)
235234
_ = r.Body.Close()
236235
if err != nil {
237236
logger.Warning(GetRealClientIP(r), err, "failed to read request body")

0 commit comments

Comments
 (0)