pkgdepcon.cc 15 KB

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