algorithms.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <iostream>
  27. #include <apt-pkg/macros.h>
  28. #ifndef APT_8_CLEANER_HEADERS
  29. #include <apt-pkg/acquire.h>
  30. using std::ostream;
  31. #endif
  32. #ifndef APT_9_CLEANER_HEADERS
  33. // include pkg{DistUpgrade,AllUpgrade,MiniizeUpgrade} here for compatibility
  34. #include <apt-pkg/upgrade.h>
  35. #include <apt-pkg/update.h>
  36. #endif
  37. class pkgSimulate : public pkgPackageManager /*{{{*/
  38. {
  39. protected:
  40. class Policy : public pkgDepCache::Policy
  41. {
  42. pkgDepCache *Cache;
  43. public:
  44. virtual VerIterator GetCandidateVer(PkgIterator const &Pkg)
  45. {
  46. return (*Cache)[Pkg].CandidateVerIter(*Cache);
  47. }
  48. Policy(pkgDepCache *Cache) : Cache(Cache) {};
  49. };
  50. unsigned char *Flags;
  51. Policy iPolicy;
  52. pkgDepCache Sim;
  53. pkgDepCache::ActionGroup group;
  54. // The Actuall installation implementation
  55. virtual bool Install(PkgIterator Pkg,std::string File);
  56. virtual bool Configure(PkgIterator Pkg);
  57. virtual bool Remove(PkgIterator Pkg,bool Purge);
  58. private:
  59. void ShortBreaks();
  60. void Describe(PkgIterator iPkg,std::ostream &out,bool Current,bool Candidate);
  61. public:
  62. pkgSimulate(pkgDepCache *Cache);
  63. ~pkgSimulate();
  64. };
  65. /*}}}*/
  66. class pkgProblemResolver /*{{{*/
  67. {
  68. private:
  69. /** \brief dpointer placeholder (for later in case we need it) */
  70. void *d;
  71. pkgDepCache &Cache;
  72. typedef pkgCache::PkgIterator PkgIterator;
  73. typedef pkgCache::VerIterator VerIterator;
  74. typedef pkgCache::DepIterator DepIterator;
  75. typedef pkgCache::PrvIterator PrvIterator;
  76. typedef pkgCache::Version Version;
  77. typedef pkgCache::Package Package;
  78. enum Flags {Protected = (1 << 0), PreInstalled = (1 << 1),
  79. Upgradable = (1 << 2), ReInstateTried = (1 << 3),
  80. ToRemove = (1 << 4)};
  81. int *Scores;
  82. unsigned char *Flags;
  83. bool Debug;
  84. // Sort stuff
  85. static pkgProblemResolver *This;
  86. static int ScoreSort(const void *a,const void *b);
  87. struct PackageKill
  88. {
  89. PkgIterator Pkg;
  90. DepIterator Dep;
  91. };
  92. void MakeScores();
  93. bool DoUpgrade(pkgCache::PkgIterator Pkg);
  94. bool ResolveInternal(bool const BrokenFix = false);
  95. bool ResolveByKeepInternal();
  96. protected:
  97. bool InstOrNewPolicyBroken(pkgCache::PkgIterator Pkg);
  98. public:
  99. inline void Protect(pkgCache::PkgIterator Pkg) {Flags[Pkg->ID] |= Protected; Cache.MarkProtected(Pkg);};
  100. inline void Remove(pkgCache::PkgIterator Pkg) {Flags[Pkg->ID] |= ToRemove;};
  101. inline void Clear(pkgCache::PkgIterator Pkg) {Flags[Pkg->ID] &= ~(Protected | ToRemove);};
  102. // Try to intelligently resolve problems by installing and removing packages
  103. bool Resolve(bool BrokenFix = false);
  104. // Try to resolve problems only by using keep
  105. bool ResolveByKeep();
  106. __deprecated void InstallProtect();
  107. pkgProblemResolver(pkgDepCache *Cache);
  108. ~pkgProblemResolver();
  109. };
  110. /*}}}*/
  111. bool pkgApplyStatus(pkgDepCache &Cache);
  112. bool pkgFixBroken(pkgDepCache &Cache);
  113. void pkgPrioSortList(pkgCache &Cache,pkgCache::Version **List);
  114. #endif