depcache.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // -*- mode: c++; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. DepCache - Dependency Extension data for the cache
  5. This class stores the cache data and a set of extension structures for
  6. monitoring the current state of all the packages. It also generates and
  7. caches the 'install' state of many things. This refers to the state of the
  8. package after an install has been run.
  9. The StateCache::State field can be -1,0,1,2 which is <,=,>,no current.
  10. StateCache::Mode is which of the 3 fields is active.
  11. This structure is important to support the readonly status of the cache
  12. file. When the data is saved the cache will be refereshed from our
  13. internal rep and written to disk. Then the actual persistent data
  14. files will be put on the disk.
  15. Each dependency is compared against 3 target versions to produce to
  16. 3 dependency results.
  17. Now - Compared using the Currently install version
  18. Install - Compared using the install version (final state)
  19. CVer - (Candidate Verion) Compared using the Candidate Version
  20. The candidate and now results are used to decide wheather a package
  21. should be automatically installed or if it should be left alone.
  22. Remember, the Candidate Version is selected based on the distribution
  23. settings for the Package. The Install Version is selected based on the
  24. state (Delete, Keep, Install) field and can be either the Current Version
  25. or the Candidate version.
  26. The Candidate version is what is shown the 'Install Version' field.
  27. ##################################################################### */
  28. /*}}}*/
  29. #ifndef PKGLIB_DEPCACHE_H
  30. #define PKGLIB_DEPCACHE_H
  31. #include <apt-pkg/configuration.h>
  32. #include <apt-pkg/pkgcache.h>
  33. #include <apt-pkg/cacheiterators.h>
  34. #include <apt-pkg/macros.h>
  35. #include <stddef.h>
  36. #include <memory>
  37. #include <list>
  38. #include <string>
  39. #include <utility>
  40. #ifndef APT_8_CLEANER_HEADERS
  41. #include <apt-pkg/progress.h>
  42. #include <apt-pkg/error.h>
  43. #endif
  44. #ifndef APT_10_CLEANER_HEADERS
  45. #include <set>
  46. #include <vector>
  47. #endif
  48. class OpProgress;
  49. class pkgVersioningSystem;
  50. class pkgDepCache : protected pkgCache::Namespace
  51. {
  52. public:
  53. /** \brief An arbitrary predicate on packages. */
  54. class InRootSetFunc
  55. {
  56. public:
  57. virtual bool InRootSet(const pkgCache::PkgIterator &/*pkg*/) {return false;};
  58. virtual ~InRootSetFunc() {};
  59. };
  60. private:
  61. /** \brief Mark a single package and all its unmarked important
  62. * dependencies during mark-and-sweep.
  63. *
  64. * Recursively invokes itself to mark all dependencies of the
  65. * package.
  66. *
  67. * \param pkg The package to mark.
  68. *
  69. * \param ver The version of the package that is to be marked.
  70. *
  71. * \param follow_recommends If \b true, recommendations of the
  72. * package will be recursively marked.
  73. *
  74. * \param follow_suggests If \b true, suggestions of the package
  75. * will be recursively marked.
  76. */
  77. APT_HIDDEN void MarkPackage(const pkgCache::PkgIterator &pkg,
  78. const pkgCache::VerIterator &ver,
  79. bool const &follow_recommends,
  80. bool const &follow_suggests);
  81. /** \brief Update the Marked field of all packages.
  82. *
  83. * Each package's StateCache::Marked field will be set to \b true
  84. * if and only if it can be reached from the root set. By
  85. * default, the root set consists of the set of manually installed
  86. * or essential packages, but it can be extended using the
  87. * parameter #rootFunc.
  88. *
  89. * \param rootFunc A callback that can be used to add extra
  90. * packages to the root set.
  91. *
  92. * \return \b false if an error occurred.
  93. */
  94. bool MarkRequired(InRootSetFunc &rootFunc);
  95. /** \brief Set the StateCache::Garbage flag on all packages that
  96. * should be removed.
  97. *
  98. * Packages that were not marked by the last call to #MarkRequired
  99. * are tested to see whether they are actually garbage. If so,
  100. * they are marked as such.
  101. *
  102. * \return \b false if an error occurred.
  103. */
  104. bool Sweep();
  105. public:
  106. // These flags are used in DepState
  107. enum DepFlags {DepNow = (1 << 0),DepInstall = (1 << 1),DepCVer = (1 << 2),
  108. DepGNow = (1 << 3),DepGInstall = (1 << 4),DepGCVer = (1 << 5)};
  109. // These flags are used in StateCache::DepState
  110. enum DepStateFlags {DepNowPolicy = (1 << 0), DepNowMin = (1 << 1),
  111. DepInstPolicy = (1 << 2), DepInstMin = (1 << 3),
  112. DepCandPolicy = (1 << 4), DepCandMin = (1 << 5)};
  113. // These flags are used in StateCache::iFlags
  114. enum InternalFlags {AutoKept = (1 << 0), Purge = (1 << 1), ReInstall = (1 << 2), Protected = (1 << 3)};
  115. enum VersionTypes {NowVersion, InstallVersion, CandidateVersion};
  116. enum ModeList {ModeDelete = 0, ModeKeep = 1, ModeInstall = 2, ModeGarbage = 3};
  117. /** \brief Represents an active action group.
  118. *
  119. * An action group is a group of actions that are currently being
  120. * performed. While an active group is active, certain routine
  121. * clean-up actions that would normally be performed after every
  122. * cache operation are delayed until the action group is
  123. * completed. This is necessary primarily to avoid inefficiencies
  124. * when modifying a large number of packages at once.
  125. *
  126. * This class represents an active action group. Creating an
  127. * instance will create an action group; destroying one will
  128. * destroy the corresponding action group.
  129. *
  130. * The following operations are suppressed by this class:
  131. *
  132. * - Keeping the Marked and Garbage flags up to date.
  133. *
  134. * \note This can be used in the future to easily accumulate
  135. * atomic actions for undo or to display "what apt did anyway";
  136. * e.g., change the counter of how many action groups are active
  137. * to a std::set of pointers to them and use those to store
  138. * information about what happened in a group in the group.
  139. */
  140. class ActionGroup
  141. {
  142. void * const d;
  143. pkgDepCache &cache;
  144. bool released;
  145. /** Action groups are noncopyable. */
  146. APT_HIDDEN ActionGroup(const ActionGroup &other);
  147. public:
  148. /** \brief Create a new ActionGroup.
  149. *
  150. * \param cache The cache that this ActionGroup should
  151. * manipulate.
  152. *
  153. * As long as this object exists, no automatic cleanup
  154. * operations will be undertaken.
  155. */
  156. explicit ActionGroup(pkgDepCache &cache);
  157. /** \brief Clean up the action group before it is destroyed.
  158. *
  159. * If it is destroyed later, no second cleanup wil be run.
  160. */
  161. void release();
  162. /** \brief Destroy the action group.
  163. *
  164. * If this is the last action group, the automatic cache
  165. * cleanup operations will be undertaken.
  166. */
  167. virtual ~ActionGroup();
  168. };
  169. /** \brief Returns \b true for packages matching a regular
  170. * expression in APT::NeverAutoRemove.
  171. */
  172. class DefaultRootSetFunc : public InRootSetFunc, public Configuration::MatchAgainstConfig
  173. {
  174. public:
  175. DefaultRootSetFunc() : Configuration::MatchAgainstConfig("APT::NeverAutoRemove") {};
  176. virtual ~DefaultRootSetFunc() {};
  177. bool InRootSet(const pkgCache::PkgIterator &pkg) APT_OVERRIDE { return pkg.end() == false && Match(pkg.Name()); };
  178. };
  179. struct StateCache
  180. {
  181. // text versions of the two version fields
  182. const char *CandVersion;
  183. const char *CurVersion;
  184. // Pointer to the candidate install version.
  185. Version *CandidateVer;
  186. // Pointer to the install version.
  187. Version *InstallVer;
  188. // Copy of Package::Flags
  189. unsigned short Flags;
  190. unsigned short iFlags; // Internal flags
  191. /** \brief \b true if this package can be reached from the root set. */
  192. bool Marked;
  193. /** \brief \b true if this package is unused and should be removed.
  194. *
  195. * This differs from !#Marked, because it is possible that some
  196. * unreachable packages will be protected from becoming
  197. * garbage.
  198. */
  199. bool Garbage;
  200. // Various tree indicators
  201. signed char Status; // -1,0,1,2
  202. unsigned char Mode; // ModeList
  203. unsigned char DepState; // DepState Flags
  204. // Update of candidate version
  205. APT_DEPRECATED_MSG("Use the method of the same name in contrib/strutl.h instead if you must") const char *StripEpoch(const char *Ver) APT_PURE;
  206. void Update(PkgIterator Pkg,pkgCache &Cache);
  207. // Various test members for the current status of the package
  208. inline bool NewInstall() const {return Status == 2 && Mode == ModeInstall;};
  209. inline bool Delete() const {return Mode == ModeDelete;};
  210. inline bool Purge() const {return Delete() == true && (iFlags & pkgDepCache::Purge) == pkgDepCache::Purge; };
  211. inline bool Keep() const {return Mode == ModeKeep;};
  212. inline bool Protect() const {return (iFlags & Protected) == Protected;};
  213. inline bool Upgrade() const {return Status > 0 && Mode == ModeInstall;};
  214. inline bool Upgradable() const {return Status >= 1 && CandidateVer != NULL;};
  215. inline bool Downgrade() const {return Status < 0 && Mode == ModeInstall;};
  216. inline bool Held() const {return Status != 0 && Keep();};
  217. inline bool NowBroken() const {return (DepState & DepNowMin) != DepNowMin;};
  218. inline bool NowPolicyBroken() const {return (DepState & DepNowPolicy) != DepNowPolicy;};
  219. inline bool InstBroken() const {return (DepState & DepInstMin) != DepInstMin;};
  220. inline bool InstPolicyBroken() const {return (DepState & DepInstPolicy) != DepInstPolicy;};
  221. inline bool Install() const {return Mode == ModeInstall;};
  222. inline bool ReInstall() const {return Delete() == false && (iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall;};
  223. inline VerIterator InstVerIter(pkgCache &Cache)
  224. {return VerIterator(Cache,InstallVer);};
  225. inline VerIterator CandidateVerIter(pkgCache &Cache)
  226. {return VerIterator(Cache,CandidateVer);};
  227. };
  228. // Helper functions
  229. void BuildGroupOrs(VerIterator const &V);
  230. void UpdateVerState(PkgIterator const &Pkg);
  231. // User Policy control
  232. class Policy
  233. {
  234. public:
  235. Policy() {
  236. InstallRecommends = _config->FindB("APT::Install-Recommends", false);
  237. InstallSuggests = _config->FindB("APT::Install-Suggests", false);
  238. }
  239. virtual VerIterator GetCandidateVer(PkgIterator const &Pkg);
  240. virtual bool IsImportantDep(DepIterator const &Dep) const;
  241. virtual signed short GetPriority(PkgIterator const &Pkg);
  242. virtual signed short GetPriority(VerIterator const &Ver, bool ConsiderFiles=true);
  243. virtual signed short GetPriority(PkgFileIterator const &File);
  244. virtual ~Policy() {};
  245. private:
  246. bool InstallRecommends;
  247. bool InstallSuggests;
  248. };
  249. private:
  250. /** The number of open "action groups"; certain post-action
  251. * operations are suppressed if this number is > 0.
  252. */
  253. int group_level;
  254. friend class ActionGroup;
  255. protected:
  256. // State information
  257. pkgCache *Cache;
  258. StateCache *PkgState;
  259. unsigned char *DepState;
  260. /** Stores the space changes after installation */
  261. signed long long iUsrSize;
  262. /** Stores how much we need to download to get the packages */
  263. unsigned long long iDownloadSize;
  264. unsigned long iInstCount;
  265. unsigned long iDelCount;
  266. unsigned long iKeepCount;
  267. unsigned long iBrokenCount;
  268. unsigned long iPolicyBrokenCount;
  269. unsigned long iBadCount;
  270. bool DebugMarker;
  271. bool DebugAutoInstall;
  272. Policy *delLocalPolicy; // For memory clean up..
  273. Policy *LocalPolicy;
  274. // Check for a matching provides
  275. bool CheckDep(DepIterator const &Dep,int const Type,PkgIterator &Res);
  276. inline bool CheckDep(DepIterator const &Dep,int const Type)
  277. {
  278. PkgIterator Res(*this,0);
  279. return CheckDep(Dep,Type,Res);
  280. }
  281. // Computes state information for deps and versions (w/o storing)
  282. unsigned char DependencyState(DepIterator const &D);
  283. unsigned char VersionState(DepIterator D,unsigned char const Check,
  284. unsigned char const SetMin,
  285. unsigned char const SetPolicy) const;
  286. // Recalculates various portions of the cache, call after changing something
  287. void Update(DepIterator Dep); // Mostly internal
  288. void Update(PkgIterator const &P);
  289. // Count manipulators
  290. void AddSizes(const PkgIterator &Pkg, bool const Invert = false);
  291. inline void RemoveSizes(const PkgIterator &Pkg) {AddSizes(Pkg, true);};
  292. void AddStates(const PkgIterator &Pkg, bool const Invert = false);
  293. inline void RemoveStates(const PkgIterator &Pkg) {AddStates(Pkg,true);};
  294. public:
  295. // Legacy.. We look like a pkgCache
  296. inline operator pkgCache &() {return *Cache;};
  297. inline Header &Head() {return *Cache->HeaderP;};
  298. inline GrpIterator GrpBegin() {return Cache->GrpBegin();};
  299. inline PkgIterator PkgBegin() {return Cache->PkgBegin();};
  300. inline GrpIterator FindGrp(std::string const &Name) {return Cache->FindGrp(Name);};
  301. inline PkgIterator FindPkg(std::string const &Name) {return Cache->FindPkg(Name);};
  302. inline PkgIterator FindPkg(std::string const &Name, std::string const &Arch) {return Cache->FindPkg(Name, Arch);};
  303. inline pkgCache &GetCache() {return *Cache;};
  304. inline pkgVersioningSystem &VS() {return *Cache->VS;};
  305. // Policy implementation
  306. APT_DEPRECATED_MSG("Confusingly named method which returns the candidate as chosen by policy (NOT as chosen via .SetCandidateVersion!). You probably want to use .GetCandidateVersion instead.") inline VerIterator GetCandidateVer(PkgIterator const &Pkg) {return /* GetCandidateVersion(Pkg); but for API compat: */ LocalPolicy->GetCandidateVer(Pkg);};
  307. inline bool IsImportantDep(DepIterator Dep) const {return LocalPolicy->IsImportantDep(Dep);};
  308. inline Policy &GetPolicy() {return *LocalPolicy;};
  309. // Accessors
  310. inline StateCache &operator [](PkgIterator const &I) {return PkgState[I->ID];};
  311. inline StateCache &operator [](PkgIterator const &I) const {return PkgState[I->ID];};
  312. inline unsigned char &operator [](DepIterator const &I) {return DepState[I->ID];};
  313. /** \return A function identifying packages in the root set other
  314. * than manually installed packages and essential packages, or \b
  315. * NULL if an error occurs.
  316. *
  317. * \todo Is this the best place for this function? Perhaps the
  318. * settings for mark-and-sweep should be stored in a single
  319. * external class?
  320. */
  321. virtual InRootSetFunc *GetRootSetFunc();
  322. /** \return \b true if the garbage collector should follow recommendations.
  323. */
  324. virtual bool MarkFollowsRecommends();
  325. /** \return \b true if the garbage collector should follow suggestions.
  326. */
  327. virtual bool MarkFollowsSuggests();
  328. /** \brief Update the Marked and Garbage fields of all packages.
  329. *
  330. * This routine is implicitly invoked after all state manipulators
  331. * and when an ActionGroup is destroyed. It invokes the private
  332. * MarkRequired() and Sweep() to do its dirty work.
  333. *
  334. * \param rootFunc A predicate that returns \b true for packages
  335. * that should be added to the root set.
  336. */
  337. bool MarkAndSweep(InRootSetFunc &rootFunc);
  338. bool MarkAndSweep();
  339. /** \name State Manipulators
  340. */
  341. // @{
  342. bool MarkKeep(PkgIterator const &Pkg, bool Soft = false,
  343. bool FromUser = true, unsigned long Depth = 0);
  344. bool MarkDelete(PkgIterator const &Pkg, bool MarkPurge = false,
  345. unsigned long Depth = 0, bool FromUser = true);
  346. bool MarkInstall(PkgIterator const &Pkg,bool AutoInst = true,
  347. unsigned long Depth = 0, bool FromUser = true,
  348. bool ForceImportantDeps = false);
  349. void MarkProtected(PkgIterator const &Pkg) { PkgState[Pkg->ID].iFlags |= Protected; };
  350. void SetReInstall(PkgIterator const &Pkg,bool To);
  351. /** @return 'the' candidate version of a package
  352. *
  353. * The version returned is the version previously set explicitly via
  354. * SetCandidate* methods like #SetCandidateVersion or if there wasn't one
  355. * set the version as chosen via #Policy.
  356. *
  357. * @param Pkg is the package to return the candidate for
  358. */
  359. pkgCache::VerIterator GetCandidateVersion(pkgCache::PkgIterator const &Pkg);
  360. void SetCandidateVersion(VerIterator TargetVer);
  361. bool SetCandidateRelease(pkgCache::VerIterator TargetVer,
  362. std::string const &TargetRel);
  363. /** Set the candidate version for dependencies too if needed.
  364. *
  365. * Sets not only the candidate version as SetCandidateVersion does,
  366. * but walks also down the dependency tree and checks if it is required
  367. * to set the candidate of the dependency to a version from the given
  368. * release, too.
  369. *
  370. * \param TargetVer new candidate version of the package
  371. * \param TargetRel try to switch to this release if needed
  372. * \param[out] Changed a list of pairs consisting of the \b old
  373. * version of the changed package and the version which
  374. * required the switch of this dependency
  375. * \return \b true if the switch was successful, \b false otherwise
  376. */
  377. bool SetCandidateRelease(pkgCache::VerIterator TargetVer,
  378. std::string const &TargetRel,
  379. std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > &Changed);
  380. /** Set the "is automatically installed" flag of Pkg. */
  381. void MarkAuto(const PkgIterator &Pkg, bool Auto);
  382. // @}
  383. /** \return \b true if it's OK for MarkInstall to install
  384. * the given package.
  385. *
  386. * The default implementation simply calls all IsInstallOk*
  387. * method mentioned below.
  388. *
  389. * Overriding implementations should use the hold-state-flag to
  390. * cache results from previous checks of this package - if possible.
  391. *
  392. * The parameters are the same as in the calling MarkInstall:
  393. * \param Pkg the package that MarkInstall wants to install.
  394. * \param AutoInst install this and all its dependencies
  395. * \param Depth recursive deep of this Marker call
  396. * \param FromUser was the install requested by the user?
  397. */
  398. virtual bool IsInstallOk(const PkgIterator &Pkg,bool AutoInst = true,
  399. unsigned long Depth = 0, bool FromUser = true);
  400. /** \return \b true if it's OK for MarkDelete to remove
  401. * the given package.
  402. *
  403. * The default implementation simply calls all IsDeleteOk*
  404. * method mentioned below, see also #IsInstallOk.
  405. *
  406. * The parameters are the same as in the calling MarkDelete:
  407. * \param Pkg the package that MarkDelete wants to remove.
  408. * \param MarkPurge should we purge instead of "only" remove?
  409. * \param Depth recursive deep of this Marker call
  410. * \param FromUser was the remove requested by the user?
  411. */
  412. virtual bool IsDeleteOk(const PkgIterator &Pkg,bool MarkPurge = false,
  413. unsigned long Depth = 0, bool FromUser = true);
  414. // read persistent states
  415. bool readStateFile(OpProgress * const prog);
  416. bool writeStateFile(OpProgress * const prog, bool const InstalledOnly=true);
  417. // Size queries
  418. inline signed long long UsrSize() {return iUsrSize;};
  419. inline unsigned long long DebSize() {return iDownloadSize;};
  420. inline unsigned long DelCount() {return iDelCount;};
  421. inline unsigned long KeepCount() {return iKeepCount;};
  422. inline unsigned long InstCount() {return iInstCount;};
  423. inline unsigned long BrokenCount() {return iBrokenCount;};
  424. inline unsigned long PolicyBrokenCount() {return iPolicyBrokenCount;};
  425. inline unsigned long BadCount() {return iBadCount;};
  426. bool Init(OpProgress * const Prog);
  427. // Generate all state information
  428. void Update(OpProgress * const Prog = 0);
  429. pkgDepCache(pkgCache * const Cache,Policy * const Plcy = 0);
  430. virtual ~pkgDepCache();
  431. protected:
  432. // methods call by IsInstallOk
  433. bool IsInstallOkMultiArchSameVersionSynced(PkgIterator const &Pkg,
  434. bool const AutoInst, unsigned long const Depth, bool const FromUser);
  435. bool IsInstallOkDependenciesSatisfiableByCandidates(PkgIterator const &Pkg,
  436. bool const AutoInst, unsigned long const Depth, bool const FromUser);
  437. // methods call by IsDeleteOk
  438. bool IsDeleteOkProtectInstallRequests(PkgIterator const &Pkg,
  439. bool const rPurge, unsigned long const Depth, bool const FromUser);
  440. private:
  441. void * const d;
  442. APT_HIDDEN bool IsModeChangeOk(ModeList const mode, PkgIterator const &Pkg,
  443. unsigned long const Depth, bool const FromUser);
  444. };
  445. #endif