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
|
package planr
import (
"fmt"
"os"
"path/filepath"
"io"
)
func Exists(filePath string) bool {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return false
}
return true
}
func CreateIfNotExists(dir string, perm os.FileMode) error {
if Exists(dir) {
return nil
}
if err := os.MkdirAll(dir, perm); err != nil {
return fmt.Errorf("failed to create directory: '%s', error: '%s'", dir, err.Error())
}
return nil
}
func CopySymlink(src, dst string, perm os.FileMode) error {
link, err := os.Readlink(src)
if err != nil {
return err
}
if err := os.Chmod(src, perm); err != nil {
return err
}
return os.Symlink(link, dst)
}
func Copy(srcFile, dstFile string, perm os.FileMode) error {
src, err := os.Open(srcFile)
if err != nil {
return err
}
defer src.Close()
dst, err := os.OpenFile(dstFile, os.O_CREATE | os.O_WRONLY | os.O_TRUNC, perm)
if err != nil {
return err
}
defer dst.Close()
_, err = io.Copy(dst, src)
if err != nil {
return err
}
return nil
}
func RecursiveCopyDir(source, destination string) error {
err := filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
relPath, err := filepath.Rel(source, path)
if err != nil {
return err
}
dst := filepath.Join(destination, relPath)
src := filepath.Join(source, relPath)
mode := info.Mode()
if relPath == "" {
return nil
}
switch info.Mode() & os.ModeType {
case os.ModeDir:
return CreateIfNotExists(dst, mode)
case os.ModeSymlink:
return CopySymlink(src, dst, mode)
default:
return Copy(src, dst, mode)
}
});
return err
}
|