query.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. "github.com/Jguer/yay/v12/pkg/db"
  10. "github.com/Jguer/yay/v12/pkg/query"
  11. "github.com/Jguer/yay/v12/pkg/settings"
  12. "github.com/Jguer/yay/v12/pkg/settings/parser"
  13. "github.com/Jguer/yay/v12/pkg/stringset"
  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. // HangingPackages returns a list of packages installed as deps
  97. // and unneeded by the system
  98. // removeOptional decides whether optional dependencies are counted or not.
  99. func hangingPackages(removeOptional bool, dbExecutor db.Executor) (hanging []string) {
  100. // safePackages represents every package in the system in one of 3 states
  101. // State = 0 - Remove package from the system
  102. // State = 1 - Keep package in the system; need to iterate over dependencies
  103. // State = 2 - Keep package and have iterated over dependencies
  104. safePackages := make(map[string]uint8)
  105. // provides stores a mapping from the provides name back to the original package name
  106. provides := make(stringset.MapStringSet)
  107. packages := dbExecutor.LocalPackages()
  108. // Mark explicit dependencies and enumerate the provides list
  109. for _, pkg := range packages {
  110. if pkg.Reason() == alpm.PkgReasonExplicit {
  111. safePackages[pkg.Name()] = 1
  112. } else {
  113. safePackages[pkg.Name()] = 0
  114. }
  115. for _, dep := range dbExecutor.PackageProvides(pkg) {
  116. provides.Add(dep.Name, pkg.Name())
  117. }
  118. }
  119. iterateAgain := true
  120. for iterateAgain {
  121. iterateAgain = false
  122. for _, pkg := range packages {
  123. if state := safePackages[pkg.Name()]; state == 0 || state == 2 {
  124. continue
  125. }
  126. safePackages[pkg.Name()] = 2
  127. deps := dbExecutor.PackageDepends(pkg)
  128. if !removeOptional {
  129. deps = append(deps, dbExecutor.PackageOptionalDepends(pkg)...)
  130. }
  131. // Update state for dependencies
  132. for _, dep := range deps {
  133. // Don't assume a dependency is installed
  134. state, ok := safePackages[dep.Name]
  135. if !ok {
  136. // Check if dep is a provides rather than actual package name
  137. if pset, ok2 := provides[dep.Name]; ok2 {
  138. for p := range pset {
  139. if safePackages[p] == 0 {
  140. iterateAgain = true
  141. safePackages[p] = 1
  142. }
  143. }
  144. }
  145. continue
  146. }
  147. if state == 0 {
  148. iterateAgain = true
  149. safePackages[dep.Name] = 1
  150. }
  151. }
  152. }
  153. }
  154. // Build list of packages to be removed
  155. for _, pkg := range packages {
  156. if safePackages[pkg.Name()] == 0 {
  157. hanging = append(hanging, pkg.Name())
  158. }
  159. }
  160. return hanging
  161. }
  162. func getFolderSize(path string) (size int64) {
  163. _ = filepath.WalkDir(path, func(p string, entry fs.DirEntry, err error) error {
  164. info, _ := entry.Info()
  165. size += info.Size()
  166. return nil
  167. })
  168. return size
  169. }
  170. // Statistics returns statistics about packages installed in system.
  171. func statistics(cfg *settings.Configuration, dbExecutor db.Executor) (res struct {
  172. Totaln int
  173. Expln int
  174. TotalSize int64
  175. pacmanCaches map[string]int64
  176. yayCache int64
  177. },
  178. ) {
  179. for _, pkg := range dbExecutor.LocalPackages() {
  180. res.TotalSize += pkg.ISize()
  181. res.Totaln++
  182. if pkg.Reason() == alpm.PkgReasonExplicit {
  183. res.Expln++
  184. }
  185. }
  186. res.pacmanCaches = make(map[string]int64)
  187. for _, path := range cfg.Runtime.PacmanConf.CacheDir {
  188. res.pacmanCaches[path] = getFolderSize(path)
  189. }
  190. res.yayCache = getFolderSize(cfg.BuildDir)
  191. return
  192. }