depcache.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: depcache.h,v 1.14 2001/02/20 07:03:17 jgg Exp $
  4. /* ######################################################################
  5. DepCache - Dependency Extension data for the cache
  6. This class stores the cache data and a set of extension structures for
  7. monitoring the current state of all the packages. It also generates and
  8. caches the 'install' state of many things. This refers to the state of the
  9. package after an install has been run.
  10. The StateCache::State field can be -1,0,1,2 which is <,=,>,no current.
  11. StateCache::Mode is which of the 3 fields is active.
  12. This structure is important to support the readonly status of the cache
  13. file. When the data is saved the cache will be refereshed from our
  14. internal rep and written to disk. Then the actual persistant data
  15. files will be put on the disk.
  16. Each dependency is compared against 3 target versions to produce to
  17. 3 dependency results.
  18. Now - Compared using the Currently install version
  19. Install - Compared using the install version (final state)
  20. CVer - (Candidate Verion) Compared using the Candidate Version
  21. The candidate and now results are used to decide wheather a package
  22. should be automatically installed or if it should be left alone.
  23. Remember, the Candidate Version is selected based on the distribution
  24. settings for the Package. The Install Version is selected based on the
  25. state (Delete, Keep, Install) field and can be either the Current Version
  26. or the Candidate version.
  27. The Candidate version is what is shown the 'Install Version' field.
  28. ##################################################################### */
  29. /*}}}*/
  30. #ifndef PKGLIB_DEPCACHE_H
  31. #define PKGLIB_DEPCACHE_H
  32. #ifdef __GNUG__
  33. #pragma interface "apt-pkg/depcache.h"
  34. #endif
  35. #include <apt-pkg/pkgcache.h>
  36. #include <apt-pkg/progress.h>
  37. class pkgDepCache : protected pkgCache::Namespace
  38. {
  39. public:
  40. // These flags are used in DepState
  41. enum DepFlags {DepNow = (1 << 0),DepInstall = (1 << 1),DepCVer = (1 << 2),
  42. DepGNow = (1 << 3),DepGInstall = (1 << 4),DepGCVer = (1 << 5)};
  43. // These flags are used in StateCache::DepState
  44. enum DepStateFlags {DepNowPolicy = (1 << 0), DepNowMin = (1 << 1),
  45. DepInstPolicy = (1 << 2), DepInstMin = (1 << 3),
  46. DepCandPolicy = (1 << 4), DepCandMin = (1 << 5)};
  47. // These flags are used in StateCache::iFlags
  48. enum InternalFlags {AutoKept = (1 << 0), Purge = (1 << 1), ReInstall = (1 << 2)};
  49. enum VersionTypes {NowVersion, InstallVersion, CandidateVersion};
  50. enum ModeList {ModeDelete = 0, ModeKeep = 1, ModeInstall = 2};
  51. struct StateCache
  52. {
  53. // Epoch stripped text versions of the two version fields
  54. const char *CandVersion;
  55. const char *CurVersion;
  56. // Pointer to the candidate install version.
  57. Version *CandidateVer;
  58. // Pointer to the install version.
  59. Version *InstallVer;
  60. // Copy of Package::Flags
  61. unsigned short Flags;
  62. unsigned short iFlags; // Internal flags
  63. // Various tree indicators
  64. signed char Status; // -1,0,1,2
  65. unsigned char Mode; // ModeList
  66. unsigned char DepState; // DepState Flags
  67. // Update of candidate version
  68. const char *StripEpoch(const char *Ver);
  69. void Update(PkgIterator Pkg,pkgCache &Cache);
  70. // Various test members for the current status of the package
  71. inline bool NewInstall() const {return Status == 2 && Mode == ModeInstall;};
  72. inline bool Delete() const {return Mode == ModeDelete;};
  73. inline bool Keep() const {return Mode == ModeKeep;};
  74. inline bool Upgrade() const {return Status > 0 && Mode == ModeInstall;};
  75. inline bool Upgradable() const {return Status >= 1;};
  76. inline bool Downgrade() const {return Status < 0 && Mode == ModeInstall;};
  77. inline bool Held() const {return Status != 0 && Keep();};
  78. inline bool NowBroken() const {return (DepState & DepNowMin) != DepNowMin;};
  79. inline bool InstBroken() const {return (DepState & DepInstMin) != DepInstMin;};
  80. inline bool Install() const {return Mode == ModeInstall;};
  81. inline VerIterator InstVerIter(pkgCache &Cache)
  82. {return VerIterator(Cache,InstallVer);};
  83. inline VerIterator CandidateVerIter(pkgCache &Cache)
  84. {return VerIterator(Cache,CandidateVer);};
  85. };
  86. // Helper functions
  87. void BuildGroupOrs(VerIterator const &V);
  88. void UpdateVerState(PkgIterator Pkg);
  89. // User Policy control
  90. class Policy
  91. {
  92. public:
  93. virtual VerIterator GetCandidateVer(PkgIterator Pkg);
  94. virtual bool IsImportantDep(DepIterator Dep);
  95. virtual ~Policy() {};
  96. };
  97. protected:
  98. // State information
  99. pkgCache *Cache;
  100. StateCache *PkgState;
  101. unsigned char *DepState;
  102. double iUsrSize;
  103. double iDownloadSize;
  104. unsigned long iInstCount;
  105. unsigned long iDelCount;
  106. unsigned long iKeepCount;
  107. unsigned long iBrokenCount;
  108. unsigned long iBadCount;
  109. Policy *delLocalPolicy; // For memory clean up..
  110. Policy *LocalPolicy;
  111. // Check for a matching provides
  112. bool CheckDep(DepIterator Dep,int Type,PkgIterator &Res);
  113. inline bool CheckDep(DepIterator Dep,int Type)
  114. {
  115. PkgIterator Res(*this,0);
  116. return CheckDep(Dep,Type,Res);
  117. }
  118. // Computes state information for deps and versions (w/o storing)
  119. unsigned char DependencyState(DepIterator &D);
  120. unsigned char VersionState(DepIterator D,unsigned char Check,
  121. unsigned char SetMin,
  122. unsigned char SetPolicy);
  123. // Recalculates various portions of the cache, call after changing something
  124. void Update(DepIterator Dep); // Mostly internal
  125. void Update(PkgIterator const &P);
  126. // Count manipulators
  127. void AddSizes(const PkgIterator &Pkg,signed long Mult = 1);
  128. inline void RemoveSizes(const PkgIterator &Pkg) {AddSizes(Pkg,-1);};
  129. void AddStates(const PkgIterator &Pkg,int Add = 1);
  130. inline void RemoveStates(const PkgIterator &Pkg) {AddStates(Pkg,-1);};
  131. public:
  132. // Legacy.. We look like a pkgCache
  133. inline operator pkgCache &() {return *Cache;};
  134. inline Header &Head() {return *Cache->HeaderP;};
  135. inline PkgIterator PkgBegin() {return Cache->PkgBegin();};
  136. inline PkgIterator FindPkg(string const &Name) {return Cache->FindPkg(Name);};
  137. inline pkgCache &GetCache() {return *Cache;};
  138. inline pkgVersioningSystem &VS() {return *Cache->VS;};
  139. // Policy implementation
  140. inline VerIterator GetCandidateVer(PkgIterator Pkg) {return LocalPolicy->GetCandidateVer(Pkg);};
  141. inline bool IsImportantDep(DepIterator Dep) {return LocalPolicy->IsImportantDep(Dep);};
  142. inline Policy &GetPolicy() {return *LocalPolicy;};
  143. // Accessors
  144. inline StateCache &operator [](PkgIterator const &I) {return PkgState[I->ID];};
  145. inline unsigned char &operator [](DepIterator const &I) {return DepState[I->ID];};
  146. // Manipulators
  147. void MarkKeep(PkgIterator const &Pkg,bool Soft = false);
  148. void MarkDelete(PkgIterator const &Pkg,bool Purge = false);
  149. void MarkInstall(PkgIterator const &Pkg,bool AutoInst = true,
  150. unsigned long Depth = 0);
  151. void SetReInstall(PkgIterator const &Pkg,bool To);
  152. void SetCandidateVersion(VerIterator TargetVer);
  153. // This is for debuging
  154. void Update(OpProgress *Prog = 0);
  155. // Size queries
  156. inline double UsrSize() {return iUsrSize;};
  157. inline double DebSize() {return iDownloadSize;};
  158. inline unsigned long DelCount() {return iDelCount;};
  159. inline unsigned long KeepCount() {return iKeepCount;};
  160. inline unsigned long InstCount() {return iInstCount;};
  161. inline unsigned long BrokenCount() {return iBrokenCount;};
  162. inline unsigned long BadCount() {return iBadCount;};
  163. bool Init(OpProgress *Prog);
  164. pkgDepCache(pkgCache *Cache,Policy *Plcy = 0);
  165. virtual ~pkgDepCache();
  166. };
  167. #endif