query.go 5.5 KB

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