query.go 5.5 KB

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