algorithms.cc 49 KB

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