algorithms.cc 45 KB

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