algorithms.cc 43 KB

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