Fetch the repository succeeded.
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
/*
Notice: This file has been modified for Hyperledger Fabric SDK Go usage.
Please review third_party pinning scripts and patches for more details.
*/
package util
import (
"archive/tar"
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"time"
"gitee.com/bupt-zkjc/fabric-sdk-go/internal/github.com/hyperledger/fabric/sdkinternal/ccmetadata"
flogging "gitee.com/bupt-zkjc/fabric-sdk-go/internal/github.com/hyperledger/fabric/sdkpatch/logbridge"
"github.com/pkg/errors"
)
var logger = flogging.MustGetLogger("chaincode.platform.util")
// WriteFolderToTarPackage writes source files to a tarball.
// This utility is used for node js chaincode packaging, but not golang chaincode.
// Golang chaincode has more sophisticated file packaging, as implemented in golang/platform.go.
func WriteFolderToTarPackage(tw *tar.Writer, srcPath string, excludeDirs []string, includeFileTypeMap map[string]bool, excludeFileTypeMap map[string]bool) error {
rootDirectory := filepath.Clean(srcPath)
logger.Debug("writing folder to package", "rootDirectory", rootDirectory)
var success bool
walkFn := func(localpath string, info os.FileInfo, err error) error {
if err != nil {
logger.Errorf("Visit %s failed: %s", localpath, err)
return err
}
if info.Mode().IsDir() {
for _, excluded := range append(excludeDirs, ".git") {
if info.Name() == excluded {
return filepath.SkipDir
}
}
return nil
}
ext := filepath.Ext(localpath)
if _, ok := includeFileTypeMap[ext]; includeFileTypeMap != nil && !ok {
return nil
}
if excludeFileTypeMap[ext] {
return nil
}
relpath, err := filepath.Rel(rootDirectory, localpath)
if err != nil {
return err
}
packagepath := filepath.ToSlash(relpath)
// if file is metadata, keep the /META-INF directory, e.g: META-INF/statedb/couchdb/indexes/indexOwner.json
// otherwise file is source code, put it in /src dir, e.g: src/marbles_chaincode.js
if strings.HasPrefix(localpath, filepath.Join(rootDirectory, "META-INF")) {
// Hidden files are not supported as metadata, therefore ignore them.
// User often doesn't know that hidden files are there, and may not be able to delete them, therefore warn user rather than error out.
if strings.HasPrefix(info.Name(), ".") {
logger.Warningf("Ignoring hidden file in metadata directory: %s", packagepath)
return nil
}
fileBytes, err := ioutil.ReadFile(localpath)
if err != nil {
return err
}
// Validate metadata file for inclusion in tar
// Validation is based on the fully qualified path of the file
err = ccmetadata.ValidateMetadataFile(packagepath, fileBytes)
if err != nil {
return err
}
} else { // file is not metadata, include in src
packagepath = path.Join("src", packagepath)
}
err = WriteFileToPackage(localpath, packagepath, tw)
if err != nil {
return fmt.Errorf("Error writing file to package: %s", err)
}
success = true
return nil
}
if err := filepath.Walk(rootDirectory, walkFn); err != nil {
logger.Infof("Error walking rootDirectory: %s", err)
return err
}
if !success {
return errors.Errorf("no source files found in '%s'", srcPath)
}
return nil
}
// WriteFileToPackage writes a file to a tar stream.
func WriteFileToPackage(localpath string, packagepath string, tw *tar.Writer) error {
logger.Debug("Writing file to tarball:", packagepath)
fd, err := os.Open(localpath)
if err != nil {
return fmt.Errorf("%s: %s", localpath, err)
}
defer fd.Close()
fi, err := fd.Stat()
if err != nil {
return fmt.Errorf("%s: %s", localpath, err)
}
header, err := tar.FileInfoHeader(fi, localpath)
if err != nil {
return fmt.Errorf("failed calculating FileInfoHeader: %s", err)
}
// Take the variance out of the tar by using zero time and fixed uid/gid.
var zeroTime time.Time
header.AccessTime = zeroTime
header.ModTime = zeroTime
header.ChangeTime = zeroTime
header.Name = packagepath
header.Mode = 0100644
header.Uid = 500
header.Gid = 500
header.Uname = ""
header.Gname = ""
err = tw.WriteHeader(header)
if err != nil {
return fmt.Errorf("failed to write header for %s: %s", localpath, err)
}
is := bufio.NewReader(fd)
_, err = io.Copy(tw, is)
if err != nil {
return fmt.Errorf("failed to write %s as %s: %s", localpath, packagepath, err)
}
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。