depcon.c 18 KB

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