algorithms.cc 40 KB

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