depPool.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. package dep
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "sort"
  7. "strconv"
  8. "sync"
  9. "github.com/Jguer/aur"
  10. alpm "github.com/Jguer/go-alpm/v2"
  11. "github.com/leonelquinteros/gotext"
  12. "github.com/Jguer/yay/v11/pkg/db"
  13. "github.com/Jguer/yay/v11/pkg/query"
  14. "github.com/Jguer/yay/v11/pkg/settings"
  15. "github.com/Jguer/yay/v11/pkg/settings/parser"
  16. "github.com/Jguer/yay/v11/pkg/stringset"
  17. "github.com/Jguer/yay/v11/pkg/text"
  18. )
  19. type Pool struct {
  20. Targets []Target
  21. Explicit stringset.StringSet
  22. Repo map[string]db.IPackage
  23. Aur map[string]*query.Pkg
  24. AurCache map[string]*query.Pkg
  25. Groups []string
  26. AlpmExecutor db.Executor
  27. Warnings *query.AURWarnings
  28. aurClient aur.ClientInterface
  29. }
  30. func newPool(dbExecutor db.Executor, aurClient aur.ClientInterface) *Pool {
  31. dp := &Pool{
  32. Targets: []Target{},
  33. Explicit: map[string]struct{}{},
  34. Repo: map[string]alpm.IPackage{},
  35. Aur: map[string]*aur.Pkg{},
  36. AurCache: map[string]*aur.Pkg{},
  37. Groups: []string{},
  38. AlpmExecutor: dbExecutor,
  39. Warnings: nil,
  40. aurClient: aurClient,
  41. }
  42. return dp
  43. }
  44. // Includes db/ prefixes and group installs.
  45. func (dp *Pool) ResolveTargets(ctx context.Context, pkgs []string,
  46. mode parser.TargetMode,
  47. ignoreProviders, noConfirm, provides bool, rebuild string, splitN int, noDeps, noCheckDeps bool, assumeInstalled []string,
  48. ) error {
  49. // RPC requests are slow
  50. // Combine as many AUR package requests as possible into a single RPC call
  51. aurTargets := make(stringset.StringSet)
  52. pkgs = query.RemoveInvalidTargets(pkgs, mode)
  53. for _, pkg := range pkgs {
  54. target := ToTarget(pkg)
  55. // skip targets already satisfied
  56. // even if the user enters db/pkg and aur/pkg the latter will
  57. // still get skipped even if it's from a different database to
  58. // the one specified
  59. // this is how pacman behaves
  60. if dp.hasPackage(target.DepString()) || isInAssumeInstalled(target.DepString(), assumeInstalled) {
  61. continue
  62. }
  63. var foundPkg db.IPackage
  64. // aur/ prefix means we only check the aur
  65. if target.DB == "aur" || mode == parser.ModeAUR {
  66. dp.Targets = append(dp.Targets, target)
  67. aurTargets.Set(target.DepString())
  68. continue
  69. }
  70. // If there's a different prefix only look in that repo
  71. if target.DB != "" {
  72. foundPkg = dp.AlpmExecutor.SatisfierFromDB(target.DepString(), target.DB)
  73. } else {
  74. // otherwise find it in any repo
  75. foundPkg = dp.AlpmExecutor.SyncSatisfier(target.DepString())
  76. }
  77. if foundPkg != nil {
  78. dp.Targets = append(dp.Targets, target)
  79. dp.Explicit.Set(foundPkg.Name())
  80. dp.ResolveRepoDependency(foundPkg, noDeps)
  81. continue
  82. } else {
  83. // check for groups
  84. // currently we don't resolve the packages in a group
  85. // only check if the group exists
  86. // would be better to check the groups from singleDB if
  87. // the user specified a db but there's no easy way to do
  88. // it without making alpm_lists so don't bother for now
  89. // db/group is probably a rare use case
  90. groupPackages := dp.AlpmExecutor.PackagesFromGroup(target.Name)
  91. if len(groupPackages) > 0 {
  92. dp.Groups = append(dp.Groups, target.String())
  93. for _, pkg := range groupPackages {
  94. dp.Explicit.Set(pkg.Name())
  95. }
  96. continue
  97. }
  98. }
  99. // if there was no db prefix check the aur
  100. if target.DB == "" {
  101. aurTargets.Set(target.DepString())
  102. }
  103. dp.Targets = append(dp.Targets, target)
  104. }
  105. if len(aurTargets) > 0 && mode.AtLeastAUR() {
  106. return dp.resolveAURPackages(ctx, aurTargets, true, ignoreProviders,
  107. noConfirm, provides, rebuild, splitN, noDeps, noCheckDeps)
  108. }
  109. return nil
  110. }
  111. // Pseudo provides finder.
  112. // Try to find provides by performing a search of the package name
  113. // This effectively performs -Ss on each package
  114. // then runs -Si on each result to cache the information.
  115. //
  116. // For example if you were to -S yay then yay -Ss would give:
  117. // yay-git yay-bin yay realyog pacui pacui-git ruby-yard
  118. // These packages will all be added to the cache in case they are needed later
  119. // Ofcouse only the first three packages provide yay, the rest are just false
  120. // positives.
  121. //
  122. // This method increases dependency resolve time.
  123. func (dp *Pool) findProvides(ctx context.Context, pkgs stringset.StringSet) error {
  124. var (
  125. mux sync.Mutex
  126. wg sync.WaitGroup
  127. )
  128. doSearch := func(pkg string) {
  129. defer wg.Done()
  130. var (
  131. err error
  132. results []query.Pkg
  133. )
  134. results, err = dp.aurClient.Search(ctx, pkg, aur.Provides)
  135. if err != nil {
  136. return
  137. }
  138. for iR := range results {
  139. mux.Lock()
  140. if _, ok := dp.AurCache[results[iR].Name]; !ok {
  141. pkgs.Set(results[iR].Name)
  142. }
  143. mux.Unlock()
  144. }
  145. }
  146. for pkg := range pkgs {
  147. if dp.AlpmExecutor.LocalPackage(pkg) != nil {
  148. continue
  149. }
  150. wg.Add(1)
  151. text.Debugln("AUR RPC Search:", pkg)
  152. go doSearch(pkg)
  153. }
  154. wg.Wait()
  155. return nil
  156. }
  157. func (dp *Pool) cacheAURPackages(ctx context.Context, _pkgs stringset.StringSet, provides bool, splitN int) error {
  158. pkgs := _pkgs.Copy()
  159. toQuery := make([]string, 0)
  160. for pkg := range pkgs {
  161. if _, ok := dp.AurCache[pkg]; ok {
  162. pkgs.Remove(pkg)
  163. }
  164. }
  165. if len(pkgs) == 0 {
  166. return nil
  167. }
  168. if provides {
  169. err := dp.findProvides(ctx, pkgs)
  170. if err != nil {
  171. return err
  172. }
  173. }
  174. for pkg := range pkgs {
  175. if _, ok := dp.AurCache[pkg]; !ok {
  176. name, _, ver := splitDep(pkg)
  177. if ver != "" {
  178. toQuery = append(toQuery, name, name+"-"+ver)
  179. } else {
  180. toQuery = append(toQuery, name)
  181. }
  182. }
  183. }
  184. info, err := query.AURInfo(ctx, dp.aurClient, toQuery, dp.Warnings, splitN)
  185. if err != nil {
  186. return err
  187. }
  188. for i := range info {
  189. // Dump everything in cache just in case we need it later
  190. pkg := &info[i]
  191. dp.AurCache[pkg.Name] = pkg
  192. }
  193. return nil
  194. }
  195. // Compute dependency lists used in Package dep searching and ordering.
  196. // Order sensitive TOFIX.
  197. func ComputeCombinedDepList(pkg *aur.Pkg, noDeps, noCheckDeps bool) []string {
  198. combinedDepList := make([]string, 0, len(pkg.Depends)+len(pkg.MakeDepends)+len(pkg.CheckDepends))
  199. if !noDeps {
  200. combinedDepList = append(combinedDepList, pkg.Depends...)
  201. }
  202. combinedDepList = append(combinedDepList, pkg.MakeDepends...)
  203. if !noCheckDeps {
  204. combinedDepList = append(combinedDepList, pkg.CheckDepends...)
  205. }
  206. return combinedDepList
  207. }
  208. func (dp *Pool) resolveAURPackages(ctx context.Context,
  209. pkgs stringset.StringSet,
  210. explicit, ignoreProviders, noConfirm, provides bool,
  211. rebuild string, splitN int, noDeps, noCheckDeps bool,
  212. ) error {
  213. newPackages := make(stringset.StringSet)
  214. newAURPackages := make(stringset.StringSet)
  215. err := dp.cacheAURPackages(ctx, pkgs, provides, splitN)
  216. if err != nil {
  217. return err
  218. }
  219. if len(pkgs) == 0 {
  220. return nil
  221. }
  222. for name := range pkgs {
  223. _, ok := dp.Aur[name]
  224. if ok {
  225. continue
  226. }
  227. pkg := dp.findSatisfierAurCache(name, ignoreProviders, noConfirm, provides)
  228. if pkg == nil {
  229. continue
  230. }
  231. if explicit {
  232. dp.Explicit.Set(pkg.Name)
  233. }
  234. dp.Aur[pkg.Name] = pkg
  235. combinedDepList := ComputeCombinedDepList(pkg, noDeps, noCheckDeps)
  236. for _, dep := range combinedDepList {
  237. newPackages.Set(dep)
  238. }
  239. }
  240. for dep := range newPackages {
  241. if dp.hasSatisfier(dep) {
  242. continue
  243. }
  244. isInstalled := dp.AlpmExecutor.LocalSatisfierExists(dep)
  245. hm := settings.HideMenus
  246. settings.HideMenus = isInstalled
  247. repoPkg := dp.AlpmExecutor.SyncSatisfier(dep) // has satisfier in repo: fetch it
  248. settings.HideMenus = hm
  249. if isInstalled && (rebuild != "tree" || repoPkg != nil) {
  250. continue
  251. }
  252. if repoPkg != nil {
  253. dp.ResolveRepoDependency(repoPkg, false)
  254. continue
  255. }
  256. // assume it's in the aur
  257. // ditch the versioning because the RPC can't handle it
  258. newAURPackages.Set(dep)
  259. }
  260. err = dp.resolveAURPackages(ctx, newAURPackages, false, ignoreProviders,
  261. noConfirm, provides, rebuild, splitN, noDeps, noCheckDeps)
  262. return err
  263. }
  264. func (dp *Pool) ResolveRepoDependency(pkg db.IPackage, noDeps bool) {
  265. dp.Repo[pkg.Name()] = pkg
  266. if noDeps {
  267. return
  268. }
  269. for _, dep := range dp.AlpmExecutor.PackageDepends(pkg) {
  270. if dp.hasSatisfier(dep.String()) {
  271. continue
  272. }
  273. // has satisfier installed: skip
  274. if dp.AlpmExecutor.LocalSatisfierExists(dep.String()) {
  275. continue
  276. }
  277. // has satisfier in repo: fetch it
  278. if repoPkg := dp.AlpmExecutor.SyncSatisfier(dep.String()); repoPkg != nil {
  279. dp.ResolveRepoDependency(repoPkg, noDeps)
  280. }
  281. }
  282. }
  283. func GetPool(ctx context.Context, pkgs []string,
  284. warnings *query.AURWarnings,
  285. dbExecutor db.Executor,
  286. aurClient aur.ClientInterface,
  287. mode parser.TargetMode,
  288. ignoreProviders, noConfirm, provides bool,
  289. rebuild string, splitN int, noDeps bool, noCheckDeps bool, assumeInstalled []string,
  290. ) (*Pool, error) {
  291. dp := newPool(dbExecutor, aurClient)
  292. dp.Warnings = warnings
  293. err := dp.ResolveTargets(ctx, pkgs, mode, ignoreProviders, noConfirm, provides,
  294. rebuild, splitN, noDeps, noCheckDeps, assumeInstalled)
  295. return dp, err
  296. }
  297. func (dp *Pool) findSatisfierAur(dep string) *query.Pkg {
  298. for _, pkg := range dp.Aur {
  299. if satisfiesAur(dep, pkg) {
  300. return pkg
  301. }
  302. }
  303. return nil
  304. }
  305. // This is mostly used to promote packages from the cache
  306. // to the Install list
  307. // Provide a pacman style provider menu if there's more than one candidate
  308. // This acts slightly differently from Pacman, It will give
  309. // a menu even if a package with a matching name exists. I believe this
  310. // method is better because most of the time you are choosing between
  311. // foo and foo-git.
  312. // Using Pacman's ways trying to install foo would never give you
  313. // a menu.
  314. // TODO: maybe intermix repo providers in the menu.
  315. func (dp *Pool) findSatisfierAurCache(dep string, ignoreProviders, noConfirm, provides bool) *query.Pkg {
  316. depName, _, _ := splitDep(dep)
  317. seen := make(stringset.StringSet)
  318. providerSlice := makeProviders(depName)
  319. if dp.AlpmExecutor.LocalPackage(depName) != nil {
  320. if pkg, ok := dp.AurCache[dep]; ok && pkgSatisfies(pkg.Name, pkg.Version, dep) {
  321. return pkg
  322. }
  323. }
  324. if ignoreProviders {
  325. for _, pkg := range dp.AurCache {
  326. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  327. for _, target := range dp.Targets {
  328. if target.Name == pkg.Name {
  329. return pkg
  330. }
  331. }
  332. }
  333. }
  334. }
  335. for _, pkg := range dp.AurCache {
  336. if seen.Get(pkg.Name) {
  337. continue
  338. }
  339. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  340. providerSlice.Pkgs = append(providerSlice.Pkgs, pkg)
  341. seen.Set(pkg.Name)
  342. continue
  343. }
  344. for _, provide := range pkg.Provides {
  345. if provideSatisfies(provide, dep, pkg.Version) {
  346. providerSlice.Pkgs = append(providerSlice.Pkgs, pkg)
  347. seen.Set(pkg.Name)
  348. continue
  349. }
  350. }
  351. }
  352. if !provides && providerSlice.Len() >= 1 {
  353. return providerSlice.Pkgs[0]
  354. }
  355. if providerSlice.Len() == 1 {
  356. return providerSlice.Pkgs[0]
  357. }
  358. if providerSlice.Len() > 1 {
  359. sort.Sort(providerSlice)
  360. return providerMenu(dep, providerSlice, noConfirm)
  361. }
  362. return nil
  363. }
  364. func (dp *Pool) findSatisfierRepo(dep string) db.IPackage {
  365. for _, pkg := range dp.Repo {
  366. if satisfiesRepo(dep, pkg, dp.AlpmExecutor) {
  367. return pkg
  368. }
  369. }
  370. return nil
  371. }
  372. func (dp *Pool) hasSatisfier(dep string) bool {
  373. return dp.findSatisfierRepo(dep) != nil || dp.findSatisfierAur(dep) != nil
  374. }
  375. func (dp *Pool) hasPackage(name string) bool {
  376. for _, pkg := range dp.Repo {
  377. if pkg.Name() == name {
  378. return true
  379. }
  380. }
  381. for _, pkg := range dp.Aur {
  382. if pkg.Name == name {
  383. return true
  384. }
  385. }
  386. for _, pkg := range dp.Groups {
  387. if pkg == name {
  388. return true
  389. }
  390. }
  391. return false
  392. }
  393. func isInAssumeInstalled(name string, assumeInstalled []string) bool {
  394. for _, pkgAndVersion := range assumeInstalled {
  395. assumeName, _, _ := splitDep(pkgAndVersion)
  396. depName, _, _ := splitDep(name)
  397. if assumeName == depName {
  398. return true
  399. }
  400. }
  401. return false
  402. }
  403. func providerMenu(dep string, providers providers, noConfirm bool) *query.Pkg {
  404. size := providers.Len()
  405. str := text.Bold(gotext.Get("There are %d providers available for %s:", size, dep))
  406. str += "\n"
  407. size = 1
  408. str += text.SprintOperationInfo(gotext.Get("Repository AUR"), "\n ")
  409. for _, pkg := range providers.Pkgs {
  410. str += fmt.Sprintf("%d) %s ", size, pkg.Name)
  411. size++
  412. }
  413. text.OperationInfoln(str)
  414. for {
  415. fmt.Println(gotext.Get("\nEnter a number (default=1): "))
  416. if noConfirm {
  417. fmt.Println("1")
  418. return providers.Pkgs[0]
  419. }
  420. numberBuf, err := text.GetInput("", false)
  421. if err != nil {
  422. fmt.Fprintln(os.Stderr, err)
  423. break
  424. }
  425. if numberBuf == "" {
  426. return providers.Pkgs[0]
  427. }
  428. num, err := strconv.Atoi(numberBuf)
  429. if err != nil {
  430. text.Errorln(gotext.Get("invalid number: %s", numberBuf))
  431. continue
  432. }
  433. if num < 1 || num >= size {
  434. text.Errorln(gotext.Get("invalid value: %d is not between %d and %d", num, 1, size-1))
  435. continue
  436. }
  437. return providers.Pkgs[num-1]
  438. }
  439. return nil
  440. }