algorithms.cc 37 KB

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