-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir.go
47 lines (41 loc) · 815 Bytes
/
dir.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
package dir
import (
"io"
"os"
"path/filepath"
"strings"
)
func Cp(src, dst string) error {
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
dstPath := filepath.Join(dst, strings.TrimPrefix(path, src))
if info.IsDir() {
err := os.MkdirAll(dstPath, info.Mode())
if err != nil {
return err
}
} else {
_, err := cpFile(path, dstPath) // return the written bytes?
if err != nil {
return err
}
}
return nil
})
}
func cpFile(src, dst string) (int64, error) {
srcFile, err := os.Open(src)
if err != nil {
return 0, err
}
defer srcFile.Close()
dstFile, err := os.Create(dst)
if err != nil {
return 0, err
}
defer dstFile.Close()
written, err := io.Copy(dstFile, srcFile)
return written, err
}