algorithms.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: algorithms.cc,v 1.7 1998/10/20 04:33:13 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 <iostream.h>
  21. /*}}}*/
  22. pkgProblemResolver *pkgProblemResolver::This = 0;
  23. // Simulate::Simulate - Constructor /*{{{*/
  24. // ---------------------------------------------------------------------
  25. /* */
  26. pkgSimulate::pkgSimulate(pkgDepCache &Cache) : pkgPackageManager(Cache),
  27. Sim(Cache)
  28. {
  29. Flags = new unsigned char[Cache.HeaderP->PackageCount];
  30. memset(Flags,0,sizeof(*Flags)*Cache.HeaderP->PackageCount);
  31. }
  32. /*}}}*/
  33. // Simulate::Install - Simulate unpacking of a package /*{{{*/
  34. // ---------------------------------------------------------------------
  35. /* */
  36. bool pkgSimulate::Install(PkgIterator iPkg,string /*File*/)
  37. {
  38. // Adapt the iterator
  39. PkgIterator Pkg = Sim.FindPkg(iPkg.Name());
  40. Flags[Pkg->ID] = 1;
  41. clog << "Inst " << Pkg.Name();
  42. Sim.MarkInstall(Pkg,false);
  43. // Look for broken conflicts+predepends.
  44. for (PkgIterator I = Sim.PkgBegin(); I.end() == false; I++)
  45. {
  46. if (Sim[I].InstallVer == 0)
  47. continue;
  48. for (DepIterator D = Sim[I].InstVerIter(Sim).DependsList(); D.end() == false; D++)
  49. if (D->Type == pkgCache::Dep::Conflicts || D->Type == pkgCache::Dep::PreDepends)
  50. {
  51. if ((Sim[D] & pkgDepCache::DepInstall) == 0)
  52. {
  53. clog << " [" << I.Name() << " on " << D.TargetPkg().Name() << ']';
  54. if (D->Type == pkgCache::Dep::Conflicts)
  55. _error->Error("Fatal, conflicts violated %s",I.Name());
  56. }
  57. }
  58. }
  59. if (Sim.BrokenCount() != 0)
  60. ShortBreaks();
  61. else
  62. clog << endl;
  63. return true;
  64. }
  65. /*}}}*/
  66. // Simulate::Configure - Simulate configuration of a Package /*{{{*/
  67. // ---------------------------------------------------------------------
  68. /* This is not an acurate simulation of relatity, we should really not
  69. install the package.. For some investigations it may be necessary
  70. however. */
  71. bool pkgSimulate::Configure(PkgIterator iPkg)
  72. {
  73. // Adapt the iterator
  74. PkgIterator Pkg = Sim.FindPkg(iPkg.Name());
  75. Flags[Pkg->ID] = 2;
  76. // Sim.MarkInstall(Pkg,false);
  77. if (Sim[Pkg].InstBroken() == true)
  78. {
  79. clog << "Conf " << Pkg.Name() << " broken" << endl;
  80. Sim.Update();
  81. // Print out each package and the failed dependencies
  82. for (pkgCache::DepIterator D = Sim[Pkg].InstVerIter(Sim).DependsList(); D.end() == false; D++)
  83. {
  84. if (Sim.IsImportantDep(D) == false ||
  85. (Sim[D] & pkgDepCache::DepInstall) != 0)
  86. continue;
  87. if (D->Type == pkgCache::Dep::Conflicts)
  88. clog << " Conflicts:" << D.TargetPkg().Name();
  89. else
  90. clog << " Depends:" << D.TargetPkg().Name();
  91. }
  92. clog << endl;
  93. _error->Error("Conf Broken %s",Pkg.Name());
  94. }
  95. else
  96. clog << "Conf " << Pkg.Name();
  97. if (Sim.BrokenCount() != 0)
  98. ShortBreaks();
  99. else
  100. clog << endl;
  101. return true;
  102. }
  103. /*}}}*/
  104. // Simulate::Remove - Simulate the removal of a package /*{{{*/
  105. // ---------------------------------------------------------------------
  106. /* */
  107. bool pkgSimulate::Remove(PkgIterator iPkg)
  108. {
  109. // Adapt the iterator
  110. PkgIterator Pkg = Sim.FindPkg(iPkg.Name());
  111. Flags[Pkg->ID] = 3;
  112. Sim.MarkDelete(Pkg);
  113. clog << "Remv " << Pkg.Name();
  114. if (Sim.BrokenCount() != 0)
  115. ShortBreaks();
  116. else
  117. clog << endl;
  118. return true;
  119. }
  120. /*}}}*/
  121. // Simulate::ShortBreaks - Print out a short line describing all breaks /*{{{*/
  122. // ---------------------------------------------------------------------
  123. /* */
  124. void pkgSimulate::ShortBreaks()
  125. {
  126. clog << " [";
  127. for (PkgIterator I = Sim.PkgBegin(); I.end() == false; I++)
  128. {
  129. if (Sim[I].InstBroken() == true)
  130. {
  131. if (Flags[I->ID] == 0)
  132. clog << I.Name() << ' ';
  133. /* else
  134. clog << I.Name() << "! ";*/
  135. }
  136. }
  137. clog << ']' << endl;
  138. }
  139. /*}}}*/
  140. // ApplyStatus - Adjust for non-ok packages /*{{{*/
  141. // ---------------------------------------------------------------------
  142. /* We attempt to change the state of the all packages that have failed
  143. installation toward their real state. The ordering code will perform
  144. the necessary calculations to deal with the problems. */
  145. bool pkgApplyStatus(pkgDepCache &Cache)
  146. {
  147. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  148. {
  149. switch (I->CurrentState)
  150. {
  151. // This means installation failed somehow
  152. case pkgCache::State::UnPacked:
  153. case pkgCache::State::HalfConfigured:
  154. Cache.MarkKeep(I);
  155. break;
  156. // This means removal failed
  157. case pkgCache::State::HalfInstalled:
  158. Cache.MarkDelete(I);
  159. break;
  160. default:
  161. if (I->InstState != pkgCache::State::Ok)
  162. return _error->Error("The package %s is not ok and I "
  163. "don't know how to fix it!",I.Name());
  164. }
  165. }
  166. return true;
  167. }
  168. /*}}}*/
  169. // FixBroken - Fix broken packages /*{{{*/
  170. // ---------------------------------------------------------------------
  171. /* This autoinstalls every broken package and then runs the problem resolver
  172. on the result. */
  173. bool pkgFixBroken(pkgDepCache &Cache)
  174. {
  175. // Auto upgrade all broken packages
  176. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  177. if (Cache[I].NowBroken() == true)
  178. Cache.MarkInstall(I,true);
  179. /* Fix packages that are in a NeedArchive state but don't have a
  180. downloadable install version */
  181. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  182. {
  183. if (I.State() != pkgCache::PkgIterator::NeedsUnpack ||
  184. Cache[I].Delete() == true)
  185. continue;
  186. if (Cache[I].InstVerIter(Cache).Downloadable() == false)
  187. continue;
  188. Cache.MarkInstall(I,true);
  189. }
  190. pkgProblemResolver Fix(Cache);
  191. return Fix.Resolve(true);
  192. }
  193. /*}}}*/
  194. // DistUpgrade - Distribution upgrade /*{{{*/
  195. // ---------------------------------------------------------------------
  196. /* This autoinstalls every package and then force installs every
  197. pre-existing package. This creates the initial set of conditions which
  198. most likely contain problems because too many things were installed.
  199. The problem resolver is used to resolve the problems.
  200. */
  201. bool pkgDistUpgrade(pkgDepCache &Cache)
  202. {
  203. /* Auto upgrade all installed packages, this provides the basis
  204. for the installation */
  205. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  206. if (I->CurrentVer != 0)
  207. Cache.MarkInstall(I,true);
  208. /* Now, auto upgrade all essential packages - this ensures that
  209. the essential packages are present and working */
  210. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  211. if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  212. Cache.MarkInstall(I,true);
  213. /* We do it again over all previously installed packages to force
  214. conflict resolution on them all. */
  215. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  216. if (I->CurrentVer != 0)
  217. Cache.MarkInstall(I,false);
  218. pkgProblemResolver Fix(Cache);
  219. // Hold back held packages.
  220. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  221. {
  222. if (I->SelectedState == pkgCache::State::Hold)
  223. {
  224. Fix.Protect(I);
  225. Cache.MarkKeep(I);
  226. }
  227. }
  228. return Fix.Resolve();
  229. }
  230. /*}}}*/
  231. // AllUpgrade - Upgrade as many packages as possible /*{{{*/
  232. // ---------------------------------------------------------------------
  233. /* Right now the system must be consistent before this can be called.
  234. It also will not change packages marked for install, it only tries
  235. to install packages not marked for install */
  236. bool pkgAllUpgrade(pkgDepCache &Cache)
  237. {
  238. pkgProblemResolver Fix(Cache);
  239. if (Cache.BrokenCount() != 0)
  240. return false;
  241. // Upgrade all installed packages
  242. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  243. {
  244. if (Cache[I].Install() == true)
  245. Fix.Protect(I);
  246. if (I->SelectedState == pkgCache::State::Hold)
  247. continue;
  248. if (I->CurrentVer != 0 && Cache[I].InstallVer != 0)
  249. Cache.MarkInstall(I,false);
  250. }
  251. return Fix.ResolveByKeep();
  252. }
  253. /*}}}*/
  254. // MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
  255. // ---------------------------------------------------------------------
  256. /* This simply goes over the entire set of packages and tries to keep
  257. each package marked for upgrade. If a conflict is generated then
  258. the package is restored. */
  259. bool pkgMinimizeUpgrade(pkgDepCache &Cache)
  260. {
  261. if (Cache.BrokenCount() != 0)
  262. return false;
  263. // We loop indefinately to get the minimal set size.
  264. bool Change = false;
  265. do
  266. {
  267. Change = false;
  268. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  269. {
  270. // Not interesting
  271. if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
  272. continue;
  273. // Keep it and see if that is OK
  274. Cache.MarkKeep(I);
  275. if (Cache.BrokenCount() != 0)
  276. Cache.MarkInstall(I,false);
  277. else
  278. Change = true;
  279. }
  280. }
  281. while (Change == true);
  282. if (Cache.BrokenCount() != 0)
  283. return _error->Error("Internal Error in pkgMinimizeUpgrade");
  284. return true;
  285. }
  286. /*}}}*/
  287. // ProblemResolver::pkgProblemResolver - Constructor /*{{{*/
  288. // ---------------------------------------------------------------------
  289. /* */
  290. pkgProblemResolver::pkgProblemResolver(pkgDepCache &Cache) : Cache(Cache)
  291. {
  292. // Allocate memory
  293. unsigned long Size = Cache.HeaderP->PackageCount;
  294. Scores = new signed short[Size];
  295. Flags = new unsigned char[Size];
  296. memset(Flags,0,sizeof(*Flags)*Size);
  297. // Set debug to true to see its decision logic
  298. Debug = _config->FindB("Debug::pkgProblemResolver",false);
  299. }
  300. /*}}}*/
  301. // ProblemResolver::ScoreSort - Sort the list by score /*{{{*/
  302. // ---------------------------------------------------------------------
  303. /* */
  304. int pkgProblemResolver::ScoreSort(const void *a,const void *b)
  305. {
  306. Package const **A = (Package const **)a;
  307. Package const **B = (Package const **)b;
  308. if (This->Scores[(*A)->ID] > This->Scores[(*B)->ID])
  309. return -1;
  310. if (This->Scores[(*A)->ID] < This->Scores[(*B)->ID])
  311. return 1;
  312. return 0;
  313. }
  314. /*}}}*/
  315. // ProblemResolver::MakeScores - Make the score table /*{{{*/
  316. // ---------------------------------------------------------------------
  317. /* */
  318. void pkgProblemResolver::MakeScores()
  319. {
  320. unsigned long Size = Cache.HeaderP->PackageCount;
  321. memset(Scores,0,sizeof(*Scores)*Size);
  322. // Generate the base scores for a package based on its properties
  323. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  324. {
  325. if (Cache[I].InstallVer == 0)
  326. continue;
  327. signed short &Score = Scores[I->ID];
  328. /* This is arbitary, it should be high enough to elevate an
  329. essantial package above most other packages but low enough
  330. to allow an obsolete essential packages to be removed by
  331. a conflicts on a powerfull normal package (ie libc6) */
  332. if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  333. Score += 100;
  334. // We transform the priority
  335. // Important Required Standard Optional Extra
  336. signed short PrioMap[] = {0,3,2,1,-1,-2};
  337. if (Cache[I].InstVerIter(Cache)->Priority <= 5)
  338. Score += PrioMap[Cache[I].InstVerIter(Cache)->Priority];
  339. /* This helps to fix oddball problems with conflicting packages
  340. on the same level. We enhance the score of installed packages */
  341. if (I->CurrentVer != 0)
  342. Score += 1;
  343. }
  344. // Now that we have the base scores we go and propogate dependencies
  345. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  346. {
  347. if (Cache[I].InstallVer == 0)
  348. continue;
  349. for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false; D++)
  350. {
  351. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  352. Scores[D.TargetPkg()->ID]++;
  353. }
  354. }
  355. // Copy the scores to advoid additive looping
  356. signed short *OldScores = new signed short[Size];
  357. memcpy(OldScores,Scores,sizeof(*Scores)*Size);
  358. /* Now we cause 1 level of dependency inheritance, that is we add the
  359. score of the packages that depend on the target Package. This
  360. fortifies high scoring packages */
  361. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  362. {
  363. if (Cache[I].InstallVer == 0)
  364. continue;
  365. for (pkgCache::DepIterator D = I.RevDependsList(); D.end() == false; D++)
  366. {
  367. // Only do it for the install version
  368. if ((pkgCache::Version *)D.ParentVer() != Cache[D.ParentPkg()].InstallVer ||
  369. (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends))
  370. continue;
  371. Scores[I->ID] += abs(OldScores[D.ParentPkg()->ID]);
  372. }
  373. }
  374. /* Now we propogate along provides. This makes the packages that
  375. provide important packages extremely important */
  376. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  377. {
  378. for (pkgCache::PrvIterator P = I.ProvidesList(); P.end() == false; P++)
  379. {
  380. // Only do it once per package
  381. if ((pkgCache::Version *)P.OwnerVer() != Cache[P.OwnerPkg()].InstallVer)
  382. continue;
  383. Scores[P.OwnerPkg()->ID] += abs(Scores[I->ID] - OldScores[I->ID]);
  384. }
  385. }
  386. /* Protected things are pushed really high up. This number should put them
  387. ahead of everything */
  388. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  389. if ((Flags[I->ID] & Protected) != 0)
  390. Scores[I->ID] += 10000;
  391. delete [] OldScores;
  392. }
  393. /*}}}*/
  394. // ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/
  395. // ---------------------------------------------------------------------
  396. /* This goes through and tries to reinstall packages to make this package
  397. installable */
  398. bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg)
  399. {
  400. if ((Flags[Pkg->ID] & Upgradable) == 0 || Cache[Pkg].Upgradable() == false)
  401. return false;
  402. Flags[Pkg->ID] &= ~Upgradable;
  403. bool WasKept = Cache[Pkg].Keep();
  404. Cache.MarkInstall(Pkg,false);
  405. // This must be a virtual package or something like that.
  406. if (Cache[Pkg].InstVerIter(Cache).end() == true)
  407. return false;
  408. // Isolate the problem dependency
  409. bool Fail = false;
  410. for (pkgCache::DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
  411. {
  412. // Compute a single dependency element (glob or)
  413. pkgCache::DepIterator Start = D;
  414. pkgCache::DepIterator End = D;
  415. unsigned char State = 0;
  416. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  417. {
  418. State |= Cache[D];
  419. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  420. if (LastOR == true)
  421. End = D;
  422. }
  423. // We only worry about critical deps.
  424. if (End.IsCritical() != true)
  425. continue;
  426. // Dep is ok
  427. if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  428. continue;
  429. // Hm, the group is broken.. I have no idea how to handle this
  430. if (Start != End)
  431. {
  432. clog << "Note, a broken or group was found in " << Pkg.Name() << "." << endl;
  433. Fail = true;
  434. break;
  435. }
  436. // Do not change protected packages
  437. PkgIterator P = Start.SmartTargetPkg();
  438. if ((Flags[P->ID] & Protected) == Protected)
  439. {
  440. if (Debug == true)
  441. clog << " Reinet Failed because of protected " << P.Name() << endl;
  442. Fail = true;
  443. break;
  444. }
  445. // Upgrade the package if the candidate version will fix the problem.
  446. if ((Cache[Start] & pkgDepCache::DepCVer) == pkgDepCache::DepCVer)
  447. {
  448. if (DoUpgrade(P) == false)
  449. {
  450. if (Debug == true)
  451. clog << " Reinst Failed because of " << P.Name() << endl;
  452. Fail = true;
  453. break;
  454. }
  455. }
  456. else
  457. {
  458. /* We let the algorithm deal with conflicts on its next iteration,
  459. it is much smarter than us */
  460. if (End->Type == pkgCache::Dep::Conflicts)
  461. continue;
  462. if (Debug == true)
  463. clog << " Reinst Failed early because of " << Start.TargetPkg().Name() << endl;
  464. Fail = true;
  465. break;
  466. }
  467. }
  468. // Undo our operations - it might be smart to undo everything this did..
  469. if (Fail == true)
  470. {
  471. if (WasKept == true)
  472. Cache.MarkKeep(Pkg);
  473. else
  474. Cache.MarkDelete(Pkg);
  475. return false;
  476. }
  477. if (Debug == true)
  478. clog << " Re-Instated " << Pkg.Name() << endl;
  479. return true;
  480. }
  481. /*}}}*/
  482. // ProblemResolver::Resolve - Run the resolution pass /*{{{*/
  483. // ---------------------------------------------------------------------
  484. /* This routines works by calculating a score for each package. The score
  485. is derived by considering the package's priority and all reverse
  486. dependents giving an integer that reflects the amount of breakage that
  487. adjusting the package will inflict.
  488. It goes from highest score to lowest and corrects all of the breaks by
  489. keeping or removing the dependant packages. If that fails then it removes
  490. the package itself and goes on. The routine should be able to intelligently
  491. go from any broken state to a fixed state.
  492. The BrokenFix flag enables a mode where the algorithm tries to
  493. upgrade packages to advoid problems. */
  494. bool pkgProblemResolver::Resolve(bool BrokenFix)
  495. {
  496. unsigned long Size = Cache.HeaderP->PackageCount;
  497. // Record which packages are marked for install
  498. bool Again = false;
  499. do
  500. {
  501. Again = false;
  502. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  503. {
  504. if (Cache[I].Install() == true)
  505. Flags[I->ID] |= PreInstalled;
  506. else
  507. {
  508. if (Cache[I].InstBroken() == true && BrokenFix == true)
  509. {
  510. Cache.MarkInstall(I,false);
  511. if (Cache[I].Install() == true)
  512. Again = true;
  513. }
  514. Flags[I->ID] &= ~PreInstalled;
  515. }
  516. Flags[I->ID] |= Upgradable;
  517. }
  518. }
  519. while (Again == true);
  520. if (Debug == true)
  521. clog << "Starting" << endl;
  522. MakeScores();
  523. /* We have to order the packages so that the broken fixing pass
  524. operates from highest score to lowest. This prevents problems when
  525. high score packages cause the removal of lower score packages that
  526. would cause the removal of even lower score packages. */
  527. pkgCache::Package **PList = new pkgCache::Package *[Size];
  528. pkgCache::Package **PEnd = PList;
  529. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  530. *PEnd++ = I;
  531. This = this;
  532. qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort);
  533. /* for (pkgCache::Package **K = PList; K != PEnd; K++)
  534. if (Scores[(*K)->ID] != 0)
  535. {
  536. pkgCache::PkgIterator Pkg(Cache,*K);
  537. clog << Scores[(*K)->ID] << ' ' << Pkg.Name() <<
  538. ' ' << (pkgCache::Version *)Pkg.CurrentVer() << ' ' <<
  539. Cache[Pkg].InstallVer << ' ' << Cache[Pkg].CandidateVer << endl;
  540. } */
  541. if (Debug == true)
  542. clog << "Starting 2" << endl;
  543. /* Now consider all broken packages. For each broken package we either
  544. remove the package or fix it's problem. We do this once, it should
  545. not be possible for a loop to form (that is a < b < c and fixing b by
  546. changing a breaks c) */
  547. bool Change = true;
  548. for (int Counter = 0; Counter != 10 && Change == true; Counter++)
  549. {
  550. Change = false;
  551. for (pkgCache::Package **K = PList; K != PEnd; K++)
  552. {
  553. pkgCache::PkgIterator I(Cache,*K);
  554. /* We attempt to install this and see if any breaks result,
  555. this takes care of some strange cases */
  556. if (Cache[I].CandidateVer != Cache[I].InstallVer &&
  557. I->CurrentVer != 0 && Cache[I].InstallVer != 0 &&
  558. (Flags[I->ID] & PreInstalled) != 0 &&
  559. (Flags[I->ID] & Protected) == 0 &&
  560. (Flags[I->ID] & ReInstateTried) == 0)
  561. {
  562. if (Debug == true)
  563. clog << " Try to Re-Instate " << I.Name() << endl;
  564. int OldBreaks = Cache.BrokenCount();
  565. pkgCache::Version *OldVer = Cache[I].InstallVer;
  566. Flags[I->ID] &= ReInstateTried;
  567. Cache.MarkInstall(I,false);
  568. if (Cache[I].InstBroken() == true ||
  569. OldBreaks < Cache.BrokenCount())
  570. {
  571. if (OldVer == 0)
  572. Cache.MarkDelete(I);
  573. else
  574. Cache.MarkKeep(I);
  575. }
  576. else
  577. if (Debug == true)
  578. clog << "Re-Instated " << I.Name() << " (" << OldBreaks << " vs " << Cache.BrokenCount() << ')' << endl;
  579. }
  580. if (Cache[I].InstallVer == 0 || Cache[I].InstBroken() == false)
  581. continue;
  582. // Isolate the problem dependency
  583. PackageKill KillList[100];
  584. PackageKill *LEnd = KillList;
  585. for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;)
  586. {
  587. // Compute a single dependency element (glob or)
  588. pkgCache::DepIterator Start = D;
  589. pkgCache::DepIterator End = D;
  590. unsigned char State = 0;
  591. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  592. {
  593. State |= Cache[D];
  594. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  595. if (LastOR == true)
  596. End = D;
  597. }
  598. // We only worry about critical deps.
  599. if (End.IsCritical() != true)
  600. continue;
  601. // Dep is ok
  602. if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  603. continue;
  604. // Hm, the group is broken.. I have no idea how to handle this
  605. if (Start != End)
  606. {
  607. clog << "Note, a broken or group was found in " << I.Name() << "." << endl;
  608. Cache.MarkDelete(I);
  609. break;
  610. }
  611. if (Debug == true)
  612. clog << "Package " << I.Name() << " has broken dep on " << End.TargetPkg().Name() << endl;
  613. /* Conflicts is simple, decide if we should remove this package
  614. or the conflicted one */
  615. pkgCache::Version **VList = End.AllTargets();
  616. bool Done = false;
  617. for (pkgCache::Version **V = VList; *V != 0; V++)
  618. {
  619. pkgCache::VerIterator Ver(Cache,*V);
  620. pkgCache::PkgIterator Pkg = Ver.ParentPkg();
  621. if (Debug == true)
  622. clog << " Considering " << Pkg.Name() << ' ' << (int)Scores[Pkg->ID] <<
  623. " as a solution to " << I.Name() << ' ' << (int)Scores[I->ID] << endl;
  624. if (Scores[I->ID] <= Scores[Pkg->ID] ||
  625. ((Cache[End] & pkgDepCache::DepGNow) == 0 &&
  626. End->Type != pkgCache::Dep::Conflicts))
  627. {
  628. if ((Flags[I->ID] & Protected) == Protected)
  629. continue;
  630. // See if a keep will do
  631. Cache.MarkKeep(I);
  632. if (Cache[I].InstBroken() == false)
  633. {
  634. if (Debug == true)
  635. clog << " Holding Back " << I.Name() << " rather than change " << End.TargetPkg().Name() << endl;
  636. }
  637. else
  638. {
  639. if (BrokenFix == false || DoUpgrade(I) == false)
  640. {
  641. if (Debug == true)
  642. clog << " Removing " << I.Name() << " rather than change " << End.TargetPkg().Name() << endl;
  643. Cache.MarkDelete(I);
  644. if (Counter > 1)
  645. Scores[I->ID] = Scores[Pkg->ID];
  646. }
  647. }
  648. Change = true;
  649. Done = true;
  650. break;
  651. }
  652. else
  653. {
  654. // Skip this if it is protected
  655. if ((Flags[Pkg->ID] & Protected) != 0)
  656. continue;
  657. LEnd->Pkg = Pkg;
  658. LEnd->Dep = End;
  659. LEnd++;
  660. if (End->Type != pkgCache::Dep::Conflicts)
  661. break;
  662. }
  663. }
  664. // Hm, nothing can possibly satisify this dep. Nuke it.
  665. if (VList[0] == 0 && End->Type != pkgCache::Dep::Conflicts &&
  666. (Flags[I->ID] & Protected) != Protected)
  667. {
  668. Cache.MarkKeep(I);
  669. if (Cache[I].InstBroken() == false)
  670. {
  671. if (Debug == true)
  672. clog << " Holding Back " << I.Name() << " because I can't find " << End.TargetPkg().Name() << endl;
  673. }
  674. else
  675. {
  676. if (Debug == true)
  677. clog << " Removing " << I.Name() << " because I can't find " << End.TargetPkg().Name() << endl;
  678. Cache.MarkDelete(I);
  679. }
  680. Change = true;
  681. Done = true;
  682. }
  683. delete [] VList;
  684. if (Done == true)
  685. break;
  686. }
  687. // Apply the kill list now
  688. if (Cache[I].InstallVer != 0)
  689. for (PackageKill *J = KillList; J != LEnd; J++)
  690. {
  691. Change = true;
  692. if ((Cache[J->Dep] & pkgDepCache::DepGNow) == 0)
  693. {
  694. if (J->Dep->Type == pkgCache::Dep::Conflicts)
  695. {
  696. if (Debug == true)
  697. clog << " Fixing " << I.Name() << " via remove of " << J->Pkg.Name() << endl;
  698. Cache.MarkDelete(J->Pkg);
  699. }
  700. }
  701. else
  702. {
  703. if (Debug == true)
  704. clog << " Fixing " << I.Name() << " via keep of " << J->Pkg.Name() << endl;
  705. Cache.MarkKeep(J->Pkg);
  706. }
  707. if (Counter > 1)
  708. Scores[J->Pkg->ID] = Scores[I->ID];
  709. }
  710. }
  711. }
  712. if (Debug == true)
  713. clog << "Done" << endl;
  714. delete [] Scores;
  715. delete [] PList;
  716. if (Cache.BrokenCount() != 0)
  717. return _error->Error("Internal error, pkgProblemResolver::Resolve generated breaks.");
  718. return true;
  719. }
  720. /*}}}*/
  721. // ProblemResolver::ResolveByKeep - Resolve problems using keep /*{{{*/
  722. // ---------------------------------------------------------------------
  723. /* This is the work horse of the soft upgrade routine. It is very gental
  724. in that it does not install or remove any packages. It is assumed that the
  725. system was non-broken previously. */
  726. bool pkgProblemResolver::ResolveByKeep()
  727. {
  728. unsigned long Size = Cache.HeaderP->PackageCount;
  729. if (Debug == true)
  730. clog << "Entering ResolveByKeep" << endl;
  731. MakeScores();
  732. /* We have to order the packages so that the broken fixing pass
  733. operates from highest score to lowest. This prevents problems when
  734. high score packages cause the removal of lower score packages that
  735. would cause the removal of even lower score packages. */
  736. pkgCache::Package **PList = new pkgCache::Package *[Size];
  737. pkgCache::Package **PEnd = PList;
  738. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  739. *PEnd++ = I;
  740. This = this;
  741. qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort);
  742. // Consider each broken package
  743. pkgCache::Package **LastStop = 0;
  744. for (pkgCache::Package **K = PList; K != PEnd; K++)
  745. {
  746. pkgCache::PkgIterator I(Cache,*K);
  747. if (Cache[I].InstallVer == 0 || Cache[I].InstBroken() == false)
  748. continue;
  749. /* Keep the package. If this works then great, otherwise we have
  750. to be significantly more agressive and manipulate its dependencies */
  751. if ((Flags[I->ID] & Protected) == 0)
  752. {
  753. if (Debug == true)
  754. clog << "Keeping package " << I.Name() << endl;
  755. Cache.MarkKeep(I);
  756. if (Cache[I].InstBroken() == false)
  757. {
  758. K = PList;
  759. continue;
  760. }
  761. }
  762. // Isolate the problem dependencies
  763. for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;)
  764. {
  765. // Compute a single dependency element (glob or)
  766. pkgCache::DepIterator Start = D;
  767. pkgCache::DepIterator End = D;
  768. unsigned char State = 0;
  769. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  770. {
  771. State |= Cache[D];
  772. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  773. if (LastOR == true)
  774. End = D;
  775. }
  776. // We only worry about critical deps.
  777. if (End.IsCritical() != true)
  778. continue;
  779. // Dep is ok
  780. if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  781. continue;
  782. // Hm, the group is broken.. I have no idea how to handle this
  783. if (Start != End)
  784. {
  785. clog << "Note, a broken or group was found in " << I.Name() << "." << endl;
  786. if ((Flags[I->ID] & Protected) == 0)
  787. Cache.MarkKeep(I);
  788. break;
  789. }
  790. if (Debug == true)
  791. clog << "Package " << I.Name() << " has broken dep on " << End.TargetPkg().Name() << endl;
  792. // Look at all the possible provides on this package
  793. pkgCache::Version **VList = End.AllTargets();
  794. for (pkgCache::Version **V = VList; *V != 0; V++)
  795. {
  796. pkgCache::VerIterator Ver(Cache,*V);
  797. pkgCache::PkgIterator Pkg = Ver.ParentPkg();
  798. // It is not keepable
  799. if (Cache[Pkg].InstallVer == 0 ||
  800. Pkg->CurrentVer == 0)
  801. continue;
  802. if ((Flags[I->ID] & Protected) == 0)
  803. {
  804. if (Debug == true)
  805. clog << " Keeping Package " << Pkg.Name() << " due to dep" << endl;
  806. Cache.MarkKeep(Pkg);
  807. }
  808. if (Cache[I].InstBroken() == false)
  809. break;
  810. }
  811. if (Cache[I].InstBroken() == false)
  812. break;
  813. }
  814. if (Cache[I].InstBroken() == true)
  815. continue;
  816. // Restart again.
  817. if (K == LastStop)
  818. return _error->Error("Internal Error, pkgProblemResolver::ResolveByKeep is looping on package %s.",I.Name());
  819. LastStop = K;
  820. K = PList;
  821. }
  822. return true;
  823. }
  824. /*}}}*/
  825. // ProblemResolver::InstallProtect - Install all protected packages /*{{{*/
  826. // ---------------------------------------------------------------------
  827. /* This is used to make sure protected packages are installed */
  828. void pkgProblemResolver::InstallProtect()
  829. {
  830. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  831. {
  832. if ((Flags[I->ID] & Protected) == Protected)
  833. {
  834. if ((Flags[I->ID] & ToRemove) == ToRemove)
  835. Cache.MarkDelete(I);
  836. else
  837. Cache.MarkInstall(I,false);
  838. }
  839. }
  840. }
  841. /*}}}*/