algorithms.cc 32 KB

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