algorithms.cc 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: algorithms.cc,v 1.32 2001/02/20 07:03:17 jgg Exp $
  4. /* ######################################################################
  5. Algorithms - A set of misc algorithms
  6. The pkgProblemResolver class has become insanely complex and
  7. very sophisticated, it handles every test case I have thrown at it
  8. to my satisfaction. Understanding exactly why all the steps the class
  9. does are required is difficult and changing though not very risky
  10. may result in other cases not working.
  11. ##################################################################### */
  12. /*}}}*/
  13. // Include Files /*{{{*/
  14. #ifdef __GNUG__
  15. #pragma implementation "apt-pkg/algorithms.h"
  16. #endif
  17. #include <apt-pkg/algorithms.h>
  18. #include <apt-pkg/error.h>
  19. #include <apt-pkg/configuration.h>
  20. #include <apt-pkg/sptr.h>
  21. #include <apti18n.h>
  22. #include <iostream.h>
  23. /*}}}*/
  24. pkgProblemResolver *pkgProblemResolver::This = 0;
  25. // Simulate::Simulate - Constructor /*{{{*/
  26. // ---------------------------------------------------------------------
  27. /* The legacy translations here of input Pkg iterators is obsolete,
  28. this is not necessary since the pkgCaches are fully shared now. */
  29. pkgSimulate::pkgSimulate(pkgDepCache *Cache) : pkgPackageManager(Cache),
  30. iPolicy(Cache),
  31. Sim(&Cache->GetCache(),&iPolicy)
  32. {
  33. Sim.Init(0);
  34. Flags = new unsigned char[Cache->Head().PackageCount];
  35. memset(Flags,0,sizeof(*Flags)*Cache->Head().PackageCount);
  36. // Fake a filename so as not to activate the media swapping
  37. string Jnk = "SIMULATE";
  38. for (unsigned int I = 0; I != Cache->Head().PackageCount; I++)
  39. FileNames[I] = Jnk;
  40. }
  41. /*}}}*/
  42. // Simulate::Describe - Describe a package /*{{{*/
  43. // ---------------------------------------------------------------------
  44. /* */
  45. void pkgSimulate::Describe(PkgIterator Pkg,ostream &out,bool Now)
  46. {
  47. VerIterator Ver(Sim);
  48. if (Now == true)
  49. Ver = Pkg.CurrentVer();
  50. else
  51. Ver = Sim[Pkg].CandidateVerIter(Sim);
  52. out << Pkg.Name();
  53. if (Ver.end() == true)
  54. return;
  55. out << " (" << Ver.VerStr() << ' ' << Ver.RelStr() << ')';
  56. }
  57. /*}}}*/
  58. // Simulate::Install - Simulate unpacking of a package /*{{{*/
  59. // ---------------------------------------------------------------------
  60. /* */
  61. bool pkgSimulate::Install(PkgIterator iPkg,string /*File*/)
  62. {
  63. // Adapt the iterator
  64. PkgIterator Pkg = Sim.FindPkg(iPkg.Name());
  65. Flags[Pkg->ID] = 1;
  66. cout << "Inst ";
  67. Describe(Pkg,cout,false);
  68. Sim.MarkInstall(Pkg,false);
  69. // Look for broken conflicts+predepends.
  70. for (PkgIterator I = Sim.PkgBegin(); I.end() == false; I++)
  71. {
  72. if (Sim[I].InstallVer == 0)
  73. continue;
  74. for (DepIterator D = Sim[I].InstVerIter(Sim).DependsList(); D.end() == false;)
  75. {
  76. DepIterator Start;
  77. DepIterator End;
  78. D.GlobOr(Start,End);
  79. if (Start->Type == pkgCache::Dep::Conflicts ||
  80. Start->Type == pkgCache::Dep::Obsoletes ||
  81. End->Type == pkgCache::Dep::PreDepends)
  82. {
  83. if ((Sim[End] & pkgDepCache::DepGInstall) == 0)
  84. {
  85. cout << " [" << I.Name() << " on " << Start.TargetPkg().Name() << ']';
  86. if (Start->Type == pkgCache::Dep::Conflicts)
  87. _error->Error("Fatal, conflicts violated %s",I.Name());
  88. }
  89. }
  90. }
  91. }
  92. if (Sim.BrokenCount() != 0)
  93. ShortBreaks();
  94. else
  95. cout << endl;
  96. return true;
  97. }
  98. /*}}}*/
  99. // Simulate::Configure - Simulate configuration of a Package /*{{{*/
  100. // ---------------------------------------------------------------------
  101. /* This is not an acurate simulation of relatity, we should really not
  102. install the package.. For some investigations it may be necessary
  103. however. */
  104. bool pkgSimulate::Configure(PkgIterator iPkg)
  105. {
  106. // Adapt the iterator
  107. PkgIterator Pkg = Sim.FindPkg(iPkg.Name());
  108. Flags[Pkg->ID] = 2;
  109. // Sim.MarkInstall(Pkg,false);
  110. if (Sim[Pkg].InstBroken() == true)
  111. {
  112. cout << "Conf " << Pkg.Name() << " broken" << endl;
  113. Sim.Update();
  114. // Print out each package and the failed dependencies
  115. for (pkgCache::DepIterator D = Sim[Pkg].InstVerIter(Sim).DependsList(); D.end() == false; D++)
  116. {
  117. if (Sim.IsImportantDep(D) == false ||
  118. (Sim[D] & pkgDepCache::DepInstall) != 0)
  119. continue;
  120. if (D->Type == pkgCache::Dep::Obsoletes)
  121. cout << " Obsoletes:" << D.TargetPkg().Name();
  122. else if (D->Type == pkgCache::Dep::Conflicts)
  123. cout << " Conflicts:" << D.TargetPkg().Name();
  124. else
  125. cout << " Depends:" << D.TargetPkg().Name();
  126. }
  127. cout << endl;
  128. _error->Error("Conf Broken %s",Pkg.Name());
  129. }
  130. else
  131. {
  132. cout << "Conf ";
  133. Describe(Pkg,cout,false);
  134. }
  135. if (Sim.BrokenCount() != 0)
  136. ShortBreaks();
  137. else
  138. cout << endl;
  139. return true;
  140. }
  141. /*}}}*/
  142. // Simulate::Remove - Simulate the removal of a package /*{{{*/
  143. // ---------------------------------------------------------------------
  144. /* */
  145. bool pkgSimulate::Remove(PkgIterator iPkg,bool Purge)
  146. {
  147. // Adapt the iterator
  148. PkgIterator Pkg = Sim.FindPkg(iPkg.Name());
  149. Flags[Pkg->ID] = 3;
  150. Sim.MarkDelete(Pkg);
  151. if (Purge == true)
  152. cout << "Purg ";
  153. else
  154. cout << "Remv ";
  155. Describe(Pkg,cout,false);
  156. if (Sim.BrokenCount() != 0)
  157. ShortBreaks();
  158. else
  159. cout << endl;
  160. return true;
  161. }
  162. /*}}}*/
  163. // Simulate::ShortBreaks - Print out a short line describing all breaks /*{{{*/
  164. // ---------------------------------------------------------------------
  165. /* */
  166. void pkgSimulate::ShortBreaks()
  167. {
  168. cout << " [";
  169. for (PkgIterator I = Sim.PkgBegin(); I.end() == false; I++)
  170. {
  171. if (Sim[I].InstBroken() == true)
  172. {
  173. if (Flags[I->ID] == 0)
  174. cout << I.Name() << ' ';
  175. /* else
  176. cout << I.Name() << "! ";*/
  177. }
  178. }
  179. cout << ']' << endl;
  180. }
  181. /*}}}*/
  182. // ApplyStatus - Adjust for non-ok packages /*{{{*/
  183. // ---------------------------------------------------------------------
  184. /* We attempt to change the state of the all packages that have failed
  185. installation toward their real state. The ordering code will perform
  186. the necessary calculations to deal with the problems. */
  187. bool pkgApplyStatus(pkgDepCache &Cache)
  188. {
  189. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  190. {
  191. // Only choice for a ReInstReq package is to reinstall
  192. if (I->InstState == pkgCache::State::ReInstReq ||
  193. I->InstState == pkgCache::State::HoldReInstReq)
  194. {
  195. if (I.CurrentVer().Downloadable() == true)
  196. Cache.MarkKeep(I);
  197. else
  198. {
  199. // Is this right? Will dpkg choke on an upgrade?
  200. if (Cache[I].CandidateVerIter(Cache).Downloadable() == true)
  201. Cache.MarkInstall(I);
  202. else
  203. return _error->Error(_("The package %s needs to be reinstalled, "
  204. "but I can't find an archive for it."),I.Name());
  205. }
  206. continue;
  207. }
  208. switch (I->CurrentState)
  209. {
  210. /* This means installation failed somehow - it does not need to be
  211. re-unpacked (probably) */
  212. case pkgCache::State::UnPacked:
  213. case pkgCache::State::HalfConfigured:
  214. if (I.CurrentVer().Downloadable() == true ||
  215. I.State() != pkgCache::PkgIterator::NeedsUnpack)
  216. Cache.MarkKeep(I);
  217. else
  218. {
  219. if (Cache[I].CandidateVerIter(Cache).Downloadable() == true)
  220. Cache.MarkInstall(I);
  221. else
  222. Cache.MarkDelete(I);
  223. }
  224. break;
  225. // This means removal failed
  226. case pkgCache::State::HalfInstalled:
  227. Cache.MarkDelete(I);
  228. break;
  229. default:
  230. if (I->InstState != pkgCache::State::Ok)
  231. return _error->Error("The package %s is not ok and I "
  232. "don't know how to fix it!",I.Name());
  233. }
  234. }
  235. return true;
  236. }
  237. /*}}}*/
  238. // FixBroken - Fix broken packages /*{{{*/
  239. // ---------------------------------------------------------------------
  240. /* This autoinstalls every broken package and then runs the problem resolver
  241. on the result. */
  242. bool pkgFixBroken(pkgDepCache &Cache)
  243. {
  244. // Auto upgrade all broken packages
  245. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  246. if (Cache[I].NowBroken() == true)
  247. Cache.MarkInstall(I,true);
  248. /* Fix packages that are in a NeedArchive state but don't have a
  249. downloadable install version */
  250. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  251. {
  252. if (I.State() != pkgCache::PkgIterator::NeedsUnpack ||
  253. Cache[I].Delete() == true)
  254. continue;
  255. if (Cache[I].InstVerIter(Cache).Downloadable() == false)
  256. continue;
  257. Cache.MarkInstall(I,true);
  258. }
  259. pkgProblemResolver Fix(&Cache);
  260. return Fix.Resolve(true);
  261. }
  262. /*}}}*/
  263. // DistUpgrade - Distribution upgrade /*{{{*/
  264. // ---------------------------------------------------------------------
  265. /* This autoinstalls every package and then force installs every
  266. pre-existing package. This creates the initial set of conditions which
  267. most likely contain problems because too many things were installed.
  268. The problem resolver is used to resolve the problems.
  269. */
  270. bool pkgDistUpgrade(pkgDepCache &Cache)
  271. {
  272. /* Auto upgrade all installed packages, this provides the basis
  273. for the installation */
  274. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  275. if (I->CurrentVer != 0)
  276. Cache.MarkInstall(I,true);
  277. /* Now, auto upgrade all essential packages - this ensures that
  278. the essential packages are present and working */
  279. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  280. if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  281. Cache.MarkInstall(I,true);
  282. /* We do it again over all previously installed packages to force
  283. conflict resolution on them all. */
  284. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  285. if (I->CurrentVer != 0)
  286. Cache.MarkInstall(I,false);
  287. pkgProblemResolver Fix(&Cache);
  288. // Hold back held packages.
  289. if (_config->FindB("APT::Ignore-Hold",false) == false)
  290. {
  291. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  292. {
  293. if (I->SelectedState == pkgCache::State::Hold)
  294. {
  295. Fix.Protect(I);
  296. Cache.MarkKeep(I);
  297. }
  298. }
  299. }
  300. return Fix.Resolve();
  301. }
  302. /*}}}*/
  303. // AllUpgrade - Upgrade as many packages as possible /*{{{*/
  304. // ---------------------------------------------------------------------
  305. /* Right now the system must be consistent before this can be called.
  306. It also will not change packages marked for install, it only tries
  307. to install packages not marked for install */
  308. bool pkgAllUpgrade(pkgDepCache &Cache)
  309. {
  310. pkgProblemResolver Fix(&Cache);
  311. if (Cache.BrokenCount() != 0)
  312. return false;
  313. // Upgrade all installed packages
  314. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  315. {
  316. if (Cache[I].Install() == true)
  317. Fix.Protect(I);
  318. if (_config->FindB("APT::Ignore-Hold",false) == false)
  319. if (I->SelectedState == pkgCache::State::Hold)
  320. continue;
  321. if (I->CurrentVer != 0 && Cache[I].InstallVer != 0)
  322. Cache.MarkInstall(I,false);
  323. }
  324. return Fix.ResolveByKeep();
  325. }
  326. /*}}}*/
  327. // MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
  328. // ---------------------------------------------------------------------
  329. /* This simply goes over the entire set of packages and tries to keep
  330. each package marked for upgrade. If a conflict is generated then
  331. the package is restored. */
  332. bool pkgMinimizeUpgrade(pkgDepCache &Cache)
  333. {
  334. if (Cache.BrokenCount() != 0)
  335. return false;
  336. // We loop indefinately to get the minimal set size.
  337. bool Change = false;
  338. unsigned int Count = 0;
  339. do
  340. {
  341. Change = false;
  342. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  343. {
  344. // Not interesting
  345. if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
  346. continue;
  347. // Keep it and see if that is OK
  348. Cache.MarkKeep(I);
  349. if (Cache.BrokenCount() != 0)
  350. Cache.MarkInstall(I,false);
  351. else
  352. {
  353. // If keep didnt actually do anything then there was no change..
  354. if (Cache[I].Upgrade() == false)
  355. Change = true;
  356. }
  357. }
  358. Count++;
  359. }
  360. while (Change == true && Count < 10);
  361. if (Cache.BrokenCount() != 0)
  362. return _error->Error("Internal Error in pkgMinimizeUpgrade");
  363. return true;
  364. }
  365. /*}}}*/
  366. // ProblemResolver::pkgProblemResolver - Constructor /*{{{*/
  367. // ---------------------------------------------------------------------
  368. /* */
  369. pkgProblemResolver::pkgProblemResolver(pkgDepCache *pCache) : Cache(*pCache)
  370. {
  371. // Allocate memory
  372. unsigned long Size = Cache.Head().PackageCount;
  373. Scores = new signed short[Size];
  374. Flags = new unsigned char[Size];
  375. memset(Flags,0,sizeof(*Flags)*Size);
  376. // Set debug to true to see its decision logic
  377. Debug = _config->FindB("Debug::pkgProblemResolver",false);
  378. }
  379. /*}}}*/
  380. // ProblemResolver::~pkgProblemResolver - Destructor /*{{{*/
  381. // ---------------------------------------------------------------------
  382. /* */
  383. pkgProblemResolver::~pkgProblemResolver()
  384. {
  385. delete [] Scores;
  386. delete [] Flags;
  387. }
  388. /*}}}*/
  389. // ProblemResolver::ScoreSort - Sort the list by score /*{{{*/
  390. // ---------------------------------------------------------------------
  391. /* */
  392. int pkgProblemResolver::ScoreSort(const void *a,const void *b)
  393. {
  394. Package const **A = (Package const **)a;
  395. Package const **B = (Package const **)b;
  396. if (This->Scores[(*A)->ID] > This->Scores[(*B)->ID])
  397. return -1;
  398. if (This->Scores[(*A)->ID] < This->Scores[(*B)->ID])
  399. return 1;
  400. return 0;
  401. }
  402. /*}}}*/
  403. // ProblemResolver::MakeScores - Make the score table /*{{{*/
  404. // ---------------------------------------------------------------------
  405. /* */
  406. void pkgProblemResolver::MakeScores()
  407. {
  408. unsigned long Size = Cache.Head().PackageCount;
  409. memset(Scores,0,sizeof(*Scores)*Size);
  410. // Generate the base scores for a package based on its properties
  411. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  412. {
  413. if (Cache[I].InstallVer == 0)
  414. continue;
  415. signed short &Score = Scores[I->ID];
  416. /* This is arbitary, it should be high enough to elevate an
  417. essantial package above most other packages but low enough
  418. to allow an obsolete essential packages to be removed by
  419. a conflicts on a powerfull normal package (ie libc6) */
  420. if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  421. Score += 100;
  422. // We transform the priority
  423. // Important Required Standard Optional Extra
  424. signed short PrioMap[] = {0,3,2,1,-1,-2};
  425. if (Cache[I].InstVerIter(Cache)->Priority <= 5)
  426. Score += PrioMap[Cache[I].InstVerIter(Cache)->Priority];
  427. /* This helps to fix oddball problems with conflicting packages
  428. on the same level. We enhance the score of installed packages */
  429. if (I->CurrentVer != 0)
  430. Score += 1;
  431. }
  432. // Now that we have the base scores we go and propogate dependencies
  433. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  434. {
  435. if (Cache[I].InstallVer == 0)
  436. continue;
  437. for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false; D++)
  438. {
  439. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  440. Scores[D.TargetPkg()->ID]++;
  441. }
  442. }
  443. // Copy the scores to advoid additive looping
  444. SPtrArray<signed short> OldScores = new signed short[Size];
  445. memcpy(OldScores,Scores,sizeof(*Scores)*Size);
  446. /* Now we cause 1 level of dependency inheritance, that is we add the
  447. score of the packages that depend on the target Package. This
  448. fortifies high scoring packages */
  449. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  450. {
  451. if (Cache[I].InstallVer == 0)
  452. continue;
  453. for (pkgCache::DepIterator D = I.RevDependsList(); D.end() == false; D++)
  454. {
  455. // Only do it for the install version
  456. if ((pkgCache::Version *)D.ParentVer() != Cache[D.ParentPkg()].InstallVer ||
  457. (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends))
  458. continue;
  459. Scores[I->ID] += abs(OldScores[D.ParentPkg()->ID]);
  460. }
  461. }
  462. /* Now we propogate along provides. This makes the packages that
  463. provide important packages extremely important */
  464. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  465. {
  466. for (pkgCache::PrvIterator P = I.ProvidesList(); P.end() == false; P++)
  467. {
  468. // Only do it once per package
  469. if ((pkgCache::Version *)P.OwnerVer() != Cache[P.OwnerPkg()].InstallVer)
  470. continue;
  471. Scores[P.OwnerPkg()->ID] += abs(Scores[I->ID] - OldScores[I->ID]);
  472. }
  473. }
  474. /* Protected things are pushed really high up. This number should put them
  475. ahead of everything */
  476. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  477. {
  478. if ((Flags[I->ID] & Protected) != 0)
  479. Scores[I->ID] += 10000;
  480. if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  481. Scores[I->ID] += 5000;
  482. }
  483. }
  484. /*}}}*/
  485. // ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/
  486. // ---------------------------------------------------------------------
  487. /* This goes through and tries to reinstall packages to make this package
  488. installable */
  489. bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg)
  490. {
  491. if ((Flags[Pkg->ID] & Upgradable) == 0 || Cache[Pkg].Upgradable() == false)
  492. return false;
  493. Flags[Pkg->ID] &= ~Upgradable;
  494. bool WasKept = Cache[Pkg].Keep();
  495. Cache.MarkInstall(Pkg,false);
  496. // This must be a virtual package or something like that.
  497. if (Cache[Pkg].InstVerIter(Cache).end() == true)
  498. return false;
  499. // Isolate the problem dependency
  500. bool Fail = false;
  501. for (pkgCache::DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
  502. {
  503. // Compute a single dependency element (glob or)
  504. pkgCache::DepIterator Start = D;
  505. pkgCache::DepIterator End = D;
  506. unsigned char State = 0;
  507. for (bool LastOR = true; D.end() == false && LastOR == true;)
  508. {
  509. State |= Cache[D];
  510. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  511. D++;
  512. if (LastOR == true)
  513. End = D;
  514. }
  515. // We only worry about critical deps.
  516. if (End.IsCritical() != true)
  517. continue;
  518. // Iterate over all the members in the or group
  519. while (1)
  520. {
  521. // Dep is ok now
  522. if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  523. break;
  524. // Do not change protected packages
  525. PkgIterator P = Start.SmartTargetPkg();
  526. if ((Flags[P->ID] & Protected) == Protected)
  527. {
  528. if (Debug == true)
  529. clog << " Reinst Failed because of protected " << P.Name() << endl;
  530. Fail = true;
  531. }
  532. else
  533. {
  534. // Upgrade the package if the candidate version will fix the problem.
  535. if ((Cache[Start] & pkgDepCache::DepCVer) == pkgDepCache::DepCVer)
  536. {
  537. if (DoUpgrade(P) == false)
  538. {
  539. if (Debug == true)
  540. clog << " Reinst Failed because of " << P.Name() << endl;
  541. Fail = true;
  542. }
  543. else
  544. {
  545. Fail = false;
  546. break;
  547. }
  548. }
  549. else
  550. {
  551. /* We let the algorithm deal with conflicts on its next iteration,
  552. it is much smarter than us */
  553. if (Start->Type == pkgCache::Dep::Conflicts ||
  554. Start->Type == pkgCache::Dep::Obsoletes)
  555. break;
  556. if (Debug == true)
  557. clog << " Reinst Failed early because of " << Start.TargetPkg().Name() << endl;
  558. Fail = true;
  559. }
  560. }
  561. if (Start == End)
  562. break;
  563. Start++;
  564. }
  565. if (Fail == true)
  566. break;
  567. }
  568. // Undo our operations - it might be smart to undo everything this did..
  569. if (Fail == true)
  570. {
  571. if (WasKept == true)
  572. Cache.MarkKeep(Pkg);
  573. else
  574. Cache.MarkDelete(Pkg);
  575. return false;
  576. }
  577. if (Debug == true)
  578. clog << " Re-Instated " << Pkg.Name() << endl;
  579. return true;
  580. }
  581. /*}}}*/
  582. // ProblemResolver::Resolve - Run the resolution pass /*{{{*/
  583. // ---------------------------------------------------------------------
  584. /* This routines works by calculating a score for each package. The score
  585. is derived by considering the package's priority and all reverse
  586. dependents giving an integer that reflects the amount of breakage that
  587. adjusting the package will inflict.
  588. It goes from highest score to lowest and corrects all of the breaks by
  589. keeping or removing the dependant packages. If that fails then it removes
  590. the package itself and goes on. The routine should be able to intelligently
  591. go from any broken state to a fixed state.
  592. The BrokenFix flag enables a mode where the algorithm tries to
  593. upgrade packages to advoid problems. */
  594. bool pkgProblemResolver::Resolve(bool BrokenFix)
  595. {
  596. unsigned long Size = Cache.Head().PackageCount;
  597. // Record which packages are marked for install
  598. bool Again = false;
  599. do
  600. {
  601. Again = false;
  602. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  603. {
  604. if (Cache[I].Install() == true)
  605. Flags[I->ID] |= PreInstalled;
  606. else
  607. {
  608. if (Cache[I].InstBroken() == true && BrokenFix == true)
  609. {
  610. Cache.MarkInstall(I,false);
  611. if (Cache[I].Install() == true)
  612. Again = true;
  613. }
  614. Flags[I->ID] &= ~PreInstalled;
  615. }
  616. Flags[I->ID] |= Upgradable;
  617. }
  618. }
  619. while (Again == true);
  620. if (Debug == true)
  621. clog << "Starting" << endl;
  622. MakeScores();
  623. /* We have to order the packages so that the broken fixing pass
  624. operates from highest score to lowest. This prevents problems when
  625. high score packages cause the removal of lower score packages that
  626. would cause the removal of even lower score packages. */
  627. SPtrArray<pkgCache::Package *> PList = new pkgCache::Package *[Size];
  628. pkgCache::Package **PEnd = PList;
  629. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  630. *PEnd++ = I;
  631. This = this;
  632. qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort);
  633. /* for (pkgCache::Package **K = PList; K != PEnd; K++)
  634. if (Scores[(*K)->ID] != 0)
  635. {
  636. pkgCache::PkgIterator Pkg(Cache,*K);
  637. clog << Scores[(*K)->ID] << ' ' << Pkg.Name() <<
  638. ' ' << (pkgCache::Version *)Pkg.CurrentVer() << ' ' <<
  639. Cache[Pkg].InstallVer << ' ' << Cache[Pkg].CandidateVer << endl;
  640. } */
  641. if (Debug == true)
  642. clog << "Starting 2" << endl;
  643. /* Now consider all broken packages. For each broken package we either
  644. remove the package or fix it's problem. We do this once, it should
  645. not be possible for a loop to form (that is a < b < c and fixing b by
  646. changing a breaks c) */
  647. bool Change = true;
  648. for (int Counter = 0; Counter != 10 && Change == true; Counter++)
  649. {
  650. Change = false;
  651. for (pkgCache::Package **K = PList; K != PEnd; K++)
  652. {
  653. pkgCache::PkgIterator I(Cache,*K);
  654. /* We attempt to install this and see if any breaks result,
  655. this takes care of some strange cases */
  656. if (Cache[I].CandidateVer != Cache[I].InstallVer &&
  657. I->CurrentVer != 0 && Cache[I].InstallVer != 0 &&
  658. (Flags[I->ID] & PreInstalled) != 0 &&
  659. (Flags[I->ID] & Protected) == 0 &&
  660. (Flags[I->ID] & ReInstateTried) == 0)
  661. {
  662. if (Debug == true)
  663. clog << " Try to Re-Instate " << I.Name() << endl;
  664. unsigned long OldBreaks = Cache.BrokenCount();
  665. pkgCache::Version *OldVer = Cache[I].InstallVer;
  666. Flags[I->ID] &= ReInstateTried;
  667. Cache.MarkInstall(I,false);
  668. if (Cache[I].InstBroken() == true ||
  669. OldBreaks < Cache.BrokenCount())
  670. {
  671. if (OldVer == 0)
  672. Cache.MarkDelete(I);
  673. else
  674. Cache.MarkKeep(I);
  675. }
  676. else
  677. if (Debug == true)
  678. clog << "Re-Instated " << I.Name() << " (" << OldBreaks << " vs " << Cache.BrokenCount() << ')' << endl;
  679. }
  680. if (Cache[I].InstallVer == 0 || Cache[I].InstBroken() == false)
  681. continue;
  682. if (Debug == true)
  683. cout << "Investigating " << I.Name() << endl;
  684. // Isolate the problem dependency
  685. PackageKill KillList[100];
  686. PackageKill *LEnd = KillList;
  687. bool InOr = false;
  688. pkgCache::DepIterator Start;
  689. pkgCache::DepIterator End;
  690. PackageKill *OldEnd = LEnd;
  691. enum {OrRemove,OrKeep} OrOp = OrRemove;
  692. for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList();
  693. D.end() == false || InOr == true;)
  694. {
  695. // Compute a single dependency element (glob or)
  696. if (Start == End)
  697. {
  698. // Decide what to do
  699. if (InOr == true)
  700. {
  701. if (OldEnd == LEnd && OrOp == OrRemove)
  702. {
  703. if ((Flags[I->ID] & Protected) != Protected)
  704. {
  705. if (Debug == true)
  706. clog << " Or group remove for " << I.Name() << endl;
  707. Cache.MarkDelete(I);
  708. }
  709. }
  710. if (OldEnd == LEnd && OrOp == OrKeep)
  711. {
  712. if (Debug == true)
  713. clog << " Or group keep for " << I.Name() << endl;
  714. Cache.MarkKeep(I);
  715. }
  716. }
  717. /* We do an extra loop (as above) to finalize the or group
  718. processing */
  719. InOr = false;
  720. OrOp = OrRemove;
  721. D.GlobOr(Start,End);
  722. if (Start.end() == true)
  723. break;
  724. // We only worry about critical deps.
  725. if (End.IsCritical() != true)
  726. continue;
  727. InOr = Start != End;
  728. OldEnd = LEnd;
  729. }
  730. else
  731. Start++;
  732. // Dep is ok
  733. if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  734. continue;
  735. if (Debug == true)
  736. clog << "Package " << I.Name() << " has broken dep on " << Start.TargetPkg().Name() << endl;
  737. /* Look across the version list. If there are no possible
  738. targets then we keep the package and bail. This is necessary
  739. if a package has a dep on another package that cant be found */
  740. SPtrArray<pkgCache::Version *> VList = Start.AllTargets();
  741. if (*VList == 0 && (Flags[I->ID] & Protected) != Protected &&
  742. Start->Type != pkgCache::Dep::Conflicts &&
  743. Start->Type != pkgCache::Dep::Obsoletes &&
  744. Cache[I].NowBroken() == false)
  745. {
  746. if (InOr == true)
  747. {
  748. /* No keep choice because the keep being OK could be the
  749. result of another element in the OR group! */
  750. continue;
  751. }
  752. Change = true;
  753. Cache.MarkKeep(I);
  754. break;
  755. }
  756. bool Done = false;
  757. for (pkgCache::Version **V = VList; *V != 0; V++)
  758. {
  759. pkgCache::VerIterator Ver(Cache,*V);
  760. pkgCache::PkgIterator Pkg = Ver.ParentPkg();
  761. if (Debug == true)
  762. clog << " Considering " << Pkg.Name() << ' ' << (int)Scores[Pkg->ID] <<
  763. " as a solution to " << I.Name() << ' ' << (int)Scores[I->ID] << endl;
  764. if (Scores[I->ID] <= Scores[Pkg->ID] ||
  765. ((Cache[Start] & pkgDepCache::DepNow) == 0 &&
  766. End->Type != pkgCache::Dep::Conflicts &&
  767. End->Type != pkgCache::Dep::Obsoletes))
  768. {
  769. // Try a little harder to fix protected packages..
  770. if ((Flags[I->ID] & Protected) == Protected)
  771. {
  772. if (DoUpgrade(Pkg) == true)
  773. {
  774. if (Scores[Pkg->ID] > Scores[I->ID])
  775. Scores[Pkg->ID] = Scores[I->ID];
  776. break;
  777. }
  778. continue;
  779. }
  780. /* See if a keep will do, unless the package is protected,
  781. then installing it will be necessary */
  782. bool Installed = Cache[I].Install();
  783. Cache.MarkKeep(I);
  784. if (Cache[I].InstBroken() == false)
  785. {
  786. // Unwind operation will be keep now
  787. if (OrOp == OrRemove)
  788. OrOp = OrKeep;
  789. // Restore
  790. if (InOr == true && Installed == true)
  791. Cache.MarkInstall(I,false);
  792. if (Debug == true)
  793. clog << " Holding Back " << I.Name() << " rather than change " << Start.TargetPkg().Name() << endl;
  794. }
  795. else
  796. {
  797. if (BrokenFix == false || DoUpgrade(I) == false)
  798. {
  799. // Consider other options
  800. if (InOr == false)
  801. {
  802. if (Debug == true)
  803. clog << " Removing " << I.Name() << " rather than change " << Start.TargetPkg().Name() << endl;
  804. Cache.MarkDelete(I);
  805. if (Counter > 1)
  806. {
  807. if (Scores[Pkg->ID] > Scores[I->ID])
  808. Scores[I->ID] = Scores[Pkg->ID];
  809. }
  810. }
  811. }
  812. }
  813. Change = true;
  814. Done = true;
  815. break;
  816. }
  817. else
  818. {
  819. if (Debug == true)
  820. clog << " Added " << Pkg.Name() << " to the remove list" << endl;
  821. // Skip adding to the kill list if it is protected
  822. if ((Flags[Pkg->ID] & Protected) != 0)
  823. continue;
  824. LEnd->Pkg = Pkg;
  825. LEnd->Dep = End;
  826. LEnd++;
  827. if (Start->Type != pkgCache::Dep::Conflicts &&
  828. Start->Type != pkgCache::Dep::Obsoletes)
  829. break;
  830. }
  831. }
  832. // Hm, nothing can possibly satisify this dep. Nuke it.
  833. if (VList[0] == 0 &&
  834. Start->Type != pkgCache::Dep::Conflicts &&
  835. Start->Type != pkgCache::Dep::Obsoletes &&
  836. (Flags[I->ID] & Protected) != Protected)
  837. {
  838. bool Installed = Cache[I].Install();
  839. Cache.MarkKeep(I);
  840. if (Cache[I].InstBroken() == false)
  841. {
  842. // Unwind operation will be keep now
  843. if (OrOp == OrRemove)
  844. OrOp = OrKeep;
  845. // Restore
  846. if (InOr == true && Installed == true)
  847. Cache.MarkInstall(I,false);
  848. if (Debug == true)
  849. clog << " Holding Back " << I.Name() << " because I can't find " << Start.TargetPkg().Name() << endl;
  850. }
  851. else
  852. {
  853. if (Debug == true)
  854. clog << " Removing " << I.Name() << " because I can't find " << Start.TargetPkg().Name() << endl;
  855. if (InOr == false)
  856. Cache.MarkDelete(I);
  857. }
  858. Change = true;
  859. Done = true;
  860. }
  861. // Try some more
  862. if (InOr == true)
  863. continue;
  864. if (Done == true)
  865. break;
  866. }
  867. // Apply the kill list now
  868. if (Cache[I].InstallVer != 0)
  869. {
  870. for (PackageKill *J = KillList; J != LEnd; J++)
  871. {
  872. Change = true;
  873. if ((Cache[J->Dep] & pkgDepCache::DepGNow) == 0)
  874. {
  875. if (J->Dep->Type == pkgCache::Dep::Conflicts ||
  876. J->Dep->Type == pkgCache::Dep::Obsoletes)
  877. {
  878. if (Debug == true)
  879. clog << " Fixing " << I.Name() << " via remove of " << J->Pkg.Name() << endl;
  880. Cache.MarkDelete(J->Pkg);
  881. }
  882. }
  883. else
  884. {
  885. if (Debug == true)
  886. clog << " Fixing " << I.Name() << " via keep of " << J->Pkg.Name() << endl;
  887. Cache.MarkKeep(J->Pkg);
  888. }
  889. if (Counter > 1)
  890. {
  891. if (Scores[I->ID] > Scores[J->Pkg->ID])
  892. Scores[J->Pkg->ID] = Scores[I->ID];
  893. }
  894. }
  895. }
  896. }
  897. }
  898. if (Debug == true)
  899. clog << "Done" << endl;
  900. if (Cache.BrokenCount() != 0)
  901. {
  902. // See if this is the result of a hold
  903. pkgCache::PkgIterator I = Cache.PkgBegin();
  904. for (;I.end() != true; I++)
  905. {
  906. if (Cache[I].InstBroken() == false)
  907. continue;
  908. if ((Flags[I->ID] & Protected) != Protected)
  909. return _error->Error(_("Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages."));
  910. }
  911. return _error->Error(_("Unable to correct problems, you have held broken packages."));
  912. }
  913. return true;
  914. }
  915. /*}}}*/
  916. // ProblemResolver::ResolveByKeep - Resolve problems using keep /*{{{*/
  917. // ---------------------------------------------------------------------
  918. /* This is the work horse of the soft upgrade routine. It is very gental
  919. in that it does not install or remove any packages. It is assumed that the
  920. system was non-broken previously. */
  921. bool pkgProblemResolver::ResolveByKeep()
  922. {
  923. unsigned long Size = Cache.Head().PackageCount;
  924. if (Debug == true)
  925. clog << "Entering ResolveByKeep" << endl;
  926. MakeScores();
  927. /* We have to order the packages so that the broken fixing pass
  928. operates from highest score to lowest. This prevents problems when
  929. high score packages cause the removal of lower score packages that
  930. would cause the removal of even lower score packages. */
  931. pkgCache::Package **PList = new pkgCache::Package *[Size];
  932. pkgCache::Package **PEnd = PList;
  933. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  934. *PEnd++ = I;
  935. This = this;
  936. qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort);
  937. // Consider each broken package
  938. pkgCache::Package **LastStop = 0;
  939. for (pkgCache::Package **K = PList; K != PEnd; K++)
  940. {
  941. pkgCache::PkgIterator I(Cache,*K);
  942. if (Cache[I].InstallVer == 0 || Cache[I].InstBroken() == false)
  943. continue;
  944. /* Keep the package. If this works then great, otherwise we have
  945. to be significantly more agressive and manipulate its dependencies */
  946. if ((Flags[I->ID] & Protected) == 0)
  947. {
  948. if (Debug == true)
  949. clog << "Keeping package " << I.Name() << endl;
  950. Cache.MarkKeep(I);
  951. if (Cache[I].InstBroken() == false)
  952. {
  953. K = PList - 1;
  954. continue;
  955. }
  956. }
  957. // Isolate the problem dependencies
  958. for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;)
  959. {
  960. // Compute a single dependency element (glob or)
  961. pkgCache::DepIterator Start = D;
  962. pkgCache::DepIterator End = D;
  963. unsigned char State = 0;
  964. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  965. {
  966. State |= Cache[D];
  967. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  968. if (LastOR == true)
  969. End = D;
  970. }
  971. // We only worry about critical deps.
  972. if (End.IsCritical() != true)
  973. continue;
  974. // Dep is ok
  975. if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  976. continue;
  977. // Hm, the group is broken.. I have no idea how to handle this
  978. if (Start != End)
  979. {
  980. clog << "Note, a broken or group was found in " << I.Name() << "." << endl;
  981. if ((Flags[I->ID] & Protected) == 0)
  982. Cache.MarkKeep(I);
  983. break;
  984. }
  985. if (Debug == true)
  986. clog << "Package " << I.Name() << " has broken dep on " << End.TargetPkg().Name() << endl;
  987. // Look at all the possible provides on this package
  988. SPtrArray<pkgCache::Version *> VList = End.AllTargets();
  989. for (pkgCache::Version **V = VList; *V != 0; V++)
  990. {
  991. pkgCache::VerIterator Ver(Cache,*V);
  992. pkgCache::PkgIterator Pkg = Ver.ParentPkg();
  993. // It is not keepable
  994. if (Cache[Pkg].InstallVer == 0 ||
  995. Pkg->CurrentVer == 0)
  996. continue;
  997. if ((Flags[I->ID] & Protected) == 0)
  998. {
  999. if (Debug == true)
  1000. clog << " Keeping Package " << Pkg.Name() << " due to dep" << endl;
  1001. Cache.MarkKeep(Pkg);
  1002. }
  1003. if (Cache[I].InstBroken() == false)
  1004. break;
  1005. }
  1006. if (Cache[I].InstBroken() == false)
  1007. break;
  1008. }
  1009. if (Cache[I].InstBroken() == true)
  1010. continue;
  1011. // Restart again.
  1012. if (K == LastStop)
  1013. return _error->Error("Internal Error, pkgProblemResolver::ResolveByKeep is looping on package %s.",I.Name());
  1014. LastStop = K;
  1015. K = PList - 1;
  1016. }
  1017. return true;
  1018. }
  1019. /*}}}*/
  1020. // ProblemResolver::InstallProtect - Install all protected packages /*{{{*/
  1021. // ---------------------------------------------------------------------
  1022. /* This is used to make sure protected packages are installed */
  1023. void pkgProblemResolver::InstallProtect()
  1024. {
  1025. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  1026. {
  1027. if ((Flags[I->ID] & Protected) == Protected)
  1028. {
  1029. if ((Flags[I->ID] & ToRemove) == ToRemove)
  1030. Cache.MarkDelete(I);
  1031. else
  1032. Cache.MarkInstall(I,false);
  1033. }
  1034. }
  1035. }
  1036. /*}}}*/
  1037. // PrioSortList - Sort a list of versions by priority /*{{{*/
  1038. // ---------------------------------------------------------------------
  1039. /* This is ment to be used in conjunction with AllTargets to get a list
  1040. of versions ordered by preference. */
  1041. static pkgCache *PrioCache;
  1042. static int PrioComp(const void *A,const void *B)
  1043. {
  1044. pkgCache::VerIterator L(*PrioCache,*(pkgCache::Version **)A);
  1045. pkgCache::VerIterator R(*PrioCache,*(pkgCache::Version **)B);
  1046. if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential &&
  1047. (L.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential)
  1048. return 1;
  1049. if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential &&
  1050. (L.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  1051. return -1;
  1052. if (L->Priority != R->Priority)
  1053. return L->Priority - R->Priority;
  1054. return strcmp(L.ParentPkg().Name(),R.ParentPkg().Name());
  1055. }
  1056. void pkgPrioSortList(pkgCache &Cache,pkgCache::Version **List)
  1057. {
  1058. unsigned long Count = 0;
  1059. PrioCache = &Cache;
  1060. for (pkgCache::Version **I = List; *I != 0; I++)
  1061. Count++;
  1062. qsort(List,Count,sizeof(*List),PrioComp);
  1063. }
  1064. /*}}}*/