private-cachefile.cc 3.2 KB

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