depcache.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // -*- mode: c++; 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. #include <regex.h>
  38. #include <vector>
  39. class pkgDepCache : protected pkgCache::Namespace
  40. {
  41. public:
  42. /** \brief An arbitrary predicate on packages. */
  43. class InRootSetFunc
  44. {
  45. public:
  46. virtual bool InRootSet(const pkgCache::PkgIterator &pkg) {return false;};
  47. virtual ~InRootSetFunc() {};
  48. };
  49. private:
  50. /** \brief Mark a single package and all its unmarked important
  51. * dependencies during mark-and-sweep.
  52. *
  53. * Recursively invokes itself to mark all dependencies of the
  54. * package.
  55. *
  56. * \param pkg The package to mark.
  57. *
  58. * \param ver The version of the package that is to be marked.
  59. *
  60. * \param follow_recommends If \b true, recommendations of the
  61. * package will be recursively marked.
  62. *
  63. * \param follow_suggests If \b true, suggestions of the package
  64. * will be recursively marked.
  65. */
  66. void MarkPackage(const pkgCache::PkgIterator &pkg,
  67. const pkgCache::VerIterator &ver,
  68. bool follow_recommends,
  69. bool follow_suggests);
  70. /** \brief Update the Marked field of all packages.
  71. *
  72. * Each package's StateCache::Marked field will be set to \b true
  73. * if and only if it can be reached from the root set. By
  74. * default, the root set consists of the set of manually installed
  75. * or essential packages, but it can be extended using the
  76. * parameter #rootFunc.
  77. *
  78. * \param rootFunc A callback that can be used to add extra
  79. * packages to the root set.
  80. *
  81. * \return \b false if an error occured.
  82. */
  83. bool MarkRequired(InRootSetFunc &rootFunc);
  84. /** \brief Set the StateCache::Garbage flag on all packages that
  85. * should be removed.
  86. *
  87. * Packages that were not marked by the last call to #MarkRequired
  88. * are tested to see whether they are actually garbage. If so,
  89. * they are marked as such.
  90. *
  91. * \return \b false if an error occured.
  92. */
  93. bool Sweep();
  94. public:
  95. // These flags are used in DepState
  96. enum DepFlags {DepNow = (1 << 0),DepInstall = (1 << 1),DepCVer = (1 << 2),
  97. DepGNow = (1 << 3),DepGInstall = (1 << 4),DepGCVer = (1 << 5)};
  98. // These flags are used in StateCache::DepState
  99. enum DepStateFlags {DepNowPolicy = (1 << 0), DepNowMin = (1 << 1),
  100. DepInstPolicy = (1 << 2), DepInstMin = (1 << 3),
  101. DepCandPolicy = (1 << 4), DepCandMin = (1 << 5)};
  102. // These flags are used in StateCache::iFlags
  103. enum InternalFlags {AutoKept = (1 << 0), Purge = (1 << 1), ReInstall = (1 << 2)};
  104. enum VersionTypes {NowVersion, InstallVersion, CandidateVersion};
  105. enum ModeList {ModeDelete = 0, ModeKeep = 1, ModeInstall = 2};
  106. /** \brief Represents an active action group.
  107. *
  108. * An action group is a group of actions that are currently being
  109. * performed. While an active group is active, certain routine
  110. * clean-up actions that would normally be performed after every
  111. * cache operation are delayed until the action group is
  112. * completed. This is necessary primarily to avoid inefficiencies
  113. * when modifying a large number of packages at once.
  114. *
  115. * This class represents an active action group. Creating an
  116. * instance will create an action group; destroying one will
  117. * destroy the corresponding action group.
  118. *
  119. * The following operations are suppressed by this class:
  120. *
  121. * - Keeping the Marked and Garbage flags up to date.
  122. *
  123. * \note This can be used in the future to easily accumulate
  124. * atomic actions for undo or to display "what apt did anyway";
  125. * e.g., change the counter of how many action groups are active
  126. * to a std::set of pointers to them and use those to store
  127. * information about what happened in a group in the group.
  128. */
  129. class ActionGroup
  130. {
  131. pkgDepCache &cache;
  132. bool released;
  133. /** Action groups are noncopyable. */
  134. ActionGroup(const ActionGroup &other);
  135. public:
  136. /** \brief Create a new ActionGroup.
  137. *
  138. * \param cache The cache that this ActionGroup should
  139. * manipulate.
  140. *
  141. * As long as this object exists, no automatic cleanup
  142. * operations will be undertaken.
  143. */
  144. ActionGroup(pkgDepCache &cache);
  145. /** \brief Clean up the action group before it is destroyed.
  146. *
  147. * If it is destroyed later, no second cleanup wil be run.
  148. */
  149. void release();
  150. /** \brief Destroy the action group.
  151. *
  152. * If this is the last action group, the automatic cache
  153. * cleanup operations will be undertaken.
  154. */
  155. ~ActionGroup();
  156. };
  157. /** \brief Returns \b true for packages matching a regular
  158. * expression in APT::NeverAutoRemove.
  159. */
  160. class DefaultRootSetFunc : public InRootSetFunc
  161. {
  162. std::vector<regex_t *> rootSetRegexp;
  163. bool constructedSuccessfully;
  164. public:
  165. DefaultRootSetFunc();
  166. ~DefaultRootSetFunc();
  167. /** \return \b true if the class initialized successfully, \b
  168. * false otherwise. Used to avoid throwing an exception, since
  169. * APT classes generally don't.
  170. */
  171. bool wasConstructedSuccessfully() const { return constructedSuccessfully; }
  172. bool InRootSet(const pkgCache::PkgIterator &pkg);
  173. };
  174. struct StateCache
  175. {
  176. // Epoch stripped text versions of the two version fields
  177. const char *CandVersion;
  178. const char *CurVersion;
  179. // Pointer to the candidate install version.
  180. Version *CandidateVer;
  181. // Pointer to the install version.
  182. Version *InstallVer;
  183. // Copy of Package::Flags
  184. unsigned short Flags;
  185. unsigned short iFlags; // Internal flags
  186. /** \brief \b true if this package can be reached from the root set. */
  187. bool Marked;
  188. /** \brief \b true if this package is unused and should be removed.
  189. *
  190. * This differs from !#Marked, because it is possible that some
  191. * unreachable packages will be protected from becoming
  192. * garbage.
  193. */
  194. bool Garbage;
  195. // Various tree indicators
  196. signed char Status; // -1,0,1,2
  197. unsigned char Mode; // ModeList
  198. unsigned char DepState; // DepState Flags
  199. // Update of candidate version
  200. const char *StripEpoch(const char *Ver);
  201. void Update(PkgIterator Pkg,pkgCache &Cache);
  202. // Various test members for the current status of the package
  203. inline bool NewInstall() const {return Status == 2 && Mode == ModeInstall;};
  204. inline bool Delete() const {return Mode == ModeDelete;};
  205. inline bool Keep() const {return Mode == ModeKeep;};
  206. inline bool Upgrade() const {return Status > 0 && Mode == ModeInstall;};
  207. inline bool Upgradable() const {return Status >= 1;};
  208. inline bool Downgrade() const {return Status < 0 && Mode == ModeInstall;};
  209. inline bool Held() const {return Status != 0 && Keep();};
  210. inline bool NowBroken() const {return (DepState & DepNowMin) != DepNowMin;};
  211. inline bool InstBroken() const {return (DepState & DepInstMin) != DepInstMin;};
  212. inline bool Install() const {return Mode == ModeInstall;};
  213. inline VerIterator InstVerIter(pkgCache &Cache)
  214. {return VerIterator(Cache,InstallVer);};
  215. inline VerIterator CandidateVerIter(pkgCache &Cache)
  216. {return VerIterator(Cache,CandidateVer);};
  217. };
  218. // Helper functions
  219. void BuildGroupOrs(VerIterator const &V);
  220. void UpdateVerState(PkgIterator Pkg);
  221. // User Policy control
  222. class Policy
  223. {
  224. public:
  225. virtual VerIterator GetCandidateVer(PkgIterator Pkg);
  226. virtual bool IsImportantDep(DepIterator Dep);
  227. virtual ~Policy() {};
  228. };
  229. private:
  230. /** The number of open "action groups"; certain post-action
  231. * operations are suppressed if this number is > 0.
  232. */
  233. int group_level;
  234. friend class ActionGroup;
  235. protected:
  236. // State information
  237. pkgCache *Cache;
  238. StateCache *PkgState;
  239. unsigned char *DepState;
  240. double iUsrSize;
  241. double iDownloadSize;
  242. unsigned long iInstCount;
  243. unsigned long iDelCount;
  244. unsigned long iKeepCount;
  245. unsigned long iBrokenCount;
  246. unsigned long iBadCount;
  247. Policy *delLocalPolicy; // For memory clean up..
  248. Policy *LocalPolicy;
  249. // Check for a matching provides
  250. bool CheckDep(DepIterator Dep,int Type,PkgIterator &Res);
  251. inline bool CheckDep(DepIterator Dep,int Type)
  252. {
  253. PkgIterator Res(*this,0);
  254. return CheckDep(Dep,Type,Res);
  255. }
  256. // Computes state information for deps and versions (w/o storing)
  257. unsigned char DependencyState(DepIterator &D);
  258. unsigned char VersionState(DepIterator D,unsigned char Check,
  259. unsigned char SetMin,
  260. unsigned char SetPolicy);
  261. // Recalculates various portions of the cache, call after changing something
  262. void Update(DepIterator Dep); // Mostly internal
  263. void Update(PkgIterator const &P);
  264. // Count manipulators
  265. void AddSizes(const PkgIterator &Pkg,signed long Mult = 1);
  266. inline void RemoveSizes(const PkgIterator &Pkg) {AddSizes(Pkg,-1);};
  267. void AddStates(const PkgIterator &Pkg,int Add = 1);
  268. inline void RemoveStates(const PkgIterator &Pkg) {AddStates(Pkg,-1);};
  269. public:
  270. // Legacy.. We look like a pkgCache
  271. inline operator pkgCache &() {return *Cache;};
  272. inline Header &Head() {return *Cache->HeaderP;};
  273. inline PkgIterator PkgBegin() {return Cache->PkgBegin();};
  274. inline PkgIterator FindPkg(string const &Name) {return Cache->FindPkg(Name);};
  275. inline pkgCache &GetCache() {return *Cache;};
  276. inline pkgVersioningSystem &VS() {return *Cache->VS;};
  277. // Policy implementation
  278. inline VerIterator GetCandidateVer(PkgIterator Pkg) {return LocalPolicy->GetCandidateVer(Pkg);};
  279. inline bool IsImportantDep(DepIterator Dep) {return LocalPolicy->IsImportantDep(Dep);};
  280. inline Policy &GetPolicy() {return *LocalPolicy;};
  281. // Accessors
  282. inline StateCache &operator [](PkgIterator const &I) {return PkgState[I->ID];};
  283. inline unsigned char &operator [](DepIterator const &I) {return DepState[I->ID];};
  284. /** \return A function identifying packages in the root set other
  285. * than manually installed packages and essential packages, or \b
  286. * NULL if an error occurs.
  287. *
  288. * \todo Is this the best place for this function? Perhaps the
  289. * settings for mark-and-sweep should be stored in a single
  290. * external class?
  291. */
  292. virtual InRootSetFunc *GetRootSetFunc();
  293. /** \return \b true if the garbage collector should follow recommendations.
  294. */
  295. virtual bool MarkFollowsRecommends();
  296. /** \return \b true if the garbage collector should follow suggestions.
  297. */
  298. virtual bool MarkFollowsSuggests();
  299. /** \brief Update the Marked and Garbage fields of all packages.
  300. *
  301. * This routine is implicitly invoked after all state manipulators
  302. * and when an ActionGroup is destroyed. It invokes #MarkRequired
  303. * and #Sweep to do its dirty work.
  304. *
  305. * \param rootFunc A predicate that returns \b true for packages
  306. * that should be added to the root set.
  307. */
  308. bool MarkAndSweep(InRootSetFunc &rootFunc)
  309. {
  310. return MarkRequired(rootFunc) && Sweep();
  311. }
  312. bool MarkAndSweep()
  313. {
  314. std::auto_ptr<InRootSetFunc> f(GetRootSetFunc());
  315. if(f.get() != NULL)
  316. return MarkAndSweep(*f.get());
  317. else
  318. return false;
  319. }
  320. /** \name State Manipulators
  321. */
  322. // @{
  323. void MarkKeep(PkgIterator const &Pkg, bool Soft = false,
  324. bool FromUser = true);
  325. void MarkDelete(PkgIterator const &Pkg,bool Purge = false);
  326. void MarkInstall(PkgIterator const &Pkg,bool AutoInst = true,
  327. unsigned long Depth = 0, bool FromUser = true);
  328. void SetReInstall(PkgIterator const &Pkg,bool To);
  329. void SetCandidateVersion(VerIterator TargetVer);
  330. /** Set the "is automatically installed" flag of Pkg. */
  331. void MarkAuto(const PkgIterator &Pkg, bool Auto);
  332. // @}
  333. // This is for debuging
  334. void Update(OpProgress *Prog = 0);
  335. // read persistent states
  336. bool readStateFile(OpProgress *prog);
  337. bool writeStateFile(OpProgress *prog);
  338. // Size queries
  339. inline double UsrSize() {return iUsrSize;};
  340. inline double DebSize() {return iDownloadSize;};
  341. inline unsigned long DelCount() {return iDelCount;};
  342. inline unsigned long KeepCount() {return iKeepCount;};
  343. inline unsigned long InstCount() {return iInstCount;};
  344. inline unsigned long BrokenCount() {return iBrokenCount;};
  345. inline unsigned long BadCount() {return iBadCount;};
  346. bool Init(OpProgress *Prog);
  347. pkgDepCache(pkgCache *Cache,Policy *Plcy = 0);
  348. virtual ~pkgDepCache();
  349. };
  350. #endif