algorithms.cc 45 KB

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