algorithms.cc 50 KB

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