]> git.saurik.com Git - apt.git/blame - apt-pkg/depcache.h
Coverage: Do not print messages from gcov
[apt.git] / apt-pkg / depcache.h
CommitLineData
74a05226 1// -*- mode: c++; mode: fold -*-
6c139d6e 2// Description /*{{{*/
6c139d6e
AL
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
1e3f4083 17 internal rep and written to disk. Then the actual persistent data
6c139d6e
AL
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 /*}}}*/
6c139d6e
AL
37#ifndef PKGLIB_DEPCACHE_H
38#define PKGLIB_DEPCACHE_H
39
1f2933a8 40#include <apt-pkg/configuration.h>
094a497d 41#include <apt-pkg/pkgcache.h>
453b82a3 42#include <apt-pkg/cacheiterators.h>
a02db58f 43#include <apt-pkg/macros.h>
453b82a3
DK
44
45#include <stddef.h>
74a05226 46
a7955daa 47#include <memory>
472ff00e 48#include <list>
453b82a3
DK
49#include <string>
50#include <utility>
472ff00e 51
b9dadc24
DK
52#ifndef APT_8_CLEANER_HEADERS
53#include <apt-pkg/progress.h>
54#include <apt-pkg/error.h>
55#endif
453b82a3
DK
56#ifndef APT_10_CLEANER_HEADERS
57#include <set>
58#include <vector>
59#endif
b9dadc24 60
472ff00e 61class OpProgress;
453b82a3 62class pkgVersioningSystem;
74a05226 63
b2e465d6 64class pkgDepCache : protected pkgCache::Namespace
6c139d6e
AL
65{
66 public:
74a05226
MV
67
68 /** \brief An arbitrary predicate on packages. */
69 class InRootSetFunc
70 {
71 public:
65512241 72 virtual bool InRootSet(const pkgCache::PkgIterator &/*pkg*/) {return false;};
74a05226
MV
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 */
3809194b 93 APT_HIDDEN void MarkPackage(const pkgCache::PkgIterator &pkg,
74a05226 94 const pkgCache::VerIterator &ver,
e0b94b97
DK
95 bool const &follow_recommends,
96 bool const &follow_suggests);
74a05226
MV
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 *
38965a34 109 * \return \b false if an error occurred.
74a05226 110 */
fa5404ab 111 bool MarkRequired(InRootSetFunc &rootFunc);
74a05226
MV
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 *
38965a34 120 * \return \b false if an error occurred.
74a05226 121 */
fa5404ab 122 bool Sweep();
74a05226
MV
123
124 public:
6c139d6e
AL
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
cc26da01 136 enum InternalFlags {AutoKept = (1 << 0), Purge = (1 << 1), ReInstall = (1 << 2), Protected = (1 << 3)};
6c139d6e
AL
137
138 enum VersionTypes {NowVersion, InstallVersion, CandidateVersion};
8fa042ca 139 enum ModeList {ModeDelete = 0, ModeKeep = 1, ModeInstall = 2, ModeGarbage = 3};
0a57c0f0 140
74a05226
MV
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 {
6c55f07a 166 void * const d;
74a05226
MV
167 pkgDepCache &cache;
168
169 bool released;
170
171 /** Action groups are noncopyable. */
3809194b 172 APT_HIDDEN ActionGroup(const ActionGroup &other);
74a05226
MV
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 */
e8afd168 182 explicit ActionGroup(pkgDepCache &cache);
74a05226
MV
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 */
c8a4ce6c 195 virtual ~ActionGroup();
74a05226
MV
196 };
197
198 /** \brief Returns \b true for packages matching a regular
199 * expression in APT::NeverAutoRemove.
200 */
1f2933a8 201 class DefaultRootSetFunc : public InRootSetFunc, public Configuration::MatchAgainstConfig
74a05226 202 {
74a05226 203 public:
b093a199 204 DefaultRootSetFunc() : Configuration::MatchAgainstConfig("APT::NeverAutoRemove") {};
1f2933a8 205 virtual ~DefaultRootSetFunc() {};
74a05226 206
3b302846 207 bool InRootSet(const pkgCache::PkgIterator &pkg) APT_OVERRIDE { return pkg.end() == false && Match(pkg.Name()); };
74a05226
MV
208 };
209
6c139d6e
AL
210 struct StateCache
211 {
a38cec81 212 // text versions of the two version fields
6c139d6e
AL
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;
b2e465d6
AL
221
222 // Copy of Package::Flags
223 unsigned short Flags;
224 unsigned short iFlags; // Internal flags
6c139d6e 225
74a05226 226 /** \brief \b true if this package can be reached from the root set. */
0a57c0f0 227 bool Marked;
74a05226
MV
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 */
0a57c0f0 235 bool Garbage;
afb1e2e3 236
6c139d6e
AL
237 // Various tree indicators
238 signed char Status; // -1,0,1,2
239 unsigned char Mode; // ModeList
240 unsigned char DepState; // DepState Flags
241
6c139d6e 242 // Update of candidate version
a38cec81 243 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;
6c139d6e
AL
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;};
97be52d4 249 inline bool Purge() const {return Delete() == true && (iFlags & pkgDepCache::Purge) == pkgDepCache::Purge; };
6c139d6e 250 inline bool Keep() const {return Mode == ModeKeep;};
cbc702ea 251 inline bool Protect() const {return (iFlags & Protected) == Protected;};
6c139d6e 252 inline bool Upgrade() const {return Status > 0 && Mode == ModeInstall;};
32cc424b 253 inline bool Upgradable() const {return Status >= 1 && CandidateVer != NULL;};
6321777b 254 inline bool Downgrade() const {return Status < 0 && Mode == ModeInstall;};
6c139d6e
AL
255 inline bool Held() const {return Status != 0 && Keep();};
256 inline bool NowBroken() const {return (DepState & DepNowMin) != DepNowMin;};
4ef9a929 257 inline bool NowPolicyBroken() const {return (DepState & DepNowPolicy) != DepNowPolicy;};
6c139d6e 258 inline bool InstBroken() const {return (DepState & DepInstMin) != DepInstMin;};
60681f93 259 inline bool InstPolicyBroken() const {return (DepState & DepInstPolicy) != DepInstPolicy;};
6c139d6e 260 inline bool Install() const {return Mode == ModeInstall;};
97be52d4 261 inline bool ReInstall() const {return Delete() == false && (iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall;};
6c139d6e
AL
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);
fd23676e 270 void UpdateVerState(PkgIterator const &Pkg);
6c139d6e 271
b2e465d6
AL
272 // User Policy control
273 class Policy
274 {
275 public:
b20c1683
MV
276 Policy() {
277 InstallRecommends = _config->FindB("APT::Install-Recommends", false);
278 InstallSuggests = _config->FindB("APT::Install-Suggests", false);
279 }
280
e841200b 281 virtual VerIterator GetCandidateVer(PkgIterator const &Pkg);
fd23676e 282 virtual bool IsImportantDep(DepIterator const &Dep) const;
6d38011b 283 virtual signed short GetPriority(PkgIterator const &Pkg);
a0a66955 284 virtual signed short GetPriority(VerIterator const &Ver, bool ConsiderFiles=true);
6d38011b
DK
285 virtual signed short GetPriority(PkgFileIterator const &File);
286
b2e465d6 287 virtual ~Policy() {};
b20c1683
MV
288
289 private:
290 bool InstallRecommends;
291 bool InstallSuggests;
b2e465d6 292 };
74a05226
MV
293
294 private:
295 /** The number of open "action groups"; certain post-action
296 * operations are suppressed if this number is > 0.
297 */
298 int group_level;
299
300 friend class ActionGroup;
b2e465d6 301
6c139d6e
AL
302 protected:
303
304 // State information
b2e465d6 305 pkgCache *Cache;
6c139d6e
AL
306 StateCache *PkgState;
307 unsigned char *DepState;
a3c4c81a
DK
308
309 /** Stores the space changes after installation */
310 signed long long iUsrSize;
311 /** Stores how much we need to download to get the packages */
312 unsigned long long iDownloadSize;
a6568219
AL
313 unsigned long iInstCount;
314 unsigned long iDelCount;
315 unsigned long iKeepCount;
316 unsigned long iBrokenCount;
4ef9a929 317 unsigned long iPolicyBrokenCount;
a6568219 318 unsigned long iBadCount;
af29ffb4
MV
319
320 bool DebugMarker;
321 bool DebugAutoInstall;
322
b2e465d6
AL
323 Policy *delLocalPolicy; // For memory clean up..
324 Policy *LocalPolicy;
325
6c139d6e 326 // Check for a matching provides
fd23676e
DK
327 bool CheckDep(DepIterator const &Dep,int const Type,PkgIterator &Res);
328 inline bool CheckDep(DepIterator const &Dep,int const Type)
6c139d6e 329 {
b2e465d6 330 PkgIterator Res(*this,0);
6c139d6e 331 return CheckDep(Dep,Type,Res);
b2e465d6 332 }
6c139d6e
AL
333
334 // Computes state information for deps and versions (w/o storing)
fd23676e
DK
335 unsigned char DependencyState(DepIterator const &D);
336 unsigned char VersionState(DepIterator D,unsigned char const Check,
337 unsigned char const SetMin,
338 unsigned char const SetPolicy) const;
6c139d6e
AL
339
340 // Recalculates various portions of the cache, call after changing something
341 void Update(DepIterator Dep); // Mostly internal
342 void Update(PkgIterator const &P);
343
344 // Count manipulators
6935cd05 345 void AddSizes(const PkgIterator &Pkg, bool const Invert = false);
81305a0b 346 inline void RemoveSizes(const PkgIterator &Pkg) {AddSizes(Pkg, true);};
6935cd05
DK
347 void AddStates(const PkgIterator &Pkg, bool const Invert = false);
348 inline void RemoveStates(const PkgIterator &Pkg) {AddStates(Pkg,true);};
b2e465d6 349
6c139d6e
AL
350 public:
351
b2e465d6
AL
352 // Legacy.. We look like a pkgCache
353 inline operator pkgCache &() {return *Cache;};
354 inline Header &Head() {return *Cache->HeaderP;};
3d5a34b2 355 inline GrpIterator GrpBegin() {return Cache->GrpBegin();};
b2e465d6 356 inline PkgIterator PkgBegin() {return Cache->PkgBegin();};
8f3ba4e8
DK
357 inline GrpIterator FindGrp(std::string const &Name) {return Cache->FindGrp(Name);};
358 inline PkgIterator FindPkg(std::string const &Name) {return Cache->FindPkg(Name);};
359 inline PkgIterator FindPkg(std::string const &Name, std::string const &Arch) {return Cache->FindPkg(Name, Arch);};
b2e465d6
AL
360
361 inline pkgCache &GetCache() {return *Cache;};
362 inline pkgVersioningSystem &VS() {return *Cache->VS;};
294a8020 363
6c139d6e 364 // Policy implementation
3d284148 365 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);};
294a8020 366
fd23676e 367 inline bool IsImportantDep(DepIterator Dep) const {return LocalPolicy->IsImportantDep(Dep);};
b2e465d6
AL
368 inline Policy &GetPolicy() {return *LocalPolicy;};
369
6c139d6e
AL
370 // Accessors
371 inline StateCache &operator [](PkgIterator const &I) {return PkgState[I->ID];};
84573326 372 inline StateCache &operator [](PkgIterator const &I) const {return PkgState[I->ID];};
6c139d6e
AL
373 inline unsigned char &operator [](DepIterator const &I) {return DepState[I->ID];};
374
74a05226
MV
375 /** \return A function identifying packages in the root set other
376 * than manually installed packages and essential packages, or \b
377 * NULL if an error occurs.
378 *
379 * \todo Is this the best place for this function? Perhaps the
380 * settings for mark-and-sweep should be stored in a single
381 * external class?
382 */
383 virtual InRootSetFunc *GetRootSetFunc();
384
385 /** \return \b true if the garbage collector should follow recommendations.
386 */
387 virtual bool MarkFollowsRecommends();
388
389 /** \return \b true if the garbage collector should follow suggestions.
390 */
391 virtual bool MarkFollowsSuggests();
392
393 /** \brief Update the Marked and Garbage fields of all packages.
394 *
395 * This routine is implicitly invoked after all state manipulators
255c9e4b
DK
396 * and when an ActionGroup is destroyed. It invokes the private
397 * MarkRequired() and Sweep() to do its dirty work.
74a05226
MV
398 *
399 * \param rootFunc A predicate that returns \b true for packages
400 * that should be added to the root set.
401 */
107d054b
DK
402 bool MarkAndSweep(InRootSetFunc &rootFunc);
403 bool MarkAndSweep();
74a05226
MV
404
405 /** \name State Manipulators
406 */
407 // @{
3d619a20 408 bool MarkKeep(PkgIterator const &Pkg, bool Soft = false,
af29ffb4 409 bool FromUser = true, unsigned long Depth = 0);
c5ca2c52 410 bool MarkDelete(PkgIterator const &Pkg, bool MarkPurge = false,
6910a2ac 411 unsigned long Depth = 0, bool FromUser = true);
3d619a20 412 bool MarkInstall(PkgIterator const &Pkg,bool AutoInst = true,
7610bb3d
MV
413 unsigned long Depth = 0, bool FromUser = true,
414 bool ForceImportantDeps = false);
cc26da01 415 void MarkProtected(PkgIterator const &Pkg) { PkgState[Pkg->ID].iFlags |= Protected; };
2d403b92 416
d0c59649 417 void SetReInstall(PkgIterator const &Pkg,bool To);
294a8020
DK
418
419 /** @return 'the' candidate version of a package
420 *
421 * The version returned is the version previously set explicitly via
422 * SetCandidate* methods like #SetCandidateVersion or if there wasn't one
89497574 423 * set the version as chosen via #Policy.
294a8020
DK
424 *
425 * @param Pkg is the package to return the candidate for
426 */
427 pkgCache::VerIterator GetCandidateVersion(pkgCache::PkgIterator const &Pkg);
27e8981a 428 void SetCandidateVersion(VerIterator TargetVer);
2c085486
DK
429 bool SetCandidateRelease(pkgCache::VerIterator TargetVer,
430 std::string const &TargetRel);
431 /** Set the candidate version for dependencies too if needed.
432 *
433 * Sets not only the candidate version as SetCandidateVersion does,
434 * but walks also down the dependency tree and checks if it is required
435 * to set the candidate of the dependency to a version from the given
436 * release, too.
437 *
438 * \param TargetVer new candidate version of the package
439 * \param TargetRel try to switch to this release if needed
440 * \param[out] Changed a list of pairs consisting of the \b old
441 * version of the changed package and the version which
442 * required the switch of this dependency
443 * \return \b true if the switch was successful, \b false otherwise
444 */
445 bool SetCandidateRelease(pkgCache::VerIterator TargetVer,
446 std::string const &TargetRel,
447 std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > &Changed);
74a05226
MV
448
449 /** Set the "is automatically installed" flag of Pkg. */
450 void MarkAuto(const PkgIterator &Pkg, bool Auto);
451 // @}
6910a2ac
DK
452
453 /** \return \b true if it's OK for MarkInstall to install
454 * the given package.
455 *
486d190d
DK
456 * The default implementation simply calls all IsInstallOk*
457 * method mentioned below.
458 *
459 * Overriding implementations should use the hold-state-flag to
460 * cache results from previous checks of this package - if possible.
c3a85f49
DK
461 *
462 * The parameters are the same as in the calling MarkInstall:
6910a2ac 463 * \param Pkg the package that MarkInstall wants to install.
486d190d 464 * \param AutoInst install this and all its dependencies
6910a2ac
DK
465 * \param Depth recursive deep of this Marker call
466 * \param FromUser was the install requested by the user?
467 */
468 virtual bool IsInstallOk(const PkgIterator &Pkg,bool AutoInst = true,
469 unsigned long Depth = 0, bool FromUser = true);
470
471 /** \return \b true if it's OK for MarkDelete to remove
472 * the given package.
473 *
486d190d
DK
474 * The default implementation simply calls all IsDeleteOk*
475 * method mentioned below, see also #IsInstallOk.
c3a85f49
DK
476 *
477 * The parameters are the same as in the calling MarkDelete:
6910a2ac 478 * \param Pkg the package that MarkDelete wants to remove.
255c9e4b 479 * \param MarkPurge should we purge instead of "only" remove?
6910a2ac
DK
480 * \param Depth recursive deep of this Marker call
481 * \param FromUser was the remove requested by the user?
482 */
1842a17d 483 virtual bool IsDeleteOk(const PkgIterator &Pkg,bool MarkPurge = false,
6910a2ac
DK
484 unsigned long Depth = 0, bool FromUser = true);
485
a83d884d 486 // read persistent states
fd23676e
DK
487 bool readStateFile(OpProgress * const prog);
488 bool writeStateFile(OpProgress * const prog, bool const InstalledOnly=true);
e331f6ed 489
6c139d6e 490 // Size queries
a3c4c81a
DK
491 inline signed long long UsrSize() {return iUsrSize;};
492 inline unsigned long long DebSize() {return iDownloadSize;};
a6568219
AL
493 inline unsigned long DelCount() {return iDelCount;};
494 inline unsigned long KeepCount() {return iKeepCount;};
495 inline unsigned long InstCount() {return iInstCount;};
496 inline unsigned long BrokenCount() {return iBrokenCount;};
4ef9a929 497 inline unsigned long PolicyBrokenCount() {return iPolicyBrokenCount;};
a6568219 498 inline unsigned long BadCount() {return iBadCount;};
b2e465d6 499
fd23676e 500 bool Init(OpProgress * const Prog);
8b32e920 501 // Generate all state information
fd23676e 502 void Update(OpProgress * const Prog = 0);
8b32e920 503
fd23676e 504 pkgDepCache(pkgCache * const Cache,Policy * const Plcy = 0);
6c139d6e 505 virtual ~pkgDepCache();
8b32e920 506
486d190d
DK
507 protected:
508 // methods call by IsInstallOk
509 bool IsInstallOkMultiArchSameVersionSynced(PkgIterator const &Pkg,
510 bool const AutoInst, unsigned long const Depth, bool const FromUser);
21b3eac8
DK
511 bool IsInstallOkDependenciesSatisfiableByCandidates(PkgIterator const &Pkg,
512 bool const AutoInst, unsigned long const Depth, bool const FromUser);
486d190d
DK
513
514 // methods call by IsDeleteOk
515 bool IsDeleteOkProtectInstallRequests(PkgIterator const &Pkg,
516 bool const rPurge, unsigned long const Depth, bool const FromUser);
517
8b32e920 518 private:
6c55f07a 519 void * const d;
c8a4ce6c 520
3809194b 521 APT_HIDDEN bool IsModeChangeOk(ModeList const mode, PkgIterator const &Pkg,
dde0c674 522 unsigned long const Depth, bool const FromUser);
6c139d6e
AL
523};
524
525#endif