depcache.h 15 KB

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