Skip to content

Commit

Permalink
[FAB-1540]Extract HashFilesInDir and IsCodeExist
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-1540

This commit extract several util functions and
Fix a bug in previous java hashfilesindir function
which actually doesn't fill the file content.

Change-Id: I2e7cba473bb1ed43fc03df93f94f14a46b487d09
Signed-off-by: grapebaba <281165273@qq.com>
  • Loading branch information
GrapeBaBa committed Jan 9, 2017
1 parent f0c43f7 commit 02b7802
Show file tree
Hide file tree
Showing 18 changed files with 151 additions and 311 deletions.
81 changes: 3 additions & 78 deletions core/chaincode/platforms/golang/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,88 +33,13 @@ import (
"github.com/op/go-logging"
"github.com/spf13/viper"

cutil "github.com/hyperledger/fabric/core/container/util"
ccutil "github.com/hyperledger/fabric/core/chaincode/platforms/util"
"github.com/hyperledger/fabric/core/util"
pb "github.com/hyperledger/fabric/protos/peer"
)

var logger = logging.MustGetLogger("golang/hash")

//core hash computation factored out for testing
func computeHash(contents []byte, hash []byte) []byte {
newSlice := make([]byte, len(hash)+len(contents))

//copy the contents
copy(newSlice[0:len(contents)], contents[:])

//add the previous hash
copy(newSlice[len(contents):], hash[:])

//compute new hash
hash = util.ComputeCryptoHash(newSlice)

return hash
}

//hashFilesInDir computes h=hash(h,file bytes) for each file in a directory
//Directory entries are traversed recursively. In the end a single
//hash value is returned for the entire directory structure
func hashFilesInDir(rootDir string, dir string, hash []byte, tw *tar.Writer) ([]byte, error) {
currentDir := filepath.Join(rootDir, dir)
logger.Debugf("hashFiles %s", currentDir)
//ReadDir returns sorted list of files in dir
fis, err := ioutil.ReadDir(currentDir)
if err != nil {
return hash, fmt.Errorf("ReadDir failed %s\n", err)
}
for _, fi := range fis {
name := filepath.Join(dir, fi.Name())
if fi.IsDir() {
var err error
hash, err = hashFilesInDir(rootDir, name, hash, tw)
if err != nil {
return hash, err
}
continue
}
fqp := filepath.Join(rootDir, name)
buf, err := ioutil.ReadFile(fqp)
if err != nil {
fmt.Printf("Error reading %s\n", err)
return hash, err
}

//get the new hash from file contents
hash = computeHash(buf, hash)

if tw != nil {
is := bytes.NewReader(buf)
if err = cutil.WriteStreamToPackage(is, fqp, filepath.Join("src", name), tw); err != nil {
return hash, fmt.Errorf("Error adding file to tar %s", err)
}
}
}
return hash, nil
}

func isCodeExist(tmppath string) error {
file, err := os.Open(tmppath)
if err != nil {
return fmt.Errorf("Download failed %s", err)
}

fi, err := file.Stat()
if err != nil {
return fmt.Errorf("Could not stat file %s", err)
}

if !fi.IsDir() {
return fmt.Errorf("File %s is not dir\n", file.Name())
}

return nil
}

