algorithms.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: algorithms.h,v 1.10 2001/05/22 04:17:41 jgg Exp $
  4. /* ######################################################################
  5. Algorithms - A set of misc algorithms
  6. This simulate class displays what the ordering code has done and
  7. analyses it with a fresh new dependency cache. In this way we can
  8. see all of the effects of an upgrade run.
  9. pkgDistUpgrade computes an upgrade that causes as many packages as
  10. possible to move to the newest version.
  11. pkgApplyStatus sets the target state based on the content of the status
  12. field in the status file. It is important to get proper crash recovery.
  13. pkgFixBroken corrects a broken system so that it is in a sane state.
  14. pkgAllUpgrade attempts to upgade as many packages as possible but
  15. without installing new packages.
  16. The problem resolver class contains a number of complex algorithms
  17. to try to best-guess an upgrade state. It solves the problem of
  18. maximizing the number of install state packages while having no broken
  19. packages.
  20. ##################################################################### */
  21. /*}}}*/
  22. #ifndef PKGLIB_ALGORITHMS_H
  23. #define PKGLIB_ALGORITHMS_H
  24. #include <apt-pkg/packagemanager.h>
  25. #include <apt-pkg/depcache.h>
  26. #include <apt-pkg/pkgcache.h>
  27. #include <apt-pkg/cacheiterators.h>
  28. #include <iostream>
  29. #include <string>
  30. #include <apt-pkg/macros.h>
  31. #ifndef APT_8_CLEANER_HEADERS
  32. #include <apt-pkg/acquire.h>
  33. using std::ostream;
  34. #endif
  35. #ifndef APT_9_CLEANER_HEADERS
  36. // include pkg{DistUpgrade,AllUpgrade,MiniizeUpgrade} here for compatibility
  37. #include <apt-pkg/upgrade.h>
  38. #include <apt-pkg/update.h>
  39. #endif
  40. class pkgSimulate : public pkgPackageManager /*{{{*/
  41. {
  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. 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. void ShortBreaks();
  63. void Describe(PkgIterator iPkg,std::ostream &out,bool Current,bool Candidate);
  64. public:
  65. pkgSimulate(pkgDepCache *Cache);
  66. ~pkgSimulate();
  67. };
  68. /*}}}*/
  69. class pkgProblemResolver /*{{{*/
  70. {
  71. private:
  72. /** \brief dpointer placeholder (for later in case we need it) */
  73. void *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. static int ScoreSort(const void *a,const void *b) APT_PURE;
  90. struct PackageKill
  91. {
  92. PkgIterator Pkg;
  93. DepIterator Dep;
  94. };
  95. void MakeScores();
  96. bool DoUpgrade(pkgCache::PkgIterator Pkg);
  97. bool ResolveInternal(bool const BrokenFix = false);
  98. 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. bool Resolve(bool BrokenFix = false);
  107. // Try to resolve problems only by using keep
  108. bool ResolveByKeep();
  109. APT_DEPRECATED void InstallProtect();
  110. pkgProblemResolver(pkgDepCache *Cache);
  111. ~pkgProblemResolver();
  112. };
  113. /*}}}*/
  114. bool pkgApplyStatus(pkgDepCache &Cache);
  115. bool pkgFixBroken(pkgDepCache &Cache);
  116. void pkgPrioSortList(pkgCache &Cache,pkgCache::Version **List);
  117. #endif