-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
109 lines (88 loc) · 1.92 KB
/
helper.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
109
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime/debug"
)
// Copy a directory tree from `src` to `dest`
func CopyTree(src string, dest string, verbose bool) error {
src = filepath.Clean(src)
dest = filepath.Clean(dest)
finalErr := filepath.Walk(src, func(path string, info os.FileInfo, walkErr error) error {
if walkErr != nil {
return walkErr
}
myDest, err := filepath.Rel(src, path)
if err != nil {
return err
}
myDest = filepath.Join(dest, myDest)
if info.IsDir() {
err = os.Mkdir(myDest, info.Mode())
if (err != nil) && (!os.IsExist(err)) {
return err
}
return nil
} else {
_, e := CopyFileLazy(path, myDest)
return e
}
})
return finalErr
}
func CopyFileLazy(src string, dest string) (int64, error) {
srcInfo, err := os.Stat(src)
if err != nil {
fmt.Println("Couldn't Read:" + src)
return 0, err
}
destInfo, err := os.Stat(dest)
if err != nil && destInfo != nil {
if (destInfo.ModTime() == srcInfo.ModTime()) && (destInfo.Size() == srcInfo.Size()) {
return destInfo.Size(), nil
}
}
source, err := os.Open(src)
if err != nil {
fmt.Println("Error Reading:" + src)
return 0, err
}
defer source.Close()
destination, err := os.Create(dest)
if err != nil {
fmt.Println("Error Writing:" + src)
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
if err != nil {
fmt.Println("Error Copying:" + src + ">" + dest)
return 0, err
}
if nBytes != srcInfo.Size() {
return 0, fmt.Errorf("failed to copy %d != %d", nBytes, srcInfo.Size())
}
return nBytes, err
}
func CheckErr(err error) {
if err != nil {
log.Fatalf(`
---- STACK -------------
%s
---- END -------------
%s`, debug.Stack(), err.Error())
}
}
func CheckErrContext(err error, context ...string) {
if err != nil {
log.Fatalf(`
---- STACK -------------
%s
---- END -------------
%s
%s`, debug.Stack(), err.Error(), context)
}
}