depcon.c 19 KB

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