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