func getCodeFromHTTP(path string) (codegopath string, err error) {
codegopath = ""
err = nil
Expand Down Expand Up @@ -265,7 +190,7 @@ func collectChaincodeFiles(spec *pb.ChaincodeSpec, tw *tar.Writer) (string, erro
}

tmppath := filepath.Join(codegopath, "src", actualcodepath)
if err = isCodeExist(tmppath); err != nil {
if err = ccutil.IsCodeExist(tmppath); err != nil {
return "", fmt.Errorf("code does not exist %s", err)
}
ctorbytes, err := proto.Marshal(ctor)
Expand All @@ -274,7 +199,7 @@ func collectChaincodeFiles(spec *pb.ChaincodeSpec, tw *tar.Writer) (string, erro
}
hash := util.GenerateHashFromSignature(actualcodepath, ctorbytes)

hash, err = hashFilesInDir(filepath.Join(codegopath, "src"), actualcodepath, hash, tw)
hash, err = ccutil.HashFilesInDir(filepath.Join(codegopath, "src"), actualcodepath, hash, tw)
if err != nil {
return "", fmt.Errorf("Could not get hashcode for %s - %s\n", path, err)
}
Expand Down
66 changes: 3 additions & 63 deletions core/chaincode/platforms/java/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,66 +27,11 @@ import (
"strings"

"github.com/golang/protobuf/proto"
cutil "github.com/hyperledger/fabric/core/container/util"
ccutil "github.com/hyperledger/fabric/core/chaincode/platforms/util"
"github.com/hyperledger/fabric/core/util"
pb "github.com/hyperledger/fabric/protos/peer"
)

//hashFilesInDir computes h=hash(h,file bytes) for each file in a directory
//Directory entries are traversed recursively. In the end a single
//hash value is returned for the entire directory structure
func hashFilesInDir(cutoff string, dir string, hash []byte, tw *tar.Writer) ([]byte, error) {
//ReadDir returns sorted list of files in dir
fis, err := ioutil.ReadDir(dir)
if err != nil {
return hash, fmt.Errorf("ReadDir failed %s\n", err)
}
for _, fi := range fis {
name := fmt.Sprintf("%s/%s", dir, fi.Name())
if fi.IsDir() {
var err error
hash, err = hashFilesInDir(cutoff, name, hash, tw)
if err != nil {
return hash, err
}
continue
}
buf, err := ioutil.ReadFile(name)
if err != nil {
fmt.Printf("Error reading %s\n", err)
return hash, err
}

newSlice := make([]byte, len(hash)+len(buf))
copy(newSlice[len(buf):], hash[:])
//hash = md5.Sum(newSlice)
hash = util.ComputeCryptoHash(newSlice)

if tw != nil {
is := bytes.NewReader(buf)
if err = cutil.WriteStreamToPackage(is, name, name[len(cutoff):], tw); err != nil {
return hash, fmt.Errorf("Error adding file to tar %s", err)
}
}
}
return hash, nil
}

func isCodeExist(tmppath string) error {
file, err := os.Open(tmppath)
if err != nil {
return fmt.Errorf("Download failer %s", err)
}
fi, err := file.Stat()
if err != nil {
return fmt.Errorf("could not stat file %s", err)
}
if !fi.IsDir() {
return fmt.Errorf("file %s is not dir\n", file.Name())
}
return nil
}

func getCodeFromHTTP(path string) (codegopath string, err error) {

var tmp string
Expand Down Expand Up @@ -153,22 +98,17 @@ func collectChaincodeFiles(spec *pb.ChaincodeSpec, tw *tar.Writer) (string, erro
return "", fmt.Errorf("Error getting code %s", err)
}

if err = isCodeExist(codepath); err != nil {
if err = ccutil.IsCodeExist(codepath); err != nil {
return "", fmt.Errorf("code does not exist %s", err)
}

root := codepath
if strings.LastIndex(root, "/") == len(root)-1 {
root = root[:len(root)-1]
}
root = root[:strings.LastIndex(root, "/")+1]
ctorbytes, err := proto.Marshal(ctor)
if err != nil {
return "", fmt.Errorf("Error marshalling constructor: %s", err)
}
hash := util.GenerateHashFromSignature(codepath, ctorbytes)

hash, err = hashFilesInDir(root, codepath, hash, tw)
hash, err = ccutil.HashFilesInDir("", codepath, hash, tw)
if err != nil {
return "", fmt.Errorf("Could not get hashcode for %s - %s\n", codepath, err)
}
Expand Down
124 changes: 0 additions & 124 deletions core/chaincode/platforms/java/hash_test.go

This file was deleted.

13 changes: 0 additions & 13 deletions core/chaincode/platforms/java/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"archive/tar"
"fmt"
"net/url"
"os"

pb "github.com/hyperledger/fabric/protos/peer"
// "path/filepath"
Expand All @@ -30,18 +29,6 @@ import (
type Platform struct {
}

// Returns whether the given file or directory exists or not
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}

//ValidateSpec validates the java chaincode specs
func (javaPlatform *Platform) ValidateSpec(spec *pb.ChaincodeSpec) error {
url, err := url.Parse(spec.ChaincodeID.Path)
Expand Down
1 change: 1 addition & 0 deletions core/chaincode/platforms/util/hashtestfiles2/x.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x
1 change: 1 addition & 0 deletions core/chaincode/platforms/util/hashtestfiles2/x/z.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
z
1 change: 1 addition & 0 deletions core/chaincode/platforms/util/hashtestfiles2/x/z/z1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
z1
1 change: 1 addition & 0 deletions core/chaincode/platforms/util/hashtestfiles2/y.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
y
1 change: 1 addition & 0 deletions core/chaincode/platforms/util/hashtestfiles2/y/y1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
y1
1 change: 1 addition & 0 deletions core/chaincode/platforms/util/hashtestfiles2/y/y2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
y2
Loading

0 comments on commit 02b7802

Please sign in to comment.