Skip to content

Commit

Permalink
change ioutil.WriteFile -> os.WriteFile
Browse files Browse the repository at this point in the history
Signed-off-by: Фёдор Партанский <pfi79@mail.ru>
  • Loading branch information
pfi79 authored and C0rWin committed Aug 18, 2023
1 parent 42ceab0 commit 4ca66d4
Show file tree
Hide file tree
Showing 53 changed files with 193 additions and 204 deletions.
7 changes: 3 additions & 4 deletions bccsp/sw/fileks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"crypto/elliptic"
"crypto/rand"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -77,16 +76,16 @@ func TestBigKeyFile(t *testing.T) {
}
copy(bigBuff, rawKey)

//>64k, so that total file size will be too big
ioutil.WriteFile(filepath.Join(ksPath, "bigfile.pem"), bigBuff, 0o666)
// >64k, so that total file size will be too big
os.WriteFile(filepath.Join(ksPath, "bigfile.pem"), bigBuff, 0o666)

_, err = ks.GetKey(ski)
require.Error(t, err)
expected := fmt.Sprintf("key with SKI %x not found in %s", ski, ksPath)
require.EqualError(t, err, expected)

// 1k, so that the key would be found
ioutil.WriteFile(filepath.Join(ksPath, "smallerfile.pem"), bigBuff[0:1<<10], 0o666)
os.WriteFile(filepath.Join(ksPath, "smallerfile.pem"), bigBuff[0:1<<10], 0o666)

_, err = ks.GetKey(ski)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion ccaas_builder/cmd/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func run() error {
return fmt.Errorf("failed to marshal updated connection.json file: %s", err)
}

err = ioutil.WriteFile(connectionDestFile, updatedConnectionBytes, fileInfo.Mode())
err = os.WriteFile(connectionDestFile, updatedConnectionBytes, fileInfo.Mode())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/common/signer/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ ZsQXrlIqlmNalfYPX+NDDELqlpXQBeEqnA==

defer os.Remove(tmpFile.Name())

err = ioutil.WriteFile(tmpFile.Name(), testCase.keyBytes, 0o600)
err = os.WriteFile(tmpFile.Name(), testCase.keyBytes, 0o600)
require.NoError(t, err)

signer, err := NewSigner(Config{
Expand Down
2 changes: 1 addition & 1 deletion cmd/configtxgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func writeFile(filename string, data []byte, perm os.FileMode) error {
return err
}
}
return ioutil.WriteFile(filename, data, perm)
return os.WriteFile(filename, data, perm)
}

func dirExists(path string) (bool, error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/configtxgen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestUnsuccessfulChannelTxFileCreation(t *testing.T) {
configTxDest := filepath.Join(tmpDir, "configtx")

config := genesisconfig.Load(genesisconfig.SampleSingleMSPChannelProfile, configtest.GetDevConfigDir())
require.NoError(t, ioutil.WriteFile(configTxDest, []byte{}, 0o440))
require.NoError(t, os.WriteFile(configTxDest, []byte{}, 0o440))
defer os.Remove(configTxDest)

require.EqualError(t, doOutputChannelCreateTx(config, nil, "foo", configTxDest), fmt.Sprintf("error writing channel create tx: open %s: permission denied", configTxDest))
Expand Down
22 changes: 11 additions & 11 deletions cmd/osnadmin/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,39 +745,39 @@ func checkCLIError(output string, exit int, err error, expectedError string) {
func generateCertificates(tempDir string) {
serverCA, err := tlsgen.NewCA()
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tempDir, "server-ca.pem"), serverCA.CertBytes(), 0o640)
err = os.WriteFile(filepath.Join(tempDir, "server-ca.pem"), serverCA.CertBytes(), 0o640)
Expect(err).NotTo(HaveOccurred())
serverKeyPair, err := serverCA.NewServerCertKeyPair("127.0.0.1")
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tempDir, "server-cert.pem"), serverKeyPair.Cert, 0o640)
err = os.WriteFile(filepath.Join(tempDir, "server-cert.pem"), serverKeyPair.Cert, 0o640)
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tempDir, "server-key.pem"), serverKeyPair.Key, 0o640)
err = os.WriteFile(filepath.Join(tempDir, "server-key.pem"), serverKeyPair.Key, 0o640)
Expect(err).NotTo(HaveOccurred())

