Skip to content

Commit 02b7802

Browse files
committed
[FAB-1540]Extract HashFilesInDir and IsCodeExist
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 <[email protected]>
1 parent f0c43f7 commit 02b7802

File tree

18 files changed

+151
-311
lines changed

18 files changed

+151
-311
lines changed

core/chaincode/platforms/golang/hash.go

+3-78
Original file line numberDiff line numberDiff line change
@@ -33,88 +33,13 @@ import (
3333
"github.com/op/go-logging"
3434
"github.com/spf13/viper"
3535

36-
cutil "github.com/hyperledger/fabric/core/container/util"
36+
ccutil "github.com/hyperledger/fabric/core/chaincode/platforms/util"
3737
"github.com/hyperledger/fabric/core/util"
3838
pb "github.com/hyperledger/fabric/protos/peer"
3939
)
4040

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

43-
//core hash computation factored out for testing
44-
func computeHash(contents []byte, hash []byte) []byte {
45-
newSlice := make([]byte, len(hash)+len(contents))
46-
47-
//copy the contents
48-
copy(newSlice[0:len(contents)], contents[:])
49-
50-
//add the previous hash
51-
copy(newSlice[len(contents):], hash[:])
52-
53-
//compute new hash
54-
hash = util.ComputeCryptoHash(newSlice)
55-
56-
return hash
57-
}
58-
59-
//hashFilesInDir computes h=hash(h,file bytes) for each file in a directory
60-
//Directory entries are traversed recursively. In the end a single
61-
//hash value is returned for the entire directory structure
62-
func hashFilesInDir(rootDir string, dir string, hash []byte, tw *tar.Writer) ([]byte, error) {
63-
currentDir := filepath.Join(rootDir, dir)
64-
logger.Debugf("hashFiles %s", currentDir)
65-
//ReadDir returns sorted list of files in dir
66-
fis, err := ioutil.ReadDir(currentDir)
67-
if err != nil {
68-
return hash, fmt.Errorf("ReadDir failed %s\n", err)
69-
}
70-
for _, fi := range fis {
71-
name := filepath.Join(dir, fi.Name())
72-
if fi.IsDir() {
73-
var err error
74-
hash, err = hashFilesInDir(rootDir, name, hash, tw)
75-
if err != nil {
76-
return hash, err
77-
}
78-
continue
79-
}
80-
fqp := filepath.Join(rootDir, name)
81-
buf, err := ioutil.ReadFile(fqp)
82-
if err != nil {
83-
fmt.Printf("Error reading %s\n", err)
84-
return hash, err
85-
}
86-
87-
//get the new hash from file contents
88-
hash = computeHash(buf, hash)
89-
90-
if tw != nil {
91-
is := bytes.NewReader(buf)
92-
if err = cutil.WriteStreamToPackage(is, fqp, filepath.Join("src", name), tw); err != nil {
93-
return hash, fmt.Errorf("Error adding file to tar %s", err)
94-
}
95-
}
96-
}
97-
return hash, nil
98-
}
99-
100-
func isCodeExist(tmppath string) error {
101-
file, err := os.Open(tmppath)
102-
if err != nil {
103-
return fmt.Errorf("Download failed %s", err)
104-
}
105-
106-
fi, err := file.Stat()
107-
if err != nil {
108-
return fmt.Errorf("Could not stat file %s", err)
109-
}
110-
111-
if !fi.IsDir() {
112-
return fmt.Errorf("File %s is not dir\n", file.Name())
113-
}
114-
115-
return nil
116-
}
117-
11843
func getCodeFromHTTP(path string) (codegopath string, err error) {
11944
codegopath = ""
12045
err = nil
@@ -265,7 +190,7 @@ func collectChaincodeFiles(spec *pb.ChaincodeSpec, tw *tar.Writer) (string, erro
265190
}
266191

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

277-
hash, err = hashFilesInDir(filepath.Join(codegopath, "src"), actualcodepath, hash, tw)
202+
hash, err = ccutil.HashFilesInDir(filepath.Join(codegopath, "src"), actualcodepath, hash, tw)
278203
if err != nil {
279204
return "", fmt.Errorf("Could not get hashcode for %s - %s\n", path, err)
280205
}

core/chaincode/platforms/java/hash.go

+3-63
Original file line numberDiff line numberDiff line change
@@ -27,66 +27,11 @@ import (
2727
"strings"
2828

2929
"github.com/golang/protobuf/proto"
30-
cutil "github.com/hyperledger/fabric/core/container/util"
30+
ccutil "github.com/hyperledger/fabric/core/chaincode/platforms/util"
3131
"github.com/hyperledger/fabric/core/util"
3232
pb "github.com/hyperledger/fabric/protos/peer"
3333
)
3434

35-
//hashFilesInDir computes h=hash(h,file bytes) for each file in a directory
36-
//Directory entries are traversed recursively. In the end a single
37-
//hash value is returned for the entire directory structure
38-
func hashFilesInDir(cutoff string, dir string, hash []byte, tw *tar.Writer) ([]byte, error) {
39-
//ReadDir returns sorted list of files in dir
40-
fis, err := ioutil.ReadDir(dir)
41-
if err != nil {
42-
return hash, fmt.Errorf("ReadDir failed %s\n", err)
43-
}
44-
for _, fi := range fis {
45-
name := fmt.Sprintf("%s/%s", dir, fi.Name())
46-
if fi.IsDir() {
47-
var err error
48-
hash, err = hashFilesInDir(cutoff, name, hash, tw)
49-
if err != nil {
50-
return hash, err
51-
}
52-
continue
53-
}
54-
buf, err := ioutil.ReadFile(name)
55-
if err != nil {
56-
fmt.Printf("Error reading %s\n", err)
57-
return hash, err
58-
}
59-
60-
newSlice := make([]byte, len(hash)+len(buf))
61-
copy(newSlice[len(buf):], hash[:])
62-
//hash = md5.Sum(newSlice)
63-
hash = util.ComputeCryptoHash(newSlice)
64-
65-
if tw != nil {
66-
is := bytes.NewReader(buf)
67-
if err = cutil.WriteStreamToPackage(is, name, name[len(cutoff):], tw); err != nil {
68-
return hash, fmt.Errorf("Error adding file to tar %s", err)
69-
}
70-
}
71-
}
72-
return hash, nil
73-
}
74-
75-
func isCodeExist(tmppath string) error {
76-
file, err := os.Open(tmppath)
77-
if err != nil {
78-
return fmt.Errorf("Download failer %s", err)
79-
}
80-
fi, err := file.Stat()
81-
if err != nil {
82-
return fmt.Errorf("could not stat file %s", err)
83-
}
84-
if !fi.IsDir() {
85-
return fmt.Errorf("file %s is not dir\n", file.Name())
86-
}
87-
return nil
88-
}
89-
9035
func getCodeFromHTTP(path string) (codegopath string, err error) {
9136

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

156-
if err = isCodeExist(codepath); err != nil {
101+
if err = ccutil.IsCodeExist(codepath); err != nil {
157102
return "", fmt.Errorf("code does not exist %s", err)
158103
}
159104

160-
root := codepath
161-
if strings.LastIndex(root, "/") == len(root)-1 {
162-
root = root[:len(root)-1]
163-
}
164-
root = root[:strings.LastIndex(root, "/")+1]
165105
ctorbytes, err := proto.Marshal(ctor)
166106
if err != nil {
167107
return "", fmt.Errorf("Error marshalling constructor: %s", err)
168108
}
169109
hash := util.GenerateHashFromSignature(codepath, ctorbytes)
170110

171-
hash, err = hashFilesInDir(root, codepath, hash, tw)
111+
hash, err = ccutil.HashFilesInDir("", codepath, hash, tw)
172112
if err != nil {
173113
return "", fmt.Errorf("Could not get hashcode for %s - %s\n", codepath, err)
174114
}

core/chaincode/platforms/java/hash_test.go

-124
This file was deleted.

core/chaincode/platforms/java/platform.go

-13
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"archive/tar"
2121
"fmt"
2222
"net/url"
23-
"os"
2423

2524
pb "github.com/hyperledger/fabric/protos/peer"
2625
// "path/filepath"
@@ -30,18 +29,6 @@ import (
3029
type Platform struct {
3130
}
3231

33-
// Returns whether the given file or directory exists or not
34-
func pathExists(path string) (bool, error) {
35-
_, err := os.Stat(path)
36-
if err == nil {
37-
return true, nil
38-
}
39-
if os.IsNotExist(err) {
40-
return false, nil
41-
}
42-
return true, err
43-
}
44-
4532
//ValidateSpec validates the java chaincode specs
4633
func (javaPlatform *Platform) ValidateSpec(spec *pb.ChaincodeSpec) error {
4734
url, err := url.Parse(spec.ChaincodeID.Path)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
z
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
z1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
y
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
y1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
y2

0 commit comments

Comments
 (0)