algorithms.cc 49 KB

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