algorithms.cc 48 KB

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