depcache.h 17 KB

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