-
-
Notifications
You must be signed in to change notification settings - Fork 362
/
query.go
257 lines (210 loc) · 6.16 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
package main
import (
"context"
"fmt"
"io/fs"
"path/filepath"
aur "github.com/Jguer/aur"
alpm "github.com/Jguer/go-alpm/v2"
mapset "github.com/deckarep/golang-set/v2"
"github.com/Jguer/yay/v12/pkg/db"
"github.com/Jguer/yay/v12/pkg/query"
"github.com/Jguer/yay/v12/pkg/runtime"
"github.com/Jguer/yay/v12/pkg/settings"
"github.com/Jguer/yay/v12/pkg/settings/parser"
"github.com/Jguer/yay/v12/pkg/text"
)
// SyncSearch presents a query to the local repos and to the AUR.
func syncSearch(ctx context.Context, pkgS []string,
dbExecutor db.Executor, queryBuilder query.Builder, verbose bool,
) error {
queryBuilder.Execute(ctx, dbExecutor, pkgS)
searchMode := query.Minimal
if verbose {
searchMode = query.Detailed
}
return queryBuilder.Results(dbExecutor, searchMode)
}
// SyncInfo serves as a pacman -Si for repo packages and AUR packages.
func syncInfo(ctx context.Context, run *runtime.Runtime,
cmdArgs *parser.Arguments, pkgS []string, dbExecutor db.Executor,
) error {
var (
info []aur.Pkg
err error
missing = false
)
pkgS = query.RemoveInvalidTargets(run.Logger, pkgS, run.Cfg.Mode)
aurS, repoS := packageSlices(pkgS, run.Cfg, dbExecutor)
if len(repoS) == 0 && len(aurS) == 0 {
if run.Cfg.Mode != parser.ModeRepo {
aurS = dbExecutor.InstalledRemotePackageNames()
}
if run.Cfg.Mode != parser.ModeAUR {
repoS = dbExecutor.InstalledSyncPackageNames()
}
}
if len(aurS) != 0 {
noDB := make([]string, 0, len(aurS))
for _, pkg := range aurS {
_, name := text.SplitDBFromName(pkg)
noDB = append(noDB, name)
}
info, err = run.AURClient.Get(ctx, &aur.Query{
Needles: noDB,
By: aur.Name,
})
if err != nil {
missing = true
run.Logger.Errorln(err)
}
}
if len(repoS) != 0 || (len(aurS) == 0 && len(repoS) == 0) {
arguments := cmdArgs.Copy()
arguments.ClearTargets()
arguments.AddTarget(repoS...)
err = run.CmdBuilder.Show(run.CmdBuilder.BuildPacmanCmd(ctx,
arguments, run.Cfg.Mode, settings.NoConfirm))
if err != nil {
return err
}
}
if len(aurS) != len(info) {
missing = true
}
for i := range info {
printInfo(run.Logger, run.Cfg, &info[i], cmdArgs.ExistsDouble("i"))
}
if missing {
err = fmt.Errorf("")
}
return err
}
// PackageSlices separates an input slice into aur and repo slices.
func packageSlices(toCheck []string, config *settings.Configuration, dbExecutor db.Executor) (aurNames, repoNames []string) {
for _, _pkg := range toCheck {
dbName, name := text.SplitDBFromName(_pkg)
if dbName == "aur" || config.Mode == parser.ModeAUR {
aurNames = append(aurNames, _pkg)
continue
} else if dbName != "" || config.Mode == parser.ModeRepo {
repoNames = append(repoNames, _pkg)
continue
}
if dbExecutor.SyncSatisfierExists(name) ||
len(dbExecutor.PackagesFromGroup(name)) != 0 {
repoNames = append(repoNames, _pkg)
} else {
aurNames = append(aurNames, _pkg)
}
}
return aurNames, repoNames
}
// MapSetMap is a Map of Sets.
type mapSetMap[T comparable] map[T]mapset.Set[T]
// Add adds a new value to the Map.
// If n is already in the map, then v is appended to the StringSet under that key.
// Otherwise a new Set is created containing v.
func (mss mapSetMap[T]) Add(n, v T) {
if _, ok := mss[n]; !ok {
mss[n] = mapset.NewSet[T]()
}
mss[n].Add(v)
}
// 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, dbExecutor db.Executor) (hanging []string) {
// 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(mapSetMap[string])
packages := dbExecutor.LocalPackages()
// Mark explicit dependencies and enumerate the provides list
for _, pkg := range packages {
if pkg.Reason() == alpm.PkgReasonExplicit {
safePackages[pkg.Name()] = 1
} else {
safePackages[pkg.Name()] = 0
}
for _, dep := range dbExecutor.PackageProvides(pkg) {
provides.Add(dep.Name, pkg.Name())
}
}
iterateAgain := true
for iterateAgain {
iterateAgain = false
for _, pkg := range packages {
if state := safePackages[pkg.Name()]; state == 0 || state == 2 {
continue
}
safePackages[pkg.Name()] = 2
deps := dbExecutor.PackageDepends(pkg)
if !removeOptional {
deps = append(deps, dbExecutor.PackageOptionalDepends(pkg)...)
}
// Update state for dependencies
for _, dep := range deps {
// 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.Iter() {
if safePackages[p] == 0 {
iterateAgain = true
safePackages[p] = 1
}
}
}
continue
}
if state == 0 {
iterateAgain = true
safePackages[dep.Name] = 1
}
}
}
}
// Build list of packages to be removed
for _, pkg := range packages {
if safePackages[pkg.Name()] == 0 {
hanging = append(hanging, pkg.Name())
}
}
return hanging
}
func getFolderSize(path string) (size int64) {
_ = filepath.WalkDir(path, func(p string, entry fs.DirEntry, err error) error {
info, _ := entry.Info()
size += info.Size()
return nil
})
return size
}
// Statistics returns statistics about packages installed in system.
func statistics(run *runtime.Runtime, dbExecutor db.Executor) (res struct {
Totaln int
Expln int
TotalSize int64
pacmanCaches map[string]int64
yayCache int64
},
) {
for _, pkg := range dbExecutor.LocalPackages() {
res.TotalSize += pkg.ISize()
res.Totaln++
if pkg.Reason() == alpm.PkgReasonExplicit {
res.Expln++
}
}
res.pacmanCaches = make(map[string]int64)
for _, path := range run.PacmanConf.CacheDir {
res.pacmanCaches[path] = getFolderSize(path)
}
res.yayCache = getFolderSize(run.Cfg.BuildDir)
return
}