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.
 
 
 
 
 
 

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