depcon.c 17 KB

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