algorithms.cc 44 KB

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