depcon.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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= 0;
  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, 0);
  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) *canfixbyremove= 0;
  198. /* The dependency is always OK if we're trying to remove the depend*ing*
  199. * package.
  200. */
  201. switch (dep->up->clientdata->istobe) {
  202. case itb_remove: case itb_deconfigure:
  203. return 1;
  204. case itb_normal:
  205. /* Only `installed' packages can be make dependency problems */
  206. switch (dep->up->status) {
  207. case stat_installed:
  208. break;
  209. case stat_notinstalled: case stat_configfiles: case stat_halfinstalled:
  210. case stat_halfconfigured: case stat_unpacked:
  211. return 1;
  212. default:
  213. internerr("unknown status depending");
  214. }
  215. break;
  216. case itb_installnew: case itb_preinstall:
  217. break;
  218. default:
  219. internerr("unknown istobe depending");
  220. }
  221. /* Describe the dependency, in case we have to moan about it. */
  222. varbufreset(whynot);
  223. varbufaddc(whynot, ' ');
  224. describedepcon(whynot, dep);
  225. varbufaddc(whynot,'\n');
  226. /* TODO: check dep_enhances as well (WTA) */
  227. if (dep->type == dep_depends || dep->type == dep_predepends ||
  228. dep->type == dep_recommends || dep->type == dep_suggests ) {
  229. /* Go through the alternatives. As soon as we find one that
  230. * we like, we return `1' straight away. Otherwise, when we get to
  231. * the end we'll have accumulated all the reasons in whynot and
  232. * can return `0'.
  233. */
  234. for (possi= dep->list; possi; possi= possi->next) {
  235. switch (possi->ed->clientdata->istobe) {
  236. case itb_remove:
  237. sprintf(linebuf,_(" %.250s is to be removed.\n"),possi->ed->name);
  238. break;
  239. case itb_deconfigure:
  240. sprintf(linebuf,_(" %.250s is to be deconfigured.\n"),possi->ed->name);
  241. break;
  242. case itb_installnew:
  243. if (versionsatisfied(&possi->ed->available,possi)) return 1;
  244. sprintf(linebuf,_(" %.250s is to be installed, but is version %.250s.\n"),
  245. possi->ed->name,
  246. versiondescribe(&possi->ed->available.version,vdew_nonambig));
  247. break;
  248. case itb_normal: case itb_preinstall:
  249. switch (possi->ed->status) {
  250. case stat_installed:
  251. if (versionsatisfied(&possi->ed->installed,possi)) return 1;
  252. sprintf(linebuf,_(" %.250s is installed, but is version %.250s.\n"),
  253. possi->ed->name,
  254. versiondescribe(&possi->ed->installed.version,vdew_nonambig));
  255. break;
  256. case stat_notinstalled:
  257. /* Don't say anything about this yet - it might be a virtual package.
  258. * Later on, if nothing has put anything in linebuf, we know that it
  259. * isn't and issue a diagnostic then.
  260. */
  261. *linebuf= 0;
  262. break;
  263. case stat_unpacked:
  264. case stat_halfconfigured:
  265. if (allowunconfigd) {
  266. if (!informativeversion(&possi->ed->configversion)) {
  267. sprintf(linebuf, _(" %.250s is unpacked, but has never been configured.\n"),
  268. possi->ed->name);
  269. break;
  270. } else if (!versionsatisfied(&possi->ed->installed, possi)) {
  271. sprintf(linebuf, _(" %.250s is unpacked, but is version %.250s.\n"),
  272. possi->ed->name,
  273. versiondescribe(&possi->ed->available.version,vdew_nonambig));
  274. break;
  275. } else if (!versionsatisfied3(&possi->ed->configversion,
  276. &possi->version,possi->verrel)) {
  277. sprintf(linebuf, _(" %.250s latest configured version is %.250s.\n"),
  278. possi->ed->name,
  279. versiondescribe(&possi->ed->configversion,vdew_nonambig));
  280. break;
  281. } else {
  282. return 1;
  283. }
  284. }
  285. default:
  286. sprintf(linebuf, _(" %.250s is %s.\n"),
  287. possi->ed->name, gettext(statusstrings[possi->ed->status]));
  288. break;
  289. }
  290. break;
  291. default:
  292. internerr("unknown istobe depended");
  293. }
  294. varbufaddstr(whynot, linebuf);
  295. /* If there was no version specified we try looking for Providers. */
  296. if (possi->verrel == dvr_none) {
  297. /* See if the package we're about to install Provides it. */
  298. for (provider= possi->ed->available.depended;
  299. provider;
  300. provider= provider->nextrev) {
  301. if (provider->up->type != dep_provides) continue;
  302. if (provider->up->up->clientdata->istobe == itb_installnew) return 1;
  303. }
  304. /* Now look at the packages already on the system. */
  305. for (provider= possi->ed->installed.depended;
  306. provider;
  307. provider= provider->nextrev) {
  308. if (provider->up->type != dep_provides) continue;
  309. switch (provider->up->up->clientdata->istobe) {
  310. case itb_installnew:
  311. /* Don't pay any attention to the Provides field of the
  312. * currently-installed version of the package we're trying
  313. * to install. We dealt with that by using the available
  314. * information above.
  315. */
  316. continue;
  317. case itb_remove:
  318. sprintf(linebuf, _(" %.250s provides %.250s but is to be removed.\n"),
  319. provider->up->up->name, possi->ed->name);
  320. break;
  321. case itb_deconfigure:
  322. sprintf(linebuf, _(" %.250s provides %.250s but is to be deconfigured.\n"),
  323. provider->up->up->name, possi->ed->name);
  324. break;
  325. case itb_normal: case itb_preinstall:
  326. if (provider->up->up->status == stat_installed) return 1;
  327. sprintf(linebuf, _(" %.250s provides %.250s but is %s.\n"),
  328. provider->up->up->name, possi->ed->name,
  329. gettext(statusstrings[provider->up->up->status]));
  330. break;
  331. default:
  332. internerr("unknown istobe provider");
  333. }
  334. varbufaddstr(whynot, linebuf);
  335. }
  336. if (!*linebuf) {
  337. /* If the package wasn't installed at all, and we haven't said
  338. * yet why this isn't satisfied, we should say so now.
  339. */
  340. sprintf(linebuf, _(" %.250s is not installed.\n"), possi->ed->name);
  341. varbufaddstr(whynot, linebuf);
  342. }
  343. }
  344. }
  345. return 0;
  346. } else {
  347. /* It's conflicts or breaks. There's only one main alternative,
  348. * but we also have to consider Providers. We return `0' as soon
  349. * as we find something that matches the conflict, and only describe
  350. * it then. If we get to the end without finding anything we return `1'.
  351. */
  352. possi= dep->list;
  353. nconflicts= 0;
  354. if (possi->ed != possi->up->up) {
  355. /* If the package conflicts with or breaks itself it must mean
  356. * other packages which provide the same virtual name. We
  357. * therefore don't look at the real package and go on to the
  358. * virtual ones.
  359. */
  360. switch (possi->ed->clientdata->istobe) {
  361. case itb_remove:
  362. break;
  363. case itb_installnew:
  364. if (!versionsatisfied(&possi->ed->available, possi)) break;
  365. sprintf(linebuf, _(" %.250s (version %.250s) is to be installed.\n"),
  366. possi->ed->name,
  367. versiondescribe(&possi->ed->available.version,vdew_nonambig));
  368. varbufaddstr(whynot, linebuf);
  369. if (!canfixbyremove) return 0;
  370. nconflicts++;
  371. *canfixbyremove= possi->ed;
  372. break;
  373. case itb_deconfigure:
  374. if (dep->type == dep_breaks) break; /* already deconfiguring this */
  375. /* fall through */
  376. case itb_normal: case itb_preinstall:
  377. switch (possi->ed->status) {
  378. case stat_notinstalled: case stat_configfiles:
  379. break;
  380. case stat_halfinstalled: case stat_unpacked:
  381. case stat_halfconfigured:
  382. if (dep->type == dep_breaks) break; /* no problem */
  383. case stat_installed:
  384. if (!versionsatisfied(&possi->ed->installed, possi)) break;
  385. sprintf(linebuf, _(" %.250s (version %.250s) is %s.\n"),
  386. possi->ed->name,
  387. versiondescribe(&possi->ed->installed.version,vdew_nonambig),
  388. gettext(statusstrings[possi->ed->status]));
  389. varbufaddstr(whynot, linebuf);
  390. if (!canfixbyremove) return 0;
  391. nconflicts++;
  392. *canfixbyremove= possi->ed;
  393. }
  394. break;
  395. default:
  396. internerr("unknown istobe conflict");
  397. }
  398. }
  399. /* If there was no version specified we try looking for Providers. */
  400. if (possi->verrel == dvr_none) {
  401. /* See if the package we're about to install Provides it. */
  402. for (provider= possi->ed->available.depended;
  403. provider;
  404. provider= provider->nextrev) {
  405. if (provider->up->type != dep_provides) continue;
  406. if (provider->up->up->clientdata->istobe != itb_installnew) continue;
  407. if (provider->up->up == dep->up) continue; /* conflicts and provides the same */
  408. sprintf(linebuf, _(" %.250s provides %.250s and is to be installed.\n"),
  409. provider->up->up->name, possi->ed->name);
  410. varbufaddstr(whynot, linebuf);
  411. /* We can't remove the one we're about to install: */
  412. if (canfixbyremove) *canfixbyremove= 0;
  413. return 0;
  414. }
  415. /* Now look at the packages already on the system. */
  416. for (provider= possi->ed->installed.depended;
  417. provider;
  418. provider= provider->nextrev) {
  419. if (provider->up->type != dep_provides) continue;
  420. if (provider->up->up == dep->up) continue; /* conflicts and provides the same */
  421. switch (provider->up->up->clientdata->istobe) {
  422. case itb_installnew:
  423. /* Don't pay any attention to the Provides field of the
  424. * currently-installed version of the package we're trying
  425. * to install. We dealt with that package by using the
  426. * available information above.
  427. */
  428. continue;
  429. case itb_remove:
  430. continue;
  431. case itb_deconfigure:
  432. if (dep->type == dep_breaks) continue; /* already deconfiguring */
  433. case itb_normal: case itb_preinstall:
  434. switch (provider->up->up->status) {
  435. case stat_notinstalled: case stat_configfiles:
  436. continue;
  437. case stat_halfinstalled: case stat_unpacked:
  438. case stat_halfconfigured:
  439. if (dep->type == dep_breaks) break; /* no problem */
  440. case stat_installed:
  441. sprintf(linebuf, _(" %.250s provides %.250s and is %s.\n"),
  442. provider->up->up->name, possi->ed->name,
  443. gettext(statusstrings[provider->up->up->status]));
  444. varbufaddstr(whynot, linebuf);
  445. if (!canfixbyremove) return 0;
  446. nconflicts++;
  447. *canfixbyremove= provider->up->up;
  448. break;
  449. }
  450. break;
  451. default:
  452. internerr("unknown istobe conflict provider");
  453. }
  454. }
  455. }
  456. if (!nconflicts) return 1;
  457. if (nconflicts>1) *canfixbyremove= 0;
  458. return 0;
  459. } /* if (dependency) {...} else {...} */
  460. }