]>
git.saurik.com Git - apt.git/blob - apt-pkg/depcache.h
1 // -*- mode: c++; mode: fold -*-
3 // $Id: depcache.h,v 1.14 2001/02/20 07:03:17 jgg Exp $
4 /* ######################################################################
6 DepCache - Dependency Extension data for the cache
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.
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.
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 persistent data
19 files will be put on the disk.
21 Each dependency is compared against 3 target versions to produce to
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.
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.
34 The Candidate version is what is shown the 'Install Version' field.
36 ##################################################################### */
38 #ifndef PKGLIB_DEPCACHE_H
39 #define PKGLIB_DEPCACHE_H
41 #include <apt-pkg/configuration.h>
42 #include <apt-pkg/pkgcache.h>
43 #include <apt-pkg/cacheiterators.h>
52 #ifndef APT_8_CLEANER_HEADERS
53 #include <apt-pkg/progress.h>
54 #include <apt-pkg/error.h>
56 #ifndef APT_10_CLEANER_HEADERS
62 class pkgVersioningSystem
;
64 class pkgDepCache
: protected pkgCache::Namespace
68 /** \brief An arbitrary predicate on packages. */
72 virtual bool InRootSet(const pkgCache::PkgIterator
&/*pkg*/) {return false;};
73 virtual ~InRootSetFunc() {};
77 /** \brief Mark a single package and all its unmarked important
78 * dependencies during mark-and-sweep.
80 * Recursively invokes itself to mark all dependencies of the
83 * \param pkg The package to mark.
85 * \param ver The version of the package that is to be marked.
87 * \param follow_recommends If \b true, recommendations of the
88 * package will be recursively marked.
90 * \param follow_suggests If \b true, suggestions of the package
91 * will be recursively marked.
93 void MarkPackage(const pkgCache::PkgIterator
&pkg
,
94 const pkgCache::VerIterator
&ver
,
95 bool const &follow_recommends
,
96 bool const &follow_suggests
);
98 /** \brief Update the Marked field of all packages.
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.
106 * \param rootFunc A callback that can be used to add extra
107 * packages to the root set.
109 * \return \b false if an error occurred.
111 bool MarkRequired(InRootSetFunc
&rootFunc
);
113 /** \brief Set the StateCache::Garbage flag on all packages that
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.
120 * \return \b false if an error occurred.
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)};
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)};
135 // These flags are used in StateCache::iFlags
136 enum InternalFlags
{AutoKept
= (1 << 0), Purge
= (1 << 1), ReInstall
= (1 << 2), Protected
= (1 << 3)};
138 enum VersionTypes
{NowVersion
, InstallVersion
, CandidateVersion
};
139 enum ModeList
{ModeDelete
= 0, ModeKeep
= 1, ModeInstall
= 2, ModeGarbage
= 3};
141 /** \brief Represents an active action group.
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.
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.
154 * The following operations are suppressed by this class:
156 * - Keeping the Marked and Garbage flags up to date.
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.
170 /** Action groups are noncopyable. */
171 ActionGroup(const ActionGroup
&other
);
173 /** \brief Create a new ActionGroup.
175 * \param cache The cache that this ActionGroup should
178 * As long as this object exists, no automatic cleanup
179 * operations will be undertaken.
181 ActionGroup(pkgDepCache
&cache
);
183 /** \brief Clean up the action group before it is destroyed.
185 * If it is destroyed later, no second cleanup wil be run.
189 /** \brief Destroy the action group.
191 * If this is the last action group, the automatic cache
192 * cleanup operations will be undertaken.
197 /** \brief Returns \b true for packages matching a regular
198 * expression in APT::NeverAutoRemove.
200 class DefaultRootSetFunc
: public InRootSetFunc
, public Configuration::MatchAgainstConfig
203 DefaultRootSetFunc() : Configuration::MatchAgainstConfig("APT::NeverAutoRemove") {};
204 virtual ~DefaultRootSetFunc() {};
206 bool InRootSet(const pkgCache::PkgIterator
&pkg
) { return pkg
.end() == false && Match(pkg
.Name()); };
211 // Epoch stripped text versions of the two version fields
212 const char *CandVersion
;
213 const char *CurVersion
;
215 // Pointer to the candidate install version.
216 Version
*CandidateVer
;
218 // Pointer to the install version.
221 // Copy of Package::Flags
222 unsigned short Flags
;
223 unsigned short iFlags
; // Internal flags
225 /** \brief \b true if this package can be reached from the root set. */
228 /** \brief \b true if this package is unused and should be removed.
230 * This differs from !#Marked, because it is possible that some
231 * unreachable packages will be protected from becoming
236 // Various tree indicators
237 signed char Status
; // -1,0,1,2
238 unsigned char Mode
; // ModeList
239 unsigned char DepState
; // DepState Flags
241 // Update of candidate version
242 const char *StripEpoch(const char *Ver
);
243 void Update(PkgIterator Pkg
,pkgCache
&Cache
);
245 // Various test members for the current status of the package
246 inline bool NewInstall() const {return Status
== 2 && Mode
== ModeInstall
;};
247 inline bool Delete() const {return Mode
== ModeDelete
;};
248 inline bool Purge() const {return Delete() == true && (iFlags
& pkgDepCache::Purge
) == pkgDepCache::Purge
; };
249 inline bool Keep() const {return Mode
== ModeKeep
;};
250 inline bool Protect() const {return (iFlags
& Protected
) == Protected
;};
251 inline bool Upgrade() const {return Status
> 0 && Mode
== ModeInstall
;};
252 inline bool Upgradable() const {return Status
>= 1;};
253 inline bool Downgrade() const {return Status
< 0 && Mode
== ModeInstall
;};
254 inline bool Held() const {return Status
!= 0 && Keep();};
255 inline bool NowBroken() const {return (DepState
& DepNowMin
) != DepNowMin
;};
256 inline bool NowPolicyBroken() const {return (DepState
& DepNowPolicy
) != DepNowPolicy
;};
257 inline bool InstBroken() const {return (DepState
& DepInstMin
) != DepInstMin
;};
258 inline bool InstPolicyBroken() const {return (DepState
& DepInstPolicy
) != DepInstPolicy
;};
259 inline bool Install() const {return Mode
== ModeInstall
;};
260 inline bool ReInstall() const {return Delete() == false && (iFlags
& pkgDepCache::ReInstall
) == pkgDepCache::ReInstall
;};
261 inline VerIterator
InstVerIter(pkgCache
&Cache
)
262 {return VerIterator(Cache
,InstallVer
);};
263 inline VerIterator
CandidateVerIter(pkgCache
&Cache
)
264 {return VerIterator(Cache
,CandidateVer
);};
268 void BuildGroupOrs(VerIterator
const &V
);
269 void UpdateVerState(PkgIterator Pkg
);
271 // User Policy control
276 InstallRecommends
= _config
->FindB("APT::Install-Recommends", false);
277 InstallSuggests
= _config
->FindB("APT::Install-Suggests", false);
280 virtual VerIterator
GetCandidateVer(PkgIterator
const &Pkg
);
281 virtual bool IsImportantDep(DepIterator
const &Dep
);
282 virtual signed short GetPriority(PkgIterator
const &Pkg
);
283 virtual signed short GetPriority(PkgFileIterator
const &File
);
285 virtual ~Policy() {};
288 bool InstallRecommends
;
289 bool InstallSuggests
;
293 /** The number of open "action groups"; certain post-action
294 * operations are suppressed if this number is > 0.
298 friend class ActionGroup
;
304 StateCache
*PkgState
;
305 unsigned char *DepState
;
307 /** Stores the space changes after installation */
308 signed long long iUsrSize
;
309 /** Stores how much we need to download to get the packages */
310 unsigned long long iDownloadSize
;
311 unsigned long iInstCount
;
312 unsigned long iDelCount
;
313 unsigned long iKeepCount
;
314 unsigned long iBrokenCount
;
315 unsigned long iPolicyBrokenCount
;
316 unsigned long iBadCount
;
319 bool DebugAutoInstall
;
321 Policy
*delLocalPolicy
; // For memory clean up..
324 // Check for a matching provides
325 bool CheckDep(DepIterator Dep
,int Type
,PkgIterator
&Res
);
326 inline bool CheckDep(DepIterator Dep
,int Type
)
328 PkgIterator
Res(*this,0);
329 return CheckDep(Dep
,Type
,Res
);
332 // Computes state information for deps and versions (w/o storing)
333 unsigned char DependencyState(DepIterator
&D
);
334 unsigned char VersionState(DepIterator D
,unsigned char Check
,
335 unsigned char SetMin
,
336 unsigned char SetPolicy
);
338 // Recalculates various portions of the cache, call after changing something
339 void Update(DepIterator Dep
); // Mostly internal
340 void Update(PkgIterator
const &P
);
342 // Count manipulators
343 void AddSizes(const PkgIterator
&Pkg
, bool const Invert
= false);
344 inline void RemoveSizes(const PkgIterator
&Pkg
) {AddSizes(Pkg
, true);};
345 void AddStates(const PkgIterator
&Pkg
, bool const Invert
= false);
346 inline void RemoveStates(const PkgIterator
&Pkg
) {AddStates(Pkg
,true);};
350 // Legacy.. We look like a pkgCache
351 inline operator pkgCache
&() {return *Cache
;};
352 inline Header
&Head() {return *Cache
->HeaderP
;};
353 inline GrpIterator
GrpBegin() {return Cache
->GrpBegin();};
354 inline PkgIterator
PkgBegin() {return Cache
->PkgBegin();};
355 inline GrpIterator
FindGrp(std::string
const &Name
) {return Cache
->FindGrp(Name
);};
356 inline PkgIterator
FindPkg(std::string
const &Name
) {return Cache
->FindPkg(Name
);};
357 inline PkgIterator
FindPkg(std::string
const &Name
, std::string
const &Arch
) {return Cache
->FindPkg(Name
, Arch
);};
359 inline pkgCache
&GetCache() {return *Cache
;};
360 inline pkgVersioningSystem
&VS() {return *Cache
->VS
;};
362 // Policy implementation
363 inline VerIterator
GetCandidateVer(PkgIterator
const &Pkg
) {return LocalPolicy
->GetCandidateVer(Pkg
);};
364 inline bool IsImportantDep(DepIterator Dep
) {return LocalPolicy
->IsImportantDep(Dep
);};
365 inline Policy
&GetPolicy() {return *LocalPolicy
;};
368 inline StateCache
&operator [](PkgIterator
const &I
) {return PkgState
[I
->ID
];};
369 inline unsigned char &operator [](DepIterator
const &I
) {return DepState
[I
->ID
];};
371 /** \return A function identifying packages in the root set other
372 * than manually installed packages and essential packages, or \b
373 * NULL if an error occurs.
375 * \todo Is this the best place for this function? Perhaps the
376 * settings for mark-and-sweep should be stored in a single
379 virtual InRootSetFunc
*GetRootSetFunc();
381 /** \return \b true if the garbage collector should follow recommendations.
383 virtual bool MarkFollowsRecommends();
385 /** \return \b true if the garbage collector should follow suggestions.
387 virtual bool MarkFollowsSuggests();
389 /** \brief Update the Marked and Garbage fields of all packages.
391 * This routine is implicitly invoked after all state manipulators
392 * and when an ActionGroup is destroyed. It invokes the private
393 * MarkRequired() and Sweep() to do its dirty work.
395 * \param rootFunc A predicate that returns \b true for packages
396 * that should be added to the root set.
398 bool MarkAndSweep(InRootSetFunc
&rootFunc
)
400 return MarkRequired(rootFunc
) && Sweep();
405 std::auto_ptr
<InRootSetFunc
> f(GetRootSetFunc());
407 return MarkAndSweep(*f
.get());
412 /** \name State Manipulators
415 bool MarkKeep(PkgIterator
const &Pkg
, bool Soft
= false,
416 bool FromUser
= true, unsigned long Depth
= 0);
417 bool MarkDelete(PkgIterator
const &Pkg
, bool MarkPurge
= false,
418 unsigned long Depth
= 0, bool FromUser
= true);
419 bool MarkInstall(PkgIterator
const &Pkg
,bool AutoInst
= true,
420 unsigned long Depth
= 0, bool FromUser
= true,
421 bool ForceImportantDeps
= false);
422 void MarkProtected(PkgIterator
const &Pkg
) { PkgState
[Pkg
->ID
].iFlags
|= Protected
; };
424 void SetReInstall(PkgIterator
const &Pkg
,bool To
);
425 void SetCandidateVersion(VerIterator TargetVer
);
426 bool SetCandidateRelease(pkgCache::VerIterator TargetVer
,
427 std::string
const &TargetRel
);
428 /** Set the candidate version for dependencies too if needed.
430 * Sets not only the candidate version as SetCandidateVersion does,
431 * but walks also down the dependency tree and checks if it is required
432 * to set the candidate of the dependency to a version from the given
435 * \param TargetVer new candidate version of the package
436 * \param TargetRel try to switch to this release if needed
437 * \param[out] Changed a list of pairs consisting of the \b old
438 * version of the changed package and the version which
439 * required the switch of this dependency
440 * \return \b true if the switch was successful, \b false otherwise
442 bool SetCandidateRelease(pkgCache::VerIterator TargetVer
,
443 std::string
const &TargetRel
,
444 std::list
<std::pair
<pkgCache::VerIterator
, pkgCache::VerIterator
> > &Changed
);
446 /** Set the "is automatically installed" flag of Pkg. */
447 void MarkAuto(const PkgIterator
&Pkg
, bool Auto
);
450 /** \return \b true if it's OK for MarkInstall to install
453 * The default implementation simply calls all IsInstallOk*
454 * method mentioned below.
456 * Overriding implementations should use the hold-state-flag to
457 * cache results from previous checks of this package - if possible.
459 * The parameters are the same as in the calling MarkInstall:
460 * \param Pkg the package that MarkInstall wants to install.
461 * \param AutoInst install this and all its dependencies
462 * \param Depth recursive deep of this Marker call
463 * \param FromUser was the install requested by the user?
465 virtual bool IsInstallOk(const PkgIterator
&Pkg
,bool AutoInst
= true,
466 unsigned long Depth
= 0, bool FromUser
= true);
468 /** \return \b true if it's OK for MarkDelete to remove
471 * The default implementation simply calls all IsDeleteOk*
472 * method mentioned below, see also #IsInstallOk.
474 * The parameters are the same as in the calling MarkDelete:
475 * \param Pkg the package that MarkDelete wants to remove.
476 * \param MarkPurge should we purge instead of "only" remove?
477 * \param Depth recursive deep of this Marker call
478 * \param FromUser was the remove requested by the user?
480 virtual bool IsDeleteOk(const PkgIterator
&Pkg
,bool MarkPurge
= false,
481 unsigned long Depth
= 0, bool FromUser
= true);
483 // read persistent states
484 bool readStateFile(OpProgress
*prog
);
485 bool writeStateFile(OpProgress
*prog
, bool InstalledOnly
=true);
488 inline signed long long UsrSize() {return iUsrSize
;};
489 inline unsigned long long DebSize() {return iDownloadSize
;};
490 inline unsigned long DelCount() {return iDelCount
;};
491 inline unsigned long KeepCount() {return iKeepCount
;};
492 inline unsigned long InstCount() {return iInstCount
;};
493 inline unsigned long BrokenCount() {return iBrokenCount
;};
494 inline unsigned long PolicyBrokenCount() {return iPolicyBrokenCount
;};
495 inline unsigned long BadCount() {return iBadCount
;};
497 bool Init(OpProgress
*Prog
);
498 // Generate all state information
499 void Update(OpProgress
*Prog
= 0);
501 pkgDepCache(pkgCache
*Cache
,Policy
*Plcy
= 0);
502 virtual ~pkgDepCache();
505 // methods call by IsInstallOk
506 bool IsInstallOkMultiArchSameVersionSynced(PkgIterator
const &Pkg
,
507 bool const AutoInst
, unsigned long const Depth
, bool const FromUser
);
509 // methods call by IsDeleteOk
510 bool IsDeleteOkProtectInstallRequests(PkgIterator
const &Pkg
,
511 bool const rPurge
, unsigned long const Depth
, bool const FromUser
);
514 bool IsModeChangeOk(ModeList
const mode
, PkgIterator
const &Pkg
,
515 unsigned long const Depth
, bool const FromUser
);