algorithms.cc 38 KB

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