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