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