algorithms.cc 51 KB

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