query.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "io/fs"
  6. "path/filepath"
  7. aur "github.com/Jguer/aur"
  8. alpm "github.com/Jguer/go-alpm/v2"
  9. mapset "github.com/deckarep/golang-set/v2"
  10. "github.com/Jguer/yay/v12/pkg/db"
  11. "github.com/Jguer/yay/v12/pkg/query"
  12. "github.com/Jguer/yay/v12/pkg/settings"
  13. "github.com/Jguer/yay/v12/pkg/settings/parser"
  14. "github.com/Jguer/yay/v12/pkg/text"
  15. )
  16. // SyncSearch presents a query to the local repos and to the AUR.
  17. func syncSearch(ctx context.Context, pkgS []string,
  18. dbExecutor db.Executor, queryBuilder query.Builder, verbose bool,
  19. ) error {
  20. queryBuilder.Execute(ctx, dbExecutor, pkgS)
  21. searchMode := query.Minimal
  22. if verbose {
  23. searchMode = query.Detailed
  24. }
  25. return queryBuilder.Results(dbExecutor, searchMode)
  26. }
  27. // SyncInfo serves as a pacman -Si for repo packages and AUR packages.
  28. func syncInfo(ctx context.Context, cfg *settings.Configuration,
  29. cmdArgs *parser.Arguments, pkgS []string, dbExecutor db.Executor,
  30. ) error {
  31. var (
  32. info []aur.Pkg
  33. err error
  34. missing = false
  35. )
  36. pkgS = query.RemoveInvalidTargets(pkgS, cfg.Mode)
  37. aurS, repoS := packageSlices(pkgS, cfg, dbExecutor)
  38. if len(aurS) != 0 {
  39. noDB := make([]string, 0, len(aurS))
  40. for _, pkg := range aurS {
  41. _, name := text.SplitDBFromName(pkg)
  42. noDB = append(noDB, name)
  43. }
  44. info, err = cfg.Runtime.AURClient.Get(ctx, &aur.Query{
  45. Needles: noDB,
  46. By: aur.Name,
  47. })
  48. if err != nil {
  49. missing = true
  50. cfg.Runtime.Logger.Errorln(err)
  51. }
  52. }
  53. if len(repoS) != 0 {
  54. arguments := cmdArgs.Copy()
  55. arguments.ClearTargets()
  56. arguments.AddTarget(repoS...)
  57. err = cfg.Runtime.CmdBuilder.Show(cfg.Runtime.CmdBuilder.BuildPacmanCmd(ctx,
  58. arguments, cfg.Mode, settings.NoConfirm))
  59. if err != nil {
  60. return err
  61. }
  62. }
  63. if len(aurS) != len(info) {
  64. missing = true
  65. }
  66. if len(info) != 0 {
  67. for i := range info {
  68. printInfo(cfg, &info[i], cmdArgs.ExistsDouble("i"))
  69. }
  70. }
  71. if missing {
  72. err = fmt.Errorf("")
  73. }
  74. return err
  75. }
  76. // PackageSlices separates an input slice into aur and repo slices.
  77. func packageSlices(toCheck []string, config *settings.Configuration, dbExecutor db.Executor) (aurNames, repoNames []string) {
  78. for _, _pkg := range toCheck {
  79. dbName, name := text.SplitDBFromName(_pkg)
  80. if dbName == "aur" || config.Mode == parser.ModeAUR {
  81. aurNames = append(aurNames, _pkg)
  82. continue
  83. } else if dbName != "" || config.Mode == parser.ModeRepo {
  84. repoNames = append(repoNames, _pkg)
  85. continue
  86. }
  87. if dbExecutor.SyncSatisfierExists(name) ||
  88. len(dbExecutor.PackagesFromGroup(name)) != 0 {
  89. repoNames = append(repoNames, _pkg)
  90. } else {
  91. aurNames = append(aurNames, _pkg)
  92. }
  93. }
  94. return aurNames, repoNames
  95. }
  96. // MapSetMap is a Map of Sets.
  97. type mapSetMap[T comparable] map[T]mapset.Set[T]
  98. // Add adds a new value to the Map.
  99. // If n is already in the map, then v is appended to the StringSet under that key.
  100. // Otherwise a new Set is created containing v.
  101. func (mss mapSetMap[T]) Add(n, v T) {
  102. if _, ok := mss[n]; !ok {
  103. mss[n] = mapset.NewSet[T]()
  104. }
  105. mss[n].Add(v)
  106. }
  107. // HangingPackages returns a list of packages installed as deps
  108. // and unneeded by the system
  109. // removeOptional decides whether optional dependencies are counted or not.
  110. func hangingPackages(removeOptional bool, dbExecutor db.Executor) (hanging []string) {
  111. // safePackages represents every package in the system in one of 3 states
  112. // State = 0 - Remove package from the system
  113. // State = 1 - Keep package in the system; need to iterate over dependencies
  114. // State = 2 - Keep package and have iterated over dependencies
  115. safePackages := make(map[string]uint8)
  116. // provides stores a mapping from the provides name back to the original package name
  117. provides := make(mapSetMap[string])
  118. packages := dbExecutor.LocalPackages()
  119. // Mark explicit dependencies and enumerate the provides list
  120. for _, pkg := range packages {
  121. if pkg.Reason() == alpm.PkgReasonExplicit {
  122. safePackages[pkg.Name()] = 1
  123. } else {
  124. safePackages[pkg.Name()] = 0
  125. }
  126. for _, dep := range dbExecutor.PackageProvides(pkg) {
  127. provides.Add(dep.Name, pkg.Name())
  128. }
  129. }
  130. iterateAgain := true
  131. for iterateAgain {
  132. iterateAgain = false
  133. for _, pkg := range packages {
  134. if state := safePackages[pkg.Name()]; state == 0 || state == 2 {
  135. continue
  136. }
  137. safePackages[pkg.Name()] = 2
  138. deps := dbExecutor.PackageDepends(pkg)
  139. if !removeOptional {
  140. deps = append(deps, dbExecutor.PackageOptionalDepends(pkg)...)
  141. }
  142. // Update state for dependencies
  143. for _, dep := range deps {
  144. // Don't assume a dependency is installed
  145. state, ok := safePackages[dep.Name]
  146. if !ok {
  147. // Check if dep is a provides rather than actual package name
  148. if pset, ok2 := provides[dep.Name]; ok2 {
  149. for p := range pset.Iter() {
  150. if safePackages[p] == 0 {
  151. iterateAgain = true
  152. safePackages[p] = 1
  153. }
  154. }
  155. }
  156. continue
  157. }
  158. if state == 0 {
  159. iterateAgain = true
  160. safePackages[dep.Name] = 1
  161. }
  162. }
  163. }
  164. }
  165. // Build list of packages to be removed
  166. for _, pkg := range packages {
  167. if safePackages[pkg.Name()] == 0 {
  168. hanging = append(hanging, pkg.Name())
  169. }
  170. }
  171. return hanging
  172. }
  173. func getFolderSize(path string) (size int64) {
  174. _ = filepath.WalkDir(path, func(p string, entry fs.DirEntry, err error) error {
  175. info, _ := entry.Info()
  176. size += info.Size()
  177. return nil
  178. })
  179. return size
  180. }
  181. // Statistics returns statistics about packages installed in system.
  182. func statistics(cfg *settings.Configuration, dbExecutor db.Executor) (res struct {
  183. Totaln int
  184. Expln int
  185. TotalSize int64
  186. pacmanCaches map[string]int64
  187. yayCache int64
  188. },
  189. ) {
  190. for _, pkg := range dbExecutor.LocalPackages() {
  191. res.TotalSize += pkg.ISize()
  192. res.Totaln++
  193. if pkg.Reason() == alpm.PkgReasonExplicit {
  194. res.Expln++
  195. }
  196. }
  197. res.pacmanCaches = make(map[string]int64)
  198. for _, path := range cfg.Runtime.PacmanConf.CacheDir {
  199. res.pacmanCaches[path] = getFolderSize(path)
  200. }
  201. res.yayCache = getFolderSize(cfg.BuildDir)
  202. return
  203. }