algorithms.cc 47 KB

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