-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
139 lines (121 loc) · 2.78 KB
/
util.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package dojoBuilder
import (
"fmt"
"io"
"os"
"regexp"
)
func IsMatchSliceMember(slice []string, st string) (bool, error) {
for _, pattern := range slice {
match, err := regexp.MatchString(pattern, st)
if err != nil {
return false, err
} else if match {
return true, nil
}
}
return false, nil
}
func CopyDir(src string, dest string) (err error) {
sfi, err := os.Lstat(src)
if err != nil {
return err
}
if !sfi.IsDir() {
return fmt.Errorf("CopyFile cannot copy a file %s", src)
} else if sfi.Mode()&os.ModeSymlink != 0 {
linkSrc, err := os.Readlink(src)
if err != nil {
return err
}
return CopyDir(linkSrc, dest)
}
err = os.MkdirAll(dest, sfi.Mode())
if err != nil {
return err
}
directory, _ := os.Open(src)
objects, err := directory.Readdir(-1)
for _, obj := range objects {
srcfilepointer := src + "/" + obj.Name()
destfilepointer := dest + "/" + obj.Name()
if obj.IsDir() {
CopyDir(srcfilepointer, destfilepointer)
if err != nil {
return
}
} else {
err = CopyFile(srcfilepointer, destfilepointer)
if err != nil {
return
}
}
}
return
}
// CopyFile copies a file from src to dest. If src and dest files exist, and are
// the same, then return success. Otherise, attempt to create a hard link
// between the two files. If that fail, copy the file contents from src to dest.
func CopyFile(src, dest string) (err error) {
sfi, err := os.Lstat(src)
if err != nil {
return
}
if !sfi.Mode().IsRegular() {
if sfi.IsDir() {
return fmt.Errorf("CopyFile cannot copy a directory %s", src)
} else if sfi.Mode()&os.ModeSymlink != 0 {
linkSrc, err := os.Readlink(src)
if err != nil {
return err
}
return CopyFile(linkSrc, dest)
} else {
return fmt.Errorf("CopyFile: non-regular source file %s (%q)", src, sfi.Mode().String())
}
}
dfi, err := os.Lstat(dest)
if err != nil {
if !os.IsNotExist(err) {
return
}
} else {
if !(dfi.Mode().IsRegular()) {
return fmt.Errorf("CopyFile: non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String())
}
if os.SameFile(sfi, dfi) {
return
}
}
if err = os.Link(src, dest); err == nil {
return
}
err = copyFileContents(src, dest)
return
}
// copyFileContents copies the contents of the file named src to the file named
// by dest. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file.
func copyFileContents(src, dest string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.Create(dest)
if err != nil {
return
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
return
}
err = out.Sync()
return
}