Skip to content

Commit

Permalink
all: Remove usage of deprecated io/ioutil (syncthing#7971)
Browse files Browse the repository at this point in the history
As of Go 1.16 io/ioutil is deprecated. This replaces usage with the
corresponding functions in package os and package io.
  • Loading branch information
calmh authored Nov 22, 2021
1 parent bf89bff commit 4b750b6
Show file tree
Hide file tree
Showing 71 changed files with 190 additions and 235 deletions.
13 changes: 6 additions & 7 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -723,7 +722,7 @@ func shouldBuildSyso(dir string) (string, error) {
}

jsonPath := filepath.Join(dir, "versioninfo.json")
err = ioutil.WriteFile(jsonPath, bs, 0644)
err = os.WriteFile(jsonPath, bs, 0644)
if err != nil {
return "", errors.New("failed to create " + jsonPath + ": " + err.Error())
}
Expand Down Expand Up @@ -762,12 +761,12 @@ func shouldCleanupSyso(sysoFilePath string) {
// exists. The permission bits are copied as well. If dst already exists and
// the contents are identical to src the modification time is not updated.
func copyFile(src, dst string, perm os.FileMode) error {
in, err := ioutil.ReadFile(src)
in, err := os.ReadFile(src)
if err != nil {
return err
}

out, err := ioutil.ReadFile(dst)
out, err := os.ReadFile(dst)
if err != nil {
// The destination probably doesn't exist, we should create
// it.
Expand All @@ -783,7 +782,7 @@ func copyFile(src, dst string, perm os.FileMode) error {

copy:
os.MkdirAll(filepath.Dir(dst), 0777)
if err := ioutil.WriteFile(dst, in, perm); err != nil {
if err := os.WriteFile(dst, in, perm); err != nil {
return err
}

Expand Down Expand Up @@ -958,7 +957,7 @@ func rmr(paths ...string) {
}

func getReleaseVersion() (string, error) {
bs, err := ioutil.ReadFile("RELEASE")
bs, err := os.ReadFile("RELEASE")
if err != nil {
return "", err
}
Expand Down Expand Up @@ -1290,7 +1289,7 @@ func zipFile(out string, files []archiveFile) {

if strings.HasSuffix(f.dst, ".txt") {
// Text file. Read it and convert line endings.
bs, err := ioutil.ReadAll(sf)
bs, err := io.ReadAll(sf)
if err != nil {
log.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/stcrashreceiver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -58,7 +57,7 @@ func main() {
func handleFailureFn(dsn, failureDir string) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
lr := io.LimitReader(req.Body, maxRequestSize)
bs, err := ioutil.ReadAll(lr)
bs, err := io.ReadAll(lr)
req.Body.Close()
if err != nil {
http.Error(w, err.Error(), 500)
Expand Down
4 changes: 2 additions & 2 deletions cmd/stcrashreceiver/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package main
import (
"bytes"
"errors"
"io/ioutil"
"io"
"regexp"
"strings"
"sync"
Expand Down Expand Up @@ -93,7 +93,7 @@ func parseCrashReport(path string, report []byte) (*raven.Packet, error) {
}

r := bytes.NewReader(report)
ctx, err := stack.ParseDump(r, ioutil.Discard, false)
ctx, err := stack.ParseDump(r, io.Discard, false)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/stcrashreceiver/sentry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"testing"
)

Expand Down Expand Up @@ -59,7 +59,7 @@ func TestParseVersion(t *testing.T) {
}

func TestParseReport(t *testing.T) {
bs, err := ioutil.ReadFile("_testdata/panic.log")
bs, err := os.ReadFile("_testdata/panic.log")
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/stcrashreceiver/sourcecodeloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"
"path/filepath"
"strings"
Expand Down Expand Up @@ -80,7 +80,7 @@ func (l *githubSourceCodeLoader) Load(filename string, line, context int) ([][]b
fmt.Println("Loading source:", resp.Status)
return nil, 0
}
data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
fmt.Println("Loading source:", err.Error())
Expand Down
5 changes: 2 additions & 3 deletions cmd/stcrashreceiver/stcrashreceiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -96,7 +95,7 @@ func (r *crashReceiver) servePut(reportID, fullPath string, w http.ResponseWrite
// Read at most maxRequestSize of report data.
log.Println("Receiving report", reportID)
lr := io.LimitReader(req.Body, maxRequestSize)
bs, err := ioutil.ReadAll(lr)
bs, err := io.ReadAll(lr)
if err != nil {
log.Println("Reading report:", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
Expand All @@ -110,7 +109,7 @@ func (r *crashReceiver) servePut(reportID, fullPath string, w http.ResponseWrite
gw.Close()

// Create an output file with the compressed report
err = ioutil.WriteFile(fullPath, buf.Bytes(), 0644)
err = os.WriteFile(fullPath, buf.Bytes(), 0644)
if err != nil {
log.Println("Saving report:", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
Expand Down
4 changes: 2 additions & 2 deletions cmd/stcrashreceiver/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"bytes"
"compress/gzip"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"path/filepath"
"time"

Expand Down Expand Up @@ -53,5 +53,5 @@ func compressAndWrite(bs []byte, fullPath string) error {
gw.Close()

// Create an output file with the compressed report
return ioutil.WriteFile(fullPath, buf.Bytes(), 0644)
return os.WriteFile(fullPath, buf.Bytes(), 0644)
}
7 changes: 3 additions & 4 deletions cmd/strelaypoolsrv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -560,7 +559,7 @@ func limit(addr string, cache *lru.Cache, lock sync.Mutex, intv time.Duration, b
}

func loadRelays(file string) []*relay {
content, err := ioutil.ReadFile(file)
content, err := os.ReadFile(file)
if err != nil {
log.Println("Failed to load relays: " + err.Error())
return nil
Expand Down Expand Up @@ -598,11 +597,11 @@ func saveRelays(file string, relays []*relay) error {
for _, relay := range relays {
content += relay.uri.String() + "\n"
}
return ioutil.WriteFile(file, []byte(content), 0777)
return os.WriteFile(file, []byte(content), 0777)
}

func createTestCertificate() tls.Certificate {
tmpDir, err := ioutil.TempDir("", "relaypoolsrv")
tmpDir, err := os.MkdirTemp("", "relaypoolsrv")
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/strelaysrv/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"bytes"
"crypto/tls"
"encoding/json"
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -56,7 +56,7 @@ func poolHandler(pool string, uri *url.URL, mapping mapping, ownCert tls.Certifi
continue
}

bs, err := ioutil.ReadAll(resp.Body)
bs, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Printf("Error joining pool %v: reading response: %v", pool, err)
Expand Down
7 changes: 3 additions & 4 deletions cmd/stsigtool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package main
import (
"flag"
"io"
"io/ioutil"
"log"
"os"

Expand Down Expand Up @@ -69,7 +68,7 @@ func gen() {
}

func sign(keyname, dataname string) {
privkey, err := ioutil.ReadFile(keyname)
privkey, err := os.ReadFile(keyname)
if err != nil {
log.Fatal(err)
}
Expand All @@ -95,15 +94,15 @@ func sign(keyname, dataname string) {
}

func verifyWithFile(signame, dataname, keyname string) {
pubkey, err := ioutil.ReadFile(keyname)
pubkey, err := os.ReadFile(keyname)
if err != nil {
log.Fatal(err)
}
verifyWithKey(signame, dataname, pubkey)
}

func verifyWithKey(signame, dataname string, pubkey []byte) {
sig, err := ioutil.ReadFile(signame)
sig, err := os.ReadFile(signame)
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/syncthing/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package cli
import (
"bufio"
"fmt"
"io/ioutil"
"io"
"os"
"strings"

Expand Down Expand Up @@ -151,7 +151,7 @@ func parseFlags(c *preCli) error {
}
}
// We don't want kong to print anything nor os.Exit (e.g. on -h)
parser, err := kong.New(c, kong.Writers(ioutil.Discard, ioutil.Discard), kong.Exit(func(int) {}))
parser, err := kong.New(c, kong.Writers(io.Discard, io.Discard), kong.Exit(func(int) {}))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/syncthing/cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"mime"
"net/http"
"os"
Expand All @@ -23,7 +23,7 @@ import (
)

func responseToBArray(response *http.Response) ([]byte, error) {
bytes, err := ioutil.ReadAll(response.Body)
bytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/syncthing/crash_reporting.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -62,7 +61,7 @@ func uploadPanicLogs(ctx context.Context, urlBase, dir string) {
// the log contents. A HEAD request is made to see if the log has already
// been reported. If not, a PUT is made with the log contents.
func uploadPanicLog(ctx context.Context, urlBase, file string) error {
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/syncthing/decrypt/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"

"github.com/syncthing/syncthing/lib/config"
Expand Down Expand Up @@ -112,7 +112,7 @@ func (c *CLI) withContinue(err error) error {
// error.
func (c *CLI) getFolderID() (string, error) {
tokenPath := filepath.Join(c.Path, c.TokenPath)
bs, err := ioutil.ReadFile(tokenPath)
bs, err := os.ReadFile(tokenPath)
if err != nil {
return "", fmt.Errorf("reading folder token: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/syncthing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof" // Need to import this to support STPROFILER.
Expand Down Expand Up @@ -500,7 +499,7 @@ func upgradeViaRest() error {
return err
}
if resp.StatusCode != 200 {
bs, err := ioutil.ReadAll(resp.Body)
bs, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return err
Expand Down
7 changes: 3 additions & 4 deletions cmd/syncthing/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package main

import (
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -18,7 +17,7 @@ import (
func TestRotatedFile(t *testing.T) {
// Verify that log rotation happens.

dir, err := ioutil.TempDir("", "syncthing")
dir, err := os.MkdirTemp("", "syncthing")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -179,7 +178,7 @@ func TestAutoClosedFile(t *testing.T) {
}

// The file should have both writes in it.
bs, err := ioutil.ReadFile(file)
bs, err := os.ReadFile(file)
if err != nil {
t.Fatal(err)
}
Expand All @@ -199,7 +198,7 @@ func TestAutoClosedFile(t *testing.T) {
}

// It should now contain three writes, as the file is always opened for appending
bs, err = ioutil.ReadFile(file)
bs, err = os.ReadFile(file)
if err != nil {
t.Fatal(err)
}
Expand Down
Loading

0 comments on commit 4b750b6

Please sign in to comment.