depcon.c 17 KB

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