6 Star 46 Fork 28

Hyperledger/fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
targzmetadataprovider.go 1.90 KB
一键复制 编辑 原始数据 按行查看 历史
/*
# Copyright State Street Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
*/
package ccprovider
import (
"archive/tar"
"bytes"
"compress/gzip"
"io"
"strings"
)
//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/"
)
type PersistenceAdapter func([]byte) ([]byte, error)
func (pa PersistenceAdapter) GetDBArtifacts(codePackage []byte) ([]byte, error) {
return pa(codePackage)
}
// MetadataAsTarEntries extracts metadata from a chaincode package
func MetadataAsTarEntries(code []byte) ([]byte, error) {
is := bytes.NewReader(code)
gr, err := gzip.NewReader(is)
if err != nil {
ccproviderLogger.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 {
ccproviderLogger.Error("Error adding header to statedb tar:", err, header.Name)
return nil, err
}
if _, err := io.Copy(tw, tr); err != nil {
ccproviderLogger.Error("Error copying file to statedb tar:", err, header.Name)
return nil, err
}
ccproviderLogger.Debug("Wrote file to statedb tar:", header.Name)
}
if err = tw.Close(); err != nil {
return nil, err
}
ccproviderLogger.Debug("Created metadata tar")
return statedbTarBuffer.Bytes(), nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hyperledger/fabric.git
git@gitee.com:hyperledger/fabric.git
hyperledger
fabric
fabric
v2.0.0

搜索帮助