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