algorithms.cc 31 KB

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