-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrepodata.go
449 lines (371 loc) · 9.8 KB
/
repodata.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package main
import (
"archive/tar"
"bufio"
"compress/gzip"
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/golang/glog"
"github.com/klauspost/compress/zstd"
"golang.org/x/tools/container/intsets"
"howett.net/plist"
)
var etagEncoding = base64.RawURLEncoding
const repoIndexFile = "index.plist"
const defaultRepository = "current"
var errNoIndex = fmt.Errorf("index not found: %s", repoIndexFile)
type FilterFunc func(*packageData) bool
type packageIndex []*packageData
const (
minSplitFilter = 3000
minIndexCapacity = 16
splitSize = 2000
)
func (ps packageIndex) Filter(fn FilterFunc) packageIndex {
if len(ps) <= minSplitFilter {
return ps.singleFilter(fn)
}
return ps.splitFilter(fn)
}
func (ps packageIndex) singleFilter(fn FilterFunc) packageIndex {
ps2 := make(packageIndex, 0, 16)
for _, p := range ps {
if fn(p) {
ps2 = append(ps2, p)
}
}
return ps2
}
func (ps packageIndex) splitFilter(fn FilterFunc) packageIndex {
var (
want int
index intsets.Sparse
subsets = make(chan *intsets.Sparse)
)
for i := 0; i < len(ps); i += splitSize {
min := i
max := min + splitSize
if max > len(ps) {
max = len(ps)
}
want++
set := ps[min:max]
go func() {
var sub intsets.Sparse
for i, p := range set {
if fn(p) {
sub.Insert(min + i)
}
}
subsets <- &sub
}()
}
for ; want > 0; want-- {
index.UnionWith(<-subsets)
}
ps2 := make(packageIndex, 0, index.Len())
for i := 0; index.TakeMin(&i); {
ps2 = append(ps2, ps[i])
}
return ps2
}
type RepoData struct {
root packageMap
index packageIndex
nameIndex []string
etag string
}
func NewRepoData() *RepoData {
return &RepoData{
root: packageMap{},
}
}
func (rd *RepoData) LoadRepo(path, repo string) error {
fi, err := os.Open(path)
if err != nil {
return err
}
defer fi.Close()
return rd.ReadRepo(fi, repo)
}
func (rd *RepoData) Index() packageIndex {
if rd == nil {
return nil
}
return rd.index
}
func (rd *RepoData) NameIndex() []string {
if rd == nil {
return nil
}
return rd.nameIndex
}
func (rd *RepoData) zstdReader(r io.Reader) (io.ReadCloser, error) {
dec, err := zstd.NewReader(r)
if err != nil {
return nil, err
}
return newErrorCloser(dec), nil
}
func (rd *RepoData) gzipReader(r io.Reader) (io.ReadCloser, error) {
return gzip.NewReader(r)
}
func (rd *RepoData) ReadRepo(r io.ReadSeeker, repo string) error {
read := func(rs io.ReadSeeker, decompress func(rs io.Reader) (io.ReadCloser, error)) error {
_, err := rs.Seek(0, io.SeekStart)
if err != nil {
return fmt.Errorf("unable to seek to head of repo file before gzip read: %w", err)
}
br := bufio.NewReader(rs)
rc, err := decompress(br)
if err != nil {
return err
}
defer rc.Close()
tr := tar.NewReader(rc)
for {
hdr, err := tr.Next()
if err != nil {
return err
}
if hdr.Name == repoIndexFile {
return rd.ReadRepoIndex(tr, repo)
}
}
return errNoIndex
}
type decompressor struct {
name string
fn func(io.Reader) (io.ReadCloser, error)
}
decompressors := []decompressor{
{"zstd", rd.zstdReader},
{"gzip", rd.gzipReader},
}
var err error
for _, dec := range decompressors {
switch err = read(r, dec.fn); {
case err == nil:
return nil // OK
case errors.Is(err, zstd.ErrMagicMismatch),
errors.Is(err, tar.ErrHeader),
errors.Is(err, io.ErrUnexpectedEOF),
errors.Is(err, gzip.ErrHeader):
// Retry as next
case err != nil:
return fmt.Errorf("error parsing %s repodata: %w", dec.name, err)
}
}
return err
}
func copyToTempFile(r io.Reader) (*os.File, error) {
tmpfile, err := ioutil.TempFile("", "index.plist")
if err != nil {
return nil, err
}
if err := os.Remove(tmpfile.Name()); err != nil {
tmpfile.Close()
return nil, err
}
if _, err := io.Copy(tmpfile, r); err != nil {
tmpfile.Close()
return nil, err
}
return tmpfile, nil
}
func (rd *RepoData) ReadRepoIndex(r io.Reader, repo string) error {
var err error
rs, ok := r.(io.ReadSeeker)
if !ok {
var f *os.File
if f, err = copyToTempFile(r); err != nil {
return err
}
defer f.Close()
rs = f
}
if repo == "" {
repo = defaultRepository
}
pkg := packageMap{}
err = plist.NewDecoder(rs).Decode(pkg)
if err != nil {
return err
}
// Merge indices and maps -- this gets around a flaw in howett.net/plist where decoding into
// an existing dataset will result in an invalid use of the reflect package and panic.
index := rd.index
for k, p := range pkg {
old, ok := rd.root[k]
if p.Name == "" {
p.Name = k
_, p.Version, p.Revision, _ = ParseVersionedName(p.PackageVersion)
}
p.Repository = repo
// Do naive case normalization for searches -- shouldn't have an impact on these
// given that everything in repodata is currently ASCII.
p.SearchPackageVersion = strings.ToLower(p.PackageVersion)
p.SearchShortDesc = strings.ToLower(p.ShortDesc)
p.ETag, err = p.computeETag()
if err != nil {
// This really shouldn't happen -- it would mean JSON encoding of packages
// was broken.
glog.Errorf("unable to compute etag for %q: %v", p.PackageVersion, err)
return err
}
rd.root[k] = p
if ok {
index[old.Index] = p
} else {
index = append(index, p)
}
}
sort.Slice(index, func(i, j int) bool {
return index[i].Name < index[j].Name
})
for i, p := range index {
p.Index = i
}
rd.index = index
names := rd.nameIndex[:0]
for _, p := range rd.index {
names = append(names, p.Name)
}
rd.nameIndex = names
etag, err := rd.computeETag()
if err != nil {
return err
}
rd.etag = etag
return nil
}
func (rd *RepoData) Package(name string) *packageData {
if rd == nil {
return nil
}
return rd.root[name]
}
func (rd *RepoData) computeETag() (string, error) {
h := sha1.New()
index := rd.Index()
if err := binary.Write(h, binary.LittleEndian, int64(len(index))); err != nil {
return "", err
}
for _, p := range index {
binary.Write(h, binary.LittleEndian, int64(len(p.PackageVersion)+len(p.ETag)))
io.WriteString(h, p.PackageVersion)
io.WriteString(h, p.ETag)
}
sum := h.Sum(make([]byte, 0, h.Size()))
etag := `W/"` + etagEncoding.EncodeToString(sum) + `"`
return etag, nil
}
func (rd *RepoData) ETag() string {
return rd.etag
}
var errNoRevision = errors.New("revision not found")
var errNoVersion = errors.New("version not found")
func ParseVersionedName(s string) (name, version string, revision int, err error) {
if s == "" {
err = errors.New("name is empty")
return
}
revidx := strings.LastIndexByte(s, '_')
if revidx == -1 {
err = errNoRevision
return
}
vsnidx := strings.LastIndexByte(s[:revidx], '-')
if vsnidx == -1 {
err = errNoVersion
return
}
revision, err = strconv.Atoi(s[revidx+1:])
if err != nil {
return
}
name, version = s[:vsnidx], s[vsnidx+1:revidx]
return
}
type urlVal url.URL
func (u *urlVal) UnmarshalText(p []byte) error {
uu, err := url.Parse(string(p))
if err != nil {
return err
}
*u = urlVal(*uu)
return nil
}
func (u *urlVal) MarshalText() ([]byte, error) {
if u == nil {
return nil, nil
}
return []byte((*url.URL)(u).String()), nil
}
type timeVal time.Time
func (t *timeVal) MarshalText() ([]byte, error) {
return []byte(time.Time(*t).Format(time.RFC3339)), nil
}
func (t *timeVal) UnmarshalText(p []byte) error {
const layout = `2006-01-02 15:04 MST`
tt, err := time.ParseInLocation(layout, string(p), time.UTC)
if err != nil {
return err
}
*t = timeVal(tt.UTC())
return nil
}
type packageMap map[string]*packageData
type packageData struct {
PackageVersion string `plist:"pkgver" json:"-"`
Name string `plist:"-" json:"name,omitempty"`
Version string `plist:"-" json:"version,omitempty"`
Revision int `plist:"-" json:"revision,omitempty"`
Repository string `plist:"-" json:"repository,omitempty"`
Architecture string `plist:"architecture" json:"architecture,omitempty"`
BuildDate timeVal `plist:"build-date" json:"build_date,omitempty"`
BuildOptions string `plist:"build-options" json:"build_options,omitempty"`
FilenameSHA256 string `plist:"filename-sha256" json:"filename_sha256,omitempty"`
FilenameSize int64 `plist:"filename-size" json:"filename_size,omitempty"`
Homepage *urlVal `plist:"homepage" json:"homepage,omitempty"`
InstalledSize int64 `plist:"installed_size" json:"installed_size,omitempty"`
License string `plist:"license" json:"license,omitempty"`
Maintainer string `plist:"maintainer" json:"maintainer,omitempty"`
ShortDesc string `plist:"short_desc" json:"short_desc,omitempty"`
Preserve bool `plist:"preserve" json:"preserve,omitempty"`
SourceRevisions string `plist:"source-revisions" json:"source_revisions,omitempty"`
RunDepends []string `plist:"run_depends" json:"run_depends,omitempty"`
ShlibRequires []string `plist:"shlib-requires" json:"shlib_requires,omitempty"`
ShlibProvides []string `plist:"shlib-provides" json:"shlib_provides,omitempty"`
Conflicts []string `plist:"conflicts" json:"conflicts,omitempty"`
Reverts []string `plist:"reverts" json:"reverts,omitempty"`
Replaces []string `plist:"replaces" json:"replaces,omitempty"`
Alternatives map[string][]string `plist:"alternatives" json:"alternatives,omitempty"`
ConfFiles []string `plist:"conf_files" json:"conf_files,omitempty"`
// Lower-case versions of PackageVersion and ShortDesc for searching
SearchPackageVersion string `plist:"-" json:"-"`
SearchShortDesc string `plist:"-" json:"-"`
Index int `plist:"-" json:"-"`
ETag string `plist:"-" json:"-"`
}
func (p *packageData) computeETag() (string, error) {
h := sha1.New()
if err := json.NewEncoder(h).Encode(p); err != nil {
return "", nil
}
sum := h.Sum(make([]byte, 0, h.Size()))
etag := `W/"` + etagEncoding.EncodeToString(sum) + `"`
return etag, nil
}