forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.go
108 lines (86 loc) · 2.38 KB
/
copy.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
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
}