algorithms.cc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: algorithms.cc,v 1.11 1998/11/14 07:20:06 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. if (_config->FindB("APT::Ingore-Hold",false) == false)
  221. {
  222. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  223. {
  224. if (I->SelectedState == pkgCache::State::Hold)
  225. {
  226. Fix.Protect(I);
  227. Cache.MarkKeep(I);
  228. }
  229. }
  230. }
  231. return Fix.Resolve();
  232. }
  233. /*}}}*/
  234. // AllUpgrade - Upgrade as many packages as possible /*{{{*/
  235. // ---------------------------------------------------------------------
  236. /* Right now the system must be consistent before this can be called.
  237. It also will not change packages marked for install, it only tries
  238. to install packages not marked for install */
  239. bool pkgAllUpgrade(pkgDepCache &Cache)
  240. {
  241. pkgProblemResolver Fix(Cache);
  242. if (Cache.BrokenCount() != 0)
  243. return false;
  244. // Upgrade all installed packages
  245. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  246. {
  247. if (Cache[I].Install() == true)
  248. Fix.Protect(I);
  249. if (_config->FindB("APT::Ingore-Hold",false) == false)
  250. if (I->SelectedState == pkgCache::State::Hold)
  251. continue;
  252. if (I->CurrentVer != 0 && Cache[I].InstallVer != 0)
  253. Cache.MarkInstall(I,false);
  254. }
  255. return Fix.ResolveByKeep();
  256. }
  257. /*}}}*/
  258. // MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
  259. // ---------------------------------------------------------------------
  260. /* This simply goes over the entire set of packages and tries to keep
  261. each package marked for upgrade. If a conflict is generated then
  262. the package is restored. */
  263. bool pkgMinimizeUpgrade(pkgDepCache &Cache)
  264. {
  265. if (Cache.BrokenCount() != 0)
  266. return false;
  267. // We loop indefinately to get the minimal set size.
  268. bool Change = false;
  269. do
  270. {
  271. Change = false;
  272. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  273. {
  274. // Not interesting
  275. if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
  276. continue;
  277. // Keep it and see if that is OK
  278. Cache.MarkKeep(I);
  279. if (Cache.BrokenCount() != 0)
  280. Cache.MarkInstall(I,false);
  281. else
  282. Change = true;
  283. }
  284. }
  285. while (Change == true);
  286. if (Cache.BrokenCount() != 0)
  287. return _error->Error("Internal Error in pkgMinimizeUpgrade");
  288. return true;
  289. }
  290. /*}}}*/
  291. // ProblemResolver::pkgProblemResolver - Constructor /*{{{*/
  292. // ---------------------------------------------------------------------
  293. /* */
  294. pkgProblemResolver::pkgProblemResolver(pkgDepCache &Cache) : Cache(Cache)
  295. {
  296. // Allocate memory
  297. unsigned long Size = Cache.HeaderP->PackageCount;
  298. Scores = new signed short[Size];
  299. Flags = new unsigned char[Size];
  300. memset(Flags,0,sizeof(*Flags)*Size);
  301. // Set debug to true to see its decision logic
  302. Debug = _config->FindB("Debug::pkgProblemResolver",false);
  303. }
  304. /*}}}*/
  305. // ProblemResolver::ScoreSort - Sort the list by score /*{{{*/
  306. // ---------------------------------------------------------------------
  307. /* */
  308. int pkgProblemResolver::ScoreSort(const void *a,const void *b)
  309. {
  310. Package const **A = (Package const **)a;
  311. Package const **B = (Package const **)b;
  312. if (This->Scores[(*A)->ID] > This->Scores[(*B)->ID])
  313. return -1;
  314. if (This->Scores[(*A)->ID] < This->Scores[(*B)->ID])
  315. return 1;
  316. return 0;
  317. }
  318. /*}}}*/
  319. // ProblemResolver::MakeScores - Make the score table /*{{{*/
  320. // ---------------------------------------------------------------------
  321. /* */
  322. void pkgProblemResolver::MakeScores()
  323. {
  324. unsigned long Size = Cache.HeaderP->PackageCount;
  325. memset(Scores,0,sizeof(*Scores)*Size);
  326. // Generate the base scores for a package based on its properties
  327. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  328. {
  329. if (Cache[I].InstallVer == 0)
  330. continue;
  331. signed short &Score = Scores[I->ID];
  332. /* This is arbitary, it should be high enough to elevate an
  333. essantial package above most other packages but low enough
  334. to allow an obsolete essential packages to be removed by
  335. a conflicts on a powerfull normal package (ie libc6) */
  336. if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  337. Score += 100;
  338. // We transform the priority
  339. // Important Required Standard Optional Extra
  340. signed short PrioMap[] = {0,3,2,1,-1,-2};
  341. if (Cache[I].InstVerIter(Cache)->Priority <= 5)
  342. Score += PrioMap[Cache[I].InstVerIter(Cache)->Priority];
  343. /* This helps to fix oddball problems with conflicting packages
  344. on the same level. We enhance the score of installed packages */
  345. if (I->CurrentVer != 0)
  346. Score += 1;
  347. }
  348. // Now that we have the base scores we go and propogate dependencies
  349. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  350. {
  351. if (Cache[I].InstallVer == 0)
  352. continue;
  353. for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false; D++)
  354. {
  355. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  356. Scores[D.TargetPkg()->ID]++;
  357. }
  358. }
  359. // Copy the scores to advoid additive looping
  360. signed short *OldScores = new signed short[Size];
  361. memcpy(OldScores,Scores,sizeof(*Scores)*Size);
  362. /* Now we cause 1 level of dependency inheritance, that is we add the
  363. score of the packages that depend on the target Package. This
  364. fortifies high scoring packages */
  365. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  366. {
  367. if (Cache[I].InstallVer == 0)
  368. continue;
  369. for (pkgCache::DepIterator D = I.RevDependsList(); D.end() == false; D++)
  370. {
  371. // Only do it for the install version
  372. if ((pkgCache::Version *)D.ParentVer() != Cache[D.ParentPkg()].InstallVer ||
  373. (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends))
  374. continue;
  375. Scores[I->ID] += abs(OldScores[D.ParentPkg()->ID]);
  376. }
  377. }
  378. /* Now we propogate along provides. This makes the packages that
  379. provide important packages extremely important */
  380. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  381. {
  382. for (pkgCache::PrvIterator P = I.ProvidesList(); P.end() == false; P++)
  383. {
  384. // Only do it once per package
  385. if ((pkgCache::Version *)P.OwnerVer() != Cache[P.OwnerPkg()].InstallVer)
  386. continue;
  387. Scores[P.OwnerPkg()->ID] += abs(Scores[I->ID] - OldScores[I->ID]);
  388. }
  389. }
  390. /* Protected things are pushed really high up. This number should put them
  391. ahead of everything */
  392. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  393. if ((Flags[I->ID] & Protected) != 0)
  394. Scores[I->ID] += 10000;
  395. delete [] OldScores;
  396. }
  397. /*}}}*/
  398. // ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/
  399. // ---------------------------------------------------------------------
  400. /* This goes through and tries to reinstall packages to make this package
  401. installable */
  402. bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg)
  403. {
  404. if ((Flags[Pkg->ID] & Upgradable) == 0 || Cache[Pkg].Upgradable() == false)
  405. return false;
  406. Flags[Pkg->ID] &= ~Upgradable;
  407. bool WasKept = Cache[Pkg].Keep();
  408. Cache.MarkInstall(Pkg,false);
  409. // This must be a virtual package or something like that.
  410. if (Cache[Pkg].InstVerIter(Cache).end() == true)
  411. return false;
  412. // Isolate the problem dependency
  413. bool Fail = false;
  414. for (pkgCache::DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
  415. {
  416. // Compute a single dependency element (glob or)
  417. pkgCache::DepIterator Start = D;
  418. pkgCache::DepIterator End = D;
  419. unsigned char State = 0;
  420. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  421. {
  422. State |= Cache[D];
  423. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  424. if (LastOR == true)
  425. End = D;
  426. }
  427. // We only worry about critical deps.
  428. if (End.IsCritical() != true)
  429. continue;
  430. // Dep is ok
  431. if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  432. continue;
  433. // Hm, the group is broken.. I have no idea how to handle this
  434. if (Start != End)
  435. {
  436. clog << "Note, a broken or group was found in " << Pkg.Name() << "." << endl;
  437. Fail = true;
  438. break;
  439. }
  440. // Do not change protected packages
  441. PkgIterator P = Start.SmartTargetPkg();
  442. if ((Flags[P->ID] & Protected) == Protected)
  443. {
  444. if (Debug == true)
  445. clog << " Reinet Failed because of protected " << P.Name() << endl;
  446. Fail = true;
  447. break;
  448. }
  449. // Upgrade the package if the candidate version will fix the problem.
  450. if ((Cache[Start] & pkgDepCache::DepCVer) == pkgDepCache::DepCVer)
  451. {
  452. if (DoUpgrade(P) == false)
  453. {
  454. if (Debug == true)
  455. clog << " Reinst Failed because of " << P.Name() << endl;
  456. Fail = true;
  457. break;
  458. }
  459. }
  460. else
  461. {
  462. /* We let the algorithm deal with conflicts on its next iteration,
  463. it is much smarter than us */
  464. if (End->Type == pkgCache::Dep::Conflicts)
  465. continue;
  466. if (Debug == true)
  467. clog << " Reinst Failed early because of " << Start.TargetPkg().Name() << endl;
  468. Fail = true;
  469. break;
  470. }
  471. }
  472. // Undo our operations - it might be smart to undo everything this did..
  473. if (Fail == true)
  474. {
  475. if (WasKept == true)
  476. Cache.MarkKeep(Pkg);
  477. else
  478. Cache.MarkDelete(Pkg);
  479. return false;
  480. }
  481. if (Debug == true)
  482. clog << " Re-Instated " << Pkg.Name() << endl;
  483. return true;
  484. }
  485. /*}}}*/
  486. // ProblemResolver::Resolve - Run the resolution pass /*{{{*/
  487. // ---------------------------------------------------------------------
  488. /* This routines works by calculating a score for each package. The score
  489. is derived by considering the package's priority and all reverse
  490. dependents giving an integer that reflects the amount of breakage that
  491. adjusting the package will inflict.
  492. It goes from highest score to lowest and corrects all of the breaks by
  493. keeping or removing the dependant packages. If that fails then it removes
  494. the package itself and goes on. The routine should be able to intelligently
  495. go from any broken state to a fixed state.
  496. The BrokenFix flag enables a mode where the algorithm tries to
  497. upgrade packages to advoid problems. */
  498. bool pkgProblemResolver::Resolve(bool BrokenFix)
  499. {
  500. unsigned long Size = Cache.HeaderP->PackageCount;
  501. // Record which packages are marked for install
  502. bool Again = false;
  503. do
  504. {
  505. Again = false;
  506. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  507. {
  508. if (Cache[I].Install() == true)
  509. Flags[I->ID] |= PreInstalled;
  510. else
  511. {
  512. if (Cache[I].InstBroken() == true && BrokenFix == true)
  513. {
  514. Cache.MarkInstall(I,false);
  515. if (Cache[I].Install() == true)
  516. Again = true;
  517. }
  518. Flags[I->ID] &= ~PreInstalled;
  519. }
  520. Flags[I->ID] |= Upgradable;
  521. }
  522. }
  523. while (Again == true);
  524. if (Debug == true)
  525. clog << "Starting" << endl;
  526. MakeScores();
  527. /* We have to order the packages so that the broken fixing pass
  528. operates from highest score to lowest. This prevents problems when
  529. high score packages cause the removal of lower score packages that
  530. would cause the removal of even lower score packages. */
  531. pkgCache::Package **PList = new pkgCache::Package *[Size];
  532. pkgCache::Package **PEnd = PList;
  533. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  534. *PEnd++ = I;
  535. This = this;
  536. qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort);
  537. /* for (pkgCache::Package **K = PList; K != PEnd; K++)
  538. if (Scores[(*K)->ID] != 0)
  539. {
  540. pkgCache::PkgIterator Pkg(Cache,*K);
  541. clog << Scores[(*K)->ID] << ' ' << Pkg.Name() <<
  542. ' ' << (pkgCache::Version *)Pkg.CurrentVer() << ' ' <<
  543. Cache[Pkg].InstallVer << ' ' << Cache[Pkg].CandidateVer << endl;
  544. } */
  545. if (Debug == true)
  546. clog << "Starting 2" << endl;
  547. /* Now consider all broken packages. For each broken package we either
  548. remove the package or fix it's problem. We do this once, it should
  549. not be possible for a loop to form (that is a < b < c and fixing b by
  550. changing a breaks c) */
  551. bool Change = true;
  552. for (int Counter = 0; Counter != 10 && Change == true; Counter++)
  553. {
  554. Change = false;
  555. for (pkgCache::Package **K = PList; K != PEnd; K++)
  556. {
  557. pkgCache::PkgIterator I(Cache,*K);
  558. /* We attempt to install this and see if any breaks result,
  559. this takes care of some strange cases */
  560. if (Cache[I].CandidateVer != Cache[I].InstallVer &&
  561. I->CurrentVer != 0 && Cache[I].InstallVer != 0 &&
  562. (Flags[I->ID] & PreInstalled) != 0 &&
  563. (Flags[I->ID] & Protected) == 0 &&
  564. (Flags[I->ID] & ReInstateTried) == 0)
  565. {
  566. if (Debug == true)
  567. clog << " Try to Re-Instate " << I.Name() << endl;
  568. unsigned long OldBreaks = Cache.BrokenCount();
  569. pkgCache::Version *OldVer = Cache[I].InstallVer;
  570. Flags[I->ID] &= ReInstateTried;
  571. Cache.MarkInstall(I,false);
  572. if (Cache[I].InstBroken() == true ||
  573. OldBreaks < Cache.BrokenCount())
  574. {
  575. if (OldVer == 0)
  576. Cache.MarkDelete(I);
  577. else
  578. Cache.MarkKeep(I);
  579. }
  580. else
  581. if (Debug == true)
  582. clog << "Re-Instated " << I.Name() << " (" << OldBreaks << " vs " << Cache.BrokenCount() << ')' << endl;
  583. }
  584. if (Cache[I].InstallVer == 0 || Cache[I].InstBroken() == false)
  585. continue;
  586. // Isolate the problem dependency
  587. PackageKill KillList[100];
  588. PackageKill *LEnd = KillList;
  589. for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;)
  590. {
  591. // Compute a single dependency element (glob or)
  592. pkgCache::DepIterator Start;
  593. pkgCache::DepIterator End;
  594. D.GlobOr(Start,End);
  595. // We only worry about critical deps.
  596. if (End.IsCritical() != true)
  597. continue;
  598. // Dep is ok
  599. if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  600. continue;
  601. // Hm, the group is broken.. I have no idea how to handle this
  602. if (Start != End)
  603. {
  604. clog << "Note, a broken or group was found in " << I.Name() << "." << endl;
  605. Cache.MarkDelete(I);
  606. break;
  607. }
  608. if (Debug == true)
  609. clog << "Package " << I.Name() << " has broken dep on " << End.TargetPkg().Name() << endl;
  610. /* Look across the version list. If there are no possible
  611. targets then we keep the package and bail. This is necessary
  612. if a package has a dep on another package that cant be found */
  613. pkgCache::Version **VList = End.AllTargets();
  614. if (*VList == 0 && (Flags[I->ID] & Protected) != Protected &&
  615. End->Type != pkgCache::Dep::Conflicts &&
  616. Cache[I].NowBroken() == false)
  617. {
  618. Change = true;
  619. Cache.MarkKeep(I);
  620. break;
  621. }
  622. bool Done = false;
  623. for (pkgCache::Version **V = VList; *V != 0; V++)
  624. {
  625. pkgCache::VerIterator Ver(Cache,*V);
  626. pkgCache::PkgIterator Pkg = Ver.ParentPkg();
  627. if (Debug == true)
  628. clog << " Considering " << Pkg.Name() << ' ' << (int)Scores[Pkg->ID] <<
  629. " as a solution to " << I.Name() << ' ' << (int)Scores[I->ID] << endl;
  630. if (Scores[I->ID] <= Scores[Pkg->ID] ||
  631. ((Cache[End] & pkgDepCache::DepGNow) == 0 &&
  632. End->Type != pkgCache::Dep::Conflicts))
  633. {
  634. if ((Flags[I->ID] & Protected) == Protected)
  635. continue;
  636. // See if a keep will do
  637. Cache.MarkKeep(I);
  638. if (Cache[I].InstBroken() == false)
  639. {
  640. if (Debug == true)
  641. clog << " Holding Back " << I.Name() << " rather than change " << End.TargetPkg().Name() << endl;
  642. }
  643. else
  644. {
  645. if (BrokenFix == false || DoUpgrade(I) == false)
  646. {
  647. if (Debug == true)
  648. clog << " Removing " << I.Name() << " rather than change " << End.TargetPkg().Name() << endl;
  649. Cache.MarkDelete(I);
  650. if (Counter > 1)
  651. Scores[I->ID] = Scores[Pkg->ID];
  652. }
  653. }
  654. Change = true;
  655. Done = true;
  656. break;
  657. }
  658. else
  659. {
  660. // Skip this if it is protected
  661. if ((Flags[Pkg->ID] & Protected) != 0)
  662. continue;
  663. LEnd->Pkg = Pkg;
  664. LEnd->Dep = End;
  665. LEnd++;
  666. if (End->Type != pkgCache::Dep::Conflicts)
  667. break;
  668. }
  669. }
  670. // Hm, nothing can possibly satisify this dep. Nuke it.
  671. if (VList[0] == 0 && End->Type != pkgCache::Dep::Conflicts &&
  672. (Flags[I->ID] & Protected) != Protected)
  673. {
  674. Cache.MarkKeep(I);
  675. if (Cache[I].InstBroken() == false)
  676. {
  677. if (Debug == true)
  678. clog << " Holding Back " << I.Name() << " because I can't find " << End.TargetPkg().Name() << endl;
  679. }
  680. else
  681. {
  682. if (Debug == true)
  683. clog << " Removing " << I.Name() << " because I can't find " << End.TargetPkg().Name() << endl;
  684. Cache.MarkDelete(I);
  685. }
  686. Change = true;
  687. Done = true;
  688. }
  689. delete [] VList;
  690. if (Done == true)
  691. break;
  692. }
  693. // Apply the kill list now
  694. if (Cache[I].InstallVer != 0)
  695. for (PackageKill *J = KillList; J != LEnd; J++)
  696. {
  697. Change = true;
  698. if ((Cache[J->Dep] & pkgDepCache::DepGNow) == 0)
  699. {
  700. if (J->Dep->Type == pkgCache::Dep::Conflicts)
  701. {
  702. if (Debug == true)
  703. clog << " Fixing " << I.Name() << " via remove of " << J->Pkg.Name() << endl;
  704. Cache.MarkDelete(J->Pkg);
  705. }
  706. }
  707. else
  708. {
  709. if (Debug == true)
  710. clog << " Fixing " << I.Name() << " via keep of " << J->Pkg.Name() << endl;
  711. Cache.MarkKeep(J->Pkg);
  712. }
  713. if (Counter > 1)
  714. Scores[J->Pkg->ID] = Scores[I->ID];
  715. }
  716. }
  717. }
  718. if (Debug == true)
  719. clog << "Done" << endl;
  720. delete [] Scores;
  721. delete [] PList;
  722. if (Cache.BrokenCount() != 0)
  723. return _error->Error("Internal error, pkgProblemResolver::Resolve generated breaks.");
  724. return true;
  725. }
  726. /*}}}*/
  727. // ProblemResolver::ResolveByKeep - Resolve problems using keep /*{{{*/
  728. // ---------------------------------------------------------------------
  729. /* This is the work horse of the soft upgrade routine. It is very gental
  730. in that it does not install or remove any packages. It is assumed that the
  731. system was non-broken previously. */
  732. bool pkgProblemResolver::ResolveByKeep()
  733. {
  734. unsigned long Size = Cache.HeaderP->PackageCount;
  735. if (Debug == true)
  736. clog << "Entering ResolveByKeep" << endl;
  737. MakeScores();
  738. /* We have to order the packages so that the broken fixing pass
  739. operates from highest score to lowest. This prevents problems when
  740. high score packages cause the removal of lower score packages that
  741. would cause the removal of even lower score packages. */
  742. pkgCache::Package **PList = new pkgCache::Package *[Size];
  743. pkgCache::Package **PEnd = PList;
  744. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  745. *PEnd++ = I;
  746. This = this;
  747. qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort);
  748. // Consider each broken package
  749. pkgCache::Package **LastStop = 0;
  750. for (pkgCache::Package **K = PList; K != PEnd; K++)
  751. {
  752. pkgCache::PkgIterator I(Cache,*K);
  753. if (Cache[I].InstallVer == 0 || Cache[I].InstBroken() == false)
  754. continue;
  755. /* Keep the package. If this works then great, otherwise we have
  756. to be significantly more agressive and manipulate its dependencies */
  757. if ((Flags[I->ID] & Protected) == 0)
  758. {
  759. if (Debug == true)
  760. clog << "Keeping package " << I.Name() << endl;
  761. Cache.MarkKeep(I);
  762. if (Cache[I].InstBroken() == false)
  763. {
  764. K = PList;
  765. continue;
  766. }
  767. }
  768. // Isolate the problem dependencies
  769. for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;)
  770. {
  771. // Compute a single dependency element (glob or)
  772. pkgCache::DepIterator Start = D;
  773. pkgCache::DepIterator End = D;
  774. unsigned char State = 0;
  775. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  776. {
  777. State |= Cache[D];
  778. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  779. if (LastOR == true)
  780. End = D;
  781. }
  782. // We only worry about critical deps.
  783. if (End.IsCritical() != true)
  784. continue;
  785. // Dep is ok
  786. if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  787. continue;
  788. // Hm, the group is broken.. I have no idea how to handle this
  789. if (Start != End)
  790. {
  791. clog << "Note, a broken or group was found in " << I.Name() << "." << endl;
  792. if ((Flags[I->ID] & Protected) == 0)
  793. Cache.MarkKeep(I);
  794. break;
  795. }
  796. if (Debug == true)
  797. clog << "Package " << I.Name() << " has broken dep on " << End.TargetPkg().Name() << endl;
  798. // Look at all the possible provides on this package
  799. pkgCache::Version **VList = End.AllTargets();
  800. for (pkgCache::Version **V = VList; *V != 0; V++)
  801. {
  802. pkgCache::VerIterator Ver(Cache,*V);
  803. pkgCache::PkgIterator Pkg = Ver.ParentPkg();
  804. // It is not keepable
  805. if (Cache[Pkg].InstallVer == 0 ||
  806. Pkg->CurrentVer == 0)
  807. continue;
  808. if ((Flags[I->ID] & Protected) == 0)
  809. {
  810. if (Debug == true)
  811. clog << " Keeping Package " << Pkg.Name() << " due to dep" << endl;
  812. Cache.MarkKeep(Pkg);
  813. }
  814. if (Cache[I].InstBroken() == false)
  815. break;
  816. }
  817. if (Cache[I].InstBroken() == false)
  818. break;
  819. }
  820. if (Cache[I].InstBroken() == true)
  821. continue;
  822. // Restart again.
  823. if (K == LastStop)
  824. return _error->Error("Internal Error, pkgProblemResolver::ResolveByKeep is looping on package %s.",I.Name());
  825. LastStop = K;
  826. K = PList;
  827. }
  828. return true;
  829. }
  830. /*}}}*/
  831. // ProblemResolver::InstallProtect - Install all protected packages /*{{{*/
  832. // ---------------------------------------------------------------------
  833. /* This is used to make sure protected packages are installed */
  834. void pkgProblemResolver::InstallProtect()
  835. {
  836. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  837. {
  838. if ((Flags[I->ID] & Protected) == Protected)
  839. {
  840. if ((Flags[I->ID] & ToRemove) == ToRemove)
  841. Cache.MarkDelete(I);
  842. else
  843. Cache.MarkInstall(I,false);
  844. }
  845. }
  846. }
  847. /*}}}*/