algorithms.cc 54 KB

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