multi: fix ioutil deprecated function

update i/o functions to use os / io package functions instead
This commit is contained in:
theedtron 2024-03-09 04:41:41 +03:00 committed by GitHub
parent 447c95c9c2
commit b66f5b8379
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 32 additions and 35 deletions

View File

@ -1,7 +1,6 @@
package addrmgr package addrmgr
import ( import (
"io/ioutil"
"math/rand" "math/rand"
"net" "net"
"os" "os"
@ -108,7 +107,7 @@ func TestAddrManagerSerialization(t *testing.T) {
// We'll start by creating our address manager backed by a temporary // We'll start by creating our address manager backed by a temporary
// directory. // directory.
tempDir, err := ioutil.TempDir("", "addrmgr") tempDir, err := os.MkdirTemp("", "addrmgr")
if err != nil { if err != nil {
t.Fatalf("unable to create temp dir: %v", err) t.Fatalf("unable to create temp dir: %v", err)
} }
@ -148,7 +147,7 @@ func TestAddrManagerV1ToV2(t *testing.T) {
// We'll start by creating our address manager backed by a temporary // We'll start by creating our address manager backed by a temporary
// directory. // directory.
tempDir, err := ioutil.TempDir("", "addrmgr") tempDir, err := os.MkdirTemp("", "addrmgr")
if err != nil { if err != nil {
t.Fatalf("unable to create temp dir: %v", err) t.Fatalf("unable to create temp dir: %v", err)
} }

View File

@ -7,7 +7,7 @@ package txsort_test
import ( import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -64,7 +64,7 @@ func TestSort(t *testing.T) {
for _, test := range tests { for _, test := range tests {
// Load and deserialize the test transaction. // Load and deserialize the test transaction.
filePath := filepath.Join("testdata", test.hexFile) filePath := filepath.Join("testdata", test.hexFile)
txHexBytes, err := ioutil.ReadFile(filePath) txHexBytes, err := os.ReadFile(filePath)
if err != nil { if err != nil {
t.Errorf("ReadFile (%s): failed to read test file: %v", t.Errorf("ReadFile (%s): failed to read test file: %v",
test.name, err) test.name, err)

View File

@ -6,9 +6,10 @@ import (
"crypto/x509" "crypto/x509"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net" "net"
"net/http" "net/http"
"os"
"github.com/btcsuite/btcd/btcjson" "github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/go-socks/socks" "github.com/btcsuite/go-socks/socks"
@ -37,7 +38,7 @@ func newHTTPClient(cfg *config) (*http.Client, error) {
// Configure TLS if needed. // Configure TLS if needed.
var tlsConfig *tls.Config var tlsConfig *tls.Config
if !cfg.NoTLS && cfg.RPCCert != "" { if !cfg.NoTLS && cfg.RPCCert != "" {
pem, err := ioutil.ReadFile(cfg.RPCCert) pem, err := os.ReadFile(cfg.RPCCert)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -95,7 +96,7 @@ func sendPostRequest(marshalledJSON []byte, cfg *config) ([]byte, error) {
} }
// Read the raw bytes and close the response. // Read the raw bytes and close the response.
respBytes, err := ioutil.ReadAll(httpResponse.Body) respBytes, err := io.ReadAll(httpResponse.Body)
httpResponse.Body.Close() httpResponse.Body.Close()
if err != nil { if err != nil {
err = fmt.Errorf("error reading json reply: %v", err) err = fmt.Errorf("error reading json reply: %v", err)

View File

@ -6,7 +6,6 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -65,11 +64,11 @@ func main() {
} }
// Write cert and key files. // 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) fmt.Fprintf(os.Stderr, "cannot write cert: %v\n", err)
os.Exit(1) os.Exit(1)
} }
if err = ioutil.WriteFile(keyFile, key, 0600); err != nil { if err = os.WriteFile(keyFile, key, 0600); err != nil {
os.Remove(certFile) os.Remove(certFile)
fmt.Fprintf(os.Stderr, "cannot write key: %v\n", err) fmt.Fprintf(os.Stderr, "cannot write key: %v\n", err)
os.Exit(1) os.Exit(1)

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -23,14 +22,14 @@ func TestCreateDefaultConfigFile(t *testing.T) {
sampleConfigFile := filepath.Join(filepath.Dir(path), "sample-btcd.conf") sampleConfigFile := filepath.Join(filepath.Dir(path), "sample-btcd.conf")
// Setup a temporary directory // Setup a temporary directory
tmpDir, err := ioutil.TempDir("", "btcd") tmpDir, err := os.MkdirTemp("", "btcd")
if err != nil { if err != nil {
t.Fatalf("Failed creating a temporary directory: %v", err) t.Fatalf("Failed creating a temporary directory: %v", err)
} }
testpath := filepath.Join(tmpDir, "test.conf") testpath := filepath.Join(tmpDir, "test.conf")
// copy config file to location of btcd binary // copy config file to location of btcd binary
data, err := ioutil.ReadFile(sampleConfigFile) data, err := os.ReadFile(sampleConfigFile)
if err != nil { if err != nil {
t.Fatalf("Failed reading sample config file: %v", err) t.Fatalf("Failed reading sample config file: %v", err)
} }
@ -39,7 +38,7 @@ func TestCreateDefaultConfigFile(t *testing.T) {
t.Fatalf("Failed obtaining app path: %v", err) t.Fatalf("Failed obtaining app path: %v", err)
} }
tmpConfigFile := filepath.Join(appPath, "sample-btcd.conf") tmpConfigFile := filepath.Join(appPath, "sample-btcd.conf")
err = ioutil.WriteFile(tmpConfigFile, data, 0644) err = os.WriteFile(tmpConfigFile, data, 0644)
if err != nil { if err != nil {
t.Fatalf("Failed copying sample config file: %v", err) t.Fatalf("Failed copying sample config file: %v", err)
} }
@ -57,7 +56,7 @@ func TestCreateDefaultConfigFile(t *testing.T) {
t.Fatalf("Failed to create a default config file: %v", err) t.Fatalf("Failed to create a default config file: %v", err)
} }
content, err := ioutil.ReadFile(testpath) content, err := os.ReadFile(testpath)
if err != nil { if err != nil {
t.Fatalf("Failed to read generated default config file: %v", err) t.Fatalf("Failed to read generated default config file: %v", err)
} }

View File

@ -7,7 +7,6 @@ package database_test
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -123,7 +122,7 @@ func Example_blockStorageAndRetrieval() {
// Typically you wouldn't want to remove the database right away like // 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 // this, nor put it in the temp directory, but it's done here to ensure
// the example cleans up after itself. // the example cleans up after itself.
dbPath, err := ioutil.TempDir("", "exampleblkstorage") dbPath, err := os.MkdirTemp("", "exampleblkstorage")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return

View File

@ -1121,7 +1121,7 @@ func main() {
// generated by btcd when it starts the RPC server and doesn't already // generated by btcd when it starts the RPC server and doesn't already
// have one. // have one.
btcdHomeDir := btcutil.AppDataDir("btcd", false) 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -1185,7 +1185,7 @@ func main() {
// generated by btcd when it starts the RPC server and doesn't already // generated by btcd when it starts the RPC server and doesn't already
// have one. // have one.
btcdHomeDir := btcutil.AppDataDir("btcd", false) 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -1288,7 +1288,7 @@ func main() {
// generated by btcd when it starts the RPC server and doesn't already // generated by btcd when it starts the RPC server and doesn't already
// have one. // have one.
btcdHomeDir := btcutil.AppDataDir("btcd", false) 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -5,7 +5,7 @@
package main package main
import ( import (
"io/ioutil" "os"
"log" "log"
"path/filepath" "path/filepath"
"time" "time"
@ -33,7 +33,7 @@ func main() {
// Connect to local btcd RPC server using websockets. // Connect to local btcd RPC server using websockets.
btcdHomeDir := btcutil.AppDataDir("btcd", false) 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -5,7 +5,7 @@
package main package main
import ( import (
"io/ioutil" "os"
"log" "log"
"path/filepath" "path/filepath"
"time" "time"
@ -29,7 +29,7 @@ func main() {
// Connect to local btcwallet RPC server using websockets. // Connect to local btcwallet RPC server using websockets.
certHomeDir := btcutil.AppDataDir("btcwallet", false) 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -4650,10 +4650,10 @@ func genCertPair(certFile, keyFile string) error {
} }
// Write cert and key files. // 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 return err
} }
if err = ioutil.WriteFile(keyFile, key, 0600); err != nil { if err = os.WriteFile(keyFile, key, 0600); err != nil {
os.Remove(certFile) os.Remove(certFile)
return err return err
} }

View File

@ -7,7 +7,7 @@ package txscript
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "os"
"testing" "testing"
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
@ -25,7 +25,7 @@ var (
func init() { func init() {
// tx 620f57c92cf05a7f7e7f7d28255d5f7089437bc48e34dcfebf7751d08b7fb8f5 // tx 620f57c92cf05a7f7e7f7d28255d5f7089437bc48e34dcfebf7751d08b7fb8f5
txHex, err := ioutil.ReadFile("data/many_inputs_tx.hex") txHex, err := os.ReadFile("data/many_inputs_tx.hex")
if err != nil { if err != nil {
panic(fmt.Sprintf("unable to read benchmark tx file: %v", err)) panic(fmt.Sprintf("unable to read benchmark tx file: %v", err))
} }

View File

@ -11,7 +11,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/fs" "io/fs"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -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 // TestScripts ensures all of the tests in script_tests.json execute with the
// expected results as defined in the test data. // expected results as defined in the test data.
func TestScripts(t *testing.T) { func TestScripts(t *testing.T) {
file, err := ioutil.ReadFile("data/script_tests.json") file, err := os.ReadFile("data/script_tests.json")
if err != nil { if err != nil {
t.Fatalf("TestScripts: %v\n", err) t.Fatalf("TestScripts: %v\n", err)
} }
@ -521,7 +521,7 @@ func testVecF64ToUint32(f float64) uint32 {
// TestTxInvalidTests ensures all of the tests in tx_invalid.json fail as // TestTxInvalidTests ensures all of the tests in tx_invalid.json fail as
// expected. // expected.
func TestTxInvalidTests(t *testing.T) { func TestTxInvalidTests(t *testing.T) {
file, err := ioutil.ReadFile("data/tx_invalid.json") file, err := os.ReadFile("data/tx_invalid.json")
if err != nil { if err != nil {
t.Fatalf("TestTxInvalidTests: %v\n", err) t.Fatalf("TestTxInvalidTests: %v\n", err)
} }
@ -679,7 +679,7 @@ testloop:
// TestTxValidTests ensures all of the tests in tx_valid.json pass as expected. // TestTxValidTests ensures all of the tests in tx_valid.json pass as expected.
func TestTxValidTests(t *testing.T) { func TestTxValidTests(t *testing.T) {
file, err := ioutil.ReadFile("data/tx_valid.json") file, err := os.ReadFile("data/tx_valid.json")
if err != nil { if err != nil {
t.Fatalf("TestTxValidTests: %v\n", err) t.Fatalf("TestTxValidTests: %v\n", err)
} }
@ -836,7 +836,7 @@ testloop:
// in sighash.json. // in sighash.json.
// https://github.com/bitcoin/bitcoin/blob/master/src/test/data/sighash.json // https://github.com/bitcoin/bitcoin/blob/master/src/test/data/sighash.json
func TestCalcSignatureHash(t *testing.T) { func TestCalcSignatureHash(t *testing.T) {
file, err := ioutil.ReadFile("data/sighash.json") file, err := os.ReadFile("data/sighash.json")
if err != nil { if err != nil {
t.Fatalf("TestCalcSignatureHash: %v\n", err) t.Fatalf("TestCalcSignatureHash: %v\n", err)
} }
@ -1044,7 +1044,7 @@ func TestTaprootReferenceTests(t *testing.T) {
return nil return nil
} }
testJson, err := ioutil.ReadFile(path) testJson, err := os.ReadFile(path)
if err != nil { if err != nil {
return fmt.Errorf("unable to read file: %v", err) return fmt.Errorf("unable to read file: %v", err)
} }