algorithms.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 pkgSimulatePrivate;
  40. class pkgSimulate : public pkgPackageManager /*{{{*/
  41. {
  42. pkgSimulatePrivate * const d;
  43. protected:
  44. class Policy : public pkgDepCache::Policy
  45. {
  46. pkgDepCache *Cache;
  47. public:
  48. virtual VerIterator GetCandidateVer(PkgIterator const &Pkg) APT_OVERRIDE
  49. {
  50. return (*Cache)[Pkg].CandidateVerIter(*Cache);
  51. }
  52. explicit Policy(pkgDepCache *Cache) : Cache(Cache) {};
  53. };
  54. unsigned char *Flags;
  55. Policy iPolicy;
  56. pkgDepCache Sim;
  57. pkgDepCache::ActionGroup group;
  58. // The Actual installation implementation
  59. virtual bool Install(PkgIterator Pkg,std::string File) APT_OVERRIDE;
  60. virtual bool Configure(PkgIterator Pkg) APT_OVERRIDE;
  61. virtual bool Remove(PkgIterator Pkg,bool Purge) APT_OVERRIDE;
  62. // FIXME: trick to avoid ABI break for virtual reimplementation; fix on next ABI break
  63. public:
  64. APT_HIDDEN bool Go2(APT::Progress::PackageManager * progress);
  65. private:
  66. APT_HIDDEN void ShortBreaks();
  67. APT_HIDDEN void Describe(PkgIterator iPkg,std::ostream &out,bool Current,bool Candidate);
  68. APT_HIDDEN bool RealInstall(PkgIterator Pkg,std::string File);
  69. APT_HIDDEN bool RealConfigure(PkgIterator Pkg);
  70. APT_HIDDEN bool RealRemove(PkgIterator Pkg,bool Purge);
  71. public:
  72. explicit pkgSimulate(pkgDepCache *Cache);
  73. virtual ~pkgSimulate();
  74. };
  75. /*}}}*/
  76. class pkgProblemResolver /*{{{*/
  77. {
  78. private:
  79. /** \brief dpointer placeholder (for later in case we need it) */
  80. void * const d;
  81. pkgDepCache &Cache;
  82. typedef pkgCache::PkgIterator PkgIterator;
  83. typedef pkgCache::VerIterator VerIterator;
  84. typedef pkgCache::DepIterator DepIterator;
  85. typedef pkgCache::PrvIterator PrvIterator;
  86. typedef pkgCache::Version Version;
  87. typedef pkgCache::Package Package;
  88. enum Flags {Protected = (1 << 0), PreInstalled = (1 << 1),
  89. Upgradable = (1 << 2), ReInstateTried = (1 << 3),
  90. ToRemove = (1 << 4)};
  91. int *Scores;
  92. unsigned char *Flags;
  93. bool Debug;
  94. // Sort stuff
  95. APT_HIDDEN int ScoreSort(Package const *A, Package const *B) APT_PURE;
  96. struct PackageKill
  97. {
  98. PkgIterator Pkg;
  99. DepIterator Dep;
  100. };
  101. APT_HIDDEN void MakeScores();
  102. APT_HIDDEN bool DoUpgrade(pkgCache::PkgIterator Pkg);
  103. protected:
  104. bool InstOrNewPolicyBroken(pkgCache::PkgIterator Pkg);
  105. public:
  106. inline void Protect(pkgCache::PkgIterator Pkg) {Flags[Pkg->ID] |= Protected; Cache.MarkProtected(Pkg);};
  107. inline void Remove(pkgCache::PkgIterator Pkg) {Flags[Pkg->ID] |= ToRemove;};
  108. inline void Clear(pkgCache::PkgIterator Pkg) {Flags[Pkg->ID] &= ~(Protected | ToRemove);};
  109. // Try to intelligently resolve problems by installing and removing packages
  110. bool Resolve(bool BrokenFix = false, OpProgress * const Progress = NULL);
  111. APT_HIDDEN bool ResolveInternal(bool const BrokenFix = false);
  112. // Try to resolve problems only by using keep
  113. bool ResolveByKeep(OpProgress * const Progress = NULL);
  114. APT_HIDDEN bool ResolveByKeepInternal();
  115. APT_DEPRECATED_MSG("NOOP as MarkInstall enforces not overriding FromUser markings") void InstallProtect();
  116. explicit pkgProblemResolver(pkgDepCache *Cache);
  117. virtual ~pkgProblemResolver();
  118. };
  119. /*}}}*/
  120. bool pkgApplyStatus(pkgDepCache &Cache);
  121. bool pkgFixBroken(pkgDepCache &Cache);
  122. void pkgPrioSortList(pkgCache &Cache,pkgCache::Version **List);
  123. #endif