private-cachefile.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Include files /*{{{*/
  2. #include<config.h>
  3. #include <apt-pkg/algorithms.h>
  4. #include <apt-pkg/upgrade.h>
  5. #include <apt-pkg/error.h>
  6. #include <apt-pkg/configuration.h>
  7. #include <apt-pkg/depcache.h>
  8. #include <apt-pkg/pkgcache.h>
  9. #include <apt-pkg/cacheset.h>
  10. #include <apt-private/private-output.h>
  11. #include <apt-private/private-cachefile.h>
  12. #include <string.h>
  13. #include <ostream>
  14. #include <cstdlib>
  15. #include <apti18n.h>
  16. /*}}}*/
  17. using namespace std;
  18. static bool SortPackagesByName(pkgCache * const Owner,
  19. map_pointer_t const A, map_pointer_t const B)
  20. {
  21. if (A == 0)
  22. return false;
  23. if (B == 0 || A == B)
  24. return true;
  25. pkgCache::Group const * const GA = Owner->GrpP + A;
  26. pkgCache::Group const * const GB = Owner->GrpP + B;
  27. return strcmp(Owner->StrP + GA->Name, Owner->StrP + GB->Name) <= 0;
  28. }
  29. SortedPackageUniverse::SortedPackageUniverse(CacheFile &Cache) :
  30. PackageUniverse{Cache}, List(Cache.UniverseList)
  31. {
  32. }
  33. void SortedPackageUniverse::LazyInit() const
  34. {
  35. if (List.empty() == false)
  36. return;
  37. pkgCache * const Owner = data();
  38. // In Multi-Arch systems Grps are easier to sort than Pkgs
  39. std::vector<map_pointer_t> GrpList;
  40. List.reserve(Owner->Head().GroupCount);
  41. for (pkgCache::GrpIterator I{Owner->GrpBegin()}; I.end() != true; ++I)
  42. GrpList.emplace_back(I - Owner->GrpP);
  43. std::stable_sort(GrpList.begin(), GrpList.end(), std::bind( &SortPackagesByName, Owner, std::placeholders::_1, std::placeholders::_2 ));
  44. List.reserve(Owner->Head().PackageCount);
  45. for (auto G : GrpList)
  46. {
  47. pkgCache::GrpIterator const Grp(*Owner, Owner->GrpP + G);
  48. for (pkgCache::PkgIterator P = Grp.PackageList(); P.end() != true; P = Grp.NextPkg(P))
  49. List.emplace_back(P - Owner->PkgP);
  50. }
  51. }
  52. // CacheFile::CheckDeps - Open the cache file /*{{{*/
  53. // ---------------------------------------------------------------------
  54. /* This routine generates the caches and then opens the dependency cache
  55. and verifies that the system is OK. */
  56. bool CacheFile::CheckDeps(bool AllowBroken)
  57. {
  58. bool FixBroken = _config->FindB("APT::Get::Fix-Broken",false);
  59. if (_error->PendingError() == true)
  60. return false;
  61. // Check that the system is OK
  62. if (DCache->DelCount() != 0 || DCache->InstCount() != 0)
  63. return _error->Error("Internal error, non-zero counts");
  64. // Apply corrections for half-installed packages
  65. if (pkgApplyStatus(*DCache) == false)
  66. return false;
  67. if (_config->FindB("APT::Get::Fix-Policy-Broken",false) == true)
  68. {
  69. FixBroken = true;
  70. if ((DCache->PolicyBrokenCount() > 0))
  71. {
  72. // upgrade all policy-broken packages with ForceImportantDeps=True
  73. for (pkgCache::PkgIterator I = Cache->PkgBegin(); !I.end(); ++I)
  74. if ((*DCache)[I].NowPolicyBroken() == true)
  75. DCache->MarkInstall(I,true,0, false, true);
  76. }
  77. }
  78. // Nothing is broken
  79. if (DCache->BrokenCount() == 0 || AllowBroken == true)
  80. return true;
  81. // Attempt to fix broken things
  82. if (FixBroken == true)
  83. {
  84. c1out << _("Correcting dependencies...") << flush;
  85. if (pkgFixBroken(*DCache) == false || DCache->BrokenCount() != 0)
  86. {
  87. c1out << _(" failed.") << endl;
  88. ShowBroken(c1out,*this,true);
  89. return _error->Error(_("Unable to correct dependencies"));
  90. }
  91. if (pkgMinimizeUpgrade(*DCache) == false)
  92. return _error->Error(_("Unable to minimize the upgrade set"));
  93. c1out << _(" Done") << endl;
  94. }
  95. else
  96. {
  97. c1out << _("You might want to run 'apt-get -f install' to correct these.") << endl;
  98. ShowBroken(c1out,*this,true);
  99. return _error->Error(_("Unmet dependencies. Try using -f."));
  100. }
  101. return true;
  102. }
  103. /*}}}*/