serverIntermediateCA, err := serverCA.NewIntermediateCA()
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tempDir, "server-intermediate-ca.pem"), serverIntermediateCA.CertBytes(), 0o640)
err = os.WriteFile(filepath.Join(tempDir, "server-intermediate-ca.pem"), serverIntermediateCA.CertBytes(), 0o640)
Expect(err).NotTo(HaveOccurred())
serverIntermediateKeyPair, err := serverIntermediateCA.NewServerCertKeyPair("127.0.0.1")
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tempDir, "server-intermediate-cert.pem"), serverIntermediateKeyPair.Cert, 0o640)
err = os.WriteFile(filepath.Join(tempDir, "server-intermediate-cert.pem"), serverIntermediateKeyPair.Cert, 0o640)
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tempDir, "server-intermediate-key.pem"), serverIntermediateKeyPair.Key, 0o640)
err = os.WriteFile(filepath.Join(tempDir, "server-intermediate-key.pem"), serverIntermediateKeyPair.Key, 0o640)
Expect(err).NotTo(HaveOccurred())

serverAndIntermediateCABytes := append(serverCA.CertBytes(), serverIntermediateCA.CertBytes()...)
err = ioutil.WriteFile(filepath.Join(tempDir, "server-ca+intermediate-ca.pem"), serverAndIntermediateCABytes, 0o640)
err = os.WriteFile(filepath.Join(tempDir, "server-ca+intermediate-ca.pem"), serverAndIntermediateCABytes, 0o640)
Expect(err).NotTo(HaveOccurred())

clientCA, err := tlsgen.NewCA()
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tempDir, "client-ca.pem"), clientCA.CertBytes(), 0o640)
err = os.WriteFile(filepath.Join(tempDir, "client-ca.pem"), clientCA.CertBytes(), 0o640)
Expect(err).NotTo(HaveOccurred())
clientKeyPair, err := clientCA.NewClientCertKeyPair()
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tempDir, "client-cert.pem"), clientKeyPair.Cert, 0o640)
err = os.WriteFile(filepath.Join(tempDir, "client-cert.pem"), clientKeyPair.Cert, 0o640)
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tempDir, "client-key.pem"), clientKeyPair.Key, 0o640)
err = os.WriteFile(filepath.Join(tempDir, "client-key.pem"), clientKeyPair.Key, 0o640)
Expect(err).NotTo(HaveOccurred())
}

Expand Down Expand Up @@ -831,7 +831,7 @@ func createBlockFile(tempDir string, configBlock *cb.Block) string {
blockBytes, err := proto.Marshal(configBlock)
Expect(err).NotTo(HaveOccurred())
blockPath := filepath.Join(tempDir, "block.pb")
err = ioutil.WriteFile(blockPath, blockBytes, 0o644)
err = os.WriteFile(blockPath, blockBytes, 0o644)
Expect(err).NotTo(HaveOccurred())
return blockPath
}
13 changes: 7 additions & 6 deletions common/fabhttp/fabhttp_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"crypto/x509"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"testing"

Expand All @@ -27,24 +28,24 @@ func TestFabHTTP(t *testing.T) {
func generateCertificates(tempDir string) {
serverCA, err := tlsgen.NewCA()
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tempDir, "server-ca.pem"), serverCA.CertBytes(), 0o640)
err = os.WriteFile(filepath.Join(tempDir, "server-ca.pem"), serverCA.CertBytes(), 0o640)
Expect(err).NotTo(HaveOccurred())
serverKeyPair, err := serverCA.NewServerCertKeyPair("127.0.0.1")
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tempDir, "server-cert.pem"), serverKeyPair.Cert, 0o640)
err = os.WriteFile(filepath.Join(tempDir, "server-cert.pem"), serverKeyPair.Cert, 0o640)
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tempDir, "server-key.pem"), serverKeyPair.Key, 0o640)
err = os.WriteFile(filepath.Join(tempDir, "server-key.pem"), serverKeyPair.Key, 0o640)
Expect(err).NotTo(HaveOccurred())

clientCA, err := tlsgen.NewCA()
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tempDir, "client-ca.pem"), clientCA.CertBytes(), 0o640)
err = os.WriteFile(filepath.Join(tempDir, "client-ca.pem"), clientCA.CertBytes(), 0o640)
Expect(err).NotTo(HaveOccurred())
clientKeyPair, err := clientCA.NewClientCertKeyPair()
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tempDir, "client-cert.pem"), clientKeyPair.Cert, 0o640)
err = os.WriteFile(filepath.Join(tempDir, "client-cert.pem"), clientKeyPair.Cert, 0o640)
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tempDir, "client-key.pem"), clientKeyPair.Key, 0o640)
err = os.WriteFile(filepath.Join(tempDir, "client-key.pem"), clientKeyPair.Key, 0o640)
Expect(err).NotTo(HaveOccurred())
}

