pkgdepcon.cc 15 KB

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