algorithms.cc 47 KB

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