depCheck.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "sync"
  6. alpm "github.com/jguer/go-alpm"
  7. )
  8. func (dp *depPool) checkInnerConflict(name string, conflict string, conflicts mapStringSet) {
  9. for _, pkg := range dp.Aur {
  10. if pkg.Name == name {
  11. continue
  12. }
  13. if satisfiesAur(conflict, pkg) {
  14. conflicts.Add(name, pkg.Name)
  15. }
  16. }
  17. for _, pkg := range dp.Repo {
  18. if pkg.Name() == name {
  19. continue
  20. }
  21. if satisfiesRepo(conflict, pkg) {
  22. conflicts.Add(name, pkg.Name())
  23. }
  24. }
  25. }
  26. func (dp *depPool) checkForwardConflict(name string, conflict string, conflicts mapStringSet) {
  27. dp.LocalDb.PkgCache().ForEach(func(pkg alpm.Package) error {
  28. if pkg.Name() == name || dp.hasPackage(pkg.Name()) {
  29. return nil
  30. }
  31. if satisfiesRepo(conflict, &pkg) {
  32. n := pkg.Name()
  33. if n != conflict {
  34. n += " (" + conflict + ")"
  35. }
  36. conflicts.Add(name, n)
  37. }
  38. return nil
  39. })
  40. }
  41. func (dp *depPool) checkReverseConflict(name string, conflict string, conflicts mapStringSet) {
  42. for _, pkg := range dp.Aur {
  43. if pkg.Name == name {
  44. continue
  45. }
  46. if satisfiesAur(conflict, pkg) {
  47. if name != conflict {
  48. name += " (" + conflict + ")"
  49. }
  50. conflicts.Add(pkg.Name, name)
  51. }
  52. }
  53. for _, pkg := range dp.Repo {
  54. if pkg.Name() == name {
  55. continue
  56. }
  57. if satisfiesRepo(conflict, pkg) {
  58. if name != conflict {
  59. name += " (" + conflict + ")"
  60. }
  61. conflicts.Add(pkg.Name(), name)
  62. }
  63. }
  64. }
  65. func (dp *depPool) checkInnerConflicts(conflicts mapStringSet) {
  66. for _, pkg := range dp.Aur {
  67. for _, conflict := range pkg.Conflicts {
  68. dp.checkInnerConflict(pkg.Name, conflict, conflicts)
  69. }
  70. }
  71. for _, pkg := range dp.Repo {
  72. pkg.Conflicts().ForEach(func(conflict alpm.Depend) error {
  73. dp.checkInnerConflict(pkg.Name(), conflict.String(), conflicts)
  74. return nil
  75. })
  76. }
  77. }
  78. func (dp *depPool) checkForwardConflicts(conflicts mapStringSet) {
  79. for _, pkg := range dp.Aur {
  80. for _, conflict := range pkg.Conflicts {
  81. dp.checkForwardConflict(pkg.Name, conflict, conflicts)
  82. }
  83. }
  84. for _, pkg := range dp.Repo {
  85. pkg.Conflicts().ForEach(func(conflict alpm.Depend) error {
  86. dp.checkForwardConflict(pkg.Name(), conflict.String(), conflicts)
  87. return nil
  88. })
  89. }
  90. }
  91. func (dp *depPool) checkReverseConflicts(conflicts mapStringSet) {
  92. dp.LocalDb.PkgCache().ForEach(func(pkg alpm.Package) error {
  93. if dp.hasPackage(pkg.Name()) {
  94. return nil
  95. }
  96. pkg.Conflicts().ForEach(func(conflict alpm.Depend) error {
  97. dp.checkReverseConflict(pkg.Name(), conflict.String(), conflicts)
  98. return nil
  99. })
  100. return nil
  101. })
  102. }
  103. func (dp *depPool) CheckConflicts() (mapStringSet, error) {
  104. var wg sync.WaitGroup
  105. innerConflicts := make(mapStringSet)
  106. conflicts := make(mapStringSet)
  107. wg.Add(2)
  108. fmt.Println(bold(cyan("::") + bold(" Checking for conflicts...")))
  109. go func() {
  110. dp.checkForwardConflicts(conflicts)
  111. dp.checkReverseConflicts(conflicts)
  112. wg.Done()
  113. }()
  114. fmt.Println(bold(cyan("::") + bold(" Checking for inner conflicts...")))
  115. go func() {
  116. dp.checkInnerConflicts(innerConflicts)
  117. wg.Done()
  118. }()
  119. wg.Wait()
  120. if len(innerConflicts) != 0 {
  121. fmt.Println()
  122. fmt.Println(bold(red(arrow)), bold("Inner conflicts found:"))
  123. for name, pkgs := range innerConflicts {
  124. str := red(bold(smallArrow)) + " " + name + ":"
  125. for pkg := range pkgs {
  126. str += " " + cyan(pkg) + ","
  127. }
  128. str = strings.TrimSuffix(str, ",")
  129. fmt.Println(str)
  130. }
  131. return nil, fmt.Errorf("Unresolvable package conflicts, aborting")
  132. }
  133. if len(conflicts) != 0 {
  134. fmt.Println()
  135. fmt.Println(bold(red(arrow)), bold("Package conflicts found:"))
  136. if !config.UseAsk {
  137. fmt.Println(bold(red(arrow)), bold("You will have to confirm these when installing"))
  138. }
  139. for name, pkgs := range conflicts {
  140. str := red(bold(smallArrow)) + " Installing " + cyan(name) + " will remove:"
  141. for pkg := range pkgs {
  142. str += " " + cyan(pkg) + ","
  143. }
  144. str = strings.TrimSuffix(str, ",")
  145. fmt.Println(str)
  146. }
  147. fmt.Println()
  148. if config.NoConfirm && !config.UseAsk {
  149. return nil, fmt.Errorf("Package conflicts can not be resolved with noconfirm, aborting")
  150. }
  151. }
  152. return conflicts, nil
  153. }
  154. type missing struct {
  155. Good stringSet
  156. Missing map[string][][]string
  157. }
  158. func (dp *depPool) _checkMissing(dep string, stack []string, missing *missing) {
  159. if missing.Good.get(dep) {
  160. return
  161. }
  162. if trees, ok := missing.Missing[dep]; ok {
  163. for _, tree := range trees {
  164. if stringSliceEqual(tree, stack) {
  165. return
  166. }
  167. }
  168. missing.Missing[dep] = append(missing.Missing[dep], stack)
  169. return
  170. }
  171. aurPkg := dp.findSatisfierAur(dep)
  172. if aurPkg != nil {
  173. missing.Good.set(dep)
  174. for _, deps := range [3][]string{aurPkg.Depends, aurPkg.MakeDepends, aurPkg.CheckDepends} {
  175. for _, aurDep := range deps {
  176. if _, err := dp.LocalDb.PkgCache().FindSatisfier(aurDep); err == nil {
  177. missing.Good.set(aurDep)
  178. continue
  179. }
  180. dp._checkMissing(aurDep, append(stack, aurPkg.Name), missing)
  181. }
  182. }
  183. return
  184. }
  185. repoPkg := dp.findSatisfierRepo(dep)
  186. if repoPkg != nil {
  187. missing.Good.set(dep)
  188. repoPkg.Depends().ForEach(func(repoDep alpm.Depend) error {
  189. if _, err := dp.LocalDb.PkgCache().FindSatisfier(repoDep.String()); err == nil {
  190. missing.Good.set(repoDep.String())
  191. return nil
  192. }
  193. dp._checkMissing(repoDep.String(), append(stack, repoPkg.Name()), missing)
  194. return nil
  195. })
  196. return
  197. }
  198. missing.Missing[dep] = [][]string{stack}
  199. }
  200. func (dp *depPool) CheckMissing() error {
  201. missing := &missing{
  202. make(stringSet),
  203. make(map[string][][]string),
  204. }
  205. for _, target := range dp.Targets {
  206. dp._checkMissing(target.DepString(), make([]string, 0), missing)
  207. }
  208. if len(missing.Missing) == 0 {
  209. return nil
  210. }
  211. fmt.Println(bold(red(arrow+" Error: ")) + "Could not find all required packages:")
  212. for dep, trees := range missing.Missing {
  213. for _, tree := range trees {
  214. fmt.Print(" ", cyan(dep))
  215. if len(tree) == 0 {
  216. fmt.Print(" (Target")
  217. } else {
  218. fmt.Print(" (Wanted by: ")
  219. for n := 0; n < len(tree)-1; n++ {
  220. fmt.Print(cyan(tree[n]), " -> ")
  221. }
  222. fmt.Print(cyan(tree[len(tree)-1]))
  223. }
  224. fmt.Println(")")
  225. }
  226. }
  227. return fmt.Errorf("")
  228. }