depcon.c 17 KB

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