depcon.c 19 KB

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