Expand Down
3 changes: 1 addition & 2 deletions common/ledger/blkstorage/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package blkstorage

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -425,7 +424,7 @@ func TestBootstrapFromSnapshotErrorPaths(t *testing.T) {
require.NoError(t, err)
env.provider.Close()
env = newTestEnv(t, NewConf(testPath, 0))
require.NoError(t, ioutil.WriteFile(bootstrappingSnapshotInfoFile, []byte("junk-data"), 0o644))
require.NoError(t, os.WriteFile(bootstrappingSnapshotInfoFile, []byte("junk-data"), 0o644))
_, err = env.provider.Open(ledgerID)
require.Contains(t, err.Error(), "error while unmarshalling bootstrappingSnapshotInfo")
})
Expand Down
8 changes: 4 additions & 4 deletions common/viperutil/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestStringFromFile(t *testing.T) {

expectedValue := "this is the text in the file"

err = ioutil.WriteFile(file.Name(), []byte(expectedValue), 0o644)
err = os.WriteFile(file.Name(), []byte(expectedValue), 0o644)
require.NoError(t, err, "uname to write temp file")

yaml := fmt.Sprintf("---\nInner:\n Single:\n File: %s", file.Name())
Expand All @@ -165,7 +165,7 @@ func TestPEMBlocksFromFile(t *testing.T) {
pems = append(pems, publicKeyCert...)
}

err = ioutil.WriteFile(file.Name(), pems, 0o644)
err = os.WriteFile(file.Name(), pems, 0o644)
require.NoError(t, err, "failed to write temp file")

yaml := fmt.Sprintf("---\nInner:\n Multiple:\n File: %s", file.Name())
Expand All @@ -191,7 +191,7 @@ func TestPEMBlocksFromFileEnv(t *testing.T) {
pems = append(pems, publicKeyCert...)
}

err = ioutil.WriteFile(file.Name(), pems, 0o644)
err = os.WriteFile(file.Name(), pems, 0o644)
require.NoError(t, err, "failed to write temp file")

envVar := testEnvPrefix + "_INNER_MULTIPLE_FILE"
Expand Down Expand Up @@ -240,7 +240,7 @@ func TestStringFromFileEnv(t *testing.T) {
require.NoError(t, err, "failed to create temp file")
defer os.Remove(file.Name())

err = ioutil.WriteFile(file.Name(), []byte(expectedValue), 0o644)
err = os.WriteFile(file.Name(), []byte(expectedValue), 0o644)
require.NoError(t, err, "failed to write temp file")

envVar := testEnvPrefix + "_INNER_SINGLE_FILE"
Expand Down
2 changes: 1 addition & 1 deletion core/chaincode/chaincode_support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ func getTarGZ(t *testing.T, name string, contents []byte) []byte {
tr.Write(contents)
tr.Close()
gw.Close()
ioutil.WriteFile("/tmp/t.gz", inputbuf.Bytes(), 0o644)
os.WriteFile("/tmp/t.gz", inputbuf.Bytes(), 0o644)
return inputbuf.Bytes()
}

Expand Down
8 changes: 4 additions & 4 deletions core/chaincode/persistence/persistence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var _ = Describe("Persistence", func() {

It("stats a file", func() {
path := filepath.Join(testDir, "stat")
err := ioutil.WriteFile(path, []byte("test"), 0o600)
err := os.WriteFile(path, []byte("test"), 0o600)
Expect(err).NotTo(HaveOccurred())

exists, err := filesystemIO.Exists(path)
Expand All @@ -74,7 +74,7 @@ var _ = Describe("Persistence", func() {

It("removes a file", func() {
path := filepath.Join(testDir, "remove")
err := ioutil.WriteFile(path, []byte("test"), 0o600)
err := os.WriteFile(path, []byte("test"), 0o600)
Expect(err).NotTo(HaveOccurred())

_, err = os.Stat(path)
Expand All @@ -89,7 +89,7 @@ var _ = Describe("Persistence", func() {

It("reads a file", func() {
path := filepath.Join(testDir, "readfile")
err := ioutil.WriteFile(path, []byte("test"), 0o600)
err := os.WriteFile(path, []byte("test"), 0o600)
Expect(err).NotTo(HaveOccurred())

_, err = os.Stat(path)
Expand All @@ -102,7 +102,7 @@ var _ = Describe("Persistence", func() {

It("reads a directory", func() {
path := filepath.Join(testDir, "readdir")
err := ioutil.WriteFile(path, []byte("test"), 0o600)
err := os.WriteFile(path, []byte("test"), 0o600)
Expect(err).NotTo(HaveOccurred())

_, err = os.Stat(path)
Expand Down
5 changes: 2 additions & 3 deletions core/chaincode/platforms/util/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand All @@ -34,7 +33,7 @@ func TestWriteFileToPackage(t *testing.T) {
filename := "test.txt"
filecontent := "hello"
filePath := filepath.Join(tempDir, filename)
err := ioutil.WriteFile(filePath, bytes.NewBufferString(filecontent).Bytes(), 0o600)
err := os.WriteFile(filePath, bytes.NewBufferString(filecontent).Bytes(), 0o600)
require.NoError(t, err, "Error creating file %s", filePath)

err = WriteFileToPackage(filePath, filename, tw)
Expand Down Expand Up @@ -214,7 +213,7 @@ func Test_WriteFolderToTarPackageFailure4(t *testing.T) {

tempDir := t.TempDir()
testFile := filepath.Join(tempDir, "test.java")
err := ioutil.WriteFile(testFile, []byte("Content"), 0o644)
err := os.WriteFile(testFile, []byte("Content"), 0o644)
require.NoError(t, err, "Error creating file", testFile)
err = os.Chmod(tempDir, 0o644)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion core/common/ccprovider/ccinfocache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func TestGetInstalledChaincodesErrorPaths(t *testing.T) {

// Set the above created directory as the chaincode install path
SetChaincodesPath(dir)
err := ioutil.WriteFile(filepath.Join(dir, "idontexist.1.0"), []byte("test"), 0o777)
err := os.WriteFile(filepath.Join(dir, "idontexist.1.0"), []byte("test"), 0o777)
require.NoError(t, err)
resp, err := GetInstalledChaincodes()
require.NoError(t, err)
Expand Down
9 changes: 4 additions & 5 deletions core/common/ccprovider/cdspackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ import (
"bytes"
"fmt"
"hash"
"io/ioutil"
"os"

"github.com/golang/protobuf/proto"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/bccsp"
)

//----- CDSData ------
// ----- CDSData ------

// CDSData is data stored in the LSCC on instantiation of a CC
// for CDSPackage. This needs to be serialized for ChaincodeData
Expand All @@ -41,7 +40,7 @@ type CDSData struct {
MetaDataHash []byte `protobuf:"bytes,2,opt,name=metadatahash,proto3"`
}

//----implement functions needed from proto.Message for proto's mar/unmarshal functions
// ----implement functions needed from proto.Message for proto's mar/unmarshal functions

// Reset resets
func (data *CDSData) Reset() { *data = CDSData{} }
Expand All @@ -62,7 +61,7 @@ type GetHasher interface {
GetHash(opts bccsp.HashOpts) (h hash.Hash, err error)
}

//--------- CDSPackage ------------
// --------- CDSPackage ------------

// CDSPackage encapsulates ChaincodeDeploymentSpec.
type CDSPackage struct {
Expand Down Expand Up @@ -283,7 +282,7 @@ func (ccpack *CDSPackage) PutChaincodeToFS() error {
return fmt.Errorf("chaincode %s exists", path)
}

if err := ioutil.WriteFile(path, ccpack.buf, 0o644); err != nil {
if err := os.WriteFile(path, ccpack.buf, 0o644); err != nil {
return err
}

Expand Down
9 changes: 4 additions & 5 deletions core/common/ccprovider/sigcdspackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package ccprovider
import (
"bytes"
"fmt"
"io/ioutil"
"os"

"github.com/golang/protobuf/proto"
Expand All @@ -30,7 +29,7 @@ import (
"github.com/hyperledger/fabric/core/common/ccpackage"
)

//----- SignedCDSData ------
// ----- SignedCDSData ------

// SignedCDSData is data stored in the LSCC on instantiation of a CC
// for SignedCDSPackage. This needs to be serialized for ChaincodeData
Expand All @@ -41,7 +40,7 @@ type SignedCDSData struct {
SignatureHash []byte `protobuf:"bytes,3,opt,name=signaturehash"`
}

//----implement functions needed from proto.Message for proto's mar/unmarshal functions
// ----implement functions needed from proto.Message for proto's mar/unmarshal functions

// Reset resets
func (data *SignedCDSData) Reset() { *data = SignedCDSData{} }
Expand All @@ -60,7 +59,7 @@ func (data *SignedCDSData) Equals(other *SignedCDSData) bool {
bytes.Equal(data.SignatureHash, other.SignatureHash)
}

//-------- SignedCDSPackage ---------
// -------- SignedCDSPackage ---------

// SignedCDSPackage encapsulates SignedChaincodeDeploymentSpec.
type SignedCDSPackage struct {
Expand Down Expand Up @@ -342,7 +341,7 @@ func (ccpack *SignedCDSPackage) PutChaincodeToFS() error {
return fmt.Errorf("chaincode %s exists", path)
}

if err := ioutil.WriteFile(path, ccpack.buf, 0o644); err != nil {
if err := os.WriteFile(path, ccpack.buf, 0o644); err != nil {
return err
}

Expand Down
Loading

0 comments on commit 4ca66d4

Please sign in to comment.