algorithms.cc 30 KB

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