You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

511 lines
19 KiB

  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 <vector>
  35. #include <memory>
  36. #include <set>
  37. #include <list>
  38. #ifndef APT_8_CLEANER_HEADERS
  39. #include <apt-pkg/progress.h>
  40. #include <apt-pkg/error.h>
  41. #endif
  42. class OpProgress;
  43. class pkgDepCache : protected pkgCache::Namespace
  44. {
  45. public:
  46. /** \brief An arbitrary predicate on packages. */
  47. class InRootSetFunc
  48. {
  49. public:
  50. virtual bool InRootSet(const pkgCache::PkgIterator &pkg) {return false;};
  51. virtual ~InRootSetFunc() {};
  52. };
  53. private:
  54. /** \brief Mark a single package and all its unmarked important
  55. * dependencies during mark-and-sweep.
  56. *
  57. * Recursively invokes itself to mark all dependencies of the
  58. * package.
  59. *
  60. * \param pkg The package to mark.
  61. *
  62. * \param ver The version of the package that is to be marked.
  63. *
  64. * \param follow_recommends If \b true, recommendations of the
  65. * package will be recursively marked.
  66. *
  67. * \param follow_suggests If \b true, suggestions of the package
  68. * will be recursively marked.
  69. */
  70. void MarkPackage(const pkgCache::PkgIterator &pkg,
  71. const pkgCache::VerIterator &ver,
  72. bool const &follow_recommends,
  73. bool const &follow_suggests);
  74. /** \brief Update the Marked field of all packages.
  75. *
  76. * Each package's StateCache::Marked field will be set to \b true
  77. * if and only if it can be reached from the root set. By
  78. * default, the root set consists of the set of manually installed
  79. * or essential packages, but it can be extended using the
  80. * parameter #rootFunc.
  81. *
  82. * \param rootFunc A callback that can be used to add extra
  83. * packages to the root set.
  84. *
  85. * \return \b false if an error occurred.
  86. */
  87. bool MarkRequired(InRootSetFunc &rootFunc);
  88. /** \brief Set the StateCache::Garbage flag on all packages that
  89. * should be removed.
  90. *
  91. * Packages that were not marked by the last call to #MarkRequired
  92. * are tested to see whether they are actually garbage. If so,
  93. * they are marked as such.
  94. *
  95. * \return \b false if an error occurred.
  96. */
  97. bool Sweep();
  98. public:
  99. // These flags are used in DepState
  100. enum DepFlags {DepNow = (1 << 0),DepInstall = (1 << 1),DepCVer = (1 << 2),
  101. DepGNow = (1 << 3),DepGInstall = (1 << 4),DepGCVer = (1 << 5)};
  102. // These flags are used in StateCache::DepState
  103. enum DepStateFlags {DepNowPolicy = (1 << 0), DepNowMin = (1 << 1),
  104. DepInstPolicy = (1 << 2), DepInstMin = (1 << 3),
  105. DepCandPolicy = (1 << 4), DepCandMin = (1 << 5)};
  106. // These flags are used in StateCache::iFlags
  107. enum InternalFlags {AutoKept = (1 << 0), Purge = (1 << 1), ReInstall = (1 << 2), Protected = (1 << 3)};
  108. enum VersionTypes {NowVersion, InstallVersion, CandidateVersion};
  109. enum ModeList {ModeDelete = 0, ModeKeep = 1, ModeInstall = 2, ModeGarbage = 3};
  110. /** \brief Represents an active action group.
  111. *
  112. * An action group is a group of actions that are currently being
  113. * performed. While an active group is active, certain routine
  114. * clean-up actions that would normally be performed after every
  115. * cache operation are delayed until the action group is
  116. * completed. This is necessary primarily to avoid inefficiencies
  117. * when modifying a large number of packages at once.
  118. *
  119. * This class represents an active action group. Creating an
  120. * instance will create an action group; destroying one will
  121. * destroy the corresponding action group.
  122. *
  123. * The following operations are suppressed by this class:
  124. *
  125. * - Keeping the Marked and Garbage flags up to date.
  126. *
  127. * \note This can be used in the future to easily accumulate
  128. * atomic actions for undo or to display "what apt did anyway";
  129. * e.g., change the counter of how many action groups are active
  130. * to a std::set of pointers to them and use those to store
  131. * information about what happened in a group in the group.
  132. */
  133. class ActionGroup
  134. {
  135. pkgDepCache &cache;
  136. bool released;
  137. /** Action groups are noncopyable. */
  138. ActionGroup(const ActionGroup &other);
  139. public:
  140. /** \brief Create a new ActionGroup.
  141. *
  142. * \param cache The cache that this ActionGroup should
  143. * manipulate.
  144. *
  145. * As long as this object exists, no automatic cleanup
  146. * operations will be undertaken.
  147. */
  148. ActionGroup(pkgDepCache &cache);
  149. /** \brief Clean up the action group before it is destroyed.
  150. *
  151. * If it is destroyed later, no second cleanup wil be run.
  152. */
  153. void release();
  154. /** \brief Destroy the action group.
  155. *
  156. * If this is the last action group, the automatic cache
  157. * cleanup operations will be undertaken.
  158. */
  159. ~ActionGroup();
  160. };
  161. /** \brief Returns \b true for packages matching a regular
  162. * expression in APT::NeverAutoRemove.
  163. */
  164. class DefaultRootSetFunc : public InRootSetFunc, public Configuration::MatchAgainstConfig
  165. {
  166. public:
  167. DefaultRootSetFunc() : Configuration::MatchAgainstConfig("APT::NeverAutoRemove") {};
  168. virtual ~DefaultRootSetFunc() {};
  169. bool InRootSet(const pkgCache::PkgIterator &pkg) { return pkg.end() == false && Match(pkg.Name()); };
  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 Purge() const {return Delete() == true && (iFlags & pkgDepCache::Purge) == pkgDepCache::Purge; };
  203. inline bool Keep() const {return Mode == ModeKeep;};
  204. inline bool Protect() const {return (iFlags & Protected) == Protected;};
  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 bool ReInstall() const {return Delete() == false && (iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall;};
  215. inline VerIterator InstVerIter(pkgCache &Cache)
  216. {return VerIterator(Cache,InstallVer);};
  217. inline VerIterator CandidateVerIter(pkgCache &Cache)
  218. {return VerIterator(Cache,CandidateVer);};
  219. };
  220. // Helper functions
  221. void BuildGroupOrs(VerIterator const &V);
  222. void UpdateVerState(PkgIterator Pkg);
  223. // User Policy control
  224. class Policy
  225. {
  226. public:
  227. Policy() {
  228. InstallRecommends = _config->FindB("APT::Install-Recommends", false);
  229. InstallSuggests = _config->FindB("APT::Install-Suggests", false);
  230. }
  231. virtual VerIterator GetCandidateVer(PkgIterator const &Pkg);
  232. virtual bool IsImportantDep(DepIterator const &Dep);
  233. virtual signed short GetPriority(PkgIterator const &Pkg);
  234. virtual signed short GetPriority(PkgFileIterator const &File);
  235. virtual ~Policy() {};
  236. private:
  237. bool InstallRecommends;
  238. bool InstallSuggests;
  239. };
  240. private:
  241. /** The number of open "action groups"; certain post-action
  242. * operations are suppressed if this number is > 0.
  243. */
  244. int group_level;
  245. friend class ActionGroup;
  246. protected:
  247. // State information
  248. pkgCache *Cache;
  249. StateCache *PkgState;
  250. unsigned char *DepState;
  251. /** Stores the space changes after installation */
  252. signed long long iUsrSize;
  253. /** Stores how much we need to download to get the packages */
  254. unsigned long long iDownloadSize;
  255. unsigned long iInstCount;
  256. unsigned long iDelCount;
  257. unsigned long iKeepCount;
  258. unsigned long iBrokenCount;
  259. unsigned long iPolicyBrokenCount;
  260. unsigned long iBadCount;
  261. bool DebugMarker;
  262. bool DebugAutoInstall;
  263. Policy *delLocalPolicy; // For memory clean up..
  264. Policy *LocalPolicy;
  265. // Check for a matching provides
  266. bool CheckDep(DepIterator Dep,int Type,PkgIterator &Res);
  267. inline bool CheckDep(DepIterator Dep,int Type)
  268. {
  269. PkgIterator Res(*this,0);
  270. return CheckDep(Dep,Type,Res);
  271. }
  272. // Computes state information for deps and versions (w/o storing)
  273. unsigned char DependencyState(DepIterator &D);
  274. unsigned char VersionState(DepIterator D,unsigned char Check,
  275. unsigned char SetMin,
  276. unsigned char SetPolicy);
  277. // Recalculates various portions of the cache, call after changing something
  278. void Update(DepIterator Dep); // Mostly internal
  279. void Update(PkgIterator const &P);
  280. // Count manipulators
  281. void AddSizes(const PkgIterator &Pkg, bool const Invert = false);
  282. inline void RemoveSizes(const PkgIterator &Pkg) {AddSizes(Pkg, true);};
  283. void AddStates(const PkgIterator &Pkg, bool const Invert = false);
  284. inline void RemoveStates(const PkgIterator &Pkg) {AddStates(Pkg,true);};
  285. public:
  286. // Legacy.. We look like a pkgCache
  287. inline operator pkgCache &() {return *Cache;};
  288. inline Header &Head() {return *Cache->HeaderP;};
  289. inline GrpIterator GrpBegin() {return Cache->GrpBegin();};
  290. inline PkgIterator PkgBegin() {return Cache->PkgBegin();};
  291. inline GrpIterator FindGrp(std::string const &Name) {return Cache->FindGrp(Name);};
  292. inline PkgIterator FindPkg(std::string const &Name) {return Cache->FindPkg(Name);};
  293. inline PkgIterator FindPkg(std::string const &Name, std::string const &Arch) {return Cache->FindPkg(Name, Arch);};
  294. inline pkgCache &GetCache() {return *Cache;};
  295. inline pkgVersioningSystem &VS() {return *Cache->VS;};
  296. // Policy implementation
  297. inline VerIterator GetCandidateVer(PkgIterator const &Pkg) {return LocalPolicy->GetCandidateVer(Pkg);};
  298. inline bool IsImportantDep(DepIterator Dep) {return LocalPolicy->IsImportantDep(Dep);};
  299. inline Policy &GetPolicy() {return *LocalPolicy;};
  300. // Accessors
  301. inline StateCache &operator [](PkgIterator const &I) {return PkgState[I->ID];};
  302. inline unsigned char &operator [](DepIterator const &I) {return DepState[I->ID];};
  303. /** \return A function identifying packages in the root set other
  304. * than manually installed packages and essential packages, or \b
  305. * NULL if an error occurs.
  306. *
  307. * \todo Is this the best place for this function? Perhaps the
  308. * settings for mark-and-sweep should be stored in a single
  309. * external class?
  310. */
  311. virtual InRootSetFunc *GetRootSetFunc();
  312. /** \return \b true if the garbage collector should follow recommendations.
  313. */
  314. virtual bool MarkFollowsRecommends();
  315. /** \return \b true if the garbage collector should follow suggestions.
  316. */
  317. virtual bool MarkFollowsSuggests();
  318. /** \brief Update the Marked and Garbage fields of all packages.
  319. *
  320. * This routine is implicitly invoked after all state manipulators
  321. * and when an ActionGroup is destroyed. It invokes #MarkRequired
  322. * and #Sweep to do its dirty work.
  323. *
  324. * \param rootFunc A predicate that returns \b true for packages
  325. * that should be added to the root set.
  326. */
  327. bool MarkAndSweep(InRootSetFunc &rootFunc)
  328. {
  329. return MarkRequired(rootFunc) && Sweep();
  330. }
  331. bool MarkAndSweep()
  332. {
  333. std::auto_ptr<InRootSetFunc> f(GetRootSetFunc());
  334. if(f.get() != NULL)
  335. return MarkAndSweep(*f.get());
  336. else
  337. return false;
  338. }
  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. void SetCandidateVersion(VerIterator TargetVer);
  352. bool SetCandidateRelease(pkgCache::VerIterator TargetVer,
  353. std::string const &TargetRel);
  354. /** Set the candidate version for dependencies too if needed.
  355. *
  356. * Sets not only the candidate version as SetCandidateVersion does,
  357. * but walks also down the dependency tree and checks if it is required
  358. * to set the candidate of the dependency to a version from the given
  359. * release, too.
  360. *
  361. * \param TargetVer new candidate version of the package
  362. * \param TargetRel try to switch to this release if needed
  363. * \param[out] Changed a list of pairs consisting of the \b old
  364. * version of the changed package and the version which
  365. * required the switch of this dependency
  366. * \return \b true if the switch was successful, \b false otherwise
  367. */
  368. bool SetCandidateRelease(pkgCache::VerIterator TargetVer,
  369. std::string const &TargetRel,
  370. std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > &Changed);
  371. /** Set the "is automatically installed" flag of Pkg. */
  372. void MarkAuto(const PkgIterator &Pkg, bool Auto);
  373. // @}
  374. /** \return \b true if it's OK for MarkInstall to install
  375. * the given package.
  376. *
  377. * The default implementation simply calls all IsInstallOk*
  378. * method mentioned below.
  379. *
  380. * Overriding implementations should use the hold-state-flag to
  381. * cache results from previous checks of this package - if possible.
  382. *
  383. * The parameters are the same as in the calling MarkInstall:
  384. * \param Pkg the package that MarkInstall wants to install.
  385. * \param AutoInst install this and all its dependencies
  386. * \param Depth recursive deep of this Marker call
  387. * \param FromUser was the install requested by the user?
  388. */
  389. virtual bool IsInstallOk(const PkgIterator &Pkg,bool AutoInst = true,
  390. unsigned long Depth = 0, bool FromUser = true);
  391. /** \return \b true if it's OK for MarkDelete to remove
  392. * the given package.
  393. *
  394. * The default implementation simply calls all IsDeleteOk*
  395. * method mentioned below, see also #IsInstallOk.
  396. *
  397. * The parameters are the same as in the calling MarkDelete:
  398. * \param Pkg the package that MarkDelete wants to remove.
  399. * \param Purge should we purge instead of "only" remove?
  400. * \param Depth recursive deep of this Marker call
  401. * \param FromUser was the remove requested by the user?
  402. */
  403. virtual bool IsDeleteOk(const PkgIterator &Pkg,bool MarkPurge = false,
  404. unsigned long Depth = 0, bool FromUser = true);
  405. // read persistent states
  406. bool readStateFile(OpProgress *prog);
  407. bool writeStateFile(OpProgress *prog, bool InstalledOnly=true);
  408. // Size queries
  409. inline signed long long UsrSize() {return iUsrSize;};
  410. inline unsigned long long DebSize() {return iDownloadSize;};
  411. inline unsigned long DelCount() {return iDelCount;};
  412. inline unsigned long KeepCount() {return iKeepCount;};
  413. inline unsigned long InstCount() {return iInstCount;};
  414. inline unsigned long BrokenCount() {return iBrokenCount;};
  415. inline unsigned long PolicyBrokenCount() {return iPolicyBrokenCount;};
  416. inline unsigned long BadCount() {return iBadCount;};
  417. bool Init(OpProgress *Prog);
  418. // Generate all state information
  419. void Update(OpProgress *Prog = 0);
  420. pkgDepCache(pkgCache *Cache,Policy *Plcy = 0);
  421. virtual ~pkgDepCache();
  422. protected:
  423. // methods call by IsInstallOk
  424. bool IsInstallOkMultiArchSameVersionSynced(PkgIterator const &Pkg,
  425. bool const AutoInst, unsigned long const Depth, bool const FromUser);
  426. // methods call by IsDeleteOk
  427. bool IsDeleteOkProtectInstallRequests(PkgIterator const &Pkg,
  428. bool const rPurge, unsigned long const Depth, bool const FromUser);
  429. private:
  430. bool IsModeChangeOk(ModeList const mode, PkgIterator const &Pkg,
  431. unsigned long const Depth, bool const FromUser);
  432. };
  433. #endif