Skip to content

Commit

Permalink
Remove usage of deprecated ioutil package functions [internal]
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Barger <artem@bargr.net>
  • Loading branch information
C0rWin authored and ale-linux committed Aug 17, 2023
1 parent fc95a82 commit b0b8e2f
Show file tree
Hide file tree
Showing 16 changed files with 39 additions and 42 deletions.
2 changes: 1 addition & 1 deletion core/common/ccprovider/ccprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (cifs *CCInfoFSImpl) PutChaincode(depSpec *pb.ChaincodeDeploymentSpec) (CCP
}

// DirEnumerator enumerates directories
type DirEnumerator func(string) ([]os.FileInfo, error)
type DirEnumerator func(string) ([]os.DirEntry, error)

// ChaincodeExtractor extracts chaincode from a given path
type ChaincodeExtractor func(ccNameVersion string, path string, getHasher GetHasher) (CCPackage, error)
Expand Down
11 changes: 5 additions & 6 deletions core/common/ccprovider/ccprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package ccprovider_test

import (
"crypto/sha256"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -39,7 +38,7 @@ func TestInstalledCCs(t *testing.T) {
}{
{
name: "Non-empty directory",
ls: ioutil.ReadDir,
ls: os.ReadDir,
extractCCFromPath: ccprovider.LoadPackage,
expected: []chaincode.InstalledChaincode{
{
Expand All @@ -57,21 +56,21 @@ func TestInstalledCCs(t *testing.T) {
},
{
name: "Nonexistent directory",
ls: ioutil.ReadDir,
ls: os.ReadDir,
extractCCFromPath: ccprovider.LoadPackage,
expected: nil,
directory: "nonexistent",
},
{
name: "Empty directory",
ls: ioutil.ReadDir,
ls: os.ReadDir,
extractCCFromPath: ccprovider.LoadPackage,
expected: nil,
directory: "empty",
},
{
name: "No permission to open directory",
ls: func(_ string) ([]os.FileInfo, error) {
ls: func(_ string) ([]os.DirEntry, error) {
return nil, errors.New("orange")
},
extractCCFromPath: ccprovider.LoadPackage,
Expand All @@ -81,7 +80,7 @@ func TestInstalledCCs(t *testing.T) {
},
{
name: "No permission on chaincode files",
ls: ioutil.ReadDir,
ls: os.ReadDir,
extractCCFromPath: func(_ string, _ string, _ ccprovider.GetHasher) (ccprovider.CCPackage, error) {
return nil, errors.New("banana")
},
Expand Down
8 changes: 4 additions & 4 deletions internal/configtxgen/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package encoder

import (
"fmt"
"io/ioutil"
"os"

"github.com/golang/protobuf/proto"
cb "github.com/hyperledger/fabric-protos-go/common"
Expand Down Expand Up @@ -251,23 +251,23 @@ func consenterProtosFromConfig(consenterMapping []*genesisconfig.Consenter) ([]*
// Expect the user to set the config value for client/server certs or identity to the
// path where they are persisted locally, then load these files to memory.
if consenter.ClientTLSCert != "" {
clientCert, err := ioutil.ReadFile(consenter.ClientTLSCert)
clientCert, err := os.ReadFile(consenter.ClientTLSCert)
if err != nil {
return nil, fmt.Errorf("cannot load client cert for consenter %s:%d: %s", c.GetHost(), c.GetPort(), err)
}
c.ClientTlsCert = clientCert
}

if consenter.ServerTLSCert != "" {
serverCert, err := ioutil.ReadFile(consenter.ServerTLSCert)
serverCert, err := os.ReadFile(consenter.ServerTLSCert)
if err != nil {
return nil, fmt.Errorf("cannot load server cert for consenter %s:%d: %s", c.GetHost(), c.GetPort(), err)
}
c.ServerTlsCert = serverCert
}

if consenter.Identity != "" {
identity, err := ioutil.ReadFile(consenter.Identity)
identity, err := os.ReadFile(consenter.Identity)
if err != nil {
return nil, fmt.Errorf("cannot load identity for consenter %s:%d: %s", c.GetHost(), c.GetPort(), err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/configtxlator/rest/configtxlator_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package rest

import (
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/golang/protobuf/proto"
Expand All @@ -23,7 +23,7 @@ func fieldBytes(fieldName string, r *http.Request) ([]byte, error) {
}
defer fieldFile.Close()

return ioutil.ReadAll(fieldFile)
return io.ReadAll(fieldFile)
}

func fieldConfigProto(fieldName string, r *http.Request) (*cb.Config, error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/configtxlator/rest/protolator_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package rest
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"
"reflect"

Expand Down Expand Up @@ -37,7 +37,7 @@ func Decode(w http.ResponseWriter, r *http.Request) {
return
}

buf, err := ioutil.ReadAll(r.Body)
buf, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, err)
Expand Down
4 changes: 2 additions & 2 deletions internal/peer/chaincode/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package chaincode

import (
"io/ioutil"
"os"

"github.com/golang/protobuf/proto"
pcommon "github.com/hyperledger/fabric-protos-go/common"
Expand Down Expand Up @@ -164,7 +164,7 @@ func (p *Packager) packageCC() error {
}

logger.Debugf("Packaged chaincode into deployment spec of size %d, output file %s", len(bytesToWrite), p.Input.OutputFile)
err = ioutil.WriteFile(p.Input.OutputFile, bytesToWrite, 0o700)
err = os.WriteFile(p.Input.OutputFile, bytesToWrite, 0o700)
if err != nil {
logger.Errorf("failed writing deployment spec to file [%s]: [%s]", p.Input.OutputFile, err)
return err
Expand Down
6 changes: 3 additions & 3 deletions internal/peer/chaincode/signpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package chaincode

import (
"fmt"
"io/ioutil"
"os"

"github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric/core/common/ccpackage"
Expand Down Expand Up @@ -46,7 +46,7 @@ func signpackage(cmd *cobra.Command, ipackageFile string, opackageFile string, c
}
}

b, err := ioutil.ReadFile(ipackageFile)
b, err := os.ReadFile(ipackageFile)
if err != nil {
return err
}
Expand All @@ -59,7 +59,7 @@ func signpackage(cmd *cobra.Command, ipackageFile string, opackageFile string, c
}

b = protoutil.MarshalOrPanic(env)
err = ioutil.WriteFile(opackageFile, b, 0o700)
err = os.WriteFile(opackageFile, b, 0o700)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/peer/channel/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package channel

import (
"fmt"
"io/ioutil"
"os"
"time"

"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -72,7 +72,7 @@ func createChannelFromDefaults(cf *ChannelCmdFactory) (*cb.Envelope, error) {
}

func createChannelFromConfigTx(configTxFileName string) (*cb.Envelope, error) {
cftx, err := ioutil.ReadFile(configTxFileName)
cftx, err := os.ReadFile(configTxFileName)
if err != nil {
return nil, ConfigTxFileNotFound(err.Error())
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func executeCreate(cf *ChannelCmdFactory) error {
if outputBlock != common.UndefinedParamValue {
file = outputBlock
}
err = ioutil.WriteFile(file, b, 0o644)
err = os.WriteFile(file, b, 0o644)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/peer/channel/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package channel

import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -105,7 +105,7 @@ func fetch(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error {
file = args[1]
}

if err = ioutil.WriteFile(file, b, 0o644); err != nil {
if err = os.WriteFile(file, b, 0o644); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions internal/peer/channel/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"

pcommon "github.com/hyperledger/fabric-protos-go/common"
pb "github.com/hyperledger/fabric-protos-go/peer"
Expand Down Expand Up @@ -59,7 +59,7 @@ func getJoinCCSpec() (*pb.ChaincodeSpec, error) {
return nil, errors.New("Must supply genesis block file")
}

gb, err := ioutil.ReadFile(genesisBlockPath)
gb, err := os.ReadFile(genesisBlockPath)
if err != nil {
return nil, GBFileNotFoundErr(err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions internal/peer/channel/signconfigtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package channel

import (
"io/ioutil"
"os"

"github.com/hyperledger/fabric/protoutil"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -45,7 +45,7 @@ func sign(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error {
}
}

fileData, err := ioutil.ReadFile(channelTxFile)
fileData, err := os.ReadFile(channelTxFile)
if err != nil {
return ConfigTxFileNotFound(err.Error())
}
Expand All @@ -62,5 +62,5 @@ func sign(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error {

sCtxEnvData := protoutil.MarshalOrPanic(sCtxEnv)

return ioutil.WriteFile(channelTxFile, sCtxEnvData, 0o660)
return os.WriteFile(channelTxFile, sCtxEnvData, 0o660)
}
4 changes: 2 additions & 2 deletions internal/peer/channel/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package channel
import (
"errors"
"fmt"
"io/ioutil"
"os"

"github.com/hyperledger/fabric/internal/peer/common"
"github.com/hyperledger/fabric/protoutil"
Expand Down Expand Up @@ -54,7 +54,7 @@ func update(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error {
}
}

fileData, err := ioutil.ReadFile(channelTxFile)
fileData, err := os.ReadFile(channelTxFile)
if err != nil {
return ConfigTxFileNotFound(err.Error())
}
Expand Down
7 changes: 3 additions & 4 deletions internal/peer/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"context"
"crypto/tls"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
Expand Down Expand Up @@ -307,7 +306,7 @@ func configFromEnv(prefix string) (address string, clientConfig comm.ClientConfi
ServerNameOverride: viper.GetString(prefix + ".tls.serverhostoverride"),
}
if secOpts.UseTLS {
caPEM, res := ioutil.ReadFile(config.GetPath(prefix + ".tls.rootcert.file"))
caPEM, res := os.ReadFile(config.GetPath(prefix + ".tls.rootcert.file"))
if res != nil {
err = errors.WithMessagef(res, "unable to load %s.tls.rootcert.file", prefix)
return
Expand All @@ -334,11 +333,11 @@ func configFromEnv(prefix string) (address string, clientConfig comm.ClientConfi

// getClientAuthInfoFromEnv reads client tls key file and cert file and returns the bytes for the files
func getClientAuthInfoFromEnv(prefix string) ([]byte, []byte, error) {
keyPEM, err := ioutil.ReadFile(config.GetPath(prefix + ".tls.clientKey.file"))
keyPEM, err := os.ReadFile(config.GetPath(prefix + ".tls.clientKey.file"))
if err != nil {
return nil, nil, errors.WithMessagef(err, "unable to load %s.tls.clientKey.file", prefix)
}
certPEM, err := ioutil.ReadFile(config.GetPath(prefix + ".tls.clientCert.file"))
certPEM, err := os.ReadFile(config.GetPath(prefix + ".tls.clientCert.file"))
if err != nil {
return nil, nil, errors.WithMessagef(err, "unable to load %s.tls.clientCert.file", prefix)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/peer/common/networkconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package common

import (
"io/ioutil"
"os"

"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
Expand Down Expand Up @@ -160,7 +160,7 @@ func GetConfig(fileName string) (*NetworkConfig, error) {
return nil, errors.New("filename cannot be empty")
}

data, err := ioutil.ReadFile(fileName)
data, err := os.ReadFile(fileName)
if err != nil {
return nil, errors.Wrap(err, "error reading connection profile")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/peer/common/peerclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package common
import (
"context"
"crypto/tls"
"io/ioutil"
"os"
"time"

pb "github.com/hyperledger/fabric-protos-go/peer"
Expand Down Expand Up @@ -66,7 +66,7 @@ func NewPeerClientForAddress(address, tlsRootCertFile string) (*PeerClient, erro
if tlsRootCertFile == "" {
return nil, errors.New("tls root cert file must be set")
}
caPEM, res := ioutil.ReadFile(tlsRootCertFile)
caPEM, res := os.ReadFile(tlsRootCertFile)
if res != nil {
return nil, errors.WithMessagef(res, "unable to load TLS root cert file from %s", tlsRootCertFile)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/peer/node/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -391,7 +390,7 @@ func serve(args []string) error {
legacyMetadataManager, err := cclifecycle.NewMetadataManager(
cclifecycle.EnumerateFunc(
func() ([]ccdef.InstalledChaincode, error) {
return ccInfoFSImpl.ListInstalledChaincodes(ccInfoFSImpl.GetChaincodeInstallPath(), ioutil.ReadDir, ccprovider.LoadPackage)
return ccInfoFSImpl.ListInstalledChaincodes(ccInfoFSImpl.GetChaincodeInstallPath(), os.ReadDir, ccprovider.LoadPackage)
},
),
)
Expand Down

0 comments on commit b0b8e2f

Please sign in to comment.