pkgdepcon.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * dselect - Debian package maintenance user interface
  3. * pkgdepcon.cc - dependency and conflict resolution
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <assert.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <dpkg/dpkg.h>
  26. #include <dpkg/dpkg-db.h>
  27. #include "dselect.h"
  28. #include "pkglist.h"
  29. bool
  30. packagelist::useavailable(pkginfo *pkg)
  31. {
  32. if (pkg->clientdata &&
  33. pkg->clientdata->selected == pkginfo::want_install &&
  34. pkg_is_informative(pkg, &pkg->available) &&
  35. (!(pkg->status == pkginfo::stat_installed ||
  36. pkg->status == pkginfo::stat_triggersawaited ||
  37. pkg->status == pkginfo::stat_triggerspending) ||
  38. dpkg_version_compare(&pkg->available.version,
  39. &pkg->installed.version) > 0))
  40. return true;
  41. else
  42. return false;
  43. }
  44. pkgbin *
  45. packagelist::find_pkgbin(pkginfo *pkg)
  46. {
  47. pkgbin *r;
  48. r= useavailable(pkg) ? &pkg->available : &pkg->installed;
  49. debug(dbg_general, "packagelist[%p]::find_pkgbin(%s) useavailable=%d",
  50. this, pkgbin_name(pkg, r, pnaw_always), useavailable(pkg));
  51. return r;
  52. }
  53. int packagelist::checkdependers(pkginfo *pkg, int changemade) {
  54. struct deppossi *possi;
  55. for (possi = pkg->set->depended.available; possi; possi = possi->rev_next) {
  56. if (!useavailable(possi->up->up))
  57. continue;
  58. changemade = max(changemade, resolvedepcon(possi->up));
  59. }
  60. for (possi = pkg->set->depended.installed; possi; possi = possi->rev_next) {
  61. if (useavailable(possi->up->up))
  62. continue;
  63. changemade = max(changemade, resolvedepcon(possi->up));
  64. }
  65. return changemade;
  66. }
  67. int packagelist::resolvesuggest() {
  68. // We continually go around looking for things to change, but we may
  69. // only change the ‘suggested’ value if we also increase the ‘priority’
  70. // Return 2 if we made a change due to a Recommended, Depends or Conficts,
  71. // or 1 if we offered or made a change because of an Optional line.
  72. debug(dbg_general, "packagelist[%p]::resolvesuggest()", this);
  73. int changemade, maxchangemade;
  74. maxchangemade= 0;
  75. for (;;) {
  76. changemade= 0;
  77. int index;
  78. for (index=0; index<nitems; index++) {
  79. if (!table[index]->pkg->set->name)
  80. continue;
  81. debug(dbg_depcon, "packagelist[%p]::resolvesuggest() loop[%i] %s / %d",
  82. this, index, pkg_name(table[index]->pkg, pnaw_always), changemade);
  83. dependency *depends;
  84. for (depends = find_pkgbin(table[index]->pkg)->depends;
  85. depends;
  86. depends= depends->next) {
  87. changemade = max(changemade, resolvedepcon(depends));
  88. }
  89. changemade= checkdependers(table[index]->pkg,changemade);
  90. for (depends = find_pkgbin(table[index]->pkg)->depends;
  91. depends;
  92. depends= depends->next) {
  93. if (depends->type != dep_provides) continue;
  94. changemade = checkdependers(&depends->list->ed->pkg, changemade);
  95. }
  96. debug(dbg_depcon, "packagelist[%p]::resolvesuggest() loop[%i] %s / -> %d",
  97. this, index, pkg_name(table[index]->pkg, pnaw_always), changemade);
  98. }
  99. if (!changemade) break;
  100. maxchangemade = max(maxchangemade, changemade);
  101. }
  102. debug(dbg_general, "packagelist[%p]::resolvesuggest() done; maxchangemade=%d",
  103. this, maxchangemade);
  104. return maxchangemade;
  105. }
  106. static int dep_update_best_to_change_stop(perpackagestate *& best, pkginfo *trythis) {
  107. // There's no point trying to select a pure virtual package.
  108. if (!trythis->clientdata) return 0;
  109. debug(dbg_depcon, "update_best_to_change(best=%s{%d}, test=%s{%d});",
  110. best ? pkg_name(best->pkg, pnaw_always) : "",
  111. best ? (int)best->spriority : -1,
  112. trythis->set->name, trythis->clientdata->spriority);
  113. // If the problem is caused by us deselecting one of these packages
  114. // we should not try to select another one instead.
  115. if (trythis->clientdata->spriority == sp_deselecting) return 1;
  116. // If we haven't found anything yet then this is our best so far.
  117. if (!best) goto yes;
  118. // If only one of the packages is available, use that one
  119. if (!pkg_is_informative(trythis, &trythis->available) &&
  120. pkg_is_informative(best->pkg, &best->pkg->available))
  121. return 0;
  122. if (pkg_is_informative(trythis, &trythis->available) &&
  123. !pkg_is_informative(best->pkg, &best->pkg->available))
  124. goto yes;
  125. // Select the package with the lowest priority (ie, the one of whom
  126. // we were least sure we wanted it deselected).
  127. if (trythis->clientdata->spriority > best->spriority) return 0;
  128. if (trythis->clientdata->spriority < best->spriority) goto yes;
  129. // Pick the package with the must fundamental recommendation level.
  130. if (trythis->priority > best->pkg->priority) return 0;
  131. if (trythis->priority < best->pkg->priority) goto yes;
  132. // If we're still unsure we'll change the first one in the list.
  133. return 0;
  134. yes:
  135. debug(dbg_depcon, "update_best_to_change(); yes");
  136. best=trythis->clientdata; return 0;
  137. }
  138. int
  139. packagelist::deselect_one_of(pkginfo *per, pkginfo *ped, dependency *dep)
  140. {
  141. perpackagestate *er= per->clientdata;
  142. perpackagestate *ed= ped->clientdata;
  143. if (!er || !would_like_to_install(er->selected,per) ||
  144. !ed || !would_like_to_install(ed->selected,ped)) return 0;
  145. add(dep, dp_must);
  146. er= per->clientdata; // these can be changed by add
  147. ed= ped->clientdata;
  148. debug(dbg_depcon,
  149. "packagelist[%p]::deselect_one_of(): er %s{%d} ed %s{%d} [%p]",
  150. this, pkg_name(er->pkg, pnaw_always), er->spriority,
  151. pkg_name(ed->pkg, pnaw_always), ed->spriority, dep);
  152. perpackagestate *best;
  153. // Try not keep packages needing reinstallation.
  154. if (per->eflag & pkginfo::eflag_reinstreq)
  155. best = ed;
  156. else if (ped->eflag & pkginfo::eflag_reinstreq)
  157. best = er;
  158. else if (er->spriority < ed->spriority) best= er; // We'd rather change the
  159. else if (er->spriority > ed->spriority) best= ed; // one with the lowest priority.
  160. // ... failing that the one with the highest priority
  161. else if (er->pkg->priority > ed->pkg->priority)
  162. best = er;
  163. else if (er->pkg->priority < ed->pkg->priority)
  164. best = ed;
  165. else best= ed; // ... failing that, the second
  166. debug(dbg_depcon, "packagelist[%p]::deselect_one_of(): best %s{%d}",
  167. this, pkg_name(best->pkg, pnaw_always), best->spriority);
  168. if (best->spriority >= sp_deselecting) return 0;
  169. best->suggested=
  170. best->pkg->status == pkginfo::stat_notinstalled
  171. ? pkginfo::want_purge : pkginfo::want_deinstall; // FIXME: configurable.
  172. best->selected= best->suggested;
  173. best->spriority= sp_deselecting;
  174. return 2;
  175. }
  176. int packagelist::resolvedepcon(dependency *depends) {
  177. perpackagestate *best, *fixbyupgrade;
  178. deppossi *possi, *provider;
  179. int r, foundany;
  180. if (debug_has_flag(dbg_depcon)) {
  181. varbuf pkg_names;
  182. for (possi = depends->list; possi; possi = possi->next) {
  183. pkg_names(' ');
  184. pkg_names(possi->ed->name);
  185. }
  186. debug(dbg_depcon,
  187. "packagelist[%p]::resolvedepcon([%p] %s --%s-->%s); (ing)->want=%s",
  188. this, depends, pkg_name(depends->up, pnaw_always),
  189. relatestrings[depends->type],
  190. pkg_names.string(), depends->up->clientdata ?
  191. wantstrings[depends->up->clientdata->suggested] : "(no clientdata)");
  192. }
  193. if (!depends->up->clientdata) return 0;
  194. switch (depends->type) {
  195. case dep_provides:
  196. case dep_replaces:
  197. return 0;
  198. case dep_enhances:
  199. case dep_suggests:
  200. case dep_recommends:
  201. case dep_depends:
  202. case dep_predepends:
  203. if (would_like_to_install(depends->up->clientdata->selected,depends->up) <= 0)
  204. return 0;
  205. fixbyupgrade= 0;
  206. possi = depends->list;
  207. while (possi && !deppossatisfied(possi, &fixbyupgrade))
  208. possi = possi->next;
  209. debug(dbg_depcon, "packagelist[%p]::resolvedepcon([%p]): depends found %s",
  210. this, depends, possi ? possi->ed->name : "[none]");
  211. if (possi) return 0;
  212. // Ensures all in the recursive list; adds info strings; ups priorities
  213. switch (depends->type) {
  214. case dep_enhances:
  215. case dep_suggests:
  216. r= add(depends, dp_may);
  217. return r;
  218. case dep_recommends:
  219. r= add(depends, dp_should);
  220. break;
  221. default:
  222. r= add(depends, dp_must);
  223. }
  224. if (fixbyupgrade) {
  225. debug(dbg_depcon,
  226. "packagelist[%p]::resolvedepcon([%p]): fixbyupgrade %s",
  227. this, depends, pkg_name(fixbyupgrade->pkg, pnaw_always));
  228. best= fixbyupgrade;
  229. } else {
  230. best= 0;
  231. for (possi= depends->list;
  232. possi;
  233. possi= possi->next) {
  234. foundany= 0;
  235. if (possi->ed->pkg.clientdata)
  236. foundany = 1;
  237. if (dep_update_best_to_change_stop(best, &possi->ed->pkg))
  238. goto mustdeselect;
  239. for (provider = possi->ed->depended.available;
  240. provider;
  241. provider = provider->rev_next) {
  242. if (provider->up->type != dep_provides) continue;
  243. if (provider->up->up->clientdata) foundany= 1;
  244. if (dep_update_best_to_change_stop(best, provider->up->up)) goto mustdeselect;
  245. }
  246. if (!foundany) addunavailable(possi);
  247. }
  248. if (!best) {
  249. debug(dbg_depcon,
  250. "packagelist[%p]::resolvedepcon([%p]): mustdeselect nobest",
  251. this, depends);
  252. return r;
  253. }
  254. }
  255. debug(dbg_depcon,
  256. "packagelist[%p]::resolvedepcon([%p]): select best=%s{%d}",
  257. this, depends, pkg_name(best->pkg, pnaw_always), best->spriority);
  258. if (best->spriority >= sp_selecting) return r;
  259. /* Always select depends. Only select recommends if we got here because
  260. * of a manually-initiated install request. */
  261. if (depends->type != dep_recommends || manual_install) {
  262. best->selected= best->suggested= pkginfo::want_install;
  263. best->spriority= sp_selecting;
  264. }
  265. return r ? 2 : 0;
  266. mustdeselect:
  267. best= depends->up->clientdata;
  268. debug(dbg_depcon,
  269. "packagelist[%p]::resolvedepcon([%p]): mustdeselect best=%s{%d}",
  270. this, depends, pkg_name(best->pkg, pnaw_always), best->spriority);
  271. if (best->spriority >= sp_deselecting) return r;
  272. /* Always remove depends, but never remove recommends. */
  273. if (depends->type != dep_recommends) {
  274. best->selected= best->suggested=
  275. best->pkg->status == pkginfo::stat_notinstalled
  276. ? pkginfo::want_purge : pkginfo::want_deinstall; // FIXME: configurable
  277. best->spriority= sp_deselecting;
  278. }
  279. return r ? 2 : 0;
  280. case dep_conflicts:
  281. case dep_breaks:
  282. debug(dbg_depcon, "packagelist[%p]::resolvedepcon([%p]): conflict",
  283. this, depends);
  284. if (would_like_to_install(depends->up->clientdata->selected,depends->up) == 0)
  285. return 0;
  286. debug(dbg_depcon,
  287. "packagelist[%p]::resolvedepcon([%p]): conflict installing 1",
  288. this, depends);
  289. if (!deppossatisfied(depends->list,0)) return 0;
  290. debug(dbg_depcon,
  291. "packagelist[%p]::resolvedepcon([%p]): conflict satisfied - ouch",
  292. this, depends);
  293. if (depends->up->set != depends->list->ed) {
  294. r = deselect_one_of(depends->up, &depends->list->ed->pkg, depends);
  295. if (r)
  296. return r;
  297. }
  298. for (provider = depends->list->ed->depended.available;
  299. provider;
  300. provider = provider->rev_next) {
  301. if (provider->up->type != dep_provides) continue;
  302. if (provider->up->up == depends->up) continue; // conflicts & provides same thing
  303. r= deselect_one_of(depends->up, provider->up->up, depends); if (r) return r;
  304. }
  305. debug(dbg_depcon, "packagelist[%p]::resolvedepcon([%p]): no desel",
  306. this, depends);
  307. return 0;
  308. default:
  309. internerr("unknown deptype %d", depends->type);
  310. }
  311. /* never reached, make gcc happy */
  312. return 1;
  313. }
  314. bool
  315. packagelist::deppossatisfied(deppossi *possi, perpackagestate **fixbyupgrade)
  316. {
  317. // ‘satisfied’ here for Conflicts and Breaks means that the
  318. // restriction is violated ie that the target package is wanted
  319. int would;
  320. pkginfo::pkgwant want= pkginfo::want_purge;
  321. if (possi->ed->pkg.clientdata) {
  322. want = possi->ed->pkg.clientdata->selected;
  323. would = would_like_to_install(want, &possi->ed->pkg);
  324. } else {
  325. would= 0;
  326. }
  327. if ((possi->up->type == dep_conflicts || possi->up->type == dep_breaks)
  328. ? possi->up->up->set != possi->ed && would != 0
  329. : would > 0) {
  330. // If it's to be installed or left installed, then either it's of
  331. // the right version, and therefore OK, or a version must have
  332. // been specified, in which case we don't need to look at the rest
  333. // anyway.
  334. if (useavailable(&possi->ed->pkg)) {
  335. assert(want == pkginfo::want_install);
  336. return versionsatisfied(&possi->ed->pkg.available, possi);
  337. } else {
  338. if (versionsatisfied(&possi->ed->pkg.installed, possi))
  339. return true;
  340. if (want == pkginfo::want_hold && fixbyupgrade && !*fixbyupgrade &&
  341. versionsatisfied(&possi->ed->pkg.available, possi) &&
  342. dpkg_version_compare(&possi->ed->pkg.available.version,
  343. &possi->ed->pkg.installed.version) > 1)
  344. *fixbyupgrade = possi->ed->pkg.clientdata;
  345. return false;
  346. }
  347. }
  348. if (possi->verrel != dpkg_relation_none)
  349. return false;
  350. deppossi *provider;
  351. for (provider = possi->ed->depended.installed;
  352. provider;
  353. provider = provider->rev_next) {
  354. if (provider->up->type == dep_provides &&
  355. ((possi->up->type != dep_conflicts && possi->up->type != dep_breaks) ||
  356. provider->up->up->set != possi->up->up->set) &&
  357. provider->up->up->clientdata &&
  358. !useavailable(provider->up->up) &&
  359. would_like_to_install(provider->up->up->clientdata->selected,
  360. provider->up->up))
  361. return true;
  362. }
  363. for (provider = possi->ed->depended.available;
  364. provider;
  365. provider = provider->rev_next) {
  366. if (provider->up->type != dep_provides ||
  367. ((possi->up->type == dep_conflicts || possi->up->type == dep_breaks) &&
  368. provider->up->up->set == possi->up->up->set) ||
  369. !provider->up->up->clientdata ||
  370. !would_like_to_install(provider->up->up->clientdata->selected,
  371. provider->up->up))
  372. continue;
  373. if (useavailable(provider->up->up))
  374. return true;
  375. if (fixbyupgrade && !*fixbyupgrade &&
  376. (!(provider->up->up->status == pkginfo::stat_installed ||
  377. provider->up->up->status == pkginfo::stat_triggerspending ||
  378. provider->up->up->status == pkginfo::stat_triggersawaited) ||
  379. dpkg_version_compare(&provider->up->up->available.version,
  380. &provider->up->up->installed.version) > 1))
  381. *fixbyupgrade = provider->up->up->clientdata;
  382. }
  383. return false;
  384. }