forked from oracle/smith
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.go
258 lines (245 loc) · 6.15 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"io"
"os"
"path/filepath"
"strings"
"github.com/Sirupsen/logrus"
)
func CopyTree(baseDir, outputDir string, globs []string, excludes []string, nss, follow, chroot bool) error {
dir, err := os.Getwd()
if err != nil {
logrus.Errorf("Failed to get working directory: %v", err)
return err
}
err = os.Chdir(baseDir)
if err != nil {
logrus.Errorf("Failed to change directory to %v: %v", baseDir, err)
return err
}
defer os.Chdir(dir)
chrootDir := ""
if chroot == true {
chrootDir = baseDir
}
exs := map[string]struct{}{}
for _, glob := range excludes {
// ignore empty globs
if glob == "" {
continue
}
// glob absolute paths as relative from current directory
if filepath.IsAbs(glob) {
glob = filepath.Clean(glob)[1:]
}
paths, err := filepath.Glob(glob)
if err == filepath.ErrBadPattern {
logrus.Errorf("Illegal filepath pattern: %v", glob)
return err
}
for _, path := range paths {
exs[path] = struct{}{}
}
}
if len(globs) == 0 {
logrus.Debugf("Skipping copy because globs is empty")
return nil
}
for _, glob := range globs {
// ignore empty globs
if glob == "" {
continue
}
// glob absolute paths as relative from current directory
if filepath.IsAbs(glob) {
glob = filepath.Clean(glob)[1:]
}
paths, err := filepath.Glob(glob)
if err == filepath.ErrBadPattern {
logrus.Errorf("Illegal filepath pattern: %v", glob)
return err
}
for _, path := range paths {
// pass excludes to walk so that subdirectories can be excluded
err = filepath.Walk(path, copier(exs, chrootDir, outputDir, nss, follow))
if err != nil {
logrus.Errorf("Failed to walk %v: %v", path, err)
return err
}
}
}
return nil
}
func Copy(src string, dst string) error {
// try hard linking first before falling back to a copy
if err := os.Link(src, dst); err == nil {
return nil
}
s, err := os.Open(src)
if err != nil {
return err
}
defer s.Close()
d, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer d.Close()
if _, err := io.Copy(d, s); err != nil {
return err
}
return nil
}
func Rchown(path string, uid, gid int) error {
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if err := os.Chown(path, uid, gid); !os.IsNotExist(err) {
return err
}
return nil
})
if err != nil {
logrus.Errorf("Failed to walk %v: %v", path, err)
return err
}
return nil
}
func ensureSymlink(dest, source string) error {
if err := os.MkdirAll(filepath.Dir(source), 0755); err != nil {
logrus.Errorf("Failed to create directories for %v: %v", source, err)
return err
}
err := os.Symlink(dest, source)
if err != nil {
if !os.IsExist(err) {
logrus.Errorf("Failed to create symlink from %v to %v", source, dest)
return err
}
sd, nerr := os.Lstat(source)
if nerr != nil {
logrus.Errorf("Failed to stat %v: %v", source, nerr)
return nerr
}
if sd.Mode()&os.ModeSymlink == 0 {
logrus.Errorf("Failed to create symlink from %v to %v", source, dest)
return err
}
ndest, nerr := os.Readlink(source)
if nerr != nil {
logrus.Errorf("Failed to read symlink %v: %v", source, nerr)
return nerr
}
if ndest != dest {
logrus.Errorf("Symlink %v already exists, but it points to %v instead of %v", source, ndest, dest)
return err
}
} else {
logrus.Debugf("Symlink created from %v to %v", source, dest)
}
return nil
}
func copier(exs map[string]struct{}, chrootDir, outputDir string, nss, follow bool) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
// if we are follow, we may find the file through walkAndCopy
if !follow || !os.IsNotExist(err) {
return err
}
}
rel := strings.TrimPrefix(path, chrootDir)
if _, ok := exs[rel]; ok {
return nil
}
if follow {
// NOTE: directory symlinks will not be excluded by excludes
nPath, err := walkAndCopySymlinks(chrootDir, outputDir, path)
if err != nil {
if os.IsNotExist(err) {
logrus.Debugf("Skipping dangling link for %v", path)
return nil
}
logrus.Errorf("Failed to walk symlinks for %v: %v", path, err)
return err
}
if path != nPath {
rel = strings.TrimPrefix(nPath, chrootDir)
if _, ok := exs[rel]; ok {
return nil
}
path = nPath
info, err = os.Lstat(path)
if err != nil {
logrus.Errorf("Failed to lstat %v: %v", path, err)
return err
}
}
}
outpath := filepath.Join(outputDir, rel)
if _, err := os.Stat(outpath); err == nil {
// if follow is false, overwrite the file at location
if follow || info.IsDir() {
logrus.Debugf("Path %v already exists", outpath)
return nil
}
logrus.Debugf("Overwriting %v", outpath)
os.RemoveAll(outpath)
}
base := outpath
if !info.IsDir() {
base = filepath.Dir(outpath)
}
if err := os.MkdirAll(base, 0755); err != nil {
logrus.Errorf("Failed to create directories %v: %v", base, err)
return err
}
if info.IsDir() {
return nil
}
// only happens if !follow
if info.Mode()&os.ModeSymlink != 0 {
dest, err := os.Readlink(path)
if err != nil {
logrus.Errorf("Symlink cannot be read: %v", err)
return err
}
// copy the symlink
if err := ensureSymlink(dest, outpath); err != nil {
return err
}
} else {
logrus.Debugf("Copying file from: %v to %v", path, outpath)
// copy the file
err = Copy(path, outpath)
if err != nil {
logrus.Errorf("Failed to copy file: %v", err)
return err
}
if info.Mode()&0100 != 0 {
// executable
os.Chmod(outpath, 0755)
if follow {
deps, err := Deps(chrootDir, path, nss)
if err != nil {
return err
}
for dep := range deps {
logrus.Debugf("Walking dependency: %v", dep)
if filepath.IsAbs(dep) {
dep = filepath.Join(chrootDir, dep)
} else {
dep, err = filepath.Abs(filepath.Join(filepath.Dir(path), dep))
if err != nil {
logrus.Errorf("Could not determine path of dep: %v", err)
return err
}
}
filepath.Walk(dep, copier(exs, chrootDir, outputDir, nss, follow))
}
}
}
}
return nil
}
}