algorithms.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Algorithms - A set of misc algorithms
  5. This simulate class displays what the ordering code has done and
  6. analyses it with a fresh new dependency cache. In this way we can
  7. see all of the effects of an upgrade run.
  8. pkgDistUpgrade computes an upgrade that causes as many packages as
  9. possible to move to the newest version.
  10. pkgApplyStatus sets the target state based on the content of the status
  11. field in the status file. It is important to get proper crash recovery.
  12. pkgFixBroken corrects a broken system so that it is in a sane state.
  13. pkgAllUpgrade attempts to upgade as many packages as possible but
  14. without installing new packages.
  15. The problem resolver class contains a number of complex algorithms
  16. to try to best-guess an upgrade state. It solves the problem of
  17. maximizing the number of install state packages while having no broken
  18. packages.
  19. ##################################################################### */
  20. /*}}}*/
  21. #ifndef PKGLIB_ALGORITHMS_H
  22. #define PKGLIB_ALGORITHMS_H
  23. #include <apt-pkg/packagemanager.h>
  24. #include <apt-pkg/depcache.h>
  25. #include <apt-pkg/pkgcache.h>
  26. #include <apt-pkg/cacheiterators.h>
  27. #include <iostream>
  28. #include <string>
  29. #include <apt-pkg/macros.h>
  30. #ifndef APT_8_CLEANER_HEADERS
  31. #include <apt-pkg/acquire.h>
  32. using std::ostream;
  33. #endif
  34. #ifndef APT_9_CLEANER_HEADERS
  35. // include pkg{DistUpgrade,AllUpgrade,MiniizeUpgrade} here for compatibility
  36. #include <apt-pkg/upgrade.h>
  37. #include <apt-pkg/update.h>
  38. #endif
  39. class pkgSimulate : public pkgPackageManager /*{{{*/
  40. {
  41. void * const d;
  42. protected:
  43. class Policy : public pkgDepCache::Policy
  44. {
  45. pkgDepCache *Cache;
  46. public:
  47. virtual VerIterator GetCandidateVer(PkgIterator const &Pkg)
  48. {
  49. return (*Cache)[Pkg].CandidateVerIter(*Cache);
  50. }
  51. explicit Policy(pkgDepCache *Cache) : Cache(Cache) {};
  52. };
  53. unsigned char *Flags;
  54. Policy iPolicy;
  55. pkgDepCache Sim;
  56. pkgDepCache::ActionGroup group;
  57. // The Actuall installation implementation
  58. virtual bool Install(PkgIterator Pkg,std::string File);
  59. virtual bool Configure(PkgIterator Pkg);
  60. virtual bool Remove(PkgIterator Pkg,bool Purge);
  61. private:
  62. APT_HIDDEN void ShortBreaks();
  63. APT_HIDDEN void Describe(PkgIterator iPkg,std::ostream &out,bool Current,bool Candidate);
  64. public:
  65. explicit pkgSimulate(pkgDepCache *Cache);
  66. virtual ~pkgSimulate();
  67. };
  68. /*}}}*/
  69. class pkgProblemResolver /*{{{*/
  70. {
  71. private:
  72. /** \brief dpointer placeholder (for later in case we need it) */
  73. void * const d;
  74. pkgDepCache &Cache;
  75. typedef pkgCache::PkgIterator PkgIterator;
  76. typedef pkgCache::VerIterator VerIterator;
  77. typedef pkgCache::DepIterator DepIterator;
  78. typedef pkgCache::PrvIterator PrvIterator;
  79. typedef pkgCache::Version Version;
  80. typedef pkgCache::Package Package;
  81. enum Flags {Protected = (1 << 0), PreInstalled = (1 << 1),
  82. Upgradable = (1 << 2), ReInstateTried = (1 << 3),
  83. ToRemove = (1 << 4)};
  84. int *Scores;
  85. unsigned char *Flags;
  86. bool Debug;
  87. // Sort stuff
  88. static pkgProblemResolver *This;
  89. APT_HIDDEN static int ScoreSort(const void *a,const void *b) APT_PURE;
  90. struct PackageKill
  91. {
  92. PkgIterator Pkg;
  93. DepIterator Dep;
  94. };
  95. APT_HIDDEN void MakeScores();
  96. APT_HIDDEN bool DoUpgrade(pkgCache::PkgIterator Pkg);
  97. APT_HIDDEN bool ResolveInternal(bool const BrokenFix = false);
  98. APT_HIDDEN bool ResolveByKeepInternal();
  99. protected:
  100. bool InstOrNewPolicyBroken(pkgCache::PkgIterator Pkg);
  101. public:
  102. inline void Protect(pkgCache::PkgIterator Pkg) {Flags[Pkg->ID] |= Protected; Cache.MarkProtected(Pkg);};
  103. inline void Remove(pkgCache::PkgIterator Pkg) {Flags[Pkg->ID] |= ToRemove;};
  104. inline void Clear(pkgCache::PkgIterator Pkg) {Flags[Pkg->ID] &= ~(Protected | ToRemove);};
  105. // Try to intelligently resolve problems by installing and removing packages
  106. #if APT_PKG_ABI >= 413
  107. bool Resolve(bool BrokenFix = false, OpProgress * const Progress = NULL);
  108. #else
  109. bool Resolve(bool BrokenFix = false);
  110. bool Resolve(bool BrokenFix, OpProgress * const Progress);
  111. #endif
  112. // Try to resolve problems only by using keep
  113. #if APT_PKG_ABI >= 413
  114. bool ResolveByKeep(OpProgress * const Progress = NULL);
  115. #else
  116. bool ResolveByKeep();
  117. bool ResolveByKeep(OpProgress * const Progress);
  118. #endif
  119. APT_DEPRECATED void InstallProtect();
  120. explicit pkgProblemResolver(pkgDepCache *Cache);
  121. virtual ~pkgProblemResolver();
  122. };
  123. /*}}}*/
  124. bool pkgApplyStatus(pkgDepCache &Cache);
  125. bool pkgFixBroken(pkgDepCache &Cache);
  126. void pkgPrioSortList(pkgCache &Cache,pkgCache::Version **List);
  127. #endif