algorithms.cc 40 KB

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