-
-
Notifications
You must be signed in to change notification settings - Fork 363
/
query.go
546 lines (456 loc) · 10.7 KB
/
query.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
package main
import (
"fmt"
"os"
"sort"
"strings"
"sync"
"time"
alpm "github.com/Jguer/go-alpm"
rpc "github.com/mikkeloscar/aur"
)
type aurWarnings struct {
Orphans []string
OutOfDate []string
Missing []string
}
// Query is a collection of Results
type aurQuery []rpc.Pkg
// Query holds the results of a repository search.
type repoQuery []alpm.Package
func (q aurQuery) Len() int {
return len(q)
}
func (q aurQuery) Less(i, j int) bool {
var result bool
switch config.SortBy {
case "votes":
result = q[i].NumVotes > q[j].NumVotes
case "popularity":
result = q[i].Popularity > q[j].Popularity
case "name":
result = lessRunes([]rune(q[i].Name), []rune(q[j].Name))
case "base":
result = lessRunes([]rune(q[i].PackageBase), []rune(q[j].PackageBase))
case "submitted":
result = q[i].FirstSubmitted < q[j].FirstSubmitted
case "modified":
result = q[i].LastModified < q[j].LastModified
case "id":
result = q[i].ID < q[j].ID
case "baseid":
result = q[i].PackageBaseID < q[j].PackageBaseID
}
if config.SortMode == bottomUp {
return !result
}
return result
}
func (q aurQuery) Swap(i, j int) {
q[i], q[j] = q[j], q[i]
}
// FilterPackages filters packages based on source and type from local repository.
func filterPackages() (local []alpm.Package, remote []alpm.Package,
localNames []string, remoteNames []string, err error) {
localDB, err := alpmHandle.LocalDB()
if err != nil {
return
}
dbList, err := alpmHandle.SyncDBs()
if err != nil {
return
}
f := func(k alpm.Package) error {
found := false
// For each DB search for our secret package.
_ = dbList.ForEach(func(d alpm.DB) error {
if found {
return nil
}
if d.Pkg(k.Name()) != nil {
found = true
local = append(local, k)
localNames = append(localNames, k.Name())
}
return nil
})
if !found {
remote = append(remote, k)
remoteNames = append(remoteNames, k.Name())
}
return nil
}
err = localDB.PkgCache().ForEach(f)
return
}
// NarrowSearch searches AUR and narrows based on subarguments
func narrowSearch(pkgS []string, sortS bool) (aurQuery, error) {
var r []rpc.Pkg
var err error
var usedIndex int
if len(pkgS) == 0 {
return nil, nil
}
for i, word := range pkgS {
r, err = rpc.Search(word)
if err == nil {
usedIndex = i
break
}
}
if err != nil {
return nil, err
}
if len(pkgS) == 1 {
if sortS {
sort.Sort(aurQuery(r))
}
return r, err
}
var aq aurQuery
var n int
for _, res := range r {
match := true
for i, pkgN := range pkgS {
if usedIndex == i {
continue
}
if !(strings.Contains(res.Name, pkgN) || strings.Contains(strings.ToLower(res.Description), pkgN)) {
match = false
break
}
}
if match {
n++
aq = append(aq, res)
}
}
if sortS {
sort.Sort(aq)
}
return aq, err
}
// SyncSearch presents a query to the local repos and to the AUR.
func syncSearch(pkgS []string) (err error) {
pkgS = removeInvalidTargets(pkgS)
var aurErr error
var repoErr error
var aq aurQuery
var pq repoQuery
if mode == modeAUR || mode == modeAny {
aq, aurErr = narrowSearch(pkgS, true)
}
if mode == modeRepo || mode == modeAny {
pq, repoErr = queryRepo(pkgS)
if repoErr != nil {
return err
}
}
switch config.SortMode {
case topDown:
if mode == modeRepo || mode == modeAny {
pq.printSearch()
}
if mode == modeAUR || mode == modeAny {
aq.printSearch(1)
}
case bottomUp:
if mode == modeAUR || mode == modeAny {
aq.printSearch(1)
}
if mode == modeRepo || mode == modeAny {
pq.printSearch()
}
default:
return fmt.Errorf("Invalid Sort Mode. Fix with yay -Y --bottomup --save")
}
if aurErr != nil {
fmt.Fprintf(os.Stderr, "Error during AUR search: %s\n", aurErr)
fmt.Fprintln(os.Stderr, "Showing Repo packages only")
}
return nil
}
// SyncInfo serves as a pacman -Si for repo packages and AUR packages.
func syncInfo(pkgS []string) (err error) {
var info []*rpc.Pkg
missing := false
pkgS = removeInvalidTargets(pkgS)
aurS, repoS, err := packageSlices(pkgS)
if err != nil {
return
}
if len(aurS) != 0 {
noDB := make([]string, 0, len(aurS))
for _, pkg := range aurS {
_, name := splitDBFromName(pkg)
noDB = append(noDB, name)
}
info, err = aurInfoPrint(noDB)
if err != nil {
missing = true
fmt.Fprintln(os.Stderr, err)
}
}
// Repo always goes first
if len(repoS) != 0 {
arguments := cmdArgs.copy()
arguments.clearTargets()
arguments.addTarget(repoS...)
err = show(passToPacman(arguments))
if err != nil {
return
}
}
if len(aurS) != len(info) {
missing = true
}
if len(info) != 0 {
for _, pkg := range info {
PrintInfo(pkg)
}
}
if missing {
err = fmt.Errorf("")
}
return
}
// Search handles repo searches. Creates a RepoSearch struct.
func queryRepo(pkgInputN []string) (s repoQuery, err error) {
dbList, err := alpmHandle.SyncDBs()
if err != nil {
return
}
dbList.ForEach(func(db alpm.DB) error {
if len(pkgInputN) == 0 {
pkgs := db.PkgCache()
s = append(s, pkgs.Slice()...)
} else {
pkgs := db.Search(pkgInputN)
s = append(s, pkgs.Slice()...)
}
return nil
})
if config.SortMode == bottomUp {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
return
}
// PackageSlices separates an input slice into aur and repo slices
func packageSlices(toCheck []string) (aur []string, repo []string, err error) {
dbList, err := alpmHandle.SyncDBs()
if err != nil {
return
}
for _, _pkg := range toCheck {
db, name := splitDBFromName(_pkg)
found := false
if db == "aur" || mode == modeAUR {
aur = append(aur, _pkg)
continue
} else if db != "" || mode == modeRepo {
repo = append(repo, _pkg)
continue
}
_ = dbList.ForEach(func(db alpm.DB) error {
if db.Pkg(name) != nil {
found = true
return fmt.Errorf("")
}
return nil
})
if !found {
found = !dbList.FindGroupPkgs(name).Empty()
}
if found {
repo = append(repo, _pkg)
} else {
aur = append(aur, _pkg)
}
}
return
}
// HangingPackages returns a list of packages installed as deps
// and unneeded by the system
// removeOptional decides whether optional dependencies are counted or not
func hangingPackages(removeOptional bool) (hanging []string, err error) {
localDB, err := alpmHandle.LocalDB()
if err != nil {
return
}
// safePackages represents every package in the system in one of 3 states
// State = 0 - Remove package from the system
// State = 1 - Keep package in the system; need to iterate over dependencies
// State = 2 - Keep package and have iterated over dependencies
safePackages := make(map[string]uint8)
// provides stores a mapping from the provides name back to the original package name
provides := make(mapStringSet)
packages := localDB.PkgCache()
// Mark explicit dependencies and enumerate the provides list
setupResources := func(pkg alpm.Package) error {
if pkg.Reason() == alpm.PkgReasonExplicit {
safePackages[pkg.Name()] = 1
} else {
safePackages[pkg.Name()] = 0
}
pkg.Provides().ForEach(func(dep alpm.Depend) error {
provides.Add(dep.Name, pkg.Name())
return nil
})
return nil
}
packages.ForEach(setupResources)
iterateAgain := true
processDependencies := func(pkg alpm.Package) error {
if state := safePackages[pkg.Name()]; state == 0 || state == 2 {
return nil
}
safePackages[pkg.Name()] = 2
// Update state for dependencies
markDependencies := func(dep alpm.Depend) error {
// Don't assume a dependency is installed
state, ok := safePackages[dep.Name]
if !ok {
// Check if dep is a provides rather than actual package name
if pset, ok2 := provides[dep.Name]; ok2 {
for p := range pset {
if safePackages[p] == 0 {
iterateAgain = true
safePackages[p] = 1
}
}
}
return nil
}
if state == 0 {
iterateAgain = true
safePackages[dep.Name] = 1
}
return nil
}
pkg.Depends().ForEach(markDependencies)
if !removeOptional {
pkg.OptionalDepends().ForEach(markDependencies)
}
return nil
}
for iterateAgain {
iterateAgain = false
packages.ForEach(processDependencies)
}
// Build list of packages to be removed
packages.ForEach(func(pkg alpm.Package) error {
if safePackages[pkg.Name()] == 0 {
hanging = append(hanging, pkg.Name())
}
return nil
})
return
}
func lastBuildTime() (time.Time, error) {
var lastTime time.Time
pkgs, _, _, _, err := filterPackages()
if err != nil {
return lastTime, err
}
for _, pkg := range pkgs {
thisTime := pkg.BuildDate()
if thisTime.After(lastTime) {
lastTime = thisTime
}
}
return lastTime, nil
}
// Statistics returns statistics about packages installed in system
func statistics() (info struct {
Totaln int
Expln int
TotalSize int64
}, err error) {
var tS int64 // TotalSize
var nPkg int
var ePkg int
localDB, err := alpmHandle.LocalDB()
if err != nil {
return
}
for _, pkg := range localDB.PkgCache().Slice() {
tS += pkg.ISize()
nPkg++
if pkg.Reason() == 0 {
ePkg++
}
}
info = struct {
Totaln int
Expln int
TotalSize int64
}{
nPkg, ePkg, tS,
}
return
}
// Queries the aur for information about specified packages.
// All packages should be queried in a single rpc request except when the number
// of packages exceeds the number set in config.RequestSplitN.
// If the number does exceed config.RequestSplitN multiple rpc requests will be
// performed concurrently.
func aurInfo(names []string, warnings *aurWarnings) ([]*rpc.Pkg, error) {
info := make([]*rpc.Pkg, 0, len(names))
seen := make(map[string]int)
var mux sync.Mutex
var wg sync.WaitGroup
var errs MultiError
makeRequest := func(n, max int) {
defer wg.Done()
tempInfo, requestErr := rpc.Info(names[n:max])
errs.Add(requestErr)
if requestErr != nil {
return
}
mux.Lock()
for _, _i := range tempInfo {
i := _i
info = append(info, &i)
}
mux.Unlock()
}
for n := 0; n < len(names); n += config.RequestSplitN {
max := min(len(names), n+config.RequestSplitN)
wg.Add(1)
go makeRequest(n, max)
}
wg.Wait()
if err := errs.Return(); err != nil {
return info, err
}
for k, pkg := range info {
seen[pkg.Name] = k
}
for _, name := range names {
i, ok := seen[name]
if !ok {
warnings.Missing = append(warnings.Missing, name)
continue
}
pkg := info[i]
if pkg.Maintainer == "" {
warnings.Orphans = append(warnings.Orphans, name)
}
if pkg.OutOfDate != 0 {
warnings.OutOfDate = append(warnings.OutOfDate, name)
}
}
return info, nil
}
func aurInfoPrint(names []string) ([]*rpc.Pkg, error) {
fmt.Println(bold(cyan("::") + bold(" Querying AUR...")))
warnings := &aurWarnings{}
info, err := aurInfo(names, warnings)
if err != nil {
return info, err
}
warnings.print()
return info, nil
}