forked from ThienLK1/fabric-v2.2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
targzmetadataprovider.go
92 lines (76 loc) · 2.15 KB
/
targzmetadataprovider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
# Copyright State Street Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
*/
package ccmetadata
import (
"archive/tar"
"bytes"
"compress/gzip"
"io"
"strings"
"github.com/pkg/errors"
)
//The targz metadata provider is reference for other providers (such as what CAR would
//implement). Currently it treats only statedb metadata but will be generalized in future
//to allow for arbitrary metadata to be packaged with the chaincode.
const (
ccPackageStatedbDir = "META-INF/statedb/"
)
//TargzMetadataProvider provides Metadata from chaincode packaged in Targz format
//(go, java and node platforms)
type TargzMetadataProvider struct {
Code []byte
}
func (tgzProv *TargzMetadataProvider) getCode() ([]byte, error) {
if tgzProv.Code == nil {
return nil, errors.New("nil code package")
}
return tgzProv.Code, nil
}
// GetMetadataAsTarEntries extracts metata data from ChaincodeDeploymentSpec
func (tgzProv *TargzMetadataProvider) GetMetadataAsTarEntries() ([]byte, error) {
code, err := tgzProv.getCode()
if err != nil {
return nil, err
}
is := bytes.NewReader(code)
gr, err := gzip.NewReader(is)
if err != nil {
logger.Errorf("Failure opening codepackage gzip stream: %s", err)
return nil, err
}
statedbTarBuffer := bytes.NewBuffer(nil)
tw := tar.NewWriter(statedbTarBuffer)
tr := tar.NewReader(gr)
// For each file in the code package tar,
// add it to the statedb artifact tar if it has "statedb" in the path
for {
header, err := tr.Next()
if err == io.EOF {
// We only get here if there are no more entries to scan
break
}
if err != nil {
return nil, err
}
if !strings.HasPrefix(header.Name, ccPackageStatedbDir) {
continue
}
if err = tw.WriteHeader(header); err != nil {
logger.Error("Error adding header to statedb tar:", err, header.Name)
return nil, err
}
if _, err := io.Copy(tw, tr); err != nil {
logger.Error("Error copying file to statedb tar:", err, header.Name)
return nil, err
}
logger.Debug("Wrote file to statedb tar:", header.Name)
}
if err = tw.Close(); err != nil {
return nil, err
}
logger.Debug("Created metadata tar")
return statedbTarBuffer.Bytes(), nil
}