depcache.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. class pkgDepCache : protected pkgCache::Namespace
  37. {
  38. public:
  39. /** \brief An arbitrary predicate on packages. */
  40. class InRootSetFunc
  41. {
  42. public:
  43. virtual bool InRootSet(const pkgCache::PkgIterator &pkg) {return false;};
  44. virtual ~InRootSetFunc() {};
  45. };
  46. private:
  47. /** \brief Mark a single package and all its unmarked important
  48. * dependencies during mark-and-sweep.
  49. *
  50. * Recursively invokes itself to mark all dependencies of the
  51. * package.
  52. *
  53. * \param pkg The package to mark.
  54. *
  55. * \param ver The version of the package that is to be marked.
  56. *
  57. * \param follow_recommends If \b true, recommendations of the
  58. * package will be recursively marked.
  59. *
  60. * \param follow_suggests If \b true, suggestions of the package
  61. * will be recursively marked.
  62. */
  63. void MarkPackage(const pkgCache::PkgIterator &pkg,
  64. const pkgCache::VerIterator &ver,
  65. bool follow_recommends,
  66. bool follow_suggests);
  67. /** \brief Update the Marked field of all packages.
  68. *
  69. * Each package's StateCache::Marked field will be set to \b true
  70. * if and only if it can be reached from the root set. By
  71. * default, the root set consists of the set of manually installed
  72. * or essential packages, but it can be extended using the
  73. * parameter #rootFunc.
  74. *
  75. * \param rootFunc A callback that can be used to add extra
  76. * packages to the root set.
  77. *
  78. * \return \b false if an error occured.
  79. */
  80. bool MarkRequired(InRootSetFunc &rootFunc);
  81. /** \brief Set the StateCache::Garbage flag on all packages that
  82. * should be removed.
  83. *
  84. * Packages that were not marked by the last call to #MarkRequired
  85. * are tested to see whether they are actually garbage. If so,
  86. * they are marked as such.
  87. *
  88. * \return \b false if an error occured.
  89. */
  90. bool Sweep();
  91. public:
  92. // These flags are used in DepState
  93. enum DepFlags {DepNow = (1 << 0),DepInstall = (1 << 1),DepCVer = (1 << 2),
  94. DepGNow = (1 << 3),DepGInstall = (1 << 4),DepGCVer = (1 << 5)};
  95. // These flags are used in StateCache::DepState
  96. enum DepStateFlags {DepNowPolicy = (1 << 0), DepNowMin = (1 << 1),
  97. DepInstPolicy = (1 << 2), DepInstMin = (1 << 3),
  98. DepCandPolicy = (1 << 4), DepCandMin = (1 << 5)};
  99. // These flags are used in StateCache::iFlags
  100. enum InternalFlags {AutoKept = (1 << 0), Purge = (1 << 1), ReInstall = (1 << 2)};
  101. enum VersionTypes {NowVersion, InstallVersion, CandidateVersion};
  102. enum ModeList {ModeDelete = 0, ModeKeep = 1, ModeInstall = 2};
  103. /** \brief Represents an active action group.
  104. *
  105. * An action group is a group of actions that are currently being
  106. * performed. While an active group is active, certain routine
  107. * clean-up actions that would normally be performed after every
  108. * cache operation are delayed until the action group is
  109. * completed. This is necessary primarily to avoid inefficiencies
  110. * when modifying a large number of packages at once.
  111. *
  112. * This class represents an active action group. Creating an
  113. * instance will create an action group; destroying one will
  114. * destroy the corresponding action group.
  115. *
  116. * The following operations are suppressed by this class:
  117. *
  118. * - Keeping the Marked and Garbage flags up to date.
  119. *
  120. * \note This can be used in the future to easily accumulate
  121. * atomic actions for undo or to display "what apt did anyway";
  122. * e.g., change the counter of how many action groups are active
  123. * to a std::set of pointers to them and use those to store
  124. * information about what happened in a group in the group.
  125. */
  126. class ActionGroup
  127. {
  128. pkgDepCache &cache;
  129. bool released;
  130. /** Action groups are noncopyable. */
  131. ActionGroup(const ActionGroup &other);
  132. public:
  133. /** \brief Create a new ActionGroup.
  134. *
  135. * \param cache The cache that this ActionGroup should
  136. * manipulate.
  137. *
  138. * As long as this object exists, no automatic cleanup
  139. * operations will be undertaken.
  140. */
  141. ActionGroup(pkgDepCache &cache);
  142. /** \brief Clean up the action group before it is destroyed.
  143. *
  144. * If it is destroyed later, no second cleanup wil be run.
  145. */
  146. void release();
  147. /** \brief Destroy the action group.
  148. *
  149. * If this is the last action group, the automatic cache
  150. * cleanup operations will be undertaken.
  151. */
  152. ~ActionGroup();
  153. };
  154. /** \brief Returns \b true for packages matching a regular
  155. * expression in APT::NeverAutoRemove.
  156. */
  157. class DefaultRootSetFunc : public InRootSetFunc
  158. {
  159. std::vector<regex_t *> rootSetRegexp;
  160. bool constructedSuccessfully;
  161. public:
  162. DefaultRootSetFunc();
  163. ~DefaultRootSetFunc();
  164. /** \return \b true if the class initialized successfully, \b
  165. * false otherwise. Used to avoid throwing an exception, since
  166. * APT classes generally don't.
  167. */
  168. bool wasConstructedSuccessfully() const { return constructedSuccessfully; }
  169. bool InRootSet(const pkgCache::PkgIterator &pkg);
  170. };
  171. struct StateCache
  172. {
  173. // Epoch stripped text versions of the two version fields
  174. const char *CandVersion;
  175. const char *CurVersion;
  176. // Pointer to the candidate install version.
  177. Version *CandidateVer;
  178. // Pointer to the install version.
  179. Version *InstallVer;
  180. // Copy of Package::Flags
  181. unsigned short Flags;
  182. unsigned short iFlags; // Internal flags
  183. /** \brief \b true if this package can be reached from the root set. */
  184. bool Marked;
  185. /** \brief \b true if this package is unused and should be removed.
  186. *
  187. * This differs from !#Marked, because it is possible that some
  188. * unreachable packages will be protected from becoming
  189. * garbage.
  190. */
  191. bool Garbage;
  192. // Various tree indicators
  193. signed char Status; // -1,0,1,2
  194. unsigned char Mode; // ModeList
  195. unsigned char DepState; // DepState Flags
  196. // Update of candidate version
  197. const char *StripEpoch(const char *Ver);
  198. void Update(PkgIterator Pkg,pkgCache &Cache);
  199. // Various test members for the current status of the package
  200. inline bool NewInstall() const {return Status == 2 && Mode == ModeInstall;};
  201. inline bool Delete() const {return Mode == ModeDelete;};
  202. inline bool Keep() const {return Mode == ModeKeep;};
  203. inline bool Upgrade() const {return Status > 0 && Mode == ModeInstall;};
  204. inline bool Upgradable() const {return Status >= 1;};
  205. inline bool Downgrade() const {return Status < 0 && Mode == ModeInstall;};
  206. inline bool Held() const {return Status != 0 && Keep();};
  207. inline bool NowBroken() const {return (DepState & DepNowMin) != DepNowMin;};
  208. inline bool NowPolicyBroken() const {return (DepState & DepNowPolicy) != DepNowPolicy;};
  209. inline bool InstBroken() const {return (DepState & DepInstMin) != DepInstMin;};
  210. inline bool InstPolicyBroken() const {return (DepState & DepInstPolicy) != DepInstPolicy;};
  211. inline bool Install() const {return Mode == ModeInstall;};
  212. inline VerIterator InstVerIter(pkgCache &Cache)
  213. {return VerIterator(Cache,InstallVer);};
  214. inline VerIterator CandidateVerIter(pkgCache &Cache)
  215. {return VerIterator(Cache,CandidateVer);};
  216. };
  217. // Helper functions
  218. void BuildGroupOrs(VerIterator const &V);
  219. void UpdateVerState(PkgIterator Pkg);
  220. // User Policy control
  221. class Policy
  222. {
  223. public:
  224. virtual VerIterator GetCandidateVer(PkgIterator Pkg);
  225. virtual bool IsImportantDep(DepIterator Dep);
  226. virtual ~Policy() {};
  227. };
  228. private:
  229. /** The number of open "action groups"; certain post-action
  230. * operations are suppressed if this number is > 0.
  231. */
  232. int group_level;
  233. friend class ActionGroup;
  234. protected:
  235. // State information
  236. pkgCache *Cache;
  237. StateCache *PkgState;
  238. unsigned char *DepState;
  239. double iUsrSize;
  240. double iDownloadSize;
  241. unsigned long iInstCount;
  242. unsigned long iDelCount;
  243. unsigned long iKeepCount;
  244. unsigned long iBrokenCount;
  245. unsigned long iPolicyBrokenCount;
  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. bool ForceImportantDeps = false);
  329. void SetReInstall(PkgIterator const &Pkg,bool To);
  330. void SetCandidateVersion(VerIterator TargetVer);
  331. /** Set the "is automatically installed" flag of Pkg. */
  332. void MarkAuto(const PkgIterator &Pkg, bool Auto);
  333. // @}
  334. // This is for debuging
  335. void Update(OpProgress *Prog = 0);
  336. // read persistent states
  337. bool readStateFile(OpProgress *prog);
  338. bool writeStateFile(OpProgress *prog, bool InstalledOnly=false);
  339. // Size queries
  340. inline double UsrSize() {return iUsrSize;};
  341. inline double DebSize() {return iDownloadSize;};
  342. inline unsigned long DelCount() {return iDelCount;};
  343. inline unsigned long KeepCount() {return iKeepCount;};
  344. inline unsigned long InstCount() {return iInstCount;};
  345. inline unsigned long BrokenCount() {return iBrokenCount;};
  346. inline unsigned long PolicyBrokenCount() {return iPolicyBrokenCount;};
  347. inline unsigned long BadCount() {return iBadCount;};
  348. bool Init(OpProgress *Prog);
  349. pkgDepCache(pkgCache *Cache,Policy *Plcy = 0);
  350. virtual ~pkgDepCache();
  351. };
  352. #endif