depCheck.go 7.3 KB

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