]> git.saurik.com Git - apt.git/blame - apt-pkg/depcache.h
Merge remote-tracking branch 'mvo/feature/hash-stats' into debian/experimental
[apt.git] / apt-pkg / depcache.h
CommitLineData
74a05226 1// -*- mode: c++; mode: fold -*-
6c139d6e 2// Description /*{{{*/
b2e465d6 3// $Id: depcache.h,v 1.14 2001/02/20 07:03:17 jgg Exp $
6c139d6e
AL
4/* ######################################################################
5
6 DepCache - Dependency Extension data for the cache
7
8 This class stores the cache data and a set of extension structures for
9 monitoring the current state of all the packages. It also generates and
10 caches the 'install' state of many things. This refers to the state of the
11 package after an install has been run.
12
13 The StateCache::State field can be -1,0,1,2 which is <,=,>,no current.
14 StateCache::Mode is which of the 3 fields is active.
15
16 This structure is important to support the readonly status of the cache
17 file. When the data is saved the cache will be refereshed from our
1e3f4083 18 internal rep and written to disk. Then the actual persistent data
6c139d6e
AL
19 files will be put on the disk.
20
21 Each dependency is compared against 3 target versions to produce to
22 3 dependency results.
23 Now - Compared using the Currently install version
24 Install - Compared using the install version (final state)
25 CVer - (Candidate Verion) Compared using the Candidate Version
26 The candidate and now results are used to decide wheather a package
27 should be automatically installed or if it should be left alone.
28
29 Remember, the Candidate Version is selected based on the distribution
30 settings for the Package. The Install Version is selected based on the
31 state (Delete, Keep, Install) field and can be either the Current Version
32 or the Candidate version.
33
34 The Candidate version is what is shown the 'Install Version' field.
35
36 ##################################################################### */
37 /*}}}*/
6c139d6e
AL
38#ifndef PKGLIB_DEPCACHE_H
39#define PKGLIB_DEPCACHE_H
40
1f2933a8 41#include <apt-pkg/configuration.h>
094a497d 42#include <apt-pkg/pkgcache.h>
453b82a3 43#include <apt-pkg/cacheiterators.h>
a02db58f 44#include <apt-pkg/macros.h>
453b82a3
DK
45
46#include <stddef.h>
74a05226 47
a7955daa 48#include <memory>
472ff00e 49#include <list>
453b82a3
DK
50#include <string>
51#include <utility>
472ff00e 52
b9dadc24
DK
53#ifndef APT_8_CLEANER_HEADERS
54#include <apt-pkg/progress.h>
55#include <apt-pkg/error.h>
56#endif
453b82a3
DK
57#ifndef APT_10_CLEANER_HEADERS
58#include <set>
59#include <vector>
60#endif
b9dadc24 61
472ff00e 62class OpProgress;
453b82a3 63class pkgVersioningSystem;
74a05226 64
b2e465d6 65class pkgDepCache : protected pkgCache::Namespace
6c139d6e
AL
66{
67 public:
74a05226
MV
68
69 /** \brief An arbitrary predicate on packages. */
70 class InRootSetFunc
71 {
72 public:
65512241 73 virtual bool InRootSet(const pkgCache::PkgIterator &/*pkg*/) {return false;};
74a05226
MV
74 virtual ~InRootSetFunc() {};
75 };
76
77 private:
78 /** \brief Mark a single package and all its unmarked important
79 * dependencies during mark-and-sweep.
80 *
81 * Recursively invokes itself to mark all dependencies of the
82 * package.
83 *
84 * \param pkg The package to mark.
85 *
86 * \param ver The version of the package that is to be marked.
87 *
88 * \param follow_recommends If \b true, recommendations of the
89 * package will be recursively marked.
90 *
91 * \param follow_suggests If \b true, suggestions of the package
92 * will be recursively marked.
93 */
94 void MarkPackage(const pkgCache::PkgIterator &pkg,
95 const pkgCache::VerIterator &ver,
e0b94b97
DK
96 bool const &follow_recommends,
97 bool const &follow_suggests);
74a05226
MV
98
99 /** \brief Update the Marked field of all packages.
100 *
101 * Each package's StateCache::Marked field will be set to \b true
102 * if and only if it can be reached from the root set. By
103 * default, the root set consists of the set of manually installed
104 * or essential packages, but it can be extended using the
105 * parameter #rootFunc.
106 *
107 * \param rootFunc A callback that can be used to add extra
108 * packages to the root set.
109 *
38965a34 110 * \return \b false if an error occurred.
74a05226
MV
111 */
112 bool MarkRequired(InRootSetFunc &rootFunc);
113
114 /** \brief Set the StateCache::Garbage flag on all packages that
115 * should be removed.
116 *
117 * Packages that were not marked by the last call to #MarkRequired
118 * are tested to see whether they are actually garbage. If so,
119 * they are marked as such.
120 *
38965a34 121 * \return \b false if an error occurred.
74a05226
MV
122 */
123 bool Sweep();
124
125 public:
6c139d6e
AL
126
127 // These flags are used in DepState
128 enum DepFlags {DepNow = (1 << 0),DepInstall = (1 << 1),DepCVer = (1 << 2),
129 DepGNow = (1 << 3),DepGInstall = (1 << 4),DepGCVer = (1 << 5)};
130
131 // These flags are used in StateCache::DepState
132 enum DepStateFlags {DepNowPolicy = (1 << 0), DepNowMin = (1 << 1),
133 DepInstPolicy = (1 << 2), DepInstMin = (1 << 3),
134 DepCandPolicy = (1 << 4), DepCandMin = (1 << 5)};
135
136 // These flags are used in StateCache::iFlags
cc26da01 137 enum InternalFlags {AutoKept = (1 << 0), Purge = (1 << 1), ReInstall = (1 << 2), Protected = (1 << 3)};
6c139d6e
AL
138
139 enum VersionTypes {NowVersion, InstallVersion, CandidateVersion};
8fa042ca 140 enum ModeList {ModeDelete = 0, ModeKeep = 1, ModeInstall = 2, ModeGarbage = 3};
0a57c0f0 141
74a05226
MV
142 /** \brief Represents an active action group.
143 *
144 * An action group is a group of actions that are currently being
145 * performed. While an active group is active, certain routine
146 * clean-up actions that would normally be performed after every
147 * cache operation are delayed until the action group is
148 * completed. This is necessary primarily to avoid inefficiencies
149 * when modifying a large number of packages at once.
150 *
151 * This class represents an active action group. Creating an
152 * instance will create an action group; destroying one will
153 * destroy the corresponding action group.
154 *
155 * The following operations are suppressed by this class:
156 *
157 * - Keeping the Marked and Garbage flags up to date.
158 *
159 * \note This can be used in the future to easily accumulate
160 * atomic actions for undo or to display "what apt did anyway";
161 * e.g., change the counter of how many action groups are active
162 * to a std::set of pointers to them and use those to store
163 * information about what happened in a group in the group.
164 */
165 class ActionGroup
166 {
167 pkgDepCache &cache;
168
169 bool released;
170
171 /** Action groups are noncopyable. */
172 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 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 ~ActionGroup();
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
01a6e24c 207 bool InRootSet(const pkgCache::PkgIterator &pkg) { return pkg.end() == false && Match(pkg.Name()); };
74a05226
MV
208 };
209
6c139d6e
AL
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;
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
a02db58f 243 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;};
0a8e3465 253 inline bool Upgradable() const {return Status >= 1;};
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);
270 void UpdateVerState(PkgIterator Pkg);
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
DK
281 virtual VerIterator GetCandidateVer(PkgIterator const &Pkg);
282 virtual bool IsImportantDep(DepIterator const &Dep);
6d38011b
DK
283 virtual signed short GetPriority(PkgIterator const &Pkg);
284 virtual signed short GetPriority(PkgFileIterator const &File);
285
b2e465d6 286 virtual ~Policy() {};
b20c1683
MV
287
288 private:
289 bool InstallRecommends;
290 bool InstallSuggests;
b2e465d6 291 };
74a05226
MV
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;
b2e465d6 300
6c139d6e
AL
301 protected:
302
303 // State information
b2e465d6 304 pkgCache *Cache;
6c139d6e
AL
305 StateCache *PkgState;
306 unsigned char *DepState;
a3c4c81a
DK
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;
a6568219
AL
312 unsigned long iInstCount;
313 unsigned long iDelCount;
314 unsigned long iKeepCount;
315 unsigned long iBrokenCount;
4ef9a929 316 unsigned long iPolicyBrokenCount;
a6568219 317 unsigned long iBadCount;
af29ffb4
MV
318
319 bool DebugMarker;
320 bool DebugAutoInstall;
321
b2e465d6
AL
322 Policy *delLocalPolicy; // For memory clean up..
323 Policy *LocalPolicy;
324
6c139d6e
AL
325 // Check for a matching provides
326 bool CheckDep(DepIterator Dep,int Type,PkgIterator &Res);
327 inline bool CheckDep(DepIterator Dep,int Type)
328 {
b2e465d6 329 PkgIterator Res(*this,0);
6c139d6e 330 return CheckDep(Dep,Type,Res);
b2e465d6 331 }
6c139d6e
AL
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
6935cd05 344 void AddSizes(const PkgIterator &Pkg, bool const Invert = false);
81305a0b 345 inline void RemoveSizes(const PkgIterator &Pkg) {AddSizes(Pkg, true);};
6935cd05
DK
346 void AddStates(const PkgIterator &Pkg, bool const Invert = false);
347 inline void RemoveStates(const PkgIterator &Pkg) {AddStates(Pkg,true);};
b2e465d6 348
6c139d6e
AL
349 public:
350
b2e465d6
AL
351 // Legacy.. We look like a pkgCache
352 inline operator pkgCache &() {return *Cache;};
353 inline Header &Head() {return *Cache->HeaderP;};
3d5a34b2 354 inline GrpIterator GrpBegin() {return Cache->GrpBegin();};
b2e465d6 355 inline PkgIterator PkgBegin() {return Cache->PkgBegin();};
8f3ba4e8
DK
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);};
b2e465d6
AL
359
360 inline pkgCache &GetCache() {return *Cache;};
361 inline pkgVersioningSystem &VS() {return *Cache->VS;};
362
6c139d6e 363 // Policy implementation
e841200b 364 inline VerIterator GetCandidateVer(PkgIterator const &Pkg) {return LocalPolicy->GetCandidateVer(Pkg);};
b2e465d6
AL
365 inline bool IsImportantDep(DepIterator Dep) {return LocalPolicy->IsImportantDep(Dep);};
366 inline Policy &GetPolicy() {return *LocalPolicy;};
367
6c139d6e
AL
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
74a05226
MV
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
255c9e4b
DK
393 * and when an ActionGroup is destroyed. It invokes the private
394 * MarkRequired() and Sweep() to do its dirty work.
74a05226
MV
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 {
401 return MarkRequired(rootFunc) && Sweep();
402 }
403
404 bool MarkAndSweep()
405 {
406 std::auto_ptr<InRootSetFunc> f(GetRootSetFunc());
407 if(f.get() != NULL)
408 return MarkAndSweep(*f.get());
409 else
410 return false;
411 }
412
413 /** \name State Manipulators
414 */
415 // @{
3d619a20 416 bool MarkKeep(PkgIterator const &Pkg, bool Soft = false,
af29ffb4 417 bool FromUser = true, unsigned long Depth = 0);
c5ca2c52 418 bool MarkDelete(PkgIterator const &Pkg, bool MarkPurge = false,
6910a2ac 419 unsigned long Depth = 0, bool FromUser = true);
3d619a20 420 bool MarkInstall(PkgIterator const &Pkg,bool AutoInst = true,
7610bb3d
MV
421 unsigned long Depth = 0, bool FromUser = true,
422 bool ForceImportantDeps = false);
cc26da01 423 void MarkProtected(PkgIterator const &Pkg) { PkgState[Pkg->ID].iFlags |= Protected; };
2d403b92 424
d0c59649 425 void SetReInstall(PkgIterator const &Pkg,bool To);
27e8981a 426 void SetCandidateVersion(VerIterator TargetVer);
2c085486
DK
427 bool SetCandidateRelease(pkgCache::VerIterator TargetVer,
428 std::string const &TargetRel);
429 /** Set the candidate version for dependencies too if needed.
430 *
431 * Sets not only the candidate version as SetCandidateVersion does,
432 * but walks also down the dependency tree and checks if it is required
433 * to set the candidate of the dependency to a version from the given
434 * release, too.
435 *
436 * \param TargetVer new candidate version of the package
437 * \param TargetRel try to switch to this release if needed
438 * \param[out] Changed a list of pairs consisting of the \b old
439 * version of the changed package and the version which
440 * required the switch of this dependency
441 * \return \b true if the switch was successful, \b false otherwise
442 */
443 bool SetCandidateRelease(pkgCache::VerIterator TargetVer,
444 std::string const &TargetRel,
445 std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > &Changed);
74a05226
MV
446
447 /** Set the "is automatically installed" flag of Pkg. */
448 void MarkAuto(const PkgIterator &Pkg, bool Auto);
449 // @}
6910a2ac
DK
450
451 /** \return \b true if it's OK for MarkInstall to install
452 * the given package.
453 *
486d190d
DK
454 * The default implementation simply calls all IsInstallOk*
455 * method mentioned below.
456 *
457 * Overriding implementations should use the hold-state-flag to
458 * cache results from previous checks of this package - if possible.
c3a85f49
DK
459 *
460 * The parameters are the same as in the calling MarkInstall:
6910a2ac 461 * \param Pkg the package that MarkInstall wants to install.
486d190d 462 * \param AutoInst install this and all its dependencies
6910a2ac
DK
463 * \param Depth recursive deep of this Marker call
464 * \param FromUser was the install requested by the user?
465 */
466 virtual bool IsInstallOk(const PkgIterator &Pkg,bool AutoInst = true,
467 unsigned long Depth = 0, bool FromUser = true);
468
469 /** \return \b true if it's OK for MarkDelete to remove
470 * the given package.
471 *
486d190d
DK
472 * The default implementation simply calls all IsDeleteOk*
473 * method mentioned below, see also #IsInstallOk.
c3a85f49
DK
474 *
475 * The parameters are the same as in the calling MarkDelete:
6910a2ac 476 * \param Pkg the package that MarkDelete wants to remove.
255c9e4b 477 * \param MarkPurge should we purge instead of "only" remove?
6910a2ac
DK
478 * \param Depth recursive deep of this Marker call
479 * \param FromUser was the remove requested by the user?
480 */
1842a17d 481 virtual bool IsDeleteOk(const PkgIterator &Pkg,bool MarkPurge = false,
6910a2ac
DK
482 unsigned long Depth = 0, bool FromUser = true);
483
a83d884d
MV
484 // read persistent states
485 bool readStateFile(OpProgress *prog);
c176c4d0 486 bool writeStateFile(OpProgress *prog, bool InstalledOnly=true);
e331f6ed 487
6c139d6e 488 // Size queries
a3c4c81a
DK
489 inline signed long long UsrSize() {return iUsrSize;};
490 inline unsigned long long DebSize() {return iDownloadSize;};
a6568219
AL
491 inline unsigned long DelCount() {return iDelCount;};
492 inline unsigned long KeepCount() {return iKeepCount;};
493 inline unsigned long InstCount() {return iInstCount;};
494 inline unsigned long BrokenCount() {return iBrokenCount;};
4ef9a929 495 inline unsigned long PolicyBrokenCount() {return iPolicyBrokenCount;};
a6568219 496 inline unsigned long BadCount() {return iBadCount;};
b2e465d6
AL
497
498 bool Init(OpProgress *Prog);
8b32e920
DK
499 // Generate all state information
500 void Update(OpProgress *Prog = 0);
501
b2e465d6 502 pkgDepCache(pkgCache *Cache,Policy *Plcy = 0);
6c139d6e 503 virtual ~pkgDepCache();
8b32e920 504
486d190d
DK
505 protected:
506 // methods call by IsInstallOk
507 bool IsInstallOkMultiArchSameVersionSynced(PkgIterator const &Pkg,
508 bool const AutoInst, unsigned long const Depth, bool const FromUser);
21b3eac8
DK
509 bool IsInstallOkDependenciesSatisfiableByCandidates(PkgIterator const &Pkg,
510 bool const AutoInst, unsigned long const Depth, bool const FromUser);
486d190d
DK
511
512 // methods call by IsDeleteOk
513 bool IsDeleteOkProtectInstallRequests(PkgIterator const &Pkg,
514 bool const rPurge, unsigned long const Depth, bool const FromUser);
515
8b32e920 516 private:
dde0c674
DK
517 bool IsModeChangeOk(ModeList const mode, PkgIterator const &Pkg,
518 unsigned long const Depth, bool const FromUser);
6c139d6e
AL
519};
520
521#endif