depCheck.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package dep
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "sync"
  7. "github.com/leonelquinteros/gotext"
  8. "github.com/Jguer/yay/v10/pkg/stringset"
  9. "github.com/Jguer/yay/v10/pkg/text"
  10. )
  11. func (dp *Pool) checkInnerConflict(name, conflict string, conflicts stringset.MapStringSet) {
  12. for _, pkg := range dp.Aur {
  13. if pkg.Name == name {
  14. continue
  15. }
  16. if satisfiesAur(conflict, pkg) {
  17. conflicts.Add(name, pkg.Name)
  18. }
  19. }
  20. for _, pkg := range dp.Repo {
  21. if pkg.Name() == name {
  22. continue
  23. }
  24. if satisfiesRepo(conflict, pkg, dp.AlpmExecutor) {
  25. conflicts.Add(name, pkg.Name())
  26. }
  27. }
  28. }
  29. func (dp *Pool) checkForwardConflict(name, conflict string, conflicts stringset.MapStringSet) {
  30. for _, pkg := range dp.AlpmExecutor.LocalPackages() {
  31. if pkg.Name() == name || dp.hasPackage(pkg.Name()) {
  32. continue
  33. }
  34. if satisfiesRepo(conflict, pkg, dp.AlpmExecutor) {
  35. n := pkg.Name()
  36. if n != conflict {
  37. n += " (" + conflict + ")"
  38. }
  39. conflicts.Add(name, n)
  40. }
  41. }
  42. }
  43. func (dp *Pool) checkReverseConflict(name, conflict string, conflicts stringset.MapStringSet) {
  44. for _, pkg := range dp.Aur {
  45. if pkg.Name == name {
  46. continue
  47. }
  48. if satisfiesAur(conflict, pkg) {
  49. if name != conflict {
  50. name += " (" + conflict + ")"
  51. }
  52. conflicts.Add(pkg.Name, name)
  53. }
  54. }
  55. for _, pkg := range dp.Repo {
  56. if pkg.Name() == name {
  57. continue
  58. }
  59. if satisfiesRepo(conflict, pkg, dp.AlpmExecutor) {
  60. if name != conflict {
  61. name += " (" + conflict + ")"
  62. }
  63. conflicts.Add(pkg.Name(), name)
  64. }
  65. }
  66. }
  67. func (dp *Pool) checkInnerConflicts(conflicts stringset.MapStringSet) {
  68. for _, pkg := range dp.Aur {
  69. for _, conflict := range pkg.Conflicts {
  70. dp.checkInnerConflict(pkg.Name, conflict, conflicts)
  71. }
  72. }
  73. for _, pkg := range dp.Repo {
  74. for _, conflict := range dp.AlpmExecutor.PackageConflicts(pkg) {
  75. dp.checkInnerConflict(pkg.Name(), conflict.String(), conflicts)
  76. }
  77. }
  78. }
  79. func (dp *Pool) checkForwardConflicts(conflicts stringset.MapStringSet) {
  80. for _, pkg := range dp.Aur {
  81. for _, conflict := range pkg.Conflicts {
  82. dp.checkForwardConflict(pkg.Name, conflict, conflicts)
  83. }
  84. }
  85. for _, pkg := range dp.Repo {
  86. for _, conflict := range dp.AlpmExecutor.PackageConflicts(pkg) {
  87. dp.checkForwardConflict(pkg.Name(), conflict.String(), conflicts)
  88. }
  89. }
  90. }
  91. func (dp *Pool) checkReverseConflicts(conflicts stringset.MapStringSet) {
  92. for _, pkg := range dp.AlpmExecutor.LocalPackages() {
  93. if dp.hasPackage(pkg.Name()) {
  94. continue
  95. }
  96. for _, conflict := range dp.AlpmExecutor.PackageConflicts(pkg) {
  97. dp.checkReverseConflict(pkg.Name(), conflict.String(), conflicts)
  98. }
  99. }
  100. }
  101. func (dp *Pool) CheckConflicts(useAsk, noConfirm bool) (stringset.MapStringSet, error) {
  102. var wg sync.WaitGroup
  103. innerConflicts := make(stringset.MapStringSet)
  104. conflicts := make(stringset.MapStringSet)
  105. wg.Add(2)
  106. text.OperationInfoln(gotext.Get("Checking for conflicts..."))
  107. go func() {
  108. dp.checkForwardConflicts(conflicts)
  109. dp.checkReverseConflicts(conflicts)
  110. wg.Done()
  111. }()
  112. text.OperationInfoln(gotext.Get("Checking for inner conflicts..."))
  113. go func() {
  114. dp.checkInnerConflicts(innerConflicts)
  115. wg.Done()
  116. }()
  117. wg.Wait()
  118. if len(innerConflicts) != 0 {
  119. text.Errorln(gotext.Get("\nInner conflicts found:"))
  120. for name, pkgs := range innerConflicts {
  121. str := text.SprintError(name + ":")
  122. for pkg := range pkgs {
  123. str += " " + text.Cyan(pkg) + ","
  124. }
  125. str = strings.TrimSuffix(str, ",")
  126. fmt.Println(str)
  127. }
  128. }
  129. if len(conflicts) != 0 {
  130. text.Errorln(gotext.Get("\nPackage conflicts found:"))
  131. for name, pkgs := range conflicts {
  132. str := text.SprintError(gotext.Get("Installing %s will remove:", text.Cyan(name)))
  133. for pkg := range pkgs {
  134. str += " " + text.Cyan(pkg) + ","
  135. }
  136. str = strings.TrimSuffix(str, ",")
  137. fmt.Println(str)
  138. }
  139. }
  140. // Add the inner conflicts to the conflicts
  141. // These are used to decide what to pass --ask to (if set) or don't pass --noconfirm to
  142. // As we have no idea what the order is yet we add every inner conflict to the slice
  143. for name, pkgs := range innerConflicts {
  144. conflicts[name] = make(stringset.StringSet)
  145. for pkg := range pkgs {
  146. conflicts[pkg] = make(stringset.StringSet)
  147. }
  148. }
  149. if len(conflicts) > 0 {
  150. if !useAsk {
  151. if noConfirm {
  152. return nil, fmt.Errorf(gotext.Get("package conflicts can not be resolved with noconfirm, aborting"))
  153. }
  154. text.Errorln(gotext.Get("Conflicting packages will have to be confirmed manually"))
  155. }
  156. }
  157. return conflicts, nil
  158. }
  159. type missing struct {
  160. Good stringset.StringSet
  161. Missing map[string][][]string
  162. }
  163. func (dp *Pool) _checkMissing(dep string, stack []string, missing *missing) {
  164. if missing.Good.Get(dep) {
  165. return
  166. }
  167. if trees, ok := missing.Missing[dep]; ok {
  168. for _, tree := range trees {
  169. if stringSliceEqual(tree, stack) {
  170. return
  171. }
  172. }
  173. missing.Missing[dep] = append(missing.Missing[dep], stack)
  174. return
  175. }
  176. aurPkg := dp.findSatisfierAur(dep)
  177. if aurPkg != nil {
  178. missing.Good.Set(dep)
  179. for _, deps := range [3][]string{aurPkg.Depends, aurPkg.MakeDepends, aurPkg.CheckDepends} {
  180. for _, aurDep := range deps {
  181. if dp.AlpmExecutor.LocalSatisfierExists(aurDep) {
  182. missing.Good.Set(aurDep)
  183. continue
  184. }
  185. dp._checkMissing(aurDep, append(stack, aurPkg.Name), missing)
  186. }
  187. }
  188. return
  189. }
  190. repoPkg := dp.findSatisfierRepo(dep)
  191. if repoPkg != nil {
  192. missing.Good.Set(dep)
  193. for _, dep := range dp.AlpmExecutor.PackageDepends(repoPkg) {
  194. if dp.AlpmExecutor.LocalSatisfierExists(dep.String()) {
  195. missing.Good.Set(dep.String())
  196. continue
  197. }
  198. dp._checkMissing(dep.String(), append(stack, repoPkg.Name()), missing)
  199. }
  200. return
  201. }
  202. missing.Missing[dep] = [][]string{stack}
  203. }
  204. func stringSliceEqual(a, b []string) bool {
  205. if a == nil && b == nil {
  206. return true
  207. }
  208. if a == nil || b == nil {
  209. return false
  210. }
  211. if len(a) != len(b) {
  212. return false
  213. }
  214. for i := 0; i < len(a); i++ {
  215. if a[i] != b[i] {
  216. return false
  217. }
  218. }
  219. return true
  220. }
  221. func (dp *Pool) CheckMissing() error {
  222. missing := &missing{
  223. make(stringset.StringSet),
  224. make(map[string][][]string),
  225. }
  226. for _, target := range dp.Targets {
  227. dp._checkMissing(target.DepString(), make([]string, 0), missing)
  228. }
  229. if len(missing.Missing) == 0 {
  230. return nil
  231. }
  232. text.Errorln(gotext.Get("Could not find all required packages:"))
  233. for dep, trees := range missing.Missing {
  234. for _, tree := range trees {
  235. fmt.Fprintf(os.Stderr, "\t%s", text.Cyan(dep))
  236. if len(tree) == 0 {
  237. fmt.Fprint(os.Stderr, gotext.Get(" (Target"))
  238. } else {
  239. fmt.Fprint(os.Stderr, gotext.Get(" (Wanted by: "))
  240. for n := 0; n < len(tree)-1; n++ {
  241. fmt.Fprint(os.Stderr, text.Cyan(tree[n]), " -> ")
  242. }
  243. fmt.Fprint(os.Stderr, text.Cyan(tree[len(tree)-1]))
  244. }
  245. fmt.Fprintln(os.Stderr, ")")
  246. }
  247. }
  248. return fmt.Errorf("")
  249. }