depcon.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * dpkg - main program for package management
  3. * depcon.c - dependency and conflict checking
  4. *
  5. * Copyright © 1994,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 <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <assert.h>
  25. #include <errno.h>
  26. #include <unistd.h>
  27. #include <dpkg/i18n.h>
  28. #include <dpkg/dpkg.h>
  29. #include <dpkg/dpkg-db.h>
  30. #include "main.h"
  31. struct cyclesofarlink {
  32. struct cyclesofarlink *back;
  33. struct pkginfo *pkg;
  34. struct deppossi *possi;
  35. };
  36. static bool findbreakcyclerecursive(struct pkginfo *pkg,
  37. struct cyclesofarlink *sofar);
  38. static bool
  39. foundcyclebroken(struct cyclesofarlink *thislink, struct cyclesofarlink *sofar,
  40. struct pkginfo *dependedon, struct deppossi *possi)
  41. {
  42. struct cyclesofarlink *sol;
  43. const char *postinstfilename;
  44. struct stat stab;
  45. if(!possi)
  46. return false;
  47. /* We're investigating the dependency `possi' to see if it
  48. * is part of a loop. To this end we look to see whether the
  49. * depended-on package is already one of the packages whose
  50. * dependencies we're searching.
  51. */
  52. for (sol=sofar; sol && sol->pkg != dependedon; sol=sol->back);
  53. /* If not, we do a recursive search on it to see what we find. */
  54. if (!sol)
  55. return findbreakcyclerecursive(dependedon, thislink);
  56. debug(dbg_depcon,"found cycle");
  57. /* Right, we now break one of the links. We prefer to break
  58. * a dependency of a package without a postinst script, as
  59. * this is a null operation. If this is not possible we break
  60. * the other link in the recursive calling tree which mentions
  61. * this package (this being the first package involved in the
  62. * cycle). It doesn't particularly matter which we pick, but if
  63. * we break the earliest dependency we came across we may be
  64. * able to do something straight away when findbreakcycle returns.
  65. */
  66. sofar= thislink;
  67. for (sol= sofar; !(sol != sofar && sol->pkg == dependedon); sol=sol->back) {
  68. postinstfilename= pkgadminfile(sol->pkg,POSTINSTFILE);
  69. if (lstat(postinstfilename,&stab)) {
  70. if (errno == ENOENT) break;
  71. ohshite(_("unable to check for existence of `%.250s'"),postinstfilename);
  72. }
  73. }
  74. /* Now we have either a package with no postinst, or the other
  75. * occurrence of the current package in the list.
  76. */
  77. sol->possi->cyclebreak = true;
  78. debug(dbg_depcon,"cycle broken at %s -> %s\n",
  79. sol->possi->up->up->name, sol->possi->ed->name);
  80. return true;
  81. }
  82. static bool
  83. findbreakcyclerecursive(struct pkginfo *pkg, struct cyclesofarlink *sofar)
  84. {
  85. /* Cycle breaking works recursively down the package dependency
  86. * tree. `sofar' is the list of packages we've descended down
  87. * already - if we encounter any of its packages again in a
  88. * dependency we have found a cycle.
  89. */
  90. struct cyclesofarlink thislink, *sol;
  91. struct dependency *dep;
  92. struct deppossi *possi, *providelink;
  93. struct pkginfo *provider;
  94. if (pkg->clientdata->color == black)
  95. return false;
  96. pkg->clientdata->color = gray;
  97. if (f_debug & dbg_depcondetail) {
  98. struct varbuf str_pkgs = VARBUF_INIT;
  99. for (sol = sofar; sol; sol = sol->back) {
  100. varbufaddstr(&str_pkgs, " <- ");
  101. varbufaddstr(&str_pkgs, sol->pkg->name);
  102. }
  103. varbufaddc(&str_pkgs, '\0');
  104. debug(dbg_depcondetail, "findbreakcyclerecursive %s %s", pkg->name,
  105. str_pkgs.buf);
  106. varbuf_destroy(&str_pkgs);
  107. }
  108. thislink.pkg= pkg;
  109. thislink.back= sofar;
  110. thislink.possi = NULL;
  111. for (dep= pkg->installed.depends; dep; dep= dep->next) {
  112. if (dep->type != dep_depends && dep->type != dep_predepends) continue;
  113. for (possi= dep->list; possi; possi= possi->next) {
  114. /* Don't find the same cycles again. */
  115. if (possi->cyclebreak) continue;
  116. thislink.possi= possi;
  117. if (foundcyclebroken(&thislink, sofar, possi->ed,possi))
  118. return true;
  119. /* Right, now we try all the providers ... */
  120. for (providelink= possi->ed->installed.depended;
  121. providelink;
  122. providelink= providelink->nextrev) {
  123. if (providelink->up->type != dep_provides) continue;
  124. provider= providelink->up->up;
  125. if (provider->clientdata->istobe == itb_normal) continue;
  126. /* We don't break things at `provides' links, so `possi' is
  127. * still the one we use.
  128. */
  129. if (foundcyclebroken(&thislink, sofar, provider, possi))
  130. return true;
  131. }
  132. }
  133. }
  134. /* Nope, we didn't find a cycle to break. */
  135. pkg->clientdata->color = black;
  136. return false;
  137. }
  138. bool
  139. findbreakcycle(struct pkginfo *pkg)
  140. {
  141. struct pkgiterator *iter;
  142. struct pkginfo *tpkg;
  143. /* Clear the visited flag of all packages before we traverse them. */
  144. iter = iterpkgstart();
  145. while ((tpkg = iterpkgnext(iter))) {
  146. tpkg->clientdata->color = white;
  147. }
  148. iterpkgend(iter);
  149. return findbreakcyclerecursive(pkg, NULL);
  150. }
  151. void describedepcon(struct varbuf *addto, struct dependency *dep) {
  152. const char *fmt;
  153. struct varbuf depstr = VARBUF_INIT;
  154. switch (dep->type) {
  155. case dep_depends:
  156. fmt = _("%s depends on %s");
  157. break;
  158. case dep_predepends:
  159. fmt = _("%s pre-depends on %s");
  160. break;
  161. case dep_recommends:
  162. fmt = _("%s recommends %s");
  163. break;
  164. case dep_suggests:
  165. fmt = _("%s suggests %s");
  166. break;
  167. case dep_breaks:
  168. fmt = _("%s breaks %s");
  169. break;
  170. case dep_conflicts:
  171. fmt = _("%s conflicts with %s");
  172. break;
  173. case dep_enhances:
  174. fmt = _("%s enhances %s");
  175. break;
  176. default:
  177. internerr("unknown deptype '%d'", dep->type);
  178. }
  179. varbufdependency(&depstr, dep);
  180. varbufaddc(&depstr, 0);
  181. varbufprintf(addto, fmt, dep->up->name, depstr.buf);
  182. varbuf_destroy(&depstr);
  183. }
  184. bool
  185. depisok(struct dependency *dep, struct varbuf *whynot,
  186. struct pkginfo **canfixbyremove, bool allowunconfigd)
  187. {
  188. /* *whynot must already have been initialised; it need not be
  189. * empty though - it will be reset before use.
  190. * If depisok returns false for ‘not OK’ it will contain a description,
  191. * newline-terminated BUT NOT NULL-TERMINATED, of the reason.
  192. * If depisok returns true it will contain garbage.
  193. * allowunconfigd should be true during the ‘Pre-Depends’ checking
  194. * before a package is unpacked, when it is sufficient for the package
  195. * to be unpacked provided that both the unpacked and previously-configured
  196. * versions are acceptable.
  197. * On false return (‘not OK’), *canfixbyremove refers to a package which
  198. * if removed (dep_conflicts) or deconfigured (dep_breaks) will fix
  199. * the problem. Caller may pass 0 for canfixbyremove and need not
  200. * initialise *canfixbyremove.
  201. */
  202. struct deppossi *possi;
  203. struct deppossi *provider;
  204. int nconflicts;
  205. /* Use this buffer so that when internationalisation comes along we
  206. * don't have to rewrite the code completely, only redo the sprintf strings
  207. * (assuming we have the fancy argument-number-specifiers).
  208. * Allow 250x3 for package names, versions, &c, + 250 for ourselves.
  209. */
  210. char linebuf[1024];
  211. assert(dep->type == dep_depends || dep->type == dep_predepends ||
  212. dep->type == dep_breaks || dep->type == dep_conflicts ||
  213. dep->type == dep_recommends || dep->type == dep_suggests ||
  214. dep->type == dep_enhances);
  215. if (canfixbyremove)
  216. *canfixbyremove = NULL;
  217. /* The dependency is always OK if we're trying to remove the depend*ing*
  218. * package.
  219. */
  220. switch (dep->up->clientdata->istobe) {
  221. case itb_remove: case itb_deconfigure:
  222. return true;
  223. case itb_normal:
  224. /* Only installed packages can be make dependency problems */
  225. switch (dep->up->status) {
  226. case stat_installed:
  227. case stat_triggerspending:
  228. case stat_triggersawaited:
  229. break;
  230. case stat_notinstalled: case stat_configfiles: case stat_halfinstalled:
  231. case stat_halfconfigured: case stat_unpacked:
  232. return true;
  233. default:
  234. internerr("unknown status depending '%d'", dep->up->status);
  235. }
  236. break;
  237. case itb_installnew: case itb_preinstall:
  238. break;
  239. default:
  240. internerr("unknown istobe depending '%d'", dep->up->clientdata->istobe);
  241. }
  242. /* Describe the dependency, in case we have to moan about it. */
  243. varbufreset(whynot);
  244. varbufaddc(whynot, ' ');
  245. describedepcon(whynot, dep);
  246. varbufaddc(whynot,'\n');
  247. /* TODO: check dep_enhances as well (WTA) */
  248. if (dep->type == dep_depends || dep->type == dep_predepends ||
  249. dep->type == dep_recommends || dep->type == dep_suggests ) {
  250. /* Go through the alternatives. As soon as we find one that
  251. * we like, we return ‘true’ straight away. Otherwise, when we get to
  252. * the end we'll have accumulated all the reasons in whynot and
  253. * can return ‘false’.
  254. */
  255. for (possi= dep->list; possi; possi= possi->next) {
  256. switch (possi->ed->clientdata->istobe) {
  257. case itb_remove:
  258. sprintf(linebuf,_(" %.250s is to be removed.\n"),possi->ed->name);
  259. break;
  260. case itb_deconfigure:
  261. sprintf(linebuf,_(" %.250s is to be deconfigured.\n"),possi->ed->name);
  262. break;
  263. case itb_installnew:
  264. if (versionsatisfied(&possi->ed->available, possi))
  265. return true;
  266. sprintf(linebuf,_(" %.250s is to be installed, but is version %.250s.\n"),
  267. possi->ed->name,
  268. versiondescribe(&possi->ed->available.version,vdew_nonambig));
  269. break;
  270. case itb_normal: case itb_preinstall:
  271. switch (possi->ed->status) {
  272. case stat_installed:
  273. case stat_triggerspending:
  274. if (versionsatisfied(&possi->ed->installed, possi))
  275. return true;
  276. sprintf(linebuf,_(" %.250s is installed, but is version %.250s.\n"),
  277. possi->ed->name,
  278. versiondescribe(&possi->ed->installed.version,vdew_nonambig));
  279. break;
  280. case stat_notinstalled:
  281. /* Don't say anything about this yet - it might be a virtual package.
  282. * Later on, if nothing has put anything in linebuf, we know that it
  283. * isn't and issue a diagnostic then.
  284. */
  285. *linebuf = '\0';
  286. break;
  287. case stat_unpacked:
  288. case stat_halfconfigured:
  289. case stat_triggersawaited:
  290. if (allowunconfigd) {
  291. if (!informativeversion(&possi->ed->configversion)) {
  292. sprintf(linebuf, _(" %.250s is unpacked, but has never been configured.\n"),
  293. possi->ed->name);
  294. break;
  295. } else if (!versionsatisfied(&possi->ed->installed, possi)) {
  296. sprintf(linebuf, _(" %.250s is unpacked, but is version %.250s.\n"),
  297. possi->ed->name,
  298. versiondescribe(&possi->ed->available.version,vdew_nonambig));
  299. break;
  300. } else if (!versionsatisfied3(&possi->ed->configversion,
  301. &possi->version,possi->verrel)) {
  302. sprintf(linebuf, _(" %.250s latest configured version is %.250s.\n"),
  303. possi->ed->name,
  304. versiondescribe(&possi->ed->configversion,vdew_nonambig));
  305. break;
  306. } else {
  307. return true;
  308. }
  309. }
  310. /* Fall through. */
  311. default:
  312. sprintf(linebuf, _(" %.250s is %s.\n"),
  313. possi->ed->name, gettext(statusstrings[possi->ed->status]));
  314. break;
  315. }
  316. break;
  317. default:
  318. internerr("unknown istobe depended '%d'", possi->ed->clientdata->istobe);
  319. }
  320. varbufaddstr(whynot, linebuf);
  321. /* If there was no version specified we try looking for Providers. */
  322. if (possi->verrel == dvr_none) {
  323. /* See if the package we're about to install Provides it. */
  324. for (provider= possi->ed->available.depended;
  325. provider;
  326. provider= provider->nextrev) {
  327. if (provider->up->type != dep_provides) continue;
  328. if (provider->up->up->clientdata->istobe == itb_installnew)
  329. return true;
  330. }
  331. /* Now look at the packages already on the system. */
  332. for (provider= possi->ed->installed.depended;
  333. provider;
  334. provider= provider->nextrev) {
  335. if (provider->up->type != dep_provides) continue;
  336. switch (provider->up->up->clientdata->istobe) {
  337. case itb_installnew:
  338. /* Don't pay any attention to the Provides field of the
  339. * currently-installed version of the package we're trying
  340. * to install. We dealt with that by using the available
  341. * information above.
  342. */
  343. continue;
  344. case itb_remove:
  345. sprintf(linebuf, _(" %.250s provides %.250s but is to be removed.\n"),
  346. provider->up->up->name, possi->ed->name);
  347. break;
  348. case itb_deconfigure:
  349. sprintf(linebuf, _(" %.250s provides %.250s but is to be deconfigured.\n"),
  350. provider->up->up->name, possi->ed->name);
  351. break;
  352. case itb_normal: case itb_preinstall:
  353. if (provider->up->up->status == stat_installed)
  354. return true;
  355. sprintf(linebuf, _(" %.250s provides %.250s but is %s.\n"),
  356. provider->up->up->name, possi->ed->name,
  357. gettext(statusstrings[provider->up->up->status]));
  358. break;
  359. default:
  360. internerr("unknown istobe provider '%d'",
  361. provider->up->up->clientdata->istobe);
  362. }
  363. varbufaddstr(whynot, linebuf);
  364. }
  365. if (!*linebuf) {
  366. /* If the package wasn't installed at all, and we haven't said
  367. * yet why this isn't satisfied, we should say so now.
  368. */
  369. sprintf(linebuf, _(" %.250s is not installed.\n"), possi->ed->name);
  370. varbufaddstr(whynot, linebuf);
  371. }
  372. }
  373. }
  374. return false;
  375. } else {
  376. /* It's conflicts or breaks. There's only one main alternative,
  377. * but we also have to consider Providers. We return ‘false’ as soon
  378. * as we find something that matches the conflict, and only describe
  379. * it then. If we get to the end without finding anything we return ‘true’.
  380. */
  381. possi= dep->list;
  382. nconflicts= 0;
  383. if (possi->ed != possi->up->up) {
  384. /* If the package conflicts with or breaks itself it must mean
  385. * other packages which provide the same virtual name. We
  386. * therefore don't look at the real package and go on to the
  387. * virtual ones.
  388. */
  389. switch (possi->ed->clientdata->istobe) {
  390. case itb_remove:
  391. break;
  392. case itb_installnew:
  393. if (!versionsatisfied(&possi->ed->available, possi)) break;
  394. sprintf(linebuf, _(" %.250s (version %.250s) is to be installed.\n"),
  395. possi->ed->name,
  396. versiondescribe(&possi->ed->available.version,vdew_nonambig));
  397. varbufaddstr(whynot, linebuf);
  398. if (!canfixbyremove)
  399. return false;
  400. nconflicts++;
  401. *canfixbyremove= possi->ed;
  402. break;
  403. case itb_deconfigure:
  404. if (dep->type == dep_breaks) break; /* already deconfiguring this */
  405. /* fall through */
  406. case itb_normal: case itb_preinstall:
  407. switch (possi->ed->status) {
  408. case stat_notinstalled: case stat_configfiles:
  409. break;
  410. case stat_halfinstalled: case stat_unpacked:
  411. case stat_halfconfigured:
  412. if (dep->type == dep_breaks) break; /* no problem */
  413. case stat_installed:
  414. case stat_triggerspending:
  415. case stat_triggersawaited:
  416. if (!versionsatisfied(&possi->ed->installed, possi)) break;
  417. sprintf(linebuf, _(" %.250s (version %.250s) is present and %s.\n"),
  418. possi->ed->name,
  419. versiondescribe(&possi->ed->installed.version,vdew_nonambig),
  420. gettext(statusstrings[possi->ed->status]));
  421. varbufaddstr(whynot, linebuf);
  422. if (!canfixbyremove)
  423. return false;
  424. nconflicts++;
  425. *canfixbyremove= possi->ed;
  426. }
  427. break;
  428. default:
  429. internerr("unknown istobe conflict '%d'",
  430. possi->ed->clientdata->istobe);
  431. }
  432. }
  433. /* If there was no version specified we try looking for Providers. */
  434. if (possi->verrel == dvr_none) {
  435. /* See if the package we're about to install Provides it. */
  436. for (provider= possi->ed->available.depended;
  437. provider;
  438. provider= provider->nextrev) {
  439. if (provider->up->type != dep_provides) continue;
  440. if (provider->up->up->clientdata->istobe != itb_installnew) continue;
  441. if (provider->up->up == dep->up) continue; /* conflicts and provides the same */
  442. sprintf(linebuf, _(" %.250s provides %.250s and is to be installed.\n"),
  443. provider->up->up->name, possi->ed->name);
  444. varbufaddstr(whynot, linebuf);
  445. /* We can't remove the one we're about to install: */
  446. if (canfixbyremove)
  447. *canfixbyremove = NULL;
  448. return false;
  449. }
  450. /* Now look at the packages already on the system. */
  451. for (provider= possi->ed->installed.depended;
  452. provider;
  453. provider= provider->nextrev) {
  454. if (provider->up->type != dep_provides) continue;
  455. if (provider->up->up == dep->up) continue; /* conflicts and provides the same */
  456. switch (provider->up->up->clientdata->istobe) {
  457. case itb_installnew:
  458. /* Don't pay any attention to the Provides field of the
  459. * currently-installed version of the package we're trying
  460. * to install. We dealt with that package by using the
  461. * available information above.
  462. */
  463. continue;
  464. case itb_remove:
  465. continue;
  466. case itb_deconfigure:
  467. if (dep->type == dep_breaks) continue; /* already deconfiguring */
  468. case itb_normal: case itb_preinstall:
  469. switch (provider->up->up->status) {
  470. case stat_notinstalled: case stat_configfiles:
  471. continue;
  472. case stat_halfinstalled: case stat_unpacked:
  473. case stat_halfconfigured:
  474. if (dep->type == dep_breaks) break; /* no problem */
  475. case stat_installed:
  476. case stat_triggerspending:
  477. case stat_triggersawaited:
  478. sprintf(linebuf,
  479. _(" %.250s provides %.250s and is present and %s.\n"),
  480. provider->up->up->name, possi->ed->name,
  481. gettext(statusstrings[provider->up->up->status]));
  482. varbufaddstr(whynot, linebuf);
  483. if (!canfixbyremove)
  484. return false;
  485. nconflicts++;
  486. *canfixbyremove= provider->up->up;
  487. break;
  488. }
  489. break;
  490. default:
  491. internerr("unknown istobe conflict provider '%d'",
  492. provider->up->up->clientdata->istobe);
  493. }
  494. }
  495. }
  496. if (!nconflicts)
  497. return true;
  498. if (nconflicts > 1)
  499. *canfixbyremove = NULL;
  500. return false;
  501. } /* if (dependency) {...} else {...} */
  502. }