Skip to content

Commit

Permalink
fix ioutil deprecated function
Browse files Browse the repository at this point in the history
update i/o functions to use os / io package functions instead
  • Loading branch information
theedtron committed Mar 6, 2024
1 parent 1a2b599 commit aa7cb9f
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 35 deletions.
5 changes: 2 additions & 3 deletions addrmgr/addrmanager_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package addrmgr

import (
"io/ioutil"
"math/rand"
"net"
"os"
Expand Down Expand Up @@ -91,7 +90,7 @@ func TestAddrManagerSerialization(t *testing.T) {

// We'll start by creating our address manager backed by a temporary
// directory.
tempDir, err := ioutil.TempDir("", "addrmgr")
tempDir, err := os.MkdirTemp("", "addrmgr")
if err != nil {
t.Fatalf("unable to create temp dir: %v", err)
}
Expand Down Expand Up @@ -131,7 +130,7 @@ func TestAddrManagerV1ToV2(t *testing.T) {

// We'll start by creating our address manager backed by a temporary
// directory.
tempDir, err := ioutil.TempDir("", "addrmgr")
tempDir, err := os.MkdirTemp("", "addrmgr")
if err != nil {
t.Fatalf("unable to create temp dir: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions btcutil/txsort/txsort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package txsort_test
import (
"bytes"
"encoding/hex"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -64,7 +64,7 @@ func TestSort(t *testing.T) {
for _, test := range tests {
// Load and deserialize the test transaction.
filePath := filepath.Join("testdata", test.hexFile)
txHexBytes, err := ioutil.ReadFile(filePath)
txHexBytes, err := os.ReadFile(filePath)
if err != nil {
t.Errorf("ReadFile (%s): failed to read test file: %v",
test.name, err)
Expand Down
7 changes: 4 additions & 3 deletions cmd/btcctl/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"os"

"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/go-socks/socks"
Expand Down Expand Up @@ -37,7 +38,7 @@ func newHTTPClient(cfg *config) (*http.Client, error) {
// Configure TLS if needed.
var tlsConfig *tls.Config
if !cfg.NoTLS && cfg.RPCCert != "" {
pem, err := ioutil.ReadFile(cfg.RPCCert)
pem, err := os.ReadFile(cfg.RPCCert)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -95,7 +96,7 @@ func sendPostRequest(marshalledJSON []byte, cfg *config) ([]byte, error) {
}

// Read the raw bytes and close the response.
respBytes, err := ioutil.ReadAll(httpResponse.Body)
respBytes, err := io.ReadAll(httpResponse.Body)
httpResponse.Body.Close()
if err != nil {
err = fmt.Errorf("error reading json reply: %v", err)
Expand Down
5 changes: 2 additions & 3 deletions cmd/gencerts/gencerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -65,11 +64,11 @@ func main() {
}

// Write cert and key files.
if err = ioutil.WriteFile(certFile, cert, 0666); err != nil {
if err = os.WriteFile(certFile, cert, 0666); err != nil {
fmt.Fprintf(os.Stderr, "cannot write cert: %v\n", err)
os.Exit(1)
}
if err = ioutil.WriteFile(keyFile, key, 0600); err != nil {
if err = os.WriteFile(keyFile, key, 0600); err != nil {
os.Remove(certFile)
fmt.Fprintf(os.Stderr, "cannot write key: %v\n", err)
os.Exit(1)
Expand Down
9 changes: 4 additions & 5 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand All @@ -23,14 +22,14 @@ func TestCreateDefaultConfigFile(t *testing.T) {
sampleConfigFile := filepath.Join(filepath.Dir(path), "sample-btcd.conf")

// Setup a temporary directory
tmpDir, err := ioutil.TempDir("", "btcd")
tmpDir, err := os.MkdirTemp("", "btcd")
if err != nil {
t.Fatalf("Failed creating a temporary directory: %v", err)
}
testpath := filepath.Join(tmpDir, "test.conf")

// copy config file to location of btcd binary
data, err := ioutil.ReadFile(sampleConfigFile)
data, err := os.ReadFile(sampleConfigFile)
if err != nil {
t.Fatalf("Failed reading sample config file: %v", err)
}
Expand All @@ -39,7 +38,7 @@ func TestCreateDefaultConfigFile(t *testing.T) {
t.Fatalf("Failed obtaining app path: %v", err)
}
tmpConfigFile := filepath.Join(appPath, "sample-btcd.conf")
err = ioutil.WriteFile(tmpConfigFile, data, 0644)
err = os.WriteFile(tmpConfigFile, data, 0644)
if err != nil {
t.Fatalf("Failed copying sample config file: %v", err)
}
Expand All @@ -57,7 +56,7 @@ func TestCreateDefaultConfigFile(t *testing.T) {
t.Fatalf("Failed to create a default config file: %v", err)
}

content, err := ioutil.ReadFile(testpath)
content, err := os.ReadFile(testpath)
if err != nil {
t.Fatalf("Failed to read generated default config file: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions database/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package database_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -123,7 +122,7 @@ func Example_blockStorageAndRetrieval() {
// Typically you wouldn't want to remove the database right away like
// this, nor put it in the temp directory, but it's done here to ensure
// the example cleans up after itself.
dbPath, err := ioutil.TempDir("", "exampleblkstorage")
dbPath, err := os.MkdirTemp("", "exampleblkstorage")
if err != nil {
fmt.Println(err)
return
Expand Down
6 changes: 3 additions & 3 deletions docs/json_rpc_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ func main() {
// generated by btcd when it starts the RPC server and doesn't already
// have one.
btcdHomeDir := btcutil.AppDataDir("btcd", false)
certs, err := ioutil.ReadFile(filepath.Join(btcdHomeDir, "rpc.cert"))
certs, err := os.ReadFile(filepath.Join(btcdHomeDir, "rpc.cert"))
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -1185,7 +1185,7 @@ func main() {
// generated by btcd when it starts the RPC server and doesn't already
// have one.
btcdHomeDir := btcutil.AppDataDir("btcd", false)
certs, err := ioutil.ReadFile(filepath.Join(btcdHomeDir, "rpc.cert"))
certs, err := os.ReadFile(filepath.Join(btcdHomeDir, "rpc.cert"))
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -1288,7 +1288,7 @@ func main() {
// generated by btcd when it starts the RPC server and doesn't already
// have one.
btcdHomeDir := btcutil.AppDataDir("btcd", false)
certs, err := ioutil.ReadFile(filepath.Join(btcdHomeDir, "rpc.cert"))
certs, err := os.ReadFile(filepath.Join(btcdHomeDir, "rpc.cert"))
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions rpcclient/examples/btcdwebsockets/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package main

import (
"io/ioutil"
"os"
"log"
"path/filepath"
"time"
Expand Down Expand Up @@ -33,7 +33,7 @@ func main() {

// Connect to local btcd RPC server using websockets.
btcdHomeDir := btcutil.AppDataDir("btcd", false)
certs, err := ioutil.ReadFile(filepath.Join(btcdHomeDir, "rpc.cert"))
certs, err := os.ReadFile(filepath.Join(btcdHomeDir, "rpc.cert"))
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions rpcclient/examples/btcwalletwebsockets/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package main

import (
"io/ioutil"
"os"
"log"
"path/filepath"
"time"
Expand All @@ -29,7 +29,7 @@ func main() {

// Connect to local btcwallet RPC server using websockets.
certHomeDir := btcutil.AppDataDir("btcwallet", false)
certs, err := ioutil.ReadFile(filepath.Join(certHomeDir, "rpc.cert"))
certs, err := os.ReadFile(filepath.Join(certHomeDir, "rpc.cert"))
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4650,10 +4650,10 @@ func genCertPair(certFile, keyFile string) error {
}

// Write cert and key files.
if err = ioutil.WriteFile(certFile, cert, 0666); err != nil {
if err = os.WriteFile(certFile, cert, 0666); err != nil {
return err
}
if err = ioutil.WriteFile(keyFile, key, 0600); err != nil {
if err = os.WriteFile(keyFile, key, 0600); err != nil {
os.Remove(certFile)
return err
}
Expand Down
4 changes: 2 additions & 2 deletions txscript/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package txscript
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/btcsuite/btcd/chaincfg"
Expand All @@ -25,7 +25,7 @@ var (

func init() {
// tx 620f57c92cf05a7f7e7f7d28255d5f7089437bc48e34dcfebf7751d08b7fb8f5
txHex, err := ioutil.ReadFile("data/many_inputs_tx.hex")
txHex, err := os.ReadFile("data/many_inputs_tx.hex")
if err != nil {
panic(fmt.Sprintf("unable to read benchmark tx file: %v", err))
}
Expand Down
12 changes: 6 additions & 6 deletions txscript/reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"errors"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -490,7 +490,7 @@ func testScripts(t *testing.T, tests [][]interface{}, useSigCache bool) {
// TestScripts ensures all of the tests in script_tests.json execute with the
// expected results as defined in the test data.
func TestScripts(t *testing.T) {
file, err := ioutil.ReadFile("data/script_tests.json")
file, err := os.ReadFile("data/script_tests.json")
if err != nil {
t.Fatalf("TestScripts: %v\n", err)
}
Expand Down Expand Up @@ -521,7 +521,7 @@ func testVecF64ToUint32(f float64) uint32 {
// TestTxInvalidTests ensures all of the tests in tx_invalid.json fail as
// expected.
func TestTxInvalidTests(t *testing.T) {
file, err := ioutil.ReadFile("data/tx_invalid.json")
file, err := os.ReadFile("data/tx_invalid.json")
if err != nil {
t.Fatalf("TestTxInvalidTests: %v\n", err)
}
Expand Down Expand Up @@ -679,7 +679,7 @@ testloop:

// TestTxValidTests ensures all of the tests in tx_valid.json pass as expected.
func TestTxValidTests(t *testing.T) {
file, err := ioutil.ReadFile("data/tx_valid.json")
file, err := os.ReadFile("data/tx_valid.json")
if err != nil {
t.Fatalf("TestTxValidTests: %v\n", err)
}
Expand Down Expand Up @@ -836,7 +836,7 @@ testloop:
// in sighash.json.
// https://github.com/bitcoin/bitcoin/blob/master/src/test/data/sighash.json
func TestCalcSignatureHash(t *testing.T) {
file, err := ioutil.ReadFile("data/sighash.json")
file, err := os.ReadFile("data/sighash.json")
if err != nil {
t.Fatalf("TestCalcSignatureHash: %v\n", err)
}
Expand Down Expand Up @@ -1044,7 +1044,7 @@ func TestTaprootReferenceTests(t *testing.T) {
return nil
}

testJson, err := ioutil.ReadFile(path)
testJson, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("unable to read file: %v", err)
}
Expand Down

0 comments on commit aa7cb9f

Please sign in to comment.