private-cachefile.cc 3.4 KB

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