-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.go
56 lines (47 loc) · 954 Bytes
/
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
package main
import (
"io"
"os"
fp "path/filepath"
)
func copyDir(src, dest string) error {
err := os.MkdirAll(dest, os.ModePerm)
if err != nil {
return err
}
return fp.Walk(
src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
destinationPath := fp.Join(dest, path[len(src):])
if info.IsDir() {
return os.MkdirAll(destinationPath, os.ModePerm)
} else {
return copyFile(path, destinationPath)
}
},
)
}
func copyFile(src, dest string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()
destDir, _ := fp.Split(dest)
_, err = os.Stat(destDir)
if os.IsNotExist(err) {
err := os.MkdirAll(destDir, os.ModePerm)
if err != nil {
return err
}
}
destinationFile, err := safeCreate(dest)
if err != nil {
return err
}
defer destinationFile.Close()
_, err = io.Copy(destinationFile, sourceFile)
return err
}