From 78ce86299fddfe0910a33a9c29a8b14eab0488db Mon Sep 17 00:00:00 2001 From: Baohua Yang Date: Wed, 3 May 2017 21:09:08 +0800 Subject: [PATCH] [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 --- core/common/ccprovider/ccprovider.go | 12 ++++++++++++ peer/chaincode/install.go | 5 ++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/core/common/ccprovider/ccprovider.go b/core/common/ccprovider/ccprovider.go index c6ee8040455..43a2a599517 100644 --- a/core/common/ccprovider/ccprovider.go +++ b/core/common/ccprovider/ccprovider.go @@ -21,6 +21,7 @@ import ( "fmt" "io/ioutil" "os" + "path/filepath" "strings" "github.com/golang/protobuf/proto" @@ -98,6 +99,17 @@ func GetChaincodePackage(ccname string, ccversion string) ([]byte, error) { return ccbytes, nil } +//ChaincodePackageExists returns whether the chaincode package exists in the file system +func ChaincodePackageExists(ccname string, ccversion string) (bool, error) { + path := filepath.Join(chaincodeInstallPath, ccname+"."+ccversion) + _, err := os.Stat(path) + if err == nil { + // chaincodepackage already exists + return true, nil + } + return false, err +} + // GetChaincodeFromFS this is a wrapper for hiding package implementation. func GetChaincodeFromFS(ccname string, ccversion string) (CCPackage, error) { //try raw CDS diff --git a/peer/chaincode/install.go b/peer/chaincode/install.go index 72fd8a489bd..64858bafb89 100644 --- a/peer/chaincode/install.go +++ b/peer/chaincode/install.go @@ -90,9 +90,8 @@ func install(msg proto.Message, cf *ChaincodeCmdFactory) error { //generateChaincode creates ChaincodeDeploymentSpec as the package to install func generateChaincode(cmd *cobra.Command, chaincodeName, chaincodeVersion string) (*pb.ChaincodeDeploymentSpec, error) { - tmppkg, _ := ccprovider.GetChaincodePackage(chaincodeName, chaincodeVersion) - if tmppkg != nil { - return nil, fmt.Errorf("chaincode %s:%s exists", chaincodeName, chaincodeVersion) + if existed, _ := ccprovider.ChaincodePackageExists(chaincodeName, chaincodeVersion); existed { + return nil, fmt.Errorf("chaincode %s:%s already exists", chaincodeName, chaincodeVersion) } spec, err := getChaincodeSpec(cmd)