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