algorithms.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: algorithms.cc,v 1.3 1998/07/12 23:58:20 jgg Exp $
  4. /* ######################################################################
  5. Algorithms - A set of misc algorithms
  6. ##################################################################### */
  7. /*}}}*/
  8. // Include Files /*{{{*/
  9. #ifdef __GNUG__
  10. #pragma implementation "apt-pkg/algorithms.h"
  11. #endif
  12. #include <apt-pkg/algorithms.h>
  13. #include <apt-pkg/error.h>
  14. #include <iostream.h>
  15. /*}}}*/
  16. pkgProblemResolver *pkgProblemResolver::This = 0;
  17. // Simulate::Simulate - Constructor /*{{{*/
  18. // ---------------------------------------------------------------------
  19. /* */
  20. pkgSimulate::pkgSimulate(pkgDepCache &Cache) : pkgPackageManager(Cache),
  21. Sim(Cache)
  22. {
  23. Flags = new unsigned char[Cache.HeaderP->PackageCount];
  24. memset(Flags,0,sizeof(*Flags)*Cache.HeaderP->PackageCount);
  25. }
  26. /*}}}*/
  27. // Simulate::Install - Simulate unpacking of a package /*{{{*/
  28. // ---------------------------------------------------------------------
  29. /* */
  30. bool pkgSimulate::Install(PkgIterator iPkg,string /*File*/)
  31. {
  32. // Adapt the iterator
  33. PkgIterator Pkg = Sim.FindPkg(iPkg.Name());
  34. Flags[Pkg->ID] = 1;
  35. cout << "Inst " << Pkg.Name();
  36. Sim.MarkInstall(Pkg,false);
  37. // Look for broken conflicts+predepends.
  38. for (PkgIterator I = Sim.PkgBegin(); I.end() == false; I++)
  39. {
  40. if (Sim[I].InstallVer == 0)
  41. continue;
  42. for (DepIterator D = Sim[I].InstVerIter(Sim).DependsList(); D.end() == false; D++)
  43. if (D->Type == pkgCache::Dep::Conflicts || D->Type == pkgCache::Dep::PreDepends)
  44. {
  45. if ((Sim[D] & pkgDepCache::DepInstall) == 0)
  46. {
  47. cout << " [" << I.Name() << " on " << D.TargetPkg().Name() << ']';
  48. if (D->Type == pkgCache::Dep::Conflicts)
  49. _error->Error("Fatal, conflicts violated %s",I.Name());
  50. }
  51. }
  52. }
  53. if (Sim.BrokenCount() != 0)
  54. ShortBreaks();
  55. else
  56. cout << endl;
  57. return true;
  58. }
  59. /*}}}*/
  60. // Simulate::Configure - Simulate configuration of a Package /*{{{*/
  61. // ---------------------------------------------------------------------
  62. /* This is not an acurate simulation of relatity, we should really not
  63. install the package.. For some investigations it may be necessary
  64. however. */
  65. bool pkgSimulate::Configure(PkgIterator iPkg)
  66. {
  67. // Adapt the iterator
  68. PkgIterator Pkg = Sim.FindPkg(iPkg.Name());
  69. Flags[Pkg->ID] = 2;
  70. // Sim.MarkInstall(Pkg,false);
  71. if (Sim[Pkg].InstBroken() == true)
  72. {
  73. cout << "Conf " << Pkg.Name() << " broken" << endl;
  74. Sim.Update();
  75. // Print out each package and the failed dependencies
  76. for (pkgCache::DepIterator D = Sim[Pkg].InstVerIter(Sim).DependsList(); D.end() == false; D++)
  77. {
  78. if (Sim.IsImportantDep(D) == false ||
  79. (Sim[D] & pkgDepCache::DepInstall) != 0)
  80. continue;
  81. if (D->Type == pkgCache::Dep::Conflicts)
  82. cout << " Conflicts:" << D.TargetPkg().Name();
  83. else
  84. cout << " Depends:" << D.TargetPkg().Name();
  85. }
  86. cout << endl;
  87. _error->Error("Conf Broken %s",Pkg.Name());
  88. }
  89. else
  90. cout << "Conf " << Pkg.Name();
  91. if (Sim.BrokenCount() != 0)
  92. ShortBreaks();
  93. else
  94. cout << endl;
  95. return true;
  96. }
  97. /*}}}*/
  98. // Simulate::Remove - Simulate the removal of a package /*{{{*/
  99. // ---------------------------------------------------------------------
  100. /* */
  101. bool pkgSimulate::Remove(PkgIterator iPkg)
  102. {
  103. // Adapt the iterator
  104. PkgIterator Pkg = Sim.FindPkg(iPkg.Name());
  105. Flags[Pkg->ID] = 3;
  106. Sim.MarkDelete(Pkg);
  107. cout << "Remv " << Pkg.Name();
  108. if (Sim.BrokenCount() != 0)
  109. ShortBreaks();
  110. else
  111. cout << endl;
  112. return true;
  113. }
  114. /*}}}*/
  115. // Simulate::ShortBreaks - Print out a short line describing all breaks /*{{{*/
  116. // ---------------------------------------------------------------------
  117. /* */
  118. void pkgSimulate::ShortBreaks()
  119. {
  120. cout << " [";
  121. for (PkgIterator I = Sim.PkgBegin(); I.end() == false; I++)
  122. {
  123. if (Sim[I].InstBroken() == true)
  124. {
  125. if (Flags[I->ID] == 0)
  126. cout << I.Name() << ' ';
  127. /* else
  128. cout << I.Name() << "! ";*/
  129. }
  130. }
  131. cout << ']' << endl;
  132. }
  133. /*}}}*/
  134. // ApplyStatus - Adjust for non-ok packages /*{{{*/
  135. // ---------------------------------------------------------------------
  136. /* We attempt to change the state of the all packages that have failed
  137. installation toward their real state. The ordering code will perform
  138. the necessary calculations to deal with the problems. */
  139. bool pkgApplyStatus(pkgDepCache &Cache)
  140. {
  141. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  142. {
  143. switch (I->CurrentState)
  144. {
  145. // This means installation failed somehow
  146. case pkgCache::State::UnPacked:
  147. case pkgCache::State::HalfConfigured:
  148. Cache.MarkKeep(I);
  149. break;
  150. // This means removal failed
  151. case pkgCache::State::HalfInstalled:
  152. Cache.MarkDelete(I);
  153. break;
  154. default:
  155. if (I->InstState != pkgCache::State::Ok)
  156. return _error->Error("The package %s is not ok and I "
  157. "don't know how to fix it!",I.Name());
  158. }
  159. }
  160. return true;
  161. }
  162. /*}}}*/
  163. // FixBroken - Fix broken packages /*{{{*/
  164. // ---------------------------------------------------------------------
  165. /* This autoinstalls every broken package and then runs ScoredFix on the
  166. result. */
  167. bool pkgFixBroken(pkgDepCache &Cache)
  168. {
  169. // Auto upgrade all broken packages
  170. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  171. if (Cache[I].NowBroken() == true)
  172. Cache.MarkInstall(I,true);
  173. /* Fix packages that are in a NeedArchive state but don't have a
  174. downloadable install version */
  175. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  176. {
  177. if (I.State() != pkgCache::PkgIterator::NeedsUnpack ||
  178. Cache[I].Delete() == true)
  179. continue;
  180. if (Cache[I].InstVerIter(Cache).Downloadable() == false)
  181. continue;
  182. Cache.MarkInstall(I,true);
  183. }
  184. pkgProblemResolver Fix(Cache);
  185. return Fix.Resolve(true);
  186. }
  187. /*}}}*/
  188. // DistUpgrade - Distribution upgrade /*{{{*/
  189. // ---------------------------------------------------------------------
  190. /* This autoinstalls every package and then force installs every
  191. pre-existing package. This creates the initial set of conditions which
  192. most likely contain problems because too many things were installed.
  193. ScoredFix is used to resolve the problems.
  194. */
  195. bool pkgDistUpgrade(pkgDepCache &Cache)
  196. {
  197. /* Auto upgrade all installed packages, this provides the basis
  198. for the installation */
  199. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  200. if (I->CurrentVer != 0)
  201. Cache.MarkInstall(I,true);
  202. /* Now, auto upgrade all essential packages - this ensures that
  203. the essential packages are present and working */
  204. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  205. if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  206. Cache.MarkInstall(I,true);
  207. /* We do it again over all previously installed packages to force
  208. conflict resolution on them all. */
  209. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  210. if (I->CurrentVer != 0)
  211. Cache.MarkInstall(I,false);
  212. pkgProblemResolver Fix(Cache);
  213. // Hold back held packages.
  214. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  215. {
  216. if (I->SelectedState == pkgCache::State::Hold)
  217. {
  218. Fix.Protect(I);
  219. Cache.MarkKeep(I);
  220. }
  221. }
  222. return Fix.Resolve();
  223. }
  224. /*}}}*/
  225. // ProblemResolver::pkgProblemResolver - Constructor /*{{{*/
  226. // ---------------------------------------------------------------------
  227. /* */
  228. pkgProblemResolver::pkgProblemResolver(pkgDepCache &Cache) : Cache(Cache)
  229. {
  230. // Allocate memory
  231. unsigned long Size = Cache.HeaderP->PackageCount;
  232. Scores = new signed short[Size];
  233. Flags = new unsigned char[Size];
  234. memset(Flags,0,sizeof(*Flags)*Size);
  235. // Set debug to true to see its decision logic
  236. Debug = false;
  237. }
  238. /*}}}*/
  239. // ProblemResolver::ScoreSort - Sort the list by score /*{{{*/
  240. // ---------------------------------------------------------------------
  241. /* */
  242. int pkgProblemResolver::ScoreSort(const void *a,const void *b)
  243. {
  244. Package const **A = (Package const **)a;
  245. Package const **B = (Package const **)b;
  246. if (This->Scores[(*A)->ID] > This->Scores[(*B)->ID])
  247. return -1;
  248. if (This->Scores[(*A)->ID] < This->Scores[(*B)->ID])
  249. return 1;
  250. return 0;
  251. }
  252. /*}}}*/
  253. // ProblemResolver::MakeScores - Make the score table /*{{{*/
  254. // ---------------------------------------------------------------------
  255. /* */
  256. void pkgProblemResolver::MakeScores()
  257. {
  258. unsigned long Size = Cache.HeaderP->PackageCount;
  259. memset(Scores,0,sizeof(*Scores)*Size);
  260. // Generate the base scores for a package based on its properties
  261. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  262. {
  263. if (Cache[I].InstallVer == 0)
  264. continue;
  265. signed short &Score = Scores[I->ID];
  266. /* This is arbitary, it should be high enough to elevate an
  267. essantial package above most other packages but low enough
  268. to allow an obsolete essential packages to be removed by
  269. a conflicts on a powerfull normal package (ie libc6) */
  270. if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  271. Score += 100;
  272. // We transform the priority
  273. // Important Required Standard Optional Extra
  274. signed short PrioMap[] = {0,3,2,1,-1,-2};
  275. if (Cache[I].InstVerIter(Cache)->Priority <= 5)
  276. Score += PrioMap[Cache[I].InstVerIter(Cache)->Priority];
  277. /* This helps to fix oddball problems with conflicting packages
  278. on the same level. We enhance the score of installed packages */
  279. if (I->CurrentVer != 0)
  280. Score += 1;
  281. }
  282. // Now that we have the base scores we go and propogate dependencies
  283. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  284. {
  285. if (Cache[I].InstallVer == 0)
  286. continue;
  287. for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false; D++)
  288. {
  289. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  290. Scores[D.TargetPkg()->ID]++;
  291. }
  292. }
  293. // Copy the scores to advoid additive looping
  294. signed short *OldScores = new signed short[Size];
  295. memcpy(OldScores,Scores,sizeof(*Scores)*Size);
  296. /* Now we cause 1 level of dependency inheritance, that is we add the
  297. score of the packages that depend on the target Package. This
  298. fortifies high scoring packages */
  299. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  300. {
  301. if (Cache[I].InstallVer == 0)
  302. continue;
  303. for (pkgCache::DepIterator D = I.RevDependsList(); D.end() == false; D++)
  304. {
  305. // Only do it for the install version
  306. if ((pkgCache::Version *)D.ParentVer() != Cache[D.ParentPkg()].InstallVer ||
  307. (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends))
  308. continue;
  309. Scores[I->ID] += abs(OldScores[D.ParentPkg()->ID]);
  310. }
  311. }
  312. /* Now we propogate along provides. This makes the packages that
  313. provide important packages extremely important */
  314. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  315. {
  316. for (pkgCache::PrvIterator P = I.ProvidesList(); P.end() == false; P++)
  317. {
  318. // Only do it once per package
  319. if ((pkgCache::Version *)P.OwnerVer() != Cache[P.OwnerPkg()].InstallVer)
  320. continue;
  321. Scores[P.OwnerPkg()->ID] += abs(Scores[I->ID] - OldScores[I->ID]);
  322. }
  323. }
  324. /* Protected things are pushed really high up. This number should put them
  325. ahead of everything */
  326. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  327. if ((Flags[I->ID] & Protected) != 0)
  328. Scores[I->ID] += 10000;
  329. delete [] OldScores;
  330. }
  331. /*}}}*/
  332. // ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/
  333. // ---------------------------------------------------------------------
  334. /* This goes through and tries to reinstall packages to make this package
  335. installable */
  336. bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg)
  337. {
  338. if ((Flags[Pkg->ID] & Upgradable) == 0 || Cache[Pkg].Upgradable() == false)
  339. return false;
  340. Flags[Pkg->ID] &= ~Upgradable;
  341. bool WasKept = Cache[Pkg].Keep();
  342. Cache.MarkInstall(Pkg,false);
  343. // Isolate the problem dependency
  344. bool Fail = false;
  345. for (pkgCache::DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
  346. {
  347. // Compute a single dependency element (glob or)
  348. pkgCache::DepIterator Start = D;
  349. pkgCache::DepIterator End = D;
  350. unsigned char State = 0;
  351. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  352. {
  353. State |= Cache[D];
  354. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  355. if (LastOR == true)
  356. End = D;
  357. }
  358. // We only worry about critical deps.
  359. if (End.IsCritical() != true)
  360. continue;
  361. // Dep is ok
  362. if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  363. continue;
  364. // Hm, the group is broken.. I have no idea how to handle this
  365. if (Start != End)
  366. {
  367. cout << "Note, a broken or group was found in " << Pkg.Name() << "." << endl;
  368. Fail = true;
  369. break;
  370. }
  371. // Upgrade the package if the candidate version will fix the problem.
  372. if ((Cache[Start] & pkgDepCache::DepCVer) == pkgDepCache::DepCVer)
  373. {
  374. PkgIterator P = Start.SmartTargetPkg();
  375. if (DoUpgrade(P) == false)
  376. {
  377. if (Debug == true)
  378. cout << " Reinst Failed because of " << P.Name() << endl;
  379. Fail = true;
  380. break;
  381. }
  382. }
  383. else
  384. {
  385. /* We let the algorithm deal with conflicts on its next iteration,
  386. it is much smarter than us */
  387. if (End->Type == pkgCache::Dep::Conflicts)
  388. continue;
  389. if (Debug == true)
  390. cout << " Reinst Failed early because of " << Start.TargetPkg().Name() << endl;
  391. Fail = true;
  392. break;
  393. }
  394. }
  395. // Undo our operations - it might be smart to undo everything this did..
  396. if (Fail == true)
  397. {
  398. if (WasKept == true)
  399. Cache.MarkKeep(Pkg);
  400. else
  401. Cache.MarkDelete(Pkg);
  402. return false;
  403. }
  404. if (Debug == true)
  405. cout << " Re-Instated " << Pkg.Name() << endl;
  406. return true;
  407. }
  408. /*}}}*/
  409. // ProblemResolver::Resolve - Run the resolution pass /*{{{*/
  410. // ---------------------------------------------------------------------
  411. /* This routines works by calculating a score for each package. The score
  412. is derived by considering the package's priority and all reverse
  413. dependents giving an integer that reflects the amount of breakage that
  414. adjusting the package will inflict.
  415. It goes from highest score to lowest and corrects all of the breaks by
  416. keeping or removing the dependant packages. If that fails then it removes
  417. the package itself and goes on. The routine should be able to intelligently
  418. go from any broken state to a fixed state.
  419. The BrokenFix flag enables a mode where the algorithm tries to
  420. upgrade packages to advoid problems. */
  421. bool pkgProblemResolver::Resolve(bool BrokenFix)
  422. {
  423. unsigned long Size = Cache.HeaderP->PackageCount;
  424. // Record which packages are marked for install
  425. bool Again = false;
  426. do
  427. {
  428. Again = false;
  429. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  430. {
  431. if (Cache[I].Install() == true)
  432. Flags[I->ID] |= PreInstalled;
  433. else
  434. {
  435. if (Cache[I].InstBroken() == true && BrokenFix == true)
  436. {
  437. Cache.MarkInstall(I,false);
  438. if (Cache[I].Install() == true)
  439. Again = true;
  440. }
  441. Flags[I->ID] &= ~PreInstalled;
  442. }
  443. Flags[I->ID] |= Upgradable;
  444. }
  445. }
  446. while (Again == true);
  447. if (Debug == true)
  448. cout << "Starting" << endl;
  449. MakeScores();
  450. /* We have to order the packages so that the broken fixing pass
  451. operates from highest score to lowest. This prevents problems when
  452. high score packages cause the removal of lower score packages that
  453. would cause the removal of even lower score packages. */
  454. pkgCache::Package **PList = new pkgCache::Package *[Size];
  455. pkgCache::Package **PEnd = PList;
  456. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  457. *PEnd++ = I;
  458. This = this;
  459. qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort);
  460. /* for (pkgCache::Package **K = PList; K != PEnd; K++)
  461. if (Scores[(*K)->ID] != 0)
  462. {
  463. pkgCache::PkgIterator Pkg(Cache,*K);
  464. cout << Scores[(*K)->ID] << ' ' << Pkg.Name() <<
  465. ' ' << (pkgCache::Version *)Pkg.CurrentVer() << ' ' <<
  466. Cache[Pkg].InstallVer << ' ' << Cache[Pkg].CandidateVer << endl;
  467. } */
  468. if (Debug == true)
  469. cout << "Starting 2" << endl;
  470. /* Now consider all broken packages. For each broken package we either
  471. remove the package or fix it's problem. We do this once, it should
  472. not be possible for a loop to form (that is a < b < c and fixing b by
  473. changing a breaks c) */
  474. bool Change = true;
  475. for (int Counter = 0; Counter != 10 && Change == true; Counter++)
  476. {
  477. Change = false;
  478. for (pkgCache::Package **K = PList; K != PEnd; K++)
  479. {
  480. pkgCache::PkgIterator I(Cache,*K);
  481. /* We attempt to install this and see if any breaks result,
  482. this takes care of some strange cases */
  483. if (Cache[I].CandidateVer != Cache[I].InstallVer &&
  484. I->CurrentVer != 0 && Cache[I].InstallVer != 0 &&
  485. (Flags[I->ID] & PreInstalled) != 0 &&
  486. (Flags[I->ID] & Protected) == 0)
  487. {
  488. if (Debug == true)
  489. cout << " Try to Re-Instate " << I.Name() << endl;
  490. int OldBreaks = Cache.BrokenCount();
  491. pkgCache::Version *OldVer = Cache[I].InstallVer;
  492. Cache.MarkInstall(I,false);
  493. if (Cache[I].InstBroken() == true ||
  494. OldBreaks < Cache.BrokenCount())
  495. {
  496. if (OldVer == 0)
  497. Cache.MarkDelete(I);
  498. else
  499. Cache.MarkKeep(I);
  500. }
  501. else
  502. if (Debug == true)
  503. cout << "Re-Instated " << I.Name() << endl;
  504. }
  505. if (Cache[I].InstallVer == 0 || Cache[I].InstBroken() == false)
  506. continue;
  507. // Isolate the problem dependency
  508. PackageKill KillList[100];
  509. PackageKill *LEnd = KillList;
  510. for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;)
  511. {
  512. // Compute a single dependency element (glob or)
  513. pkgCache::DepIterator Start = D;
  514. pkgCache::DepIterator End = D;
  515. unsigned char State = 0;
  516. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  517. {
  518. State |= Cache[D];
  519. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  520. if (LastOR == true)
  521. End = D;
  522. }
  523. // We only worry about critical deps.
  524. if (End.IsCritical() != true)
  525. continue;
  526. // Dep is ok
  527. if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  528. continue;
  529. // Hm, the group is broken.. I have no idea how to handle this
  530. if (Start != End)
  531. {
  532. cout << "Note, a broken or group was found in " << I.Name() << "." << endl;
  533. Cache.MarkDelete(I);
  534. break;
  535. }
  536. if (Debug == true)
  537. cout << "Package " << I.Name() << " has broken dep on " << End.TargetPkg().Name() << endl;
  538. /* Conflicts is simple, decide if we should remove this package
  539. or the conflicted one */
  540. pkgCache::Version **VList = End.AllTargets();
  541. bool Done = false;
  542. for (pkgCache::Version **V = VList; *V != 0; V++)
  543. {
  544. pkgCache::VerIterator Ver(Cache,*V);
  545. pkgCache::PkgIterator Pkg = Ver.ParentPkg();
  546. if (Debug == true)
  547. cout << " Considering " << Pkg.Name() << ' ' << (int)Scores[Pkg->ID] <<
  548. " as a solution to " << I.Name() << ' ' << (int)Scores[I->ID] << endl;
  549. if (Scores[I->ID] <= Scores[Pkg->ID] ||
  550. ((Cache[End] & pkgDepCache::DepGNow) == 0 &&
  551. End->Type != pkgCache::Dep::Conflicts))
  552. {
  553. if ((Flags[I->ID] & Protected) != 0)
  554. continue;
  555. // See if a keep will do
  556. Cache.MarkKeep(I);
  557. if (Cache[I].InstBroken() == false)
  558. {
  559. if (Debug == true)
  560. cout << " Holding Back " << I.Name() << " rather than change " << End.TargetPkg().Name() << endl;
  561. }
  562. else
  563. {
  564. if (BrokenFix == false || DoUpgrade(I) == false)
  565. {
  566. if (Debug == true)
  567. cout << " Removing " << I.Name() << " rather than change " << End.TargetPkg().Name() << endl;
  568. Cache.MarkDelete(I);
  569. if (Counter > 1)
  570. Scores[I->ID] = Scores[Pkg->ID];
  571. }
  572. }
  573. Change = true;
  574. Done = true;
  575. break;
  576. }
  577. else
  578. {
  579. // Skip this if it is protected
  580. if ((Flags[Pkg->ID] & Protected) != 0)
  581. continue;
  582. LEnd->Pkg = Pkg;
  583. LEnd->Dep = End;
  584. LEnd++;
  585. if (End->Type != pkgCache::Dep::Conflicts)
  586. break;
  587. }
  588. }
  589. // Hm, nothing can possibly satisify this dep. Nuke it.
  590. if (VList[0] == 0 && End->Type != pkgCache::Dep::Conflicts)
  591. {
  592. Cache.MarkKeep(I);
  593. if (Cache[I].InstBroken() == false)
  594. {
  595. if (Debug == true)
  596. cout << " Holding Back " << I.Name() << " because I can't find " << End.TargetPkg().Name() << endl;
  597. }
  598. else
  599. {
  600. if (Debug == true)
  601. cout << " Removing " << I.Name() << " because I can't find " << End.TargetPkg().Name() << endl;
  602. Cache.MarkDelete(I);
  603. }
  604. Change = true;
  605. Done = true;
  606. }
  607. delete [] VList;
  608. if (Done == true)
  609. break;
  610. }
  611. // Apply the kill list now
  612. if (Cache[I].InstallVer != 0)
  613. for (PackageKill *J = KillList; J != LEnd; J++)
  614. {
  615. Change = true;
  616. if ((Cache[J->Dep] & pkgDepCache::DepGNow) == 0)
  617. {
  618. if (J->Dep->Type == pkgCache::Dep::Conflicts)
  619. {
  620. if (Debug == true)
  621. cout << " Fixing " << I.Name() << " via remove of " << J->Pkg.Name() << endl;
  622. Cache.MarkDelete(J->Pkg);
  623. }
  624. }
  625. else
  626. {
  627. if (Debug == true)
  628. cout << " Fixing " << I.Name() << " via keep of " << J->Pkg.Name() << endl;
  629. Cache.MarkKeep(J->Pkg);
  630. }
  631. if (Counter > 1)
  632. Scores[J->Pkg->ID] = Scores[I->ID];
  633. }
  634. }
  635. }
  636. if (Debug == true)
  637. cout << "Done" << endl;
  638. delete [] Scores;
  639. delete [] PList;
  640. if (Cache.BrokenCount() != 0)
  641. return _error->Error("Internal error, ScoredFix generated breaks.");
  642. return true;
  643. }
  644. /*}}}*/