algorithms.cc 46 KB

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