From b66f5b837916252a9da959955ca340451e4c1855 Mon Sep 17 00:00:00 2001 From: theedtron Date: Sat, 9 Mar 2024 04:41:41 +0300 Subject: [PATCH] multi: fix ioutil deprecated function update i/o functions to use os / io package functions instead --- addrmgr/addrmanager_internal_test.go | 5 ++--- btcutil/txsort/txsort_test.go | 4 ++-- cmd/btcctl/httpclient.go | 7 ++++--- cmd/gencerts/gencerts.go | 5 ++--- config_test.go | 9 ++++----- database/example_test.go | 3 +-- docs/json_rpc_api.md | 6 +++--- rpcclient/examples/btcdwebsockets/main.go | 4 ++-- rpcclient/examples/btcwalletwebsockets/main.go | 4 ++-- rpcserver.go | 4 ++-- txscript/bench_test.go | 4 ++-- txscript/reference_test.go | 12 ++++++------ 12 files changed, 32 insertions(+), 35 deletions(-) diff --git a/addrmgr/addrmanager_internal_test.go b/addrmgr/addrmanager_internal_test.go index b58ff4b4..ff19eae1 100644 --- a/addrmgr/addrmanager_internal_test.go +++ b/addrmgr/addrmanager_internal_test.go @@ -1,7 +1,6 @@ package addrmgr import ( - "io/ioutil" "math/rand" "net" "os" @@ -108,7 +107,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) } @@ -148,7 +147,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) } diff --git a/btcutil/txsort/txsort_test.go b/btcutil/txsort/txsort_test.go index dd214929..16a3e61c 100644 --- a/btcutil/txsort/txsort_test.go +++ b/btcutil/txsort/txsort_test.go @@ -7,7 +7,7 @@ package txsort_test import ( "bytes" "encoding/hex" - "io/ioutil" + "os" "path/filepath" "testing" @@ -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) diff --git a/cmd/btcctl/httpclient.go b/cmd/btcctl/httpclient.go index 2a0f6dff..c7b4b7e3 100644 --- a/cmd/btcctl/httpclient.go +++ b/cmd/btcctl/httpclient.go @@ -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" @@ -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 } @@ -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) diff --git a/cmd/gencerts/gencerts.go b/cmd/gencerts/gencerts.go index 27c9ae38..328d5ea7 100644 --- a/cmd/gencerts/gencerts.go +++ b/cmd/gencerts/gencerts.go @@ -6,7 +6,6 @@ package main import ( "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -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) diff --git a/config_test.go b/config_test.go index e54a9f5f..42a0cd4b 100644 --- a/config_test.go +++ b/config_test.go @@ -1,7 +1,6 @@ package main import ( - "io/ioutil" "os" "path/filepath" "regexp" @@ -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) } @@ -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) } @@ -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) } diff --git a/database/example_test.go b/database/example_test.go index b64baf2c..1110d0db 100644 --- a/database/example_test.go +++ b/database/example_test.go @@ -7,7 +7,6 @@ package database_test import ( "bytes" "fmt" - "io/ioutil" "os" "path/filepath" @@ -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 diff --git a/docs/json_rpc_api.md b/docs/json_rpc_api.md index 2c7d4554..fc32040d 100644 --- a/docs/json_rpc_api.md +++ b/docs/json_rpc_api.md @@ -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) } @@ -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) } @@ -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) } diff --git a/rpcclient/examples/btcdwebsockets/main.go b/rpcclient/examples/btcdwebsockets/main.go index e3f4c13e..878526b0 100644 --- a/rpcclient/examples/btcdwebsockets/main.go +++ b/rpcclient/examples/btcdwebsockets/main.go @@ -5,7 +5,7 @@ package main import ( - "io/ioutil" + "os" "log" "path/filepath" "time" @@ -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) } diff --git a/rpcclient/examples/btcwalletwebsockets/main.go b/rpcclient/examples/btcwalletwebsockets/main.go index 3cbd9a36..a63ef3db 100644 --- a/rpcclient/examples/btcwalletwebsockets/main.go +++ b/rpcclient/examples/btcwalletwebsockets/main.go @@ -5,7 +5,7 @@ package main import ( - "io/ioutil" + "os" "log" "path/filepath" "time" @@ -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) } diff --git a/rpcserver.go b/rpcserver.go index d6f3167f..eaa5f056 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -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 } diff --git a/txscript/bench_test.go b/txscript/bench_test.go index 0d1aa914..60b0d9e1 100644 --- a/txscript/bench_test.go +++ b/txscript/bench_test.go @@ -7,7 +7,7 @@ package txscript import ( "bytes" "fmt" - "io/ioutil" + "os" "testing" "github.com/btcsuite/btcd/chaincfg" @@ -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)) } diff --git a/txscript/reference_test.go b/txscript/reference_test.go index 59acdb8d..16f06c4f 100644 --- a/txscript/reference_test.go +++ b/txscript/reference_test.go @@ -11,7 +11,7 @@ import ( "errors" "fmt" "io/fs" - "io/ioutil" + "os" "path/filepath" "strconv" "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 // 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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) }