algorithms.cc 46 KB

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