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.
 
 
 
 
 
 

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