depcache.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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/configuration.h>
  33. #include <apt-pkg/pkgcache.h>
  34. #include <apt-pkg/progress.h>
  35. #include <apt-pkg/error.h>
  36. #include <vector>
  37. #include <memory>
  38. #include <set>
  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 const &follow_recommends,
  69. bool const &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 occurred.
  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 occurred.
  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), Protected = (1 << 3)};
  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, public Configuration::MatchAgainstConfig
  161. {
  162. public:
  163. DefaultRootSetFunc() : Configuration::MatchAgainstConfig("APT::NeverAutoRemove") {};
  164. virtual ~DefaultRootSetFunc() {};
  165. bool InRootSet(const pkgCache::PkgIterator &pkg) { return pkg.end() == false && Match(pkg.Name()); };
  166. };
  167. struct StateCache
  168. {
  169. // Epoch stripped text versions of the two version fields
  170. const char *CandVersion;
  171. const char *CurVersion;
  172. // Pointer to the candidate install version.
  173. Version *CandidateVer;
  174. // Pointer to the install version.
  175. Version *InstallVer;
  176. // Copy of Package::Flags
  177. unsigned short Flags;
  178. unsigned short iFlags; // Internal flags
  179. /** \brief \b true if this package can be reached from the root set. */
  180. bool Marked;
  181. /** \brief \b true if this package is unused and should be removed.
  182. *
  183. * This differs from !#Marked, because it is possible that some
  184. * unreachable packages will be protected from becoming
  185. * garbage.
  186. */
  187. bool Garbage;
  188. // Various tree indicators
  189. signed char Status; // -1,0,1,2
  190. unsigned char Mode; // ModeList
  191. unsigned char DepState; // DepState Flags
  192. // Update of candidate version
  193. const char *StripEpoch(const char *Ver);
  194. void Update(PkgIterator Pkg,pkgCache &Cache);
  195. // Various test members for the current status of the package
  196. inline bool NewInstall() const {return Status == 2 && Mode == ModeInstall;};
  197. inline bool Delete() const {return Mode == ModeDelete;};
  198. inline bool Keep() const {return Mode == ModeKeep;};
  199. inline bool Upgrade() const {return Status > 0 && Mode == ModeInstall;};
  200. inline bool Upgradable() const {return Status >= 1;};
  201. inline bool Downgrade() const {return Status < 0 && Mode == ModeInstall;};
  202. inline bool Held() const {return Status != 0 && Keep();};
  203. inline bool NowBroken() const {return (DepState & DepNowMin) != DepNowMin;};
  204. inline bool NowPolicyBroken() const {return (DepState & DepNowPolicy) != DepNowPolicy;};
  205. inline bool InstBroken() const {return (DepState & DepInstMin) != DepInstMin;};
  206. inline bool InstPolicyBroken() const {return (DepState & DepInstPolicy) != DepInstPolicy;};
  207. inline bool Install() const {return Mode == ModeInstall;};
  208. inline VerIterator InstVerIter(pkgCache &Cache)
  209. {return VerIterator(Cache,InstallVer);};
  210. inline VerIterator CandidateVerIter(pkgCache &Cache)
  211. {return VerIterator(Cache,CandidateVer);};
  212. };
  213. // Helper functions
  214. void BuildGroupOrs(VerIterator const &V);
  215. void UpdateVerState(PkgIterator Pkg);
  216. // User Policy control
  217. class Policy
  218. {
  219. public:
  220. virtual VerIterator GetCandidateVer(PkgIterator const &Pkg);
  221. virtual bool IsImportantDep(DepIterator const &Dep);
  222. virtual ~Policy() {};
  223. };
  224. private:
  225. /** The number of open "action groups"; certain post-action
  226. * operations are suppressed if this number is > 0.
  227. */
  228. int group_level;
  229. friend class ActionGroup;
  230. protected:
  231. // State information
  232. pkgCache *Cache;
  233. StateCache *PkgState;
  234. unsigned char *DepState;
  235. /** Stores the space changes after installation */
  236. signed long long iUsrSize;
  237. /** Stores how much we need to download to get the packages */
  238. unsigned long long iDownloadSize;
  239. unsigned long iInstCount;
  240. unsigned long iDelCount;
  241. unsigned long iKeepCount;
  242. unsigned long iBrokenCount;
  243. unsigned long iPolicyBrokenCount;
  244. unsigned long iBadCount;
  245. bool DebugMarker;
  246. bool DebugAutoInstall;
  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, bool const &Invert = false);
  266. inline void RemoveSizes(const PkgIterator &Pkg) {AddSizes(Pkg, true);};
  267. void AddSizes(const PkgIterator &Pkg,signed long Mult) __deprecated;
  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 GrpIterator GrpBegin() {return Cache->GrpBegin();};
  275. inline PkgIterator PkgBegin() {return Cache->PkgBegin();};
  276. inline GrpIterator FindGrp(string const &Name) {return Cache->FindGrp(Name);};
  277. inline PkgIterator FindPkg(string const &Name) {return Cache->FindPkg(Name);};
  278. inline PkgIterator FindPkg(string const &Name, string const &Arch) {return Cache->FindPkg(Name, Arch);};
  279. inline pkgCache &GetCache() {return *Cache;};
  280. inline pkgVersioningSystem &VS() {return *Cache->VS;};
  281. // Policy implementation
  282. inline VerIterator GetCandidateVer(PkgIterator const &Pkg) {return LocalPolicy->GetCandidateVer(Pkg);};
  283. inline bool IsImportantDep(DepIterator Dep) {return LocalPolicy->IsImportantDep(Dep);};
  284. inline Policy &GetPolicy() {return *LocalPolicy;};
  285. // Accessors
  286. inline StateCache &operator [](PkgIterator const &I) {return PkgState[I->ID];};
  287. inline unsigned char &operator [](DepIterator const &I) {return DepState[I->ID];};
  288. /** \return A function identifying packages in the root set other
  289. * than manually installed packages and essential packages, or \b
  290. * NULL if an error occurs.
  291. *
  292. * \todo Is this the best place for this function? Perhaps the
  293. * settings for mark-and-sweep should be stored in a single
  294. * external class?
  295. */
  296. virtual InRootSetFunc *GetRootSetFunc();
  297. /** \return \b true if the garbage collector should follow recommendations.
  298. */
  299. virtual bool MarkFollowsRecommends();
  300. /** \return \b true if the garbage collector should follow suggestions.
  301. */
  302. virtual bool MarkFollowsSuggests();
  303. /** \brief Update the Marked and Garbage fields of all packages.
  304. *
  305. * This routine is implicitly invoked after all state manipulators
  306. * and when an ActionGroup is destroyed. It invokes #MarkRequired
  307. * and #Sweep to do its dirty work.
  308. *
  309. * \param rootFunc A predicate that returns \b true for packages
  310. * that should be added to the root set.
  311. */
  312. bool MarkAndSweep(InRootSetFunc &rootFunc)
  313. {
  314. return MarkRequired(rootFunc) && Sweep();
  315. }
  316. bool MarkAndSweep()
  317. {
  318. std::auto_ptr<InRootSetFunc> f(GetRootSetFunc());
  319. if(f.get() != NULL)
  320. return MarkAndSweep(*f.get());
  321. else
  322. return false;
  323. }
  324. /** \name State Manipulators
  325. */
  326. // @{
  327. void MarkKeep(PkgIterator const &Pkg, bool Soft = false,
  328. bool FromUser = true, unsigned long Depth = 0);
  329. void MarkDelete(PkgIterator const &Pkg, bool Purge = false,
  330. unsigned long Depth = 0, bool FromUser = true);
  331. void MarkInstall(PkgIterator const &Pkg,bool AutoInst = true,
  332. unsigned long Depth = 0, bool FromUser = true,
  333. bool ForceImportantDeps = false);
  334. void MarkProtected(PkgIterator const &Pkg) { PkgState[Pkg->ID].iFlags |= Protected; };
  335. void SetReInstall(PkgIterator const &Pkg,bool To);
  336. // FIXME: Remove the unused boolean parameter on abi break
  337. void SetCandidateVersion(VerIterator TargetVer, bool const &Pseudo = true);
  338. bool SetCandidateRelease(pkgCache::VerIterator TargetVer,
  339. std::string const &TargetRel);
  340. /** Set the candidate version for dependencies too if needed.
  341. *
  342. * Sets not only the candidate version as SetCandidateVersion does,
  343. * but walks also down the dependency tree and checks if it is required
  344. * to set the candidate of the dependency to a version from the given
  345. * release, too.
  346. *
  347. * \param TargetVer new candidate version of the package
  348. * \param TargetRel try to switch to this release if needed
  349. * \param[out] Changed a list of pairs consisting of the \b old
  350. * version of the changed package and the version which
  351. * required the switch of this dependency
  352. * \return \b true if the switch was successful, \b false otherwise
  353. */
  354. bool SetCandidateRelease(pkgCache::VerIterator TargetVer,
  355. std::string const &TargetRel,
  356. std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > &Changed);
  357. /** Set the "is automatically installed" flag of Pkg. */
  358. void MarkAuto(const PkgIterator &Pkg, bool Auto);
  359. // @}
  360. /** \return \b true if it's OK for MarkInstall to install
  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 MarkInstall:
  371. * \param Pkg the package that MarkInstall wants to install.
  372. * \param AutoInst needs a previous MarkInstall this package?
  373. * \param Depth recursive deep of this Marker call
  374. * \param FromUser was the install requested by the user?
  375. */
  376. virtual bool IsInstallOk(const PkgIterator &Pkg,bool AutoInst = true,
  377. unsigned long Depth = 0, bool FromUser = true);
  378. /** \return \b true if it's OK for MarkDelete to remove
  379. * the given package.
  380. *
  381. * See the default implementation for a simple example how this
  382. * method can be used.
  383. * Overriding implementations should use the hold-state-flag to cache
  384. * results from previous checks of this package - also it should
  385. * be used if the default resolver implementation is also used to
  386. * ensure that these packages are handled like "normal" dpkg holds.
  387. *
  388. * The parameters are the same as in the calling MarkDelete:
  389. * \param Pkg the package that MarkDelete wants to remove.
  390. * \param Purge should we purge instead of "only" remove?
  391. * \param Depth recursive deep of this Marker call
  392. * \param FromUser was the remove requested by the user?
  393. */
  394. virtual bool IsDeleteOk(const PkgIterator &Pkg,bool Purge = false,
  395. unsigned long Depth = 0, bool FromUser = true);
  396. // read persistent states
  397. bool readStateFile(OpProgress *prog);
  398. bool writeStateFile(OpProgress *prog, bool InstalledOnly=true);
  399. // Size queries
  400. inline signed long long UsrSize() {return iUsrSize;};
  401. inline unsigned long long DebSize() {return iDownloadSize;};
  402. inline unsigned long DelCount() {return iDelCount;};
  403. inline unsigned long KeepCount() {return iKeepCount;};
  404. inline unsigned long InstCount() {return iInstCount;};
  405. inline unsigned long BrokenCount() {return iBrokenCount;};
  406. inline unsigned long PolicyBrokenCount() {return iPolicyBrokenCount;};
  407. inline unsigned long BadCount() {return iBadCount;};
  408. bool Init(OpProgress *Prog);
  409. // Generate all state information
  410. void Update(OpProgress *Prog = 0);
  411. pkgDepCache(pkgCache *Cache,Policy *Plcy = 0);
  412. virtual ~pkgDepCache();
  413. private:
  414. // Helper for Update(OpProgress) to remove pseudoinstalled arch all packages
  415. // FIXME: they are private so shouldn't affect abi, but just in case…
  416. __deprecated bool RemovePseudoInstalledPkg(PkgIterator &Pkg, std::set<unsigned long> &recheck) { return true; };
  417. __deprecated bool ReInstallPseudoForGroup(unsigned long const &Grp, std::set<unsigned long> &recheck) { return true; };
  418. __deprecated bool ReInstallPseudoForGroup(pkgCache::PkgIterator const &P, std::set<unsigned long> &recheck) { return true; };
  419. bool IsModeChangeOk(ModeList const mode, PkgIterator const &Pkg,
  420. unsigned long const Depth, bool const FromUser);
  421. };
  422. #endif