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.
 
 
 
 
 
 

1305 lines
40 KiB

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