-
Notifications
You must be signed in to change notification settings - Fork 8
/
copy.go
85 lines (69 loc) · 1.3 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
package main
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/reconquest/ser-go"
)
func copyFile(src string, dest string) error {
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
destFile, err := os.Create(dest)
if err != nil {
return err
}
defer destFile.Close()
_, err = io.Copy(destFile, srcFile)
if err != nil {
return err
}
stat, err := os.Stat(src)
if err != nil {
return err
}
err = os.Chmod(dest, stat.Mode())
if err != nil {
return ser.Errorf(
err, "can't change file mode: %s", dest,
)
}
return nil
}
func copyDir(src string, dest string) (err error) {
srcStat, err := os.Stat(src)
if err != nil {
return err
}
if !srcStat.IsDir() {
return ser.Errorf(
err, "%s is not directory", src,
)
}
err = os.MkdirAll(dest, srcStat.Mode())
if err != nil {
return ser.Errorf(
err, "can't mkdir %s", dest,
)
}
entries, err := ioutil.ReadDir(src)
for _, entry := range entries {
srcEntry := filepath.Join(src, entry.Name())
destEntry := filepath.Join(dest, entry.Name())
if entry.IsDir() {
err = copyDir(srcEntry, destEntry)
} else {
err = copyFile(srcEntry, destEntry)
}
if err != nil {
return ser.Errorf(
err,
"can't copy %s -> %s", srcEntry, destEntry,
)
}
}
return nil
}