depcon.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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.h>
  30. #include <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. return findbreakcyclerecursive(pkg, NULL);
  141. }
  142. void describedepcon(struct varbuf *addto, struct dependency *dep) {
  143. const char *fmt;
  144. struct varbuf depstr = VARBUF_INIT;
  145. switch (dep->type) {
  146. case dep_depends:
  147. fmt = _("%s depends on %s");
  148. break;
  149. case dep_predepends:
  150. fmt = _("%s pre-depends on %s");
  151. break;
  152. case dep_recommends:
  153. fmt = _("%s recommends %s");
  154. break;
  155. case dep_suggests:
  156. fmt = _("%s suggests %s");
  157. break;
  158. case dep_breaks:
  159. fmt = _("%s breaks %s");
  160. break;
  161. case dep_conflicts:
  162. fmt = _("%s conflicts with %s");
  163. break;
  164. case dep_enhances:
  165. fmt = _("%s enhances %s");
  166. break;
  167. default:
  168. internerr("unknown deptype '%d'", dep->type);
  169. }
  170. varbufdependency(&depstr, dep);
  171. varbufaddc(&depstr, 0);
  172. varbufprintf(addto, fmt, dep->up->name, depstr.buf);
  173. varbuffree(&depstr);
  174. }
  175. int depisok(struct dependency *dep, struct varbuf *whynot,
  176. struct pkginfo **canfixbyremove, int allowunconfigd) {
  177. /* *whynot must already have been initialised; it need not be
  178. * empty though - it will be reset before use.
  179. * If depisok returns 0 for `not OK' it will contain a description,
  180. * newline-terminated BUT NOT NULL-TERMINATED, of the reason.
  181. * If depisok returns 1 it will contain garbage.
  182. * allowunconfigd should be non-zero during the `Pre-Depends' checking
  183. * before a package is unpacked, when it is sufficient for the package
  184. * to be unpacked provided that both the unpacked and previously-configured
  185. * versions are acceptable.
  186. * On 0 return (`not OK'), *canfixbyremove refers to a package which
  187. * if removed (dep_conflicts) or deconfigured (dep_breaks) will fix
  188. * the problem. Caller may pass 0 for canfixbyremove and need not
  189. * initialise *canfixbyremove.
  190. */
  191. struct deppossi *possi;
  192. struct deppossi *provider;
  193. int nconflicts;
  194. /* Use this buffer so that when internationalisation comes along we
  195. * don't have to rewrite the code completely, only redo the sprintf strings
  196. * (assuming we have the fancy argument-number-specifiers).
  197. * Allow 250x3 for package names, versions, &c, + 250 for ourselves.
  198. */
  199. char linebuf[1024];
  200. assert(dep->type == dep_depends || dep->type == dep_predepends ||
  201. dep->type == dep_breaks || dep->type == dep_conflicts ||
  202. dep->type == dep_recommends || dep->type == dep_suggests ||
  203. dep->type == dep_enhances);
  204. if (canfixbyremove)
  205. *canfixbyremove = NULL;
  206. /* The dependency is always OK if we're trying to remove the depend*ing*
  207. * package.
  208. */
  209. switch (dep->up->clientdata->istobe) {
  210. case itb_remove: case itb_deconfigure:
  211. return 1;
  212. case itb_normal:
  213. /* Only installed packages can be make dependency problems */
  214. switch (dep->up->status) {
  215. case stat_installed:
  216. case stat_triggerspending:
  217. case stat_triggersawaited:
  218. break;
  219. case stat_notinstalled: case stat_configfiles: case stat_halfinstalled:
  220. case stat_halfconfigured: case stat_unpacked:
  221. return 1;
  222. default:
  223. internerr("unknown status depending '%d'", dep->up->status);
  224. }
  225. break;
  226. case itb_installnew: case itb_preinstall:
  227. break;
  228. default:
  229. internerr("unknown istobe depending '%d'", dep->up->clientdata->istobe);
  230. }
  231. /* Describe the dependency, in case we have to moan about it. */
  232. varbufreset(whynot);
  233. varbufaddc(whynot, ' ');
  234. describedepcon(whynot, dep);
  235. varbufaddc(whynot,'\n');
  236. /* TODO: check dep_enhances as well (WTA) */
  237. if (dep->type == dep_depends || dep->type == dep_predepends ||
  238. dep->type == dep_recommends || dep->type == dep_suggests ) {
  239. /* Go through the alternatives. As soon as we find one that
  240. * we like, we return `1' straight away. Otherwise, when we get to
  241. * the end we'll have accumulated all the reasons in whynot and
  242. * can return `0'.
  243. */
  244. for (possi= dep->list; possi; possi= possi->next) {
  245. switch (possi->ed->clientdata->istobe) {
  246. case itb_remove:
  247. sprintf(linebuf,_(" %.250s is to be removed.\n"),possi->ed->name);
  248. break;
  249. case itb_deconfigure:
  250. sprintf(linebuf,_(" %.250s is to be deconfigured.\n"),possi->ed->name);
  251. break;
  252. case itb_installnew:
  253. if (versionsatisfied(&possi->ed->available,possi)) return 1;
  254. sprintf(linebuf,_(" %.250s is to be installed, but is version %.250s.\n"),
  255. possi->ed->name,
  256. versiondescribe(&possi->ed->available.version,vdew_nonambig));
  257. break;
  258. case itb_normal: case itb_preinstall:
  259. switch (possi->ed->status) {
  260. case stat_installed:
  261. case stat_triggerspending:
  262. if (versionsatisfied(&possi->ed->installed,possi)) return 1;
  263. sprintf(linebuf,_(" %.250s is installed, but is version %.250s.\n"),
  264. possi->ed->name,
  265. versiondescribe(&possi->ed->installed.version,vdew_nonambig));
  266. break;
  267. case stat_notinstalled:
  268. /* Don't say anything about this yet - it might be a virtual package.
  269. * Later on, if nothing has put anything in linebuf, we know that it
  270. * isn't and issue a diagnostic then.
  271. */
  272. *linebuf= 0;
  273. break;
  274. case stat_unpacked:
  275. case stat_halfconfigured:
  276. case stat_triggersawaited:
  277. if (allowunconfigd) {
  278. if (!informativeversion(&possi->ed->configversion)) {
  279. sprintf(linebuf, _(" %.250s is unpacked, but has never been configured.\n"),
  280. possi->ed->name);
  281. break;
  282. } else if (!versionsatisfied(&possi->ed->installed, possi)) {
  283. sprintf(linebuf, _(" %.250s is unpacked, but is version %.250s.\n"),
  284. possi->ed->name,
  285. versiondescribe(&possi->ed->available.version,vdew_nonambig));
  286. break;
  287. } else if (!versionsatisfied3(&possi->ed->configversion,
  288. &possi->version,possi->verrel)) {
  289. sprintf(linebuf, _(" %.250s latest configured version is %.250s.\n"),
  290. possi->ed->name,
  291. versiondescribe(&possi->ed->configversion,vdew_nonambig));
  292. break;
  293. } else {
  294. return 1;
  295. }
  296. }
  297. /* Fall through. */
  298. default:
  299. sprintf(linebuf, _(" %.250s is %s.\n"),
  300. possi->ed->name, gettext(statusstrings[possi->ed->status]));
  301. break;
  302. }
  303. break;
  304. default:
  305. internerr("unknown istobe depended '%d'", possi->ed->clientdata->istobe);
  306. }
  307. varbufaddstr(whynot, linebuf);
  308. /* If there was no version specified we try looking for Providers. */
  309. if (possi->verrel == dvr_none) {
  310. /* See if the package we're about to install Provides it. */
  311. for (provider= possi->ed->available.depended;
  312. provider;
  313. provider= provider->nextrev) {
  314. if (provider->up->type != dep_provides) continue;
  315. if (provider->up->up->clientdata->istobe == itb_installnew) return 1;
  316. }
  317. /* Now look at the packages already on the system. */
  318. for (provider= possi->ed->installed.depended;
  319. provider;
  320. provider= provider->nextrev) {
  321. if (provider->up->type != dep_provides) continue;
  322. switch (provider->up->up->clientdata->istobe) {
  323. case itb_installnew:
  324. /* Don't pay any attention to the Provides field of the
  325. * currently-installed version of the package we're trying
  326. * to install. We dealt with that by using the available
  327. * information above.
  328. */
  329. continue;
  330. case itb_remove:
  331. sprintf(linebuf, _(" %.250s provides %.250s but is to be removed.\n"),
  332. provider->up->up->name, possi->ed->name);
  333. break;
  334. case itb_deconfigure:
  335. sprintf(linebuf, _(" %.250s provides %.250s but is to be deconfigured.\n"),
  336. provider->up->up->name, possi->ed->name);
  337. break;
  338. case itb_normal: case itb_preinstall:
  339. if (provider->up->up->status == stat_installed) return 1;
  340. sprintf(linebuf, _(" %.250s provides %.250s but is %s.\n"),
  341. provider->up->up->name, possi->ed->name,
  342. gettext(statusstrings[provider->up->up->status]));
  343. break;
  344. default:
  345. internerr("unknown istobe provider '%d'",
  346. provider->up->up->clientdata->istobe);
  347. }
  348. varbufaddstr(whynot, linebuf);
  349. }
  350. if (!*linebuf) {
  351. /* If the package wasn't installed at all, and we haven't said
  352. * yet why this isn't satisfied, we should say so now.
  353. */
  354. sprintf(linebuf, _(" %.250s is not installed.\n"), possi->ed->name);
  355. varbufaddstr(whynot, linebuf);
  356. }
  357. }
  358. }
  359. return 0;
  360. } else {
  361. /* It's conflicts or breaks. There's only one main alternative,
  362. * but we also have to consider Providers. We return `0' as soon
  363. * as we find something that matches the conflict, and only describe
  364. * it then. If we get to the end without finding anything we return `1'.
  365. */
  366. possi= dep->list;
  367. nconflicts= 0;
  368. if (possi->ed != possi->up->up) {
  369. /* If the package conflicts with or breaks itself it must mean
  370. * other packages which provide the same virtual name. We
  371. * therefore don't look at the real package and go on to the
  372. * virtual ones.
  373. */
  374. switch (possi->ed->clientdata->istobe) {
  375. case itb_remove:
  376. break;
  377. case itb_installnew:
  378. if (!versionsatisfied(&possi->ed->available, possi)) break;
  379. sprintf(linebuf, _(" %.250s (version %.250s) is to be installed.\n"),
  380. possi->ed->name,
  381. versiondescribe(&possi->ed->available.version,vdew_nonambig));
  382. varbufaddstr(whynot, linebuf);
  383. if (!canfixbyremove) return 0;
  384. nconflicts++;
  385. *canfixbyremove= possi->ed;
  386. break;
  387. case itb_deconfigure:
  388. if (dep->type == dep_breaks) break; /* already deconfiguring this */
  389. /* fall through */
  390. case itb_normal: case itb_preinstall:
  391. switch (possi->ed->status) {
  392. case stat_notinstalled: case stat_configfiles:
  393. break;
  394. case stat_halfinstalled: case stat_unpacked:
  395. case stat_halfconfigured:
  396. if (dep->type == dep_breaks) break; /* no problem */
  397. case stat_installed:
  398. case stat_triggerspending:
  399. case stat_triggersawaited:
  400. if (!versionsatisfied(&possi->ed->installed, possi)) break;
  401. sprintf(linebuf, _(" %.250s (version %.250s) is present and %s.\n"),
  402. possi->ed->name,
  403. versiondescribe(&possi->ed->installed.version,vdew_nonambig),
  404. gettext(statusstrings[possi->ed->status]));
  405. varbufaddstr(whynot, linebuf);
  406. if (!canfixbyremove) return 0;
  407. nconflicts++;
  408. *canfixbyremove= possi->ed;
  409. }
  410. break;
  411. default:
  412. internerr("unknown istobe conflict '%d'",
  413. possi->ed->clientdata->istobe);
  414. }
  415. }
  416. /* If there was no version specified we try looking for Providers. */
  417. if (possi->verrel == dvr_none) {
  418. /* See if the package we're about to install Provides it. */
  419. for (provider= possi->ed->available.depended;
  420. provider;
  421. provider= provider->nextrev) {
  422. if (provider->up->type != dep_provides) continue;
  423. if (provider->up->up->clientdata->istobe != itb_installnew) continue;
  424. if (provider->up->up == dep->up) continue; /* conflicts and provides the same */
  425. sprintf(linebuf, _(" %.250s provides %.250s and is to be installed.\n"),
  426. provider->up->up->name, possi->ed->name);
  427. varbufaddstr(whynot, linebuf);
  428. /* We can't remove the one we're about to install: */
  429. if (canfixbyremove)
  430. *canfixbyremove = NULL;
  431. return 0;
  432. }
  433. /* Now look at the packages already on the system. */
  434. for (provider= possi->ed->installed.depended;
  435. provider;
  436. provider= provider->nextrev) {
  437. if (provider->up->type != dep_provides) continue;
  438. if (provider->up->up == dep->up) continue; /* conflicts and provides the same */
  439. switch (provider->up->up->clientdata->istobe) {
  440. case itb_installnew:
  441. /* Don't pay any attention to the Provides field of the
  442. * currently-installed version of the package we're trying
  443. * to install. We dealt with that package by using the
  444. * available information above.
  445. */
  446. continue;
  447. case itb_remove:
  448. continue;
  449. case itb_deconfigure:
  450. if (dep->type == dep_breaks) continue; /* already deconfiguring */
  451. case itb_normal: case itb_preinstall:
  452. switch (provider->up->up->status) {
  453. case stat_notinstalled: case stat_configfiles:
  454. continue;
  455. case stat_halfinstalled: case stat_unpacked:
  456. case stat_halfconfigured:
  457. if (dep->type == dep_breaks) break; /* no problem */
  458. case stat_installed:
  459. case stat_triggerspending:
  460. case stat_triggersawaited:
  461. sprintf(linebuf,
  462. _(" %.250s provides %.250s and is present and %s.\n"),
  463. provider->up->up->name, possi->ed->name,
  464. gettext(statusstrings[provider->up->up->status]));
  465. varbufaddstr(whynot, linebuf);
  466. if (!canfixbyremove) return 0;
  467. nconflicts++;
  468. *canfixbyremove= provider->up->up;
  469. break;
  470. }
  471. break;
  472. default:
  473. internerr("unknown istobe conflict provider '%d'",
  474. provider->up->up->clientdata->istobe);
  475. }
  476. }
  477. }
  478. if (!nconflicts) return 1;
  479. if (nconflicts > 1)
  480. *canfixbyremove = NULL;
  481. return 0;
  482. } /* if (dependency) {...} else {...} */
  483. }