-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.go
58 lines (55 loc) · 1.03 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
package main
import (
"io"
"io/ioutil"
"os"
"path/filepath"
)
func CopyFile(src, dst string, mode os.FileMode) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
if _, err := io.Copy(out, in); err != nil {
return err
}
if err := out.Sync(); err != nil {
return err
}
if err := out.Chmod(mode); err != nil {
return err
}
return nil
}
func CopyDir(src string, dst string) error {
if err := os.MkdirAll(dst, os.ModePerm); err != nil {
return err
}
entries, err := ioutil.ReadDir(src)
if err != nil {
return err
}
for _, entry := range entries {
srcPath := filepath.Join(src, entry.Name())
dstPath := filepath.Join(dst, entry.Name())
if entry.IsDir() {
if err := CopyDir(srcPath, dstPath); err != nil {
return err
}
} else {
if entry.Mode()&os.ModeSymlink != 0 {
continue
}
if err := CopyFile(srcPath, dstPath, entry.Mode()); err != nil {
return err
}
}
}
return nil
}