depPool.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 _, pkg := range info {
  189. // Dump everything in cache just in case we need it later
  190. dp.AurCache[pkg.Name] = pkg
  191. }
  192. return nil
  193. }
  194. // Compute dependency lists used in Package dep searching and ordering.
  195. // Order sensitive TOFIX.
  196. func ComputeCombinedDepList(pkg *aur.Pkg, noDeps, noCheckDeps bool) []string {
  197. combinedDepList := make([]string, 0, len(pkg.Depends)+len(pkg.MakeDepends)+len(pkg.CheckDepends))
  198. if !noDeps {
  199. combinedDepList = append(combinedDepList, pkg.Depends...)
  200. }
  201. combinedDepList = append(combinedDepList, pkg.MakeDepends...)
  202. if !noCheckDeps {
  203. combinedDepList = append(combinedDepList, pkg.CheckDepends...)
  204. }
  205. return combinedDepList
  206. }
  207. func (dp *Pool) resolveAURPackages(ctx context.Context,
  208. pkgs stringset.StringSet,
  209. explicit, ignoreProviders, noConfirm, provides bool,
  210. rebuild string, splitN int, noDeps, noCheckDeps bool,
  211. ) error {
  212. newPackages := make(stringset.StringSet)
  213. newAURPackages := make(stringset.StringSet)
  214. err := dp.cacheAURPackages(ctx, pkgs, provides, splitN)
  215. if err != nil {
  216. return err
  217. }
  218. if len(pkgs) == 0 {
  219. return nil
  220. }
  221. for name := range pkgs {
  222. _, ok := dp.Aur[name]
  223. if ok {
  224. continue
  225. }
  226. pkg := dp.findSatisfierAurCache(name, ignoreProviders, noConfirm, provides)
  227. if pkg == nil {
  228. continue
  229. }
  230. if explicit {
  231. dp.Explicit.Set(pkg.Name)
  232. }
  233. dp.Aur[pkg.Name] = pkg
  234. combinedDepList := ComputeCombinedDepList(pkg, noDeps, noCheckDeps)
  235. for _, dep := range combinedDepList {
  236. newPackages.Set(dep)
  237. }
  238. }
  239. for dep := range newPackages {
  240. if dp.hasSatisfier(dep) {
  241. continue
  242. }
  243. isInstalled := dp.AlpmExecutor.LocalSatisfierExists(dep)
  244. hm := settings.HideMenus
  245. settings.HideMenus = isInstalled
  246. repoPkg := dp.AlpmExecutor.SyncSatisfier(dep) // has satisfier in repo: fetch it
  247. settings.HideMenus = hm
  248. if isInstalled && (rebuild != "tree" || repoPkg != nil) {
  249. continue
  250. }
  251. if repoPkg != nil {
  252. dp.ResolveRepoDependency(repoPkg, false)
  253. continue
  254. }
  255. // assume it's in the aur
  256. // ditch the versioning because the RPC can't handle it
  257. newAURPackages.Set(dep)
  258. }
  259. err = dp.resolveAURPackages(ctx, newAURPackages, false, ignoreProviders,
  260. noConfirm, provides, rebuild, splitN, noDeps, noCheckDeps)
  261. return err
  262. }
  263. func (dp *Pool) ResolveRepoDependency(pkg db.IPackage, noDeps bool) {
  264. dp.Repo[pkg.Name()] = pkg
  265. if noDeps {
  266. return
  267. }
  268. for _, dep := range dp.AlpmExecutor.PackageDepends(pkg) {
  269. if dp.hasSatisfier(dep.String()) {
  270. continue
  271. }
  272. // has satisfier installed: skip
  273. if dp.AlpmExecutor.LocalSatisfierExists(dep.String()) {
  274. continue
  275. }
  276. // has satisfier in repo: fetch it
  277. if repoPkg := dp.AlpmExecutor.SyncSatisfier(dep.String()); repoPkg != nil {
  278. dp.ResolveRepoDependency(repoPkg, noDeps)
  279. }
  280. }
  281. }
  282. func GetPool(ctx context.Context, pkgs []string,
  283. warnings *query.AURWarnings,
  284. dbExecutor db.Executor,
  285. aurClient aur.ClientInterface,
  286. mode parser.TargetMode,
  287. ignoreProviders, noConfirm, provides bool,
  288. rebuild string, splitN int, noDeps bool, noCheckDeps bool, assumeInstalled []string,
  289. ) (*Pool, error) {
  290. dp := newPool(dbExecutor, aurClient)
  291. dp.Warnings = warnings
  292. err := dp.ResolveTargets(ctx, pkgs, mode, ignoreProviders, noConfirm, provides,
  293. rebuild, splitN, noDeps, noCheckDeps, assumeInstalled)
  294. return dp, err
  295. }
  296. func (dp *Pool) findSatisfierAur(dep string) *query.Pkg {
  297. for _, pkg := range dp.Aur {
  298. if satisfiesAur(dep, pkg) {
  299. return pkg
  300. }
  301. }
  302. return nil
  303. }
  304. // This is mostly used to promote packages from the cache
  305. // to the Install list
  306. // Provide a pacman style provider menu if there's more than one candidate
  307. // This acts slightly differently from Pacman, It will give
  308. // a menu even if a package with a matching name exists. I believe this
  309. // method is better because most of the time you are choosing between
  310. // foo and foo-git.
  311. // Using Pacman's ways trying to install foo would never give you
  312. // a menu.
  313. // TODO: maybe intermix repo providers in the menu.
  314. func (dp *Pool) findSatisfierAurCache(dep string, ignoreProviders, noConfirm, provides bool) *query.Pkg {
  315. depName, _, _ := splitDep(dep)
  316. seen := make(stringset.StringSet)
  317. providerSlice := makeProviders(depName)
  318. if dp.AlpmExecutor.LocalPackage(depName) != nil {
  319. if pkg, ok := dp.AurCache[dep]; ok && pkgSatisfies(pkg.Name, pkg.Version, dep) {
  320. return pkg
  321. }
  322. }
  323. if ignoreProviders {
  324. for _, pkg := range dp.AurCache {
  325. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  326. for _, target := range dp.Targets {
  327. if target.Name == pkg.Name {
  328. return pkg
  329. }
  330. }
  331. }
  332. }
  333. }
  334. for _, pkg := range dp.AurCache {
  335. if seen.Get(pkg.Name) {
  336. continue
  337. }
  338. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  339. providerSlice.Pkgs = append(providerSlice.Pkgs, pkg)
  340. seen.Set(pkg.Name)
  341. continue
  342. }
  343. for _, provide := range pkg.Provides {
  344. if provideSatisfies(provide, dep, pkg.Version) {
  345. providerSlice.Pkgs = append(providerSlice.Pkgs, pkg)
  346. seen.Set(pkg.Name)
  347. continue
  348. }
  349. }
  350. }
  351. if !provides && providerSlice.Len() >= 1 {
  352. return providerSlice.Pkgs[0]
  353. }
  354. if providerSlice.Len() == 1 {
  355. return providerSlice.Pkgs[0]
  356. }
  357. if providerSlice.Len() > 1 {
  358. sort.Sort(providerSlice)
  359. return providerMenu(dep, providerSlice, noConfirm)
  360. }
  361. return nil
  362. }
  363. func (dp *Pool) findSatisfierRepo(dep string) db.IPackage {
  364. for _, pkg := range dp.Repo {
  365. if satisfiesRepo(dep, pkg, dp.AlpmExecutor) {
  366. return pkg
  367. }
  368. }
  369. return nil
  370. }
  371. func (dp *Pool) hasSatisfier(dep string) bool {
  372. return dp.findSatisfierRepo(dep) != nil || dp.findSatisfierAur(dep) != nil
  373. }
  374. func (dp *Pool) hasPackage(name string) bool {
  375. for _, pkg := range dp.Repo {
  376. if pkg.Name() == name {
  377. return true
  378. }
  379. }
  380. for _, pkg := range dp.Aur {
  381. if pkg.Name == name {
  382. return true
  383. }
  384. }
  385. for _, pkg := range dp.Groups {
  386. if pkg == name {
  387. return true
  388. }
  389. }
  390. return false
  391. }
  392. func isInAssumeInstalled(name string, assumeInstalled []string) bool {
  393. for _, pkgAndVersion := range assumeInstalled {
  394. assumeName, _, _ := splitDep(pkgAndVersion)
  395. depName, _, _ := splitDep(name)
  396. if assumeName == depName {
  397. return true
  398. }
  399. }
  400. return false
  401. }
  402. func providerMenu(dep string, providers providers, noConfirm bool) *query.Pkg {
  403. size := providers.Len()
  404. str := text.Bold(gotext.Get("There are %d providers available for %s:", size, dep))
  405. str += "\n"
  406. size = 1
  407. str += text.SprintOperationInfo(gotext.Get("Repository AUR"), "\n ")
  408. for _, pkg := range providers.Pkgs {
  409. str += fmt.Sprintf("%d) %s ", size, pkg.Name)
  410. size++
  411. }
  412. text.OperationInfoln(str)
  413. for {
  414. fmt.Println(gotext.Get("\nEnter a number (default=1): "))
  415. if noConfirm {
  416. fmt.Println("1")
  417. return providers.Pkgs[0]
  418. }
  419. numberBuf, err := text.GetInput("", false)
  420. if err != nil {
  421. fmt.Fprintln(os.Stderr, err)
  422. break
  423. }
  424. if numberBuf == "" {
  425. return providers.Pkgs[0]
  426. }
  427. num, err := strconv.Atoi(numberBuf)
  428. if err != nil {
  429. text.Errorln(gotext.Get("invalid number: %s", numberBuf))
  430. continue
  431. }
  432. if num < 1 || num >= size {
  433. text.Errorln(gotext.Get("invalid value: %d is not between %d and %d", num, 1, size-1))
  434. continue
  435. }
  436. return providers.Pkgs[num-1]
  437. }
  438. return nil
  439. }