-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.go
257 lines (238 loc) · 6.17 KB
/
parse.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
package main
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"path"
"path/filepath"
"strconv"
"strings"
)
type buildSpec struct {
Mod string // E.g. github.com/mjl-/gobuild. Never starts or ends with slash, and is never empty.
Version string
Dir string // Always starts with slash. Never ends with slash unless "/".
Goos string
Goarch string
Goversion string
Stripped bool
}
// filename to store the binary as. With .exe for windows.
func (bs buildSpec) filename() string {
var name string
if bs.Dir != "/" {
name = path.Base(bs.Dir)
} else {
name = path.Base(bs.Mod)
}
if bs.Goos == "windows" {
name += ".exe"
}
return name
}
// Variant of Dir that is either empty or otherwise has no leading but does have a
// trailing slash. Makes it easier to make some clean path by simple concatenation.
// Returns eg "" or "cmd/x/".
func (bs buildSpec) appendDir() string {
if bs.Dir == "/" {
return ""
}
return bs.Dir[1:] + "/"
}
// Used in transparency log lookups, and used to calculate directory where build results are stored.
// Can be parsed with parseBuildSpec.
func (bs buildSpec) String() string {
var variant string
if bs.Stripped {
variant = "-stripped"
}
return fmt.Sprintf("%s@%s/%s%s-%s-%s%s/", bs.Mod, bs.Version, bs.appendDir(), bs.Goos, bs.Goarch, bs.Goversion, variant)
}
// GOBIN-relative name of file created by "go get". Used as key to prevent
// concurrent builds that would create the same output file. This does not take
// into account that compiles for the same GOOS/GOARCH as host will just write to
// $GOBIN.
func (bs buildSpec) outputPath() string {
var name string
if bs.Dir != "/" {
name = filepath.Base(bs.Dir)
} else {
name = filepath.Base(bs.Mod)
}
if bs.Goos == "windows" {
name += ".exe"
}
return fmt.Sprintf("%s-%s/%s", bs.Goos, bs.Goarch, name)
}
// Local directory where results are stored, both successful and failed.
// Directories of failed builds can be removed, for a retry.
func (bs buildSpec) storeDir() string {
sha := sha256.Sum256([]byte(bs.String()))
sum := base64.RawURLEncoding.EncodeToString(sha[:20])
return filepath.Join(resultDir, sum[:1], sum)
}
type buildResult struct {
buildSpec
Filesize int64
Sum string
}
// Parse string of the form: module@version/dir/goos-goarch-goversion[-stripped]/.
// String generates strings that parseBuildSpec parses.
func parseBuildSpec(s string) (buildSpec, error) {
bs := buildSpec{}
// First peel off goos-goarch-goversion[-stripped]/ from end.
if !strings.HasSuffix(s, "/") {
return bs, fmt.Errorf("missing trailing slash")
}
s = s[:len(s)-1]
t := strings.Split(s, "/")
last := t[len(t)-1]
s = s[:len(s)-len(last)]
t = strings.Split(last, "-")
if len(t) != 3 && len(t) != 4 {
return bs, fmt.Errorf("bad goos-goarch-goversion[-stripped] %q", last)
}
bs.Goos = t[0]
bs.Goarch = t[1]
if _, ok := targets.available[bs.Goos+"/"+bs.Goarch]; !ok {
return bs, fmt.Errorf("unsupported target %s/%s", bs.Goos, bs.Goarch)
}
bs.Goversion = t[2]
if len(t) == 4 {
if t[3] != "stripped" {
return bs, fmt.Errorf("unrecognized variant %s", t[3])
}
bs.Stripped = true
}
t = strings.SplitN(s, "@", 2)
if len(t) != 2 {
return bs, fmt.Errorf("missing @ version")
}
bs.Mod = t[0]
if bs.Mod == "" {
return bs, fmt.Errorf("empty module")
}
if !strings.Contains(bs.Mod, ".") {
return bs, fmt.Errorf("module must contain dot")
}
s = t[1]
t = strings.SplitN(s, "/", 2)
if len(t) != 2 {
return bs, fmt.Errorf("missing slash for package dir")
}
bs.Version = t[0]
if bs.Version == "" {
return bs, fmt.Errorf("empty version")
}
bs.Dir = "/" + t[1]
if bs.Dir != "/" {
if !strings.HasSuffix(bs.Dir, "/") {
return bs, fmt.Errorf("missing slash at end of package dir")
} else {
bs.Dir = bs.Dir[:len(bs.Dir)-1]
}
}
if path.Clean(bs.Mod) != bs.Mod {
return bs, fmt.Errorf("non-canonical module name %q", bs.Mod)
}
if path.Clean(bs.Dir) != bs.Dir {
return bs, fmt.Errorf("non-canonical package dir %q", bs.Dir)
}
return bs, nil
}
// Parse module[@version/dir].
func parseGetSpec(s string) (buildSpec, error) {
bs := buildSpec{}
t := strings.SplitN(s, "@", 2)
if len(t) != 2 {
bs.Mod = s
if path.Clean(bs.Mod) != bs.Mod {
return bs, fmt.Errorf("non-canonical module directory")
}
bs.Version = "latest"
bs.Dir = "/"
return bs, nil
}
bs.Mod = t[0]
if bs.Mod == "" {
return bs, fmt.Errorf("empty module")
}
s = t[1]
t = strings.SplitN(s, "/", 2)
bs.Version = t[0]
if bs.Version == "" {
return bs, fmt.Errorf("empty version")
}
bs.Dir = "/"
if len(t) == 2 {
bs.Dir += strings.TrimRight(t[1], "/")
}
if path.Clean(bs.Mod) != bs.Mod {
return bs, fmt.Errorf("non-canonical module name %q", bs.Mod)
}
if path.Clean(bs.Dir) != bs.Dir {
return bs, fmt.Errorf("non-canonical package dir %q", bs.Dir)
}
return bs, nil
}
func parseRecord(data []byte) (*buildResult, error) {
msg := string(data)
if !strings.HasSuffix(msg, "\n") {
return nil, fmt.Errorf("does not end in newline")
}
msg = msg[:len(msg)-1]
t := strings.Split(msg, " ")
if len(t) != 8 && len(t) != 9 {
return nil, fmt.Errorf("bad record, got %d records, expected 8 or 9", len(t))
}
size, err := strconv.ParseInt(t[6], 10, 64)
if err != nil {
return nil, fmt.Errorf("bad filesize %s: %v", t[6], err)
}
var stripped bool
if len(t) == 9 {
switch t[8] {
case "":
case "stripped":
stripped = true
default:
return nil, fmt.Errorf("bad variant %s", t[8])
}
}
br := &buildResult{buildSpec{t[0], t[1], t[2], t[3], t[4], t[5], stripped}, size, t[7]}
return br, nil
}
func (br buildResult) packRecord() ([]byte, error) {
var variant string
if br.Stripped {
variant = "stripped"
}
fields := []string{
br.Mod,
br.Version,
br.Dir,
br.Goos,
br.Goarch,
br.Goversion,
fmt.Sprintf("%d", br.Filesize),
br.Sum,
variant,
}
for i, f := range fields {
if f == "" && i != 8 {
return nil, fmt.Errorf("bad empty field %d", i)
}
for _, c := range f {
if c <= ' ' {
return nil, fmt.Errorf("bad field %d in record: %q", i, f)
}
}
}
if len(br.Sum) != 28 {
return nil, fmt.Errorf("bad length for sum")
}
if br.Filesize == 0 {
return nil, fmt.Errorf("bad filesize 0")
}
return []byte(strings.Join(fields, " ") + "\n"), nil
}