You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1026 lines
32 KiB

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