algorithms.cc 45 KB

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