/
copy.go
203 lines (187 loc) · 4.72 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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package context
import (
"fmt"
"hash"
"io"
"os"
"path"
"path/filepath"
"sort"
"strings"
"github.com/kardianos/govendor/internal/pathos"
"github.com/pkg/errors"
)
type fileInfoSort []os.FileInfo
func (l fileInfoSort) Len() int {
return len(l)
}
func (l fileInfoSort) Less(i, j int) bool {
a := l[i]
b := l[j]
if a.IsDir() == b.IsDir() {
return l[i].Name() < l[j].Name()
}
return !a.IsDir()
}
func (l fileInfoSort) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
// CopyPackage copies the files from the srcPath to the destPath, destPath
// folder and parents are are created if they don't already exist.
func (ctx *Context) CopyPackage(destPath, srcPath, lookRoot, pkgPath string, ignoreFiles []string, tree bool, h hash.Hash, beforeCopy func(deps []string) error) error {
if pathos.FileStringEquals(destPath, srcPath) {
return fmt.Errorf("Attempting to copy package to same location %q.", destPath)
}
err := os.MkdirAll(destPath, 0777)
if err != nil {
return err
}
// Ensure the dest is empty of files.
destDir, err := os.Open(destPath)
if err != nil {
return err
}
ignoreTest := false
for _, ignore := range ctx.ignoreTag {
if ignore == "test" {
ignoreTest = true
break
}
}
fl, err := destDir.Readdir(-1)
destDir.Close()
if err != nil {
return err
}
for _, fi := range fl {
if fi.IsDir() {
if tree {
err = errors.Wrap(os.RemoveAll(filepath.Join(destPath, fi.Name())), "remove all existing tree entries")
if err != nil {
return err
}
}
continue
}
err = errors.Wrap(os.Remove(filepath.Join(destPath, fi.Name())), "remove existing file")
if err != nil {
return err
}
}
// Copy files into dest.
srcDir, err := os.Open(srcPath)
if err != nil {
return errors.Wrap(err, "open srcPath directory")
}
fl, err = srcDir.Readdir(-1)
srcDir.Close()
if err != nil {
return errors.Wrap(err, "src readdir")
}
if h != nil {
// Write relative path to GOPATH.
h.Write([]byte(strings.Trim(pkgPath, "/")))
// Sort file list to present a stable hash.
sort.Sort(fileInfoSort(fl))
}
fileLoop:
for _, fi := range fl {
name := fi.Name()
if name[0] == '.' {
continue
}
if fi.IsDir() {
isTestdata := name == "testdata"
if !tree && !isTestdata {
continue
}
if name[0] == '_' {
continue
}
if ignoreTest {
if strings.HasSuffix(name, "_test") || isTestdata {
continue
}
}
nextDestPath := filepath.Join(destPath, name)
nextSrcPath := filepath.Join(srcPath, name)
var nextIgnoreFiles, deps []string
if !isTestdata && !strings.Contains(pkgPath, "/testdata/") {
nextIgnoreFiles, deps, err = ctx.getIgnoreFiles(nextSrcPath)
if err != nil {
return err
}
}
if beforeCopy != nil {
err = beforeCopy(deps)
if err != nil {
return errors.Wrap(err, "beforeCopy")
}
}
err = ctx.CopyPackage(nextDestPath, nextSrcPath, lookRoot, path.Join(pkgPath, name), nextIgnoreFiles, true, h, beforeCopy)
if err != nil {
return errors.Wrapf(err,
"CopyPackage dest=%q src=%q lookRoot=%q pkgPath=%q ignoreFiles=%q tree=%t has beforeCopy=%t",
nextDestPath, nextSrcPath, lookRoot, path.Join(pkgPath, name), nextIgnoreFiles, true, beforeCopy != nil,
)
}
continue
}
for _, ignore := range ignoreFiles {
if pathos.FileStringEquals(name, ignore) {
continue fileLoop
}
}
if h != nil {
h.Write([]byte(name))
}
err = copyFile(
filepath.Join(destPath, name),
filepath.Join(srcPath, name),
h,
)
if err != nil {
return errors.Wrapf(err, "copyFile dest=%q src=%q", filepath.Join(destPath, name), filepath.Join(srcPath, name))
}
}
return errors.Wrapf(licenseCopy(lookRoot, srcPath, filepath.Join(ctx.RootDir, ctx.VendorFolder), pkgPath), "licenseCopy srcPath=%q", srcPath)
}
func copyFile(destPath, srcPath string, h hash.Hash) error {
ss, err := os.Stat(srcPath)
if err != nil {
return errors.Wrap(err, "copyFile Stat")
}
src, err := os.Open(srcPath)
if err != nil {
return errors.Wrapf(err, "open src=%q", srcPath)
}
defer src.Close()
// Ensure we are not trying to copy a directory. May happen with symlinks.
if st, err := src.Stat(); err == nil {
if st.IsDir() {
return nil
}
}
dest, err := os.Create(destPath)
if err != nil {
return errors.Wrapf(err, "create dest=%q", destPath)
}
r := io.Reader(src)
if h != nil {
r = io.TeeReader(src, h)
}
_, err = io.Copy(dest, r)
// Close before setting mod and time.
dest.Close()
if err != nil {
return errors.Wrap(err, "copy")
}
err = os.Chmod(destPath, ss.Mode())
if err != nil {
return err
}
return os.Chtimes(destPath, ss.ModTime(), ss.ModTime())
}