-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollect.go
More file actions
294 lines (278 loc) · 8.18 KB
/
Copy pathcollect.go
File metadata and controls
294 lines (278 loc) · 8.18 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
)
const maxMetadata = 32 << 20
const maxAssets = 100000
type options struct {
Dir, GOOS, GOARCH, CGO, Tags string
Tests bool
Patterns []string
MaxBytes int64
}
type scope struct {
GOOS string `json:"goos"`
GOARCH string `json:"goarch"`
CGO string `json:"cgo"`
Tags []string `json:"tags"`
Tests bool `json:"tests"`
Patterns []string `json:"packages"`
}
type asset struct {
Package string `json:"package"`
Kind string `json:"kind"`
Path string `json:"path"`
Bytes int64 `json:"bytes"`
SHA256 string `json:"sha256"`
}
type manifest struct {
Schema int `json:"schema"`
GoVersion string `json:"go_version"`
Scope scope `json:"scope"`
Assets []asset `json:"assets"`
TotalBytes int64 `json:"total_bytes"`
}
type goPackage struct {
Dir, ImportPath string
ForTest string
EmbedFiles, TestEmbedFiles, XTestEmbedFiles []string
Error *struct{ Err string }
}
// boundedBuffer stops excessive command output instead of retaining it in memory.
type boundedBuffer struct {
bytes.Buffer
limit int
}
func (b *boundedBuffer) Write(p []byte) (int, error) {
if len(p) > b.limit-b.Len() {
return 0, errors.New("Go output exceeded metadata limit")
}
return b.Buffer.Write(p)
}
func goCommand(ctx context.Context, dir string, env []string, args ...string) ([]byte, error) {
cmd := exec.CommandContext(ctx, "go", args...)
cmd.Dir, cmd.Env = dir, env
out := &boundedBuffer{limit: maxMetadata}
stderr := &boundedBuffer{limit: 64 << 10}
cmd.Stdout, cmd.Stderr = out, stderr
if err := cmd.Run(); err != nil {
if ctx.Err() != nil {
return nil, fmt.Errorf("go %s interrupted: %w", args[0], ctx.Err())
}
return nil, fmt.Errorf("go %s: %w: %s", args[0], err, strings.TrimSpace(stderr.String()))
}
if args[0] == "list" && strings.Contains(stderr.String(), "matched no packages") {
return nil, fmt.Errorf("incomplete package selection: %s", strings.TrimSpace(stderr.String()))
}
return out.Bytes(), nil
}
func controlledEnv(opt options) []string {
overrides := map[string]string{
"GOOS": opt.GOOS, "GOARCH": opt.GOARCH, "CGO_ENABLED": opt.CGO,
"GOFLAGS": "", "GOWORK": "off", "GOTOOLCHAIN": "local",
"GOPROXY": "off", "GOSUMDB": "off", "GO111MODULE": "on",
}
env := make([]string, 0, len(os.Environ())+len(overrides))
for _, s := range os.Environ() {
key, _, _ := strings.Cut(s, "=")
if _, replace := overrides[strings.ToUpper(key)]; !replace {
env = append(env, s)
}
}
for key, value := range overrides {
env = append(env, key+"="+value)
}
return env
}
func collect(ctx context.Context, opt options) (manifest, error) {
m := manifest{Schema: 1, Assets: []asset{}}
if opt.CGO != "0" && opt.CGO != "1" {
return m, errors.New("--cgo must be 0 or 1")
}
if opt.MaxBytes <= 0 || opt.MaxBytes > 1<<50 {
return m, errors.New("--max-bytes must be between 1 and 1125899906842624")
}
root, err := filepath.Abs(opt.Dir)
if err != nil {
return m, err
}
root, err = filepath.EvalSymlinks(root)
if err != nil {
return m, err
}
info, err := os.Stat(filepath.Join(root, "go.mod"))
if err != nil || !info.Mode().IsRegular() {
return m, errors.New("--dir must be a module root containing a regular go.mod file")
}
patterns := slices.Clone(opt.Patterns)
if len(patterns) == 0 {
patterns = []string{"./..."}
}
for _, p := range patterns {
if p != "." && !strings.HasPrefix(p, "./") {
return m, fmt.Errorf("package %q must be . or start with ./", p)
}
for _, component := range strings.Split(p, "/") {
if component == ".." || strings.ContainsAny(component, "\\@") {
return m, fmt.Errorf("invalid local package pattern %q", p)
}
}
}
slices.Sort(patterns)
patterns = slices.Compact(patterns)
tags := strings.FieldsFunc(opt.Tags, func(r rune) bool { return r == ',' || r == ' ' })
slices.Sort(tags)
tags = slices.Compact(tags)
if tags == nil {
tags = []string{}
}
m.Scope = scope{opt.GOOS, opt.GOARCH, opt.CGO, tags, opt.Tests, patterns}
env := controlledEnv(opt)
data, err := goCommand(ctx, root, env, "env", "GOVERSION")
if err != nil {
return m, err
}
m.GoVersion = strings.TrimSpace(string(data))
args := []string{"list", "-mod=readonly", "-json", "-tags=" + strings.Join(tags, ",")}
if opt.Tests {
args = append(args, "-test")
}
args = append(args, patterns...)
data, err = goCommand(ctx, root, env, args...)
if err != nil {
return m, err
}
decoder := json.NewDecoder(bytes.NewReader(data))
seen := map[string]bool{}
count := 0
rootFS, err := os.OpenRoot(root)
if err != nil {
return m, err
}
defer rootFS.Close()
for {
var pkg goPackage
if err := decoder.Decode(&pkg); err != nil {
if errors.Is(err, io.EOF) {
break
}
return m, fmt.Errorf("decode go list: %w", err)
}
count++
if pkg.Error != nil {
return m, errors.New(pkg.Error.Err)
}
// With -test, Go emits synthetic variants as well as the original
// package. Its original now contains the resolved test asset lists.
if pkg.ForTest != "" {
continue
}
dir, err := filepath.Rel(root, pkg.Dir)
if err != nil || !filepath.IsLocal(dir) {
return m, fmt.Errorf("package %q is outside the selected module", pkg.ImportPath)
}
groups := []struct {
kind string
names []string
}{{"production", pkg.EmbedFiles}}
if opt.Tests {
groups = append(groups, struct {
kind string
names []string
}{"test", pkg.TestEmbedFiles}, struct {
kind string
names []string
}{"external_test", pkg.XTestEmbedFiles})
}
for _, group := range groups {
for _, name := range group.names {
if !fs.ValidPath(name) || strings.Contains(name, "\\") {
return m, fmt.Errorf("invalid embedded path %q", name)
}
rel := filepath.ToSlash(filepath.Join(dir, filepath.FromSlash(name)))
a := asset{Package: pkg.ImportPath, Kind: group.kind, Path: rel}
if seen[assetKey(a)] {
continue
}
seen[assetKey(a)] = true
if len(seen) > maxAssets {
return m, errors.New("asset count exceeds 100000")
}
if err := ctx.Err(); err != nil {
return m, err
}
a.Bytes, a.SHA256, err = hashAsset(ctx, rootFS, rel, opt.MaxBytes-m.TotalBytes)
if err != nil {
return m, fmt.Errorf("asset %q: %w", rel, err)
}
m.TotalBytes += a.Bytes
m.Assets = append(m.Assets, a)
}
}
}
if count == 0 {
return m, errors.New("no packages matched; refusing to report a successful empty scan")
}
slices.SortFunc(m.Assets, func(a, b asset) int { return strings.Compare(assetKey(a), assetKey(b)) })
return m, nil
}
type contextReader struct {
ctx context.Context
r io.Reader
}
func (r contextReader) Read(p []byte) (int, error) {
if err := r.ctx.Err(); err != nil {
return 0, err
}
return r.r.Read(p)
}
func hashAsset(ctx context.Context, root *os.Root, path string, remaining int64) (int64, string, error) {
f, err := root.Open(filepath.FromSlash(path))
if err != nil {
return 0, "", err
}
defer f.Close()
before, err := f.Stat()
if err != nil {
return 0, "", err
}
if !before.Mode().IsRegular() {
return 0, "", errors.New("not a regular file")
}
if before.Size() > remaining {
return 0, "", errors.New("total asset bytes exceed --max-bytes")
}
hash := sha256.New()
n, err := io.Copy(hash, io.LimitReader(contextReader{ctx, f}, remaining+1))
if err != nil {
return 0, "", err
}
if n > remaining {
return 0, "", errors.New("total asset bytes exceed --max-bytes")
}
after, err := f.Stat()
if err != nil {
return 0, "", err
}
if n != before.Size() || after.Size() != before.Size() || !after.ModTime().Equal(before.ModTime()) {
return 0, "", errors.New("file changed while scanning; retry on a stable tree")
}
return n, hex.EncodeToString(hash.Sum(nil)), nil
}
func assetKey(a asset) string { return a.Package + "\x00" + a.Kind + "\x00" + a.Path }
func sameScope(a, b scope) bool {
return a.GOOS == b.GOOS && a.GOARCH == b.GOARCH && a.CGO == b.CGO && a.Tests == b.Tests && slices.Equal(a.Tags, b.Tags) && slices.Equal(a.Patterns, b.Patterns)
}