3 Star 6 Fork 7

Gitee 极速下载/Hyperledger fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/hyperledger/fabric
克隆/下载
copy.go 2.38 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package externalbuilder
import (
"io"
"os"
"path/filepath"
"github.com/hyperledger/fabric/common/flogging"
"github.com/pkg/errors"
)
// MoveOrCopyDir attempts to copy src to dest by firstly trying to move, then copy upon failure.
func MoveOrCopyDir(logger *flogging.FabricLogger, srcroot, destroot string) error {
mvErr := os.Rename(srcroot, destroot)
if mvErr == nil {
return nil
}
logger.Debugf("Failed to move %s to %s: %s, try copy instead", srcroot, destroot, mvErr)
info, err := os.Stat(srcroot)
if err != nil {
return errors.WithMessagef(err, "failed to stat dir: %s", srcroot)
}
if err = os.MkdirAll(destroot, info.Mode()); err != nil {
return errors.WithMessagef(err, "failed to make dir: %s", destroot)
}
cpErr := CopyDir(srcroot, destroot)
if cpErr == nil {
return nil
}
logger.Errorf("Failed to copy %s to %s: %s", srcroot, destroot, cpErr)
rmErr := os.RemoveAll(destroot)
if rmErr != nil {
logger.Errorf("Failed to clean targeting dir %s: %s", destroot, rmErr)
}
return errors.WithMessagef(cpErr, "failed to copy %s to %s", srcroot, destroot)
}
// CopyDir creates a copy of a dir
func CopyDir(srcroot, destroot string) error {
return filepath.Walk(srcroot, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
srcsubpath, err := filepath.Rel(srcroot, path)
if err != nil {
return err
}
destpath := filepath.Join(destroot, srcsubpath)
if info.IsDir() { // its a dir, make corresponding dir in the dest
if err = os.MkdirAll(destpath, info.Mode()); err != nil {
return err
}
return nil
}
// its a file, copy to corresponding path in the dest.
// Intermediate directories are ensured to exist because parent
// node is always visited before children in `filepath.Walk`.
if err = copyFile(path, destpath); err != nil {
return err
}
return nil
})
}
func copyFile(srcpath, destpath string) error {
srcFile, err := os.Open(srcpath)
if err != nil {
return err
}
defer srcFile.Close()
info, err := srcFile.Stat()
if err != nil {
return err
}
destFile, err := os.Create(destpath)
if err != nil {
return err
}
defer destFile.Close()
if err = os.Chmod(destFile.Name(), info.Mode()); err != nil {
return err
}
if _, err = io.Copy(destFile, srcFile); err != nil {
return err
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/fabric.git
git@gitee.com:mirrors/fabric.git
mirrors
fabric
Hyperledger fabric
v2.0.1

搜索帮助

0d507c66 1850385 C8b1a773 1850385