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.
 
 
 
 
 
 

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