private-cachefile.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/cacheiterators.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. // CacheFile::NameComp - QSort compare by name /*{{{*/
  19. // ---------------------------------------------------------------------
  20. /* */
  21. pkgCache *CacheFile::SortCache = 0;
  22. int CacheFile::NameComp(const void *a,const void *b)
  23. {
  24. if (*(pkgCache::Package **)a == 0 || *(pkgCache::Package **)b == 0)
  25. return *(pkgCache::Package **)a - *(pkgCache::Package **)b;
  26. const pkgCache::Package &A = **(pkgCache::Package **)a;
  27. const pkgCache::Package &B = **(pkgCache::Package **)b;
  28. const pkgCache::Group * const GA = SortCache->GrpP + A.Group;
  29. const pkgCache::Group * const GB = SortCache->GrpP + B.Group;
  30. return strcmp(SortCache->StrP + GA->Name,SortCache->StrP + GB->Name);
  31. }
  32. /*}}}*/
  33. // CacheFile::Sort - Sort by name /*{{{*/
  34. // ---------------------------------------------------------------------
  35. /* */
  36. void CacheFile::Sort()
  37. {
  38. delete [] List;
  39. List = new pkgCache::Package *[Cache->Head().PackageCount];
  40. memset(List,0,sizeof(*List)*Cache->Head().PackageCount);
  41. pkgCache::PkgIterator I = Cache->PkgBegin();
  42. for (;I.end() != true; ++I)
  43. List[I->ID] = I;
  44. SortCache = *this;
  45. qsort(List,Cache->Head().PackageCount,sizeof(*List),NameComp);
  46. }
  47. /*}}}*/
  48. // CacheFile::CheckDeps - Open the cache file /*{{{*/
  49. // ---------------------------------------------------------------------
  50. /* This routine generates the caches and then opens the dependency cache
  51. and verifies that the system is OK. */
  52. bool CacheFile::CheckDeps(bool AllowBroken)
  53. {
  54. bool FixBroken = _config->FindB("APT::Get::Fix-Broken",false);
  55. if (_error->PendingError() == true)
  56. return false;
  57. // Check that the system is OK
  58. if (DCache->DelCount() != 0 || DCache->InstCount() != 0)
  59. return _error->Error("Internal error, non-zero counts");
  60. // Apply corrections for half-installed packages
  61. if (pkgApplyStatus(*DCache) == false)
  62. return false;
  63. if (_config->FindB("APT::Get::Fix-Policy-Broken",false) == true)
  64. {
  65. FixBroken = true;
  66. if ((DCache->PolicyBrokenCount() > 0))
  67. {
  68. // upgrade all policy-broken packages with ForceImportantDeps=True
  69. for (pkgCache::PkgIterator I = Cache->PkgBegin(); !I.end(); ++I)
  70. if ((*DCache)[I].NowPolicyBroken() == true)
  71. DCache->MarkInstall(I,true,0, false, true);
  72. }
  73. }
  74. // Nothing is broken
  75. if (DCache->BrokenCount() == 0 || AllowBroken == true)
  76. return true;
  77. // Attempt to fix broken things
  78. if (FixBroken == true)
  79. {
  80. c1out << _("Correcting dependencies...") << flush;
  81. if (pkgFixBroken(*DCache) == false || DCache->BrokenCount() != 0)
  82. {
  83. c1out << _(" failed.") << endl;
  84. ShowBroken(c1out,*this,true);
  85. return _error->Error(_("Unable to correct dependencies"));
  86. }
  87. if (pkgMinimizeUpgrade(*DCache) == false)
  88. return _error->Error(_("Unable to minimize the upgrade set"));
  89. c1out << _(" Done") << endl;
  90. }
  91. else
  92. {
  93. c1out << _("You might want to run 'apt-get -f install' to correct these.") << endl;
  94. ShowBroken(c1out,*this,true);
  95. return _error->Error(_("Unmet dependencies. Try using -f."));
  96. }
  97. return true;
  98. }
  99. /*}}}*/