You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1356 lines
44 KiB

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