You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1428 lines
44 KiB

  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 AddProtected = _config->FindI("pkgProblemResolver::Scores::AddProtected",10000);
  448. signed short AddEssential = _config->FindI("pkgProblemResolver::Scores::AddEssential",5000);
  449. if (_config->FindB("Debug::pkgProblemResolver::ShowScores",false) == true)
  450. clog << "Settings used to calculate pkgProblemResolver::Scores::" << endl
  451. << " Important => " << PrioMap[1] << endl
  452. << " Required => " << PrioMap[2] << endl
  453. << " Standard => " << PrioMap[3] << endl
  454. << " Optional => " << PrioMap[4] << endl
  455. << " Extra => " << PrioMap[5] << endl
  456. << " Essentials => " << PrioEssentials << endl
  457. << " InstalledAndNotObsolete => " << PrioInstalledAndNotObsolete << endl
  458. << " Depends => " << PrioDepends << endl
  459. << " AddProtected => " << AddProtected << endl
  460. << " AddEssential => " << AddEssential << endl;
  461. // Generate the base scores for a package based on its properties
  462. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  463. {
  464. if (Cache[I].InstallVer == 0)
  465. continue;
  466. signed short &Score = Scores[I->ID];
  467. /* This is arbitrary, it should be high enough to elevate an
  468. essantial package above most other packages but low enough
  469. to allow an obsolete essential packages to be removed by
  470. a conflicts on a powerfull normal package (ie libc6) */
  471. if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  472. Score += PrioEssentials;
  473. // We transform the priority
  474. if (Cache[I].InstVerIter(Cache)->Priority <= 5)
  475. Score += PrioMap[Cache[I].InstVerIter(Cache)->Priority];
  476. /* This helps to fix oddball problems with conflicting packages
  477. on the same level. We enhance the score of installed packages
  478. if those are not obsolete
  479. */
  480. if (I->CurrentVer != 0 && Cache[I].CandidateVer != 0 && Cache[I].CandidateVerIter(Cache).Downloadable())
  481. Score += PrioInstalledAndNotObsolete;
  482. }
  483. // Now that we have the base scores we go and propogate dependencies
  484. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  485. {
  486. if (Cache[I].InstallVer == 0)
  487. continue;
  488. for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false; D++)
  489. {
  490. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  491. Scores[D.TargetPkg()->ID]+= PrioDepends;
  492. }
  493. }
  494. // Copy the scores to advoid additive looping
  495. SPtrArray<signed short> OldScores = new signed short[Size];
  496. memcpy(OldScores,Scores,sizeof(*Scores)*Size);
  497. /* Now we cause 1 level of dependency inheritance, that is we add the
  498. score of the packages that depend on the target Package. This
  499. fortifies high scoring packages */
  500. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  501. {
  502. if (Cache[I].InstallVer == 0)
  503. continue;
  504. for (pkgCache::DepIterator D = I.RevDependsList(); D.end() == false; D++)
  505. {
  506. // Only do it for the install version
  507. if ((pkgCache::Version *)D.ParentVer() != Cache[D.ParentPkg()].InstallVer ||
  508. (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends))
  509. continue;
  510. Scores[I->ID] += abs(OldScores[D.ParentPkg()->ID]);
  511. }
  512. }
  513. /* Now we propogate along provides. This makes the packages that
  514. provide important packages extremely important */
  515. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  516. {
  517. for (pkgCache::PrvIterator P = I.ProvidesList(); P.end() == false; P++)
  518. {
  519. // Only do it once per package
  520. if ((pkgCache::Version *)P.OwnerVer() != Cache[P.OwnerPkg()].InstallVer)
  521. continue;
  522. Scores[P.OwnerPkg()->ID] += abs(Scores[I->ID] - OldScores[I->ID]);
  523. }
  524. }
  525. /* Protected things are pushed really high up. This number should put them
  526. ahead of everything */
  527. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  528. {
  529. if ((Flags[I->ID] & Protected) != 0)
  530. Scores[I->ID] += AddProtected;
  531. if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  532. Scores[I->ID] += AddEssential;
  533. }
  534. }
  535. /*}}}*/
  536. // ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/
  537. // ---------------------------------------------------------------------
  538. /* This goes through and tries to reinstall packages to make this package
  539. installable */
  540. bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg)
  541. {
  542. pkgDepCache::ActionGroup group(Cache);
  543. if ((Flags[Pkg->ID] & Upgradable) == 0 || Cache[Pkg].Upgradable() == false)
  544. return false;
  545. if ((Flags[Pkg->ID] & Protected) == Protected)
  546. return false;
  547. Flags[Pkg->ID] &= ~Upgradable;
  548. bool WasKept = Cache[Pkg].Keep();
  549. Cache.MarkInstall(Pkg, false, 0, false);
  550. // This must be a virtual package or something like that.
  551. if (Cache[Pkg].InstVerIter(Cache).end() == true)
  552. return false;
  553. // Isolate the problem dependency
  554. bool Fail = false;
  555. for (pkgCache::DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
  556. {
  557. // Compute a single dependency element (glob or)
  558. pkgCache::DepIterator Start = D;
  559. pkgCache::DepIterator End = D;
  560. unsigned char State = 0;
  561. for (bool LastOR = true; D.end() == false && LastOR == true;)
  562. {
  563. State |= Cache[D];
  564. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  565. D++;
  566. if (LastOR == true)
  567. End = D;
  568. }
  569. // We only worry about critical deps.
  570. if (End.IsCritical() != true)
  571. continue;
  572. // Iterate over all the members in the or group
  573. while (1)
  574. {
  575. // Dep is ok now
  576. if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  577. break;
  578. // Do not change protected packages
  579. PkgIterator P = Start.SmartTargetPkg();
  580. if ((Flags[P->ID] & Protected) == Protected)
  581. {
  582. if (Debug == true)
  583. clog << " Reinst Failed because of protected " << P.Name() << endl;
  584. Fail = true;
  585. }
  586. else
  587. {
  588. // Upgrade the package if the candidate version will fix the problem.
  589. if ((Cache[Start] & pkgDepCache::DepCVer) == pkgDepCache::DepCVer)
  590. {
  591. if (DoUpgrade(P) == false)
  592. {
  593. if (Debug == true)
  594. clog << " Reinst Failed because of " << P.Name() << endl;
  595. Fail = true;
  596. }
  597. else
  598. {
  599. Fail = false;
  600. break;
  601. }
  602. }
  603. else
  604. {
  605. /* We let the algorithm deal with conflicts on its next iteration,
  606. it is much smarter than us */
  607. if (Start->Type == pkgCache::Dep::Conflicts ||
  608. Start->Type == pkgCache::Dep::DpkgBreaks ||
  609. Start->Type == pkgCache::Dep::Obsoletes)
  610. break;
  611. if (Debug == true)
  612. clog << " Reinst Failed early because of " << Start.TargetPkg().Name() << endl;
  613. Fail = true;
  614. }
  615. }
  616. if (Start == End)
  617. break;
  618. Start++;
  619. }
  620. if (Fail == true)
  621. break;
  622. }
  623. // Undo our operations - it might be smart to undo everything this did..
  624. if (Fail == true)
  625. {
  626. if (WasKept == true)
  627. Cache.MarkKeep(Pkg, false, false);
  628. else
  629. Cache.MarkDelete(Pkg);
  630. return false;
  631. }
  632. if (Debug == true)
  633. clog << " Re-Instated " << Pkg.Name() << endl;
  634. return true;
  635. }
  636. /*}}}*/
  637. // ProblemResolver::Resolve - Run the resolution pass /*{{{*/
  638. // ---------------------------------------------------------------------
  639. /* This routines works by calculating a score for each package. The score
  640. is derived by considering the package's priority and all reverse
  641. dependents giving an integer that reflects the amount of breakage that
  642. adjusting the package will inflict.
  643. It goes from highest score to lowest and corrects all of the breaks by
  644. keeping or removing the dependant packages. If that fails then it removes
  645. the package itself and goes on. The routine should be able to intelligently
  646. go from any broken state to a fixed state.
  647. The BrokenFix flag enables a mode where the algorithm tries to
  648. upgrade packages to advoid problems. */
  649. bool pkgProblemResolver::Resolve(bool BrokenFix)
  650. {
  651. pkgDepCache::ActionGroup group(Cache);
  652. unsigned long Size = Cache.Head().PackageCount;
  653. // Record which packages are marked for install
  654. bool Again = false;
  655. do
  656. {
  657. Again = false;
  658. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  659. {
  660. if (Cache[I].Install() == true)
  661. Flags[I->ID] |= PreInstalled;
  662. else
  663. {
  664. if (Cache[I].InstBroken() == true && BrokenFix == true)
  665. {
  666. Cache.MarkInstall(I, false, 0, false);
  667. if (Cache[I].Install() == true)
  668. Again = true;
  669. }
  670. Flags[I->ID] &= ~PreInstalled;
  671. }
  672. Flags[I->ID] |= Upgradable;
  673. }
  674. }
  675. while (Again == true);
  676. if (Debug == true)
  677. clog << "Starting" << endl;
  678. MakeScores();
  679. /* We have to order the packages so that the broken fixing pass
  680. operates from highest score to lowest. This prevents problems when
  681. high score packages cause the removal of lower score packages that
  682. would cause the removal of even lower score packages. */
  683. SPtrArray<pkgCache::Package *> PList = new pkgCache::Package *[Size];
  684. pkgCache::Package **PEnd = PList;
  685. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  686. *PEnd++ = I;
  687. This = this;
  688. qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort);
  689. if (_config->FindB("Debug::pkgProblemResolver::ShowScores",false) == true)
  690. {
  691. clog << "Show Scores" << endl;
  692. for (pkgCache::Package **K = PList; K != PEnd; K++)
  693. if (Scores[(*K)->ID] != 0)
  694. {
  695. pkgCache::PkgIterator Pkg(Cache,*K);
  696. clog << Scores[(*K)->ID] << ' ' << Pkg << std::endl;
  697. }
  698. }
  699. if (Debug == true)
  700. clog << "Starting 2" << endl;
  701. /* Now consider all broken packages. For each broken package we either
  702. remove the package or fix it's problem. We do this once, it should
  703. not be possible for a loop to form (that is a < b < c and fixing b by
  704. changing a breaks c) */
  705. bool Change = true;
  706. for (int Counter = 0; Counter != 10 && Change == true; Counter++)
  707. {
  708. Change = false;
  709. for (pkgCache::Package **K = PList; K != PEnd; K++)
  710. {
  711. pkgCache::PkgIterator I(Cache,*K);
  712. /* We attempt to install this and see if any breaks result,
  713. this takes care of some strange cases */
  714. if (Cache[I].CandidateVer != Cache[I].InstallVer &&
  715. I->CurrentVer != 0 && Cache[I].InstallVer != 0 &&
  716. (Flags[I->ID] & PreInstalled) != 0 &&
  717. (Flags[I->ID] & Protected) == 0 &&
  718. (Flags[I->ID] & ReInstateTried) == 0)
  719. {
  720. if (Debug == true)
  721. clog << " Try to Re-Instate " << I.Name() << endl;
  722. unsigned long OldBreaks = Cache.BrokenCount();
  723. pkgCache::Version *OldVer = Cache[I].InstallVer;
  724. Flags[I->ID] &= ReInstateTried;
  725. Cache.MarkInstall(I, false, 0, false);
  726. if (Cache[I].InstBroken() == true ||
  727. OldBreaks < Cache.BrokenCount())
  728. {
  729. if (OldVer == 0)
  730. Cache.MarkDelete(I);
  731. else
  732. Cache.MarkKeep(I, false, false);
  733. }
  734. else
  735. if (Debug == true)
  736. clog << "Re-Instated " << I.Name() << " (" << OldBreaks << " vs " << Cache.BrokenCount() << ')' << endl;
  737. }
  738. if (Cache[I].InstallVer == 0 || Cache[I].InstBroken() == false)
  739. continue;
  740. if (Debug == true)
  741. clog << "Investigating " << I.Name() << endl;
  742. // Isolate the problem dependency
  743. PackageKill KillList[100];
  744. PackageKill *LEnd = KillList;
  745. bool InOr = false;
  746. pkgCache::DepIterator Start;
  747. pkgCache::DepIterator End;
  748. PackageKill *OldEnd = LEnd;
  749. enum {OrRemove,OrKeep} OrOp = OrRemove;
  750. for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList();
  751. D.end() == false || InOr == true;)
  752. {
  753. // Compute a single dependency element (glob or)
  754. if (Start == End)
  755. {
  756. // Decide what to do
  757. if (InOr == true)
  758. {
  759. if (OldEnd == LEnd && OrOp == OrRemove)
  760. {
  761. if ((Flags[I->ID] & Protected) != Protected)
  762. {
  763. if (Debug == true)
  764. clog << " Or group remove for " << I.Name() << endl;
  765. Cache.MarkDelete(I);
  766. Change = true;
  767. }
  768. }
  769. if (OldEnd == LEnd && OrOp == OrKeep)
  770. {
  771. if (Debug == true)
  772. clog << " Or group keep for " << I.Name() << endl;
  773. Cache.MarkKeep(I, false, false);
  774. Change = true;
  775. }
  776. }
  777. /* We do an extra loop (as above) to finalize the or group
  778. processing */
  779. InOr = false;
  780. OrOp = OrRemove;
  781. D.GlobOr(Start,End);
  782. if (Start.end() == true)
  783. break;
  784. // We only worry about critical deps.
  785. if (End.IsCritical() != true)
  786. continue;
  787. InOr = Start != End;
  788. OldEnd = LEnd;
  789. }
  790. else
  791. {
  792. Start++;
  793. // We only worry about critical deps.
  794. if (Start.IsCritical() != true)
  795. continue;
  796. }
  797. // Dep is ok
  798. if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  799. {
  800. InOr = false;
  801. continue;
  802. }
  803. if (Debug == true)
  804. clog << "Package " << I.Name() << " has broken " << Start.DepType() << " on " << Start.TargetPkg().Name() << endl;
  805. /* Look across the version list. If there are no possible
  806. targets then we keep the package and bail. This is necessary
  807. if a package has a dep on another package that cant be found */
  808. SPtrArray<pkgCache::Version *> VList = Start.AllTargets();
  809. if (*VList == 0 && (Flags[I->ID] & Protected) != Protected &&
  810. Start->Type != pkgCache::Dep::Conflicts &&
  811. Start->Type != pkgCache::Dep::DpkgBreaks &&
  812. Start->Type != pkgCache::Dep::Obsoletes &&
  813. Cache[I].NowBroken() == false)
  814. {
  815. if (InOr == true)
  816. {
  817. /* No keep choice because the keep being OK could be the
  818. result of another element in the OR group! */
  819. continue;
  820. }
  821. Change = true;
  822. Cache.MarkKeep(I, false, false);
  823. break;
  824. }
  825. bool Done = false;
  826. for (pkgCache::Version **V = VList; *V != 0; V++)
  827. {
  828. pkgCache::VerIterator Ver(Cache,*V);
  829. pkgCache::PkgIterator Pkg = Ver.ParentPkg();
  830. if (Debug == true)
  831. clog << " Considering " << Pkg.Name() << ' ' << (int)Scores[Pkg->ID] <<
  832. " as a solution to " << I.Name() << ' ' << (int)Scores[I->ID] << endl;
  833. /* Try to fix the package under consideration rather than
  834. fiddle with the VList package */
  835. if (Scores[I->ID] <= Scores[Pkg->ID] ||
  836. ((Cache[Start] & pkgDepCache::DepNow) == 0 &&
  837. End->Type != pkgCache::Dep::Conflicts &&
  838. End->Type != pkgCache::Dep::DpkgBreaks &&
  839. End->Type != pkgCache::Dep::Obsoletes))
  840. {
  841. // Try a little harder to fix protected packages..
  842. if ((Flags[I->ID] & Protected) == Protected)
  843. {
  844. if (DoUpgrade(Pkg) == true)
  845. {
  846. if (Scores[Pkg->ID] > Scores[I->ID])
  847. Scores[Pkg->ID] = Scores[I->ID];
  848. break;
  849. }
  850. continue;
  851. }
  852. /* See if a keep will do, unless the package is protected,
  853. then installing it will be necessary */
  854. bool Installed = Cache[I].Install();
  855. Cache.MarkKeep(I, false, false);
  856. if (Cache[I].InstBroken() == false)
  857. {
  858. // Unwind operation will be keep now
  859. if (OrOp == OrRemove)
  860. OrOp = OrKeep;
  861. // Restore
  862. if (InOr == true && Installed == true)
  863. Cache.MarkInstall(I, false, 0, false);
  864. if (Debug == true)
  865. clog << " Holding Back " << I.Name() << " rather than change " << Start.TargetPkg().Name() << endl;
  866. }
  867. else
  868. {
  869. if (BrokenFix == false || DoUpgrade(I) == false)
  870. {
  871. // Consider other options
  872. if (InOr == false)
  873. {
  874. if (Debug == true)
  875. clog << " Removing " << I.Name() << " rather than change " << Start.TargetPkg().Name() << endl;
  876. Cache.MarkDelete(I);
  877. if (Counter > 1)
  878. {
  879. if (Scores[Pkg->ID] > Scores[I->ID])
  880. Scores[I->ID] = Scores[Pkg->ID];
  881. }
  882. }
  883. }
  884. }
  885. Change = true;
  886. Done = true;
  887. break;
  888. }
  889. else
  890. {
  891. /* This is a conflicts, and the version we are looking
  892. at is not the currently selected version of the
  893. package, which means it is not necessary to
  894. remove/keep */
  895. if (Cache[Pkg].InstallVer != Ver &&
  896. (Start->Type == pkgCache::Dep::Conflicts ||
  897. Start->Type == pkgCache::Dep::Obsoletes))
  898. continue;
  899. if (Start->Type == pkgCache::Dep::DpkgBreaks)
  900. {
  901. // first, try upgradring the package, if that
  902. // does not help, the breaks goes onto the
  903. // kill list
  904. // FIXME: use DoUpgrade(Pkg) instead?
  905. if (Cache[End] & pkgDepCache::DepGCVer)
  906. {
  907. if (Debug)
  908. clog << " Upgrading " << Pkg.Name() << " due to Breaks field in " << I.Name() << endl;
  909. Cache.MarkInstall(Pkg, false, 0, false);
  910. continue;
  911. }
  912. }
  913. // Skip adding to the kill list if it is protected
  914. if ((Flags[Pkg->ID] & Protected) != 0)
  915. continue;
  916. if (Debug == true)
  917. clog << " Added " << Pkg.Name() << " to the remove list" << endl;
  918. LEnd->Pkg = Pkg;
  919. LEnd->Dep = End;
  920. LEnd++;
  921. if (Start->Type != pkgCache::Dep::Conflicts &&
  922. Start->Type != pkgCache::Dep::Obsoletes)
  923. break;
  924. }
  925. }
  926. // Hm, nothing can possibly satisify this dep. Nuke it.
  927. if (VList[0] == 0 &&
  928. Start->Type != pkgCache::Dep::Conflicts &&
  929. Start->Type != pkgCache::Dep::DpkgBreaks &&
  930. Start->Type != pkgCache::Dep::Obsoletes &&
  931. (Flags[I->ID] & Protected) != Protected)
  932. {
  933. bool Installed = Cache[I].Install();
  934. Cache.MarkKeep(I);
  935. if (Cache[I].InstBroken() == false)
  936. {
  937. // Unwind operation will be keep now
  938. if (OrOp == OrRemove)
  939. OrOp = OrKeep;
  940. // Restore
  941. if (InOr == true && Installed == true)
  942. Cache.MarkInstall(I, false, 0, false);
  943. if (Debug == true)
  944. clog << " Holding Back " << I.Name() << " because I can't find " << Start.TargetPkg().Name() << endl;
  945. }
  946. else
  947. {
  948. if (Debug == true)
  949. clog << " Removing " << I.Name() << " because I can't find " << Start.TargetPkg().Name() << endl;
  950. if (InOr == false)
  951. Cache.MarkDelete(I);
  952. }
  953. Change = true;
  954. Done = true;
  955. }
  956. // Try some more
  957. if (InOr == true)
  958. continue;
  959. if (Done == true)
  960. break;
  961. }
  962. // Apply the kill list now
  963. if (Cache[I].InstallVer != 0)
  964. {
  965. for (PackageKill *J = KillList; J != LEnd; J++)
  966. {
  967. Change = true;
  968. if ((Cache[J->Dep] & pkgDepCache::DepGNow) == 0)
  969. {
  970. if (J->Dep->Type == pkgCache::Dep::Conflicts ||
  971. J->Dep->Type == pkgCache::Dep::DpkgBreaks ||
  972. J->Dep->Type == pkgCache::Dep::Obsoletes)
  973. {
  974. if (Debug == true)
  975. clog << " Fixing " << I.Name() << " via remove of " << J->Pkg.Name() << endl;
  976. Cache.MarkDelete(J->Pkg);
  977. }
  978. }
  979. else
  980. {
  981. if (Debug == true)
  982. clog << " Fixing " << I.Name() << " via keep of " << J->Pkg.Name() << endl;
  983. Cache.MarkKeep(J->Pkg, false, false);
  984. }
  985. if (Counter > 1)
  986. {
  987. if (Scores[I->ID] > Scores[J->Pkg->ID])
  988. Scores[J->Pkg->ID] = Scores[I->ID];
  989. }
  990. }
  991. }
  992. }
  993. }
  994. if (Debug == true)
  995. clog << "Done" << endl;
  996. if (Cache.BrokenCount() != 0)
  997. {
  998. // See if this is the result of a hold
  999. pkgCache::PkgIterator I = Cache.PkgBegin();
  1000. for (;I.end() != true; I++)
  1001. {
  1002. if (Cache[I].InstBroken() == false)
  1003. continue;
  1004. if ((Flags[I->ID] & Protected) != Protected)
  1005. return _error->Error(_("Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages."));
  1006. }
  1007. return _error->Error(_("Unable to correct problems, you have held broken packages."));
  1008. }
  1009. // set the auto-flags (mvo: I'm not sure if we _really_ need this, but
  1010. // I didn't managed
  1011. pkgCache::PkgIterator I = Cache.PkgBegin();
  1012. for (;I.end() != true; I++) {
  1013. if (Cache[I].NewInstall() && !(Flags[I->ID] & PreInstalled)) {
  1014. if(_config->FindI("Debug::pkgAutoRemove",false)) {
  1015. std::clog << "Resolve installed new pkg: " << I.Name()
  1016. << " (now marking it as auto)" << std::endl;
  1017. }
  1018. Cache[I].Flags |= pkgCache::Flag::Auto;
  1019. }
  1020. }
  1021. return true;
  1022. }
  1023. /*}}}*/
  1024. // ProblemResolver::ResolveByKeep - Resolve problems using keep /*{{{*/
  1025. // ---------------------------------------------------------------------
  1026. /* This is the work horse of the soft upgrade routine. It is very gental
  1027. in that it does not install or remove any packages. It is assumed that the
  1028. system was non-broken previously. */
  1029. bool pkgProblemResolver::ResolveByKeep()
  1030. {
  1031. pkgDepCache::ActionGroup group(Cache);
  1032. unsigned long Size = Cache.Head().PackageCount;
  1033. MakeScores();
  1034. /* We have to order the packages so that the broken fixing pass
  1035. operates from highest score to lowest. This prevents problems when
  1036. high score packages cause the removal of lower score packages that
  1037. would cause the removal of even lower score packages. */
  1038. pkgCache::Package **PList = new pkgCache::Package *[Size];
  1039. pkgCache::Package **PEnd = PList;
  1040. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  1041. *PEnd++ = I;
  1042. This = this;
  1043. qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort);
  1044. if (_config->FindB("Debug::pkgProblemResolver::ShowScores",false) == true)
  1045. {
  1046. clog << "Show Scores" << endl;
  1047. for (pkgCache::Package **K = PList; K != PEnd; K++)
  1048. if (Scores[(*K)->ID] != 0)
  1049. {
  1050. pkgCache::PkgIterator Pkg(Cache,*K);
  1051. clog << Scores[(*K)->ID] << ' ' << Pkg << std::endl;
  1052. }
  1053. }
  1054. if (Debug == true)
  1055. clog << "Entering ResolveByKeep" << endl;
  1056. // Consider each broken package
  1057. pkgCache::Package **LastStop = 0;
  1058. for (pkgCache::Package **K = PList; K != PEnd; K++)
  1059. {
  1060. pkgCache::PkgIterator I(Cache,*K);
  1061. if (Cache[I].InstallVer == 0 || Cache[I].InstBroken() == false)
  1062. continue;
  1063. /* Keep the package. If this works then great, otherwise we have
  1064. to be significantly more agressive and manipulate its dependencies */
  1065. if ((Flags[I->ID] & Protected) == 0)
  1066. {
  1067. if (Debug == true)
  1068. clog << "Keeping package " << I.Name() << endl;
  1069. Cache.MarkKeep(I, false, false);
  1070. if (Cache[I].InstBroken() == false)
  1071. {
  1072. K = PList - 1;
  1073. continue;
  1074. }
  1075. }
  1076. // Isolate the problem dependencies
  1077. for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;)
  1078. {
  1079. DepIterator Start;
  1080. DepIterator End;
  1081. D.GlobOr(Start,End);
  1082. // We only worry about critical deps.
  1083. if (End.IsCritical() != true)
  1084. continue;
  1085. // Dep is ok
  1086. if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
  1087. continue;
  1088. /* Hm, the group is broken.. I suppose the best thing to do is to
  1089. is to try every combination of keep/not-keep for the set, but thats
  1090. slow, and this never happens, just be conservative and assume the
  1091. list of ors is in preference and keep till it starts to work. */
  1092. while (true)
  1093. {
  1094. if (Debug == true)
  1095. clog << "Package " << I.Name() << " has broken " << Start.DepType() << " on " << Start.TargetPkg().Name() << endl;
  1096. // Look at all the possible provides on this package
  1097. SPtrArray<pkgCache::Version *> VList = Start.AllTargets();
  1098. for (pkgCache::Version **V = VList; *V != 0; V++)
  1099. {
  1100. pkgCache::VerIterator Ver(Cache,*V);
  1101. pkgCache::PkgIterator Pkg = Ver.ParentPkg();
  1102. // It is not keepable
  1103. if (Cache[Pkg].InstallVer == 0 ||
  1104. Pkg->CurrentVer == 0)
  1105. continue;
  1106. if ((Flags[I->ID] & Protected) == 0)
  1107. {
  1108. if (Debug == true)
  1109. clog << " Keeping Package " << Pkg.Name() << " due to " << Start.DepType() << endl;
  1110. Cache.MarkKeep(Pkg, false, false);
  1111. }
  1112. if (Cache[I].InstBroken() == false)
  1113. break;
  1114. }
  1115. if (Cache[I].InstBroken() == false)
  1116. break;
  1117. if (Start == End)
  1118. break;
  1119. Start++;
  1120. }
  1121. if (Cache[I].InstBroken() == false)
  1122. break;
  1123. }
  1124. if (Cache[I].InstBroken() == true)
  1125. continue;
  1126. // Restart again.
  1127. if (K == LastStop)
  1128. return _error->Error("Internal Error, pkgProblemResolver::ResolveByKeep is looping on package %s.",I.Name());
  1129. LastStop = K;
  1130. K = PList - 1;
  1131. }
  1132. return true;
  1133. }
  1134. /*}}}*/
  1135. // ProblemResolver::InstallProtect - Install all protected packages /*{{{*/
  1136. // ---------------------------------------------------------------------
  1137. /* This is used to make sure protected packages are installed */
  1138. void pkgProblemResolver::InstallProtect()
  1139. {
  1140. pkgDepCache::ActionGroup group(Cache);
  1141. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  1142. {
  1143. if ((Flags[I->ID] & Protected) == Protected)
  1144. {
  1145. if ((Flags[I->ID] & ToRemove) == ToRemove)
  1146. Cache.MarkDelete(I);
  1147. else
  1148. {
  1149. // preserve the information whether the package was auto
  1150. // or manually installed
  1151. bool autoInst = (Cache[I].Flags & pkgCache::Flag::Auto);
  1152. Cache.MarkInstall(I, false, 0, !autoInst);
  1153. }
  1154. }
  1155. }
  1156. }
  1157. /*}}}*/
  1158. // PrioSortList - Sort a list of versions by priority /*{{{*/
  1159. // ---------------------------------------------------------------------
  1160. /* This is ment to be used in conjunction with AllTargets to get a list
  1161. of versions ordered by preference. */
  1162. static pkgCache *PrioCache;
  1163. static int PrioComp(const void *A,const void *B)
  1164. {
  1165. pkgCache::VerIterator L(*PrioCache,*(pkgCache::Version **)A);
  1166. pkgCache::VerIterator R(*PrioCache,*(pkgCache::Version **)B);
  1167. if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential &&
  1168. (R.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential)
  1169. return 1;
  1170. if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential &&
  1171. (R.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  1172. return -1;
  1173. if (L->Priority != R->Priority)
  1174. return R->Priority - L->Priority;
  1175. return strcmp(L.ParentPkg().Name(),R.ParentPkg().Name());
  1176. }
  1177. void pkgPrioSortList(pkgCache &Cache,pkgCache::Version **List)
  1178. {
  1179. unsigned long Count = 0;
  1180. PrioCache = &Cache;
  1181. for (pkgCache::Version **I = List; *I != 0; I++)
  1182. Count++;
  1183. qsort(List,Count,sizeof(*List),PrioComp);
  1184. }
  1185. /*}}}*/
  1186. // CacheFile::ListUpdate - update the cache files /*{{{*/
  1187. // ---------------------------------------------------------------------
  1188. /* This is a simple wrapper to update the cache. it will fetch stuff
  1189. * from the network (or any other sources defined in sources.list)
  1190. */
  1191. bool ListUpdate(pkgAcquireStatus &Stat,
  1192. pkgSourceList &List,
  1193. int PulseInterval)
  1194. {
  1195. pkgAcquire::RunResult res;
  1196. pkgAcquire Fetcher(&Stat);
  1197. // Populate it with the source selection
  1198. if (List.GetIndexes(&Fetcher) == false)
  1199. return false;
  1200. // Run scripts
  1201. RunScripts("APT::Update::Pre-Invoke");
  1202. // check arguments
  1203. if(PulseInterval>0)
  1204. res = Fetcher.Run(PulseInterval);
  1205. else
  1206. res = Fetcher.Run();
  1207. if (res == pkgAcquire::Failed)
  1208. return false;
  1209. bool Failed = false;
  1210. bool TransientNetworkFailure = false;
  1211. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin();
  1212. I != Fetcher.ItemsEnd(); I++)
  1213. {
  1214. if ((*I)->Status == pkgAcquire::Item::StatDone)
  1215. continue;
  1216. (*I)->Finished();
  1217. ::URI uri((*I)->DescURI());
  1218. uri.User.clear();
  1219. uri.Password.clear();
  1220. string descUri = string(uri);
  1221. _error->Warning(_("Failed to fetch %s %s\n"), descUri.c_str(),
  1222. (*I)->ErrorText.c_str());
  1223. if ((*I)->Status == pkgAcquire::Item::StatTransientNetworkError)
  1224. {
  1225. TransientNetworkFailure = true;
  1226. continue;
  1227. }
  1228. Failed = true;
  1229. }
  1230. // Clean out any old list files
  1231. // Keep "APT::Get::List-Cleanup" name for compatibility, but
  1232. // this is really a global option for the APT library now
  1233. if (!TransientNetworkFailure && !Failed &&
  1234. (_config->FindB("APT::Get::List-Cleanup",true) == true &&
  1235. _config->FindB("APT::List-Cleanup",true) == true))
  1236. {
  1237. if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
  1238. Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
  1239. // something went wrong with the clean
  1240. return false;
  1241. }
  1242. if (TransientNetworkFailure == true)
  1243. _error->Warning(_("Some index files failed to download, they have been ignored, or old ones used instead."));
  1244. else if (Failed == true)
  1245. return _error->Error(_("Some index files failed to download, they have been ignored, or old ones used instead."));
  1246. // Run the success scripts if all was fine
  1247. if(!TransientNetworkFailure && !Failed)
  1248. RunScripts("APT::Update::Post-Invoke-Success");
  1249. // Run the other scripts
  1250. RunScripts("APT::Update::Post-Invoke");
  1251. return true;
  1252. }
  1253. /*}}}*/