From ab83343c8718e292c8e392949318678e93df3e56 Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 13 Jun 2023 21:22:44 +0700 Subject: [PATCH] multi: repleace ioutil.ReadFile --- cert/selfsigned_test.go | 18 +++++++++--------- cert/tls.go | 6 +++--- chanbackup/backupfile.go | 3 +-- chanbackup/backupfile_test.go | 3 +-- cmd/lncli/cmd_macaroon.go | 5 +++-- cmd/lncli/cmd_open_channel.go | 3 +-- cmd/lncli/cmd_profile.go | 3 +-- cmd/lncli/cmd_walletunlocker.go | 3 +-- cmd/lncli/commands.go | 3 +-- cmd/lncli/profile.go | 7 ++++--- config.go | 5 ++--- config_builder.go | 3 +-- itest/lnd_channel_backup_test.go | 13 ++++++------- itest/lnd_misc_test.go | 6 +++--- kvdb/backend.go | 2 +- lnd.go | 3 +-- lntest/node/harness_node.go | 2 +- lnwallet/rpcwallet/rpcwallet.go | 6 +++--- lnwallet/transactions_test.go | 4 ++-- routing/pathfind_test.go | 4 ++-- tls_manager_test.go | 9 +++++---- tor/cmd_onion.go | 2 +- tor/controller.go | 4 ++-- 23 files changed, 55 insertions(+), 62 deletions(-) diff --git a/cert/selfsigned_test.go b/cert/selfsigned_test.go index 614225a5a..ca55c2772 100644 --- a/cert/selfsigned_test.go +++ b/cert/selfsigned_test.go @@ -1,7 +1,7 @@ package cert_test import ( - "io/ioutil" + "os" "path/filepath" "testing" "time" @@ -42,10 +42,10 @@ func TestIsOutdatedCert(t *testing.T) { // number of IPs and domains. for numIPs := 1; numIPs <= len(extraIPs); numIPs++ { for numDomains := 1; numDomains <= len(extraDomains); numDomains++ { - certBytes, err := ioutil.ReadFile(certPath) + certBytes, err := os.ReadFile(certPath) require.NoError(t, err) - keyBytes, err := ioutil.ReadFile(keyPath) + keyBytes, err := os.ReadFile(keyPath) require.NoError(t, err) _, parsedCert, err := cert.LoadCertFromBytes( @@ -98,10 +98,10 @@ func TestIsOutdatedPermutation(t *testing.T) { err = cert.WriteCertPair(certPath, keyPath, certBytes, keyBytes) require.NoError(t, err) - certBytes, err = ioutil.ReadFile(certPath) + certBytes, err = os.ReadFile(certPath) require.NoError(t, err) - keyBytes, err = ioutil.ReadFile(keyPath) + keyBytes, err = os.ReadFile(keyPath) require.NoError(t, err) _, parsedCert, err := cert.LoadCertFromBytes(certBytes, keyBytes) @@ -171,10 +171,10 @@ func TestTLSDisableAutofill(t *testing.T) { require.NoError(t, err) // Read certs from disk. - certBytes, err = ioutil.ReadFile(certPath) + certBytes, err = os.ReadFile(certPath) require.NoError(t, err) - keyBytes, err = ioutil.ReadFile(keyPath) + keyBytes, err = os.ReadFile(keyPath) require.NoError(t, err) // Load the certificate. @@ -230,10 +230,10 @@ func TestTLSConfig(t *testing.T) { err = cert.WriteCertPair(certPath, keyPath, certBytes, keyBytes) require.NoError(t, err) - certBytes, err = ioutil.ReadFile(certPath) + certBytes, err = os.ReadFile(certPath) require.NoError(t, err) - keyBytes, err = ioutil.ReadFile(keyPath) + keyBytes, err = os.ReadFile(keyPath) require.NoError(t, err) // Load the certificate. diff --git a/cert/tls.go b/cert/tls.go index 4e5d7c81e..6f60ae198 100644 --- a/cert/tls.go +++ b/cert/tls.go @@ -3,7 +3,7 @@ package cert import ( "crypto/tls" "crypto/x509" - "io/ioutil" + "os" "sync" ) @@ -31,11 +31,11 @@ var ( func GetCertBytesFromPath(certPath, keyPath string) (certBytes, keyBytes []byte, err error) { - certBytes, err = ioutil.ReadFile(certPath) + certBytes, err = os.ReadFile(certPath) if err != nil { return nil, nil, err } - keyBytes, err = ioutil.ReadFile(keyPath) + keyBytes, err = os.ReadFile(keyPath) if err != nil { return nil, nil, err } diff --git a/chanbackup/backupfile.go b/chanbackup/backupfile.go index 9c1e788c1..5ba6aa756 100644 --- a/chanbackup/backupfile.go +++ b/chanbackup/backupfile.go @@ -2,7 +2,6 @@ package chanbackup import ( "fmt" - "io/ioutil" "os" "path/filepath" @@ -138,7 +137,7 @@ func (b *MultiFile) ExtractMulti(keyChain keychain.KeyRing) (*Multi, error) { // Now that we've confirmed the target file is populated, we'll read // all the contents of the file. This function ensures that file is // always closed, even if we can't read the contents. - multiBytes, err := ioutil.ReadFile(b.fileName) + multiBytes, err := os.ReadFile(b.fileName) if err != nil { return nil, err } diff --git a/chanbackup/backupfile_test.go b/chanbackup/backupfile_test.go index fbd0cc7dd..b0d3ba66f 100644 --- a/chanbackup/backupfile_test.go +++ b/chanbackup/backupfile_test.go @@ -3,7 +3,6 @@ package chanbackup import ( "bytes" "fmt" - "io/ioutil" "math/rand" "os" "path/filepath" @@ -27,7 +26,7 @@ func assertBackupMatches(t *testing.T, filePath string, t.Helper() - packedBackup, err := ioutil.ReadFile(filePath) + packedBackup, err := os.ReadFile(filePath) require.NoError(t, err, "unable to test file") if !bytes.Equal(packedBackup, currentBackup) { diff --git a/cmd/lncli/cmd_macaroon.go b/cmd/lncli/cmd_macaroon.go index 54e03057a..894e8da6b 100644 --- a/cmd/lncli/cmd_macaroon.go +++ b/cmd/lncli/cmd_macaroon.go @@ -6,6 +6,7 @@ import ( "fmt" "io/ioutil" "net" + "os" "strconv" "strings" "unicode" @@ -352,7 +353,7 @@ func printMacaroon(ctx *cli.Context) error { macPath := lncfg.CleanAndExpandPath(ctx.String("macaroon_file")) // Load the specified macaroon file. - macBytes, err = ioutil.ReadFile(macPath) + macBytes, err = os.ReadFile(macPath) if err != nil { return fmt.Errorf("unable to read macaroon path %v: %v", macPath, err) @@ -441,7 +442,7 @@ func constrainMacaroon(ctx *cli.Context) error { sourceMacFile := lncfg.CleanAndExpandPath(args.First()) args = args.Tail() - sourceMacBytes, err := ioutil.ReadFile(sourceMacFile) + sourceMacBytes, err := os.ReadFile(sourceMacFile) if err != nil { return fmt.Errorf("error trying to read source macaroon file "+ "%s: %v", sourceMacFile, err) diff --git a/cmd/lncli/cmd_open_channel.go b/cmd/lncli/cmd_open_channel.go index 74cb1668a..e3044c835 100644 --- a/cmd/lncli/cmd_open_channel.go +++ b/cmd/lncli/cmd_open_channel.go @@ -9,7 +9,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "os" "strconv" "strings" @@ -1016,7 +1015,7 @@ func readTerminalOrFile(quit chan struct{}) (string, error) { // If it's a path to an existing file and it's small enough, let's try // to read its content now. - content, err := ioutil.ReadFile(maybeFile) + content, err := os.ReadFile(maybeFile) if err != nil { return "", err } diff --git a/cmd/lncli/cmd_profile.go b/cmd/lncli/cmd_profile.go index 21666be96..3b1bf6487 100644 --- a/cmd/lncli/cmd_profile.go +++ b/cmd/lncli/cmd_profile.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "io/ioutil" "os" "path" "strings" @@ -423,7 +422,7 @@ func profileAddMacaroon(ctx *cli.Context) error { // Now load and possibly encrypt the macaroon file. macPath := lncfg.CleanAndExpandPath(ctx.GlobalString("macaroonpath")) - macBytes, err := ioutil.ReadFile(macPath) + macBytes, err := os.ReadFile(macPath) if err != nil { return fmt.Errorf("unable to read macaroon path: %w", err) } diff --git a/cmd/lncli/cmd_walletunlocker.go b/cmd/lncli/cmd_walletunlocker.go index 9227d10d4..650ef80ea 100644 --- a/cmd/lncli/cmd_walletunlocker.go +++ b/cmd/lncli/cmd_walletunlocker.go @@ -5,7 +5,6 @@ import ( "bytes" "encoding/hex" "fmt" - "io/ioutil" "os" "strconv" "strings" @@ -712,7 +711,7 @@ func createWatchOnly(ctx *cli.Context) error { } jsonFile := lncfg.CleanAndExpandPath(ctx.Args().First()) - jsonBytes, err := ioutil.ReadFile(jsonFile) + jsonBytes, err := os.ReadFile(jsonFile) if err != nil { return fmt.Errorf("error reading JSON from file %v: %v", jsonFile, err) diff --git a/cmd/lncli/commands.go b/cmd/lncli/commands.go index 5ffca5c73..a510138eb 100644 --- a/cmd/lncli/commands.go +++ b/cmd/lncli/commands.go @@ -9,7 +9,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "math" "os" "strconv" @@ -2833,7 +2832,7 @@ func parseChanBackups(ctx *cli.Context) (*lnrpc.RestoreChanBackupRequest, error) }, nil case ctx.IsSet("multi_file"): - packedMulti, err := ioutil.ReadFile(ctx.String("multi_file")) + packedMulti, err := os.ReadFile(ctx.String("multi_file")) if err != nil { return nil, fmt.Errorf("unable to decode multi packed "+ "backup: %v", err) diff --git a/cmd/lncli/profile.go b/cmd/lncli/profile.go index 90ac69c0e..75dc9ab72 100644 --- a/cmd/lncli/profile.go +++ b/cmd/lncli/profile.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "io/ioutil" + "os" "path" "strings" @@ -129,7 +130,7 @@ func profileFromContext(ctx *cli.Context, store, skipMacaroons bool) ( var tlsCert []byte if tlsCertPath != "" && !insecure { var err error - tlsCert, err = ioutil.ReadFile(tlsCertPath) + tlsCert, err = os.ReadFile(tlsCertPath) if err != nil { return nil, fmt.Errorf("could not load TLS cert "+ "file: %v", err) @@ -167,7 +168,7 @@ func profileFromContext(ctx *cli.Context, store, skipMacaroons bool) ( } // Now load and possibly encrypt the macaroon file. - macBytes, err := ioutil.ReadFile(macPath) + macBytes, err := os.ReadFile(macPath) if err != nil { return nil, fmt.Errorf("unable to read macaroon path (check "+ "the network setting!): %v", err) @@ -222,7 +223,7 @@ func loadProfileFile(file string) (*profileFile, error) { return nil, errNoProfileFile } - content, err := ioutil.ReadFile(file) + content, err := os.ReadFile(file) if err != nil { return nil, fmt.Errorf("could not load profile file %s: %w", file, err) diff --git a/config.go b/config.go index 8a347948b..7084500b4 100644 --- a/config.go +++ b/config.go @@ -9,7 +9,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net" "os" "os/user" @@ -1846,7 +1845,7 @@ func parseRPCParams(cConfig *lncfg.Chain, nodeConfig interface{}, // We convert the cookie into a user name and password. if conf.RPCCookie != "" { - cookie, err := ioutil.ReadFile(conf.RPCCookie) + cookie, err := os.ReadFile(conf.RPCCookie) if err != nil { return fmt.Errorf("cannot read cookie file: %w", err) @@ -2119,7 +2118,7 @@ func extractBitcoindRPCParams(networkName, bitcoindDataDir, bitcoindConfigPath, if rpcCookiePath != "" { cookiePath = rpcCookiePath } - cookie, err := ioutil.ReadFile(cookiePath) + cookie, err := os.ReadFile(cookiePath) if err == nil { splitCookie := strings.Split(string(cookie), ":") if len(splitCookie) == 2 { diff --git a/config_builder.go b/config_builder.go index bbf291506..895f7e4ad 100644 --- a/config_builder.go +++ b/config_builder.go @@ -6,7 +6,6 @@ import ( "database/sql" "errors" "fmt" - "io/ioutil" "net" "os" "path/filepath" @@ -329,7 +328,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context, case d.cfg.WalletUnlockPasswordFile != "" && walletExists: d.logger.Infof("Attempting automatic wallet unlock with " + "password provided in file") - pwBytes, err := ioutil.ReadFile(d.cfg.WalletUnlockPasswordFile) + pwBytes, err := os.ReadFile(d.cfg.WalletUnlockPasswordFile) if err != nil { return nil, nil, nil, fmt.Errorf("error reading "+ "password from file %s: %v", diff --git a/itest/lnd_channel_backup_test.go b/itest/lnd_channel_backup_test.go index c29859e32..0e7393dc3 100644 --- a/itest/lnd_channel_backup_test.go +++ b/itest/lnd_channel_backup_test.go @@ -3,7 +3,6 @@ package itest import ( "context" "fmt" - "io/ioutil" "os" "path/filepath" "strconv" @@ -285,7 +284,7 @@ func testChannelBackupRestoreBasic(ht *lntest.HarnessTest) { // Read the entire Multi backup stored within // this node's channel.backup file. - multi, err := ioutil.ReadFile(backupFilePath) + multi, err := os.ReadFile(backupFilePath) require.NoError(st, err) // Now that we have Dave's backup file, we'll @@ -376,7 +375,7 @@ func testChannelBackupRestoreBasic(ht *lntest.HarnessTest) { // Read the entire Multi backup stored within // this node's channel.backup file. - multi, err := ioutil.ReadFile(backupFilePath) + multi, err := os.ReadFile(backupFilePath) require.NoError(st, err) // Now that we have Dave's backup file, we'll @@ -501,7 +500,7 @@ func runChanRestoreScenarioUnConfirmed(ht *lntest.HarnessTest, useFile bool) { backupFilePath := dave.Cfg.ChanBackupPath() // Read the entire Multi backup stored within this node's // channel.backup file. - multi, err = ioutil.ReadFile(backupFilePath) + multi, err = os.ReadFile(backupFilePath) require.NoError(ht, err) } else { // For this restoration method, we'll grab the current @@ -646,7 +645,7 @@ func runChanRestoreScenarioCommitTypes(ht *lntest.HarnessTest, // Read the entire Multi backup stored within this node's // channels.backup file. - multi, err := ioutil.ReadFile(backupFilePath) + multi, err := os.ReadFile(backupFilePath) require.NoError(ht, err) // If this was a zero conf taproot channel, then since it's private, @@ -774,7 +773,7 @@ func runChanRestoreScenarioForceClose(ht *lntest.HarnessTest, zeroConf bool) { // Read the entire Multi backup stored within this node's // channel.backup file. - multi, err := ioutil.ReadFile(backupFilePath) + multi, err := os.ReadFile(backupFilePath) require.NoError(ht, err) // Now that we have Dave's backup file, we'll create a new nodeRestorer @@ -907,7 +906,7 @@ func testChannelBackupUpdates(ht *lntest.HarnessTest) { // the on disk back up file to our currentBackup pointer above. assertBackupFileState := func() { err := wait.NoError(func() error { - packedBackup, err := ioutil.ReadFile(backupFilePath) + packedBackup, err := os.ReadFile(backupFilePath) if err != nil { return fmt.Errorf("unable to read backup "+ "file: %v", err) diff --git a/itest/lnd_misc_test.go b/itest/lnd_misc_test.go index 7124f1488..163fc436d 100644 --- a/itest/lnd_misc_test.go +++ b/itest/lnd_misc_test.go @@ -4,7 +4,7 @@ import ( "context" "encoding/hex" "fmt" - "io/ioutil" + "os" "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/txscript" @@ -708,7 +708,7 @@ func testAbandonChannel(ht *lntest.HarnessTest) { // To make sure the channel is removed from the backup file as well // when being abandoned, grab a backup snapshot so we can compare it // with the later state. - bkupBefore, err := ioutil.ReadFile(alice.Cfg.ChanBackupPath()) + bkupBefore, err := os.ReadFile(alice.Cfg.ChanBackupPath()) require.NoError(ht, err, "channel backup before abandoning channel") // Send request to abandon channel. @@ -733,7 +733,7 @@ func testAbandonChannel(ht *lntest.HarnessTest) { // Make sure the channel is no longer in the channel backup list. err = wait.NoError(func() error { - bkupAfter, err := ioutil.ReadFile(alice.Cfg.ChanBackupPath()) + bkupAfter, err := os.ReadFile(alice.Cfg.ChanBackupPath()) if err != nil { return fmt.Errorf("could not get channel backup "+ "before abandoning channel: %v", err) diff --git a/kvdb/backend.go b/kvdb/backend.go index f034cee7e..c115c4f7d 100644 --- a/kvdb/backend.go +++ b/kvdb/backend.go @@ -218,7 +218,7 @@ func lastCompactionDate(dbFile string) (time.Time, error) { return zeroTime, nil } - tsBytes, err := ioutil.ReadFile(tsFile) + tsBytes, err := os.ReadFile(tsFile) if err != nil { return zeroTime, err } diff --git a/lnd.go b/lnd.go index f4ef03a07..0a85e1bf3 100644 --- a/lnd.go +++ b/lnd.go @@ -8,7 +8,6 @@ import ( "context" "errors" "fmt" - "io/ioutil" "net" "net/http" "net/http/pprof" @@ -83,7 +82,7 @@ func AdminAuthOptions(cfg *Config, skipMacaroons bool) ([]grpc.DialOption, // Get the admin macaroon if macaroons are active. if !skipMacaroons && !cfg.NoMacaroons { // Load the admin macaroon file. - macBytes, err := ioutil.ReadFile(cfg.AdminMacPath) + macBytes, err := os.ReadFile(cfg.AdminMacPath) if err != nil { return nil, fmt.Errorf("unable to read macaroon "+ "path (check the network setting!): %v", err) diff --git a/lntest/node/harness_node.go b/lntest/node/harness_node.go index de6f40a77..1afb095d8 100644 --- a/lntest/node/harness_node.go +++ b/lntest/node/harness_node.go @@ -297,7 +297,7 @@ func (hn *HarnessNode) ReadMacaroon(macPath string, timeout time.Duration) ( // using it. var mac *macaroon.Macaroon err := wait.NoError(func() error { - macBytes, err := ioutil.ReadFile(macPath) + macBytes, err := os.ReadFile(macPath) if err != nil { return fmt.Errorf("error reading macaroon file: %w", err) diff --git a/lnwallet/rpcwallet/rpcwallet.go b/lnwallet/rpcwallet/rpcwallet.go index 5a16490e5..13f435579 100644 --- a/lnwallet/rpcwallet/rpcwallet.go +++ b/lnwallet/rpcwallet/rpcwallet.go @@ -7,7 +7,7 @@ import ( "crypto/x509" "errors" "fmt" - "io/ioutil" + "os" "time" "github.com/btcsuite/btcd/btcec/v2" @@ -1259,7 +1259,7 @@ func extractSignature(in *psbt.PInput, func connectRPC(hostPort, tlsCertPath, macaroonPath string, timeout time.Duration) (*grpc.ClientConn, error) { - certBytes, err := ioutil.ReadFile(tlsCertPath) + certBytes, err := os.ReadFile(tlsCertPath) if err != nil { return nil, fmt.Errorf("error reading TLS cert file %v: %w", tlsCertPath, err) @@ -1271,7 +1271,7 @@ func connectRPC(hostPort, tlsCertPath, macaroonPath string, "certificate") } - macBytes, err := ioutil.ReadFile(macaroonPath) + macBytes, err := os.ReadFile(macaroonPath) if err != nil { return nil, fmt.Errorf("error reading macaroon file %v: %w", macaroonPath, err) diff --git a/lnwallet/transactions_test.go b/lnwallet/transactions_test.go index ab0ef7328..1f15523c4 100644 --- a/lnwallet/transactions_test.go +++ b/lnwallet/transactions_test.go @@ -8,8 +8,8 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net" + "os" "sort" "strings" "testing" @@ -229,7 +229,7 @@ func TestCommitmentAndHTLCTransactions(t *testing.T) { var testCases []testCase - jsonText, err := ioutil.ReadFile(set.jsonFile) + jsonText, err := os.ReadFile(set.jsonFile) require.NoError(t, err) err = json.Unmarshal(jsonText, &testCases) diff --git a/routing/pathfind_test.go b/routing/pathfind_test.go index de4935a81..57f723ce6 100644 --- a/routing/pathfind_test.go +++ b/routing/pathfind_test.go @@ -8,9 +8,9 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "math" "net" + "os" "reflect" "strings" "testing" @@ -183,7 +183,7 @@ func makeTestGraph(t *testing.T, useCache bool) (*channeldb.ChannelGraph, func parseTestGraph(t *testing.T, useCache bool, path string) ( *testGraphInstance, error) { - graphJSON, err := ioutil.ReadFile(path) + graphJSON, err := os.ReadFile(path) if err != nil { return nil, err } diff --git a/tls_manager_test.go b/tls_manager_test.go index 935834971..348f095bc 100644 --- a/tls_manager_test.go +++ b/tls_manager_test.go @@ -12,6 +12,7 @@ import ( "io/ioutil" "math/big" "net" + "os" "testing" "time" @@ -199,7 +200,7 @@ func TestGenerateEphemeralCert(t *testing.T) { keyBytes, err := tlsManager.loadEphemeralCertificate() require.NoError(t, err, "failed to generate new certificate") - certBytes, err := ioutil.ReadFile(tmpCertPath) + certBytes, err := os.ReadFile(tmpCertPath) require.NoError(t, err) tlsr, err := cert.NewTLSReloader(certBytes, keyBytes) @@ -207,15 +208,15 @@ func TestGenerateEphemeralCert(t *testing.T) { tlsManager.tlsReloader = tlsr // Make sure .tmp file is created at the tmp cert path. - _, err = ioutil.ReadFile(tmpCertPath) + _, err = os.ReadFile(tmpCertPath) require.NoError(t, err, "couldn't find temp cert file") // But no key should be stored. - _, err = ioutil.ReadFile(cfg.TLSKeyPath) + _, err = os.ReadFile(cfg.TLSKeyPath) require.Error(t, err, "shouldn't have found file") // And no permanent cert file should be stored. - _, err = ioutil.ReadFile(cfg.TLSCertPath) + _, err = os.ReadFile(cfg.TLSCertPath) require.Error(t, err, "shouldn't have found a permanent cert file") // Now test that when we reload the certificate it generates the new diff --git a/tor/cmd_onion.go b/tor/cmd_onion.go index 72f508fc0..3ac928b40 100644 --- a/tor/cmd_onion.go +++ b/tor/cmd_onion.go @@ -124,7 +124,7 @@ func (f *OnionFile) PrivateKey() ([]byte, error) { } // Try to read the Tor private key to pass into the AddOnion call. - privateKeyContent, err := ioutil.ReadFile(f.privateKeyPath) + privateKeyContent, err := os.ReadFile(f.privateKeyPath) if err != nil { return nil, err } diff --git a/tor/controller.go b/tor/controller.go index 241b3ddbe..47ea6e129 100644 --- a/tor/controller.go +++ b/tor/controller.go @@ -8,8 +8,8 @@ import ( "encoding/hex" "errors" "fmt" - "io/ioutil" "net/textproto" + "os" "regexp" "strconv" "strings" @@ -597,7 +597,7 @@ func (c *Controller) getAuthCookie(info protocolInfo) ([]byte, error) { cookieFilePath = strings.Trim(cookieFilePath, "\"") // Read the cookie from the file and ensure it has the correct length. - cookie, err := ioutil.ReadFile(cookieFilePath) + cookie, err := os.ReadFile(cookieFilePath) if err != nil { return nil, err }