Skip to content

Commit 78ce862

Browse files
committed
[FAB-3617] Add ChaincodePackageExists function
Currently, we detect a chaincodepackage's existense by trying reading its content. This is heavy when the package is large (very often). This patchset add a ChaincodePackageExists function, to saves the effort by detecting the existence using os.stat(). Change-Id: I99b16eeaee2bb6e80087e08f3ad136d01c044857 Signed-off-by: Baohua Yang <[email protected]>
1 parent f533ae6 commit 78ce862

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

core/common/ccprovider/ccprovider.go

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"io/ioutil"
2323
"os"
24+
"path/filepath"
2425
"strings"
2526

2627
"github.com/golang/protobuf/proto"
@@ -98,6 +99,17 @@ func GetChaincodePackage(ccname string, ccversion string) ([]byte, error) {
9899
return ccbytes, nil
99100
}
100101

102+
//ChaincodePackageExists returns whether the chaincode package exists in the file system
103+
func ChaincodePackageExists(ccname string, ccversion string) (bool, error) {
104+
path := filepath.Join(chaincodeInstallPath, ccname+"."+ccversion)
105+
_, err := os.Stat(path)
106+
if err == nil {
107+
// chaincodepackage already exists
108+
return true, nil
109+
}
110+
return false, err
111+
}
112+
101113
// GetChaincodeFromFS this is a wrapper for hiding package implementation.
102114
func GetChaincodeFromFS(ccname string, ccversion string) (CCPackage, error) {
103115
//try raw CDS

peer/chaincode/install.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@ func install(msg proto.Message, cf *ChaincodeCmdFactory) error {
9090

9191
//generateChaincode creates ChaincodeDeploymentSpec as the package to install
9292
func generateChaincode(cmd *cobra.Command, chaincodeName, chaincodeVersion string) (*pb.ChaincodeDeploymentSpec, error) {
93-
tmppkg, _ := ccprovider.GetChaincodePackage(chaincodeName, chaincodeVersion)
94-
if tmppkg != nil {
95-
return nil, fmt.Errorf("chaincode %s:%s exists", chaincodeName, chaincodeVersion)
93+
if existed, _ := ccprovider.ChaincodePackageExists(chaincodeName, chaincodeVersion); existed {
94+
return nil, fmt.Errorf("chaincode %s:%s already exists", chaincodeName, chaincodeVersion)
9695
}
9796

9897
spec, err := getChaincodeSpec(cmd)

0 commit comments

Comments
 (0)