multi: repleace ioutil.ReadFile

This commit is contained in:
erik 2023-06-13 21:22:44 +07:00 committed by ErikEk
parent 619c8f4eb8
commit ab83343c87
23 changed files with 55 additions and 62 deletions

View file

@ -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.

View file

@ -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
}

View file

@ -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
}

View file

@ -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) {

View file

@ -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)

View file

@ -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
}

View file

@ -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)
}

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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 {

View file

@ -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",

View file

@ -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)

View file

@ -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)

View file

@ -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
}

3
lnd.go
View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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
}

View file

@ -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

View file

@ -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
}

View file

@ -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
}