algorithms.cc 52 KB

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