]>
git.saurik.com Git - apt.git/blob - apt-pkg/depcache.h
08e68355814d1ebd66d03bc7257a531dc82d34c1
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 persistant 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/progress.h>
44 #include <apt-pkg/error.h>
50 class pkgDepCache
: protected pkgCache::Namespace
54 /** \brief An arbitrary predicate on packages. */
58 virtual bool InRootSet(const pkgCache::PkgIterator
&pkg
) {return false;};
59 virtual ~InRootSetFunc() {};
63 /** \brief Mark a single package and all its unmarked important
64 * dependencies during mark-and-sweep.
66 * Recursively invokes itself to mark all dependencies of the
69 * \param pkg The package to mark.
71 * \param ver The version of the package that is to be marked.
73 * \param follow_recommends If \b true, recommendations of the
74 * package will be recursively marked.
76 * \param follow_suggests If \b true, suggestions of the package
77 * will be recursively marked.
79 void MarkPackage(const pkgCache::PkgIterator
&pkg
,
80 const pkgCache::VerIterator
&ver
,
81 bool const &follow_recommends
,
82 bool const &follow_suggests
);
84 /** \brief Update the Marked field of all packages.
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.
92 * \param rootFunc A callback that can be used to add extra
93 * packages to the root set.
95 * \return \b false if an error occurred.
97 bool MarkRequired(InRootSetFunc
&rootFunc
);
99 /** \brief Set the StateCache::Garbage flag on all packages that
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.
106 * \return \b false if an error occurred.
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)};
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)};
121 // These flags are used in StateCache::iFlags
122 enum InternalFlags
{AutoKept
= (1 << 0), Purge
= (1 << 1), ReInstall
= (1 << 2)};
124 enum VersionTypes
{NowVersion
, InstallVersion
, CandidateVersion
};
125 enum ModeList
{ModeDelete
= 0, ModeKeep
= 1, ModeInstall
= 2};
127 /** \brief Represents an active action group.
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.
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.
140 * The following operations are suppressed by this class:
142 * - Keeping the Marked and Garbage flags up to date.
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.
156 /** Action groups are noncopyable. */
157 ActionGroup(const ActionGroup
&other
);
159 /** \brief Create a new ActionGroup.
161 * \param cache The cache that this ActionGroup should
164 * As long as this object exists, no automatic cleanup
165 * operations will be undertaken.
167 ActionGroup(pkgDepCache
&cache
);
169 /** \brief Clean up the action group before it is destroyed.
171 * If it is destroyed later, no second cleanup wil be run.
175 /** \brief Destroy the action group.
177 * If this is the last action group, the automatic cache
178 * cleanup operations will be undertaken.
183 /** \brief Returns \b true for packages matching a regular
184 * expression in APT::NeverAutoRemove.
186 class DefaultRootSetFunc
: public InRootSetFunc
, public Configuration::MatchAgainstConfig
189 DefaultRootSetFunc() : Configuration::MatchAgainstConfig("APT::NeverAutoRemove") {};
190 virtual ~DefaultRootSetFunc() {};
192 bool InRootSet(const pkgCache::PkgIterator
&pkg
) { return pkg
.end() == true && Match(pkg
.Name()); };
197 // Epoch stripped text versions of the two version fields
198 const char *CandVersion
;
199 const char *CurVersion
;
201 // Pointer to the candidate install version.
202 Version
*CandidateVer
;
204 // Pointer to the install version.
207 // Copy of Package::Flags
208 unsigned short Flags
;
209 unsigned short iFlags
; // Internal flags
211 /** \brief \b true if this package can be reached from the root set. */
214 /** \brief \b true if this package is unused and should be removed.
216 * This differs from !#Marked, because it is possible that some
217 * unreachable packages will be protected from becoming
222 // Various tree indicators
223 signed char Status
; // -1,0,1,2
224 unsigned char Mode
; // ModeList
225 unsigned char DepState
; // DepState Flags
227 // Update of candidate version
228 const char *StripEpoch(const char *Ver
);
229 void Update(PkgIterator Pkg
,pkgCache
&Cache
);
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
;};
234 inline bool Keep() const {return Mode
== ModeKeep
;};
235 inline bool Upgrade() const {return Status
> 0 && Mode
== ModeInstall
;};
236 inline bool Upgradable() const {return Status
>= 1;};
237 inline bool Downgrade() const {return Status
< 0 && Mode
== ModeInstall
;};
238 inline bool Held() const {return Status
!= 0 && Keep();};
239 inline bool NowBroken() const {return (DepState
& DepNowMin
) != DepNowMin
;};
240 inline bool NowPolicyBroken() const {return (DepState
& DepNowPolicy
) != DepNowPolicy
;};
241 inline bool InstBroken() const {return (DepState
& DepInstMin
) != DepInstMin
;};
242 inline bool InstPolicyBroken() const {return (DepState
& DepInstPolicy
) != DepInstPolicy
;};
243 inline bool Install() const {return Mode
== ModeInstall
;};
244 inline VerIterator
InstVerIter(pkgCache
&Cache
)
245 {return VerIterator(Cache
,InstallVer
);};
246 inline VerIterator
CandidateVerIter(pkgCache
&Cache
)
247 {return VerIterator(Cache
,CandidateVer
);};
251 void BuildGroupOrs(VerIterator
const &V
);
252 void UpdateVerState(PkgIterator Pkg
);
254 // User Policy control
259 virtual VerIterator
GetCandidateVer(PkgIterator
const &Pkg
);
260 virtual bool IsImportantDep(DepIterator
const &Dep
);
262 virtual ~Policy() {};
266 /** The number of open "action groups"; certain post-action
267 * operations are suppressed if this number is > 0.
271 friend class ActionGroup
;
277 StateCache
*PkgState
;
278 unsigned char *DepState
;
280 /** Stores the space changes after installation */
281 signed long long iUsrSize
;
282 /** Stores how much we need to download to get the packages */
283 unsigned long long iDownloadSize
;
284 unsigned long iInstCount
;
285 unsigned long iDelCount
;
286 unsigned long iKeepCount
;
287 unsigned long iBrokenCount
;
288 unsigned long iPolicyBrokenCount
;
289 unsigned long iBadCount
;
292 bool DebugAutoInstall
;
294 Policy
*delLocalPolicy
; // For memory clean up..
297 // Check for a matching provides
298 bool CheckDep(DepIterator Dep
,int Type
,PkgIterator
&Res
);
299 inline bool CheckDep(DepIterator Dep
,int Type
)
301 PkgIterator
Res(*this,0);
302 return CheckDep(Dep
,Type
,Res
);
305 // Computes state information for deps and versions (w/o storing)
306 unsigned char DependencyState(DepIterator
&D
);
307 unsigned char VersionState(DepIterator D
,unsigned char Check
,
308 unsigned char SetMin
,
309 unsigned char SetPolicy
);
311 // Recalculates various portions of the cache, call after changing something
312 void Update(DepIterator Dep
); // Mostly internal
313 void Update(PkgIterator
const &P
);
315 // Count manipulators
316 void AddSizes(const PkgIterator
&Pkg
, bool const &Invert
= false);
317 inline void RemoveSizes(const PkgIterator
&Pkg
) {AddSizes(Pkg
, true);};
318 void AddSizes(const PkgIterator
&Pkg
,signed long Mult
) __deprecated
;
319 void AddStates(const PkgIterator
&Pkg
,int Add
= 1);
320 inline void RemoveStates(const PkgIterator
&Pkg
) {AddStates(Pkg
,-1);};
324 // Legacy.. We look like a pkgCache
325 inline operator pkgCache
&() {return *Cache
;};
326 inline Header
&Head() {return *Cache
->HeaderP
;};
327 inline GrpIterator
GrpBegin() {return Cache
->GrpBegin();};
328 inline PkgIterator
PkgBegin() {return Cache
->PkgBegin();};
329 inline GrpIterator
FindGrp(string
const &Name
) {return Cache
->FindGrp(Name
);};
330 inline PkgIterator
FindPkg(string
const &Name
) {return Cache
->FindPkg(Name
);};
331 inline PkgIterator
FindPkg(string
const &Name
, string
const &Arch
) {return Cache
->FindPkg(Name
, Arch
);};
333 inline pkgCache
&GetCache() {return *Cache
;};
334 inline pkgVersioningSystem
&VS() {return *Cache
->VS
;};
336 // Policy implementation
337 inline VerIterator
GetCandidateVer(PkgIterator
const &Pkg
) {return LocalPolicy
->GetCandidateVer(Pkg
);};
338 inline bool IsImportantDep(DepIterator Dep
) {return LocalPolicy
->IsImportantDep(Dep
);};
339 inline Policy
&GetPolicy() {return *LocalPolicy
;};
342 inline StateCache
&operator [](PkgIterator
const &I
) {return PkgState
[I
->ID
];};
343 inline unsigned char &operator [](DepIterator
const &I
) {return DepState
[I
->ID
];};
345 /** \return A function identifying packages in the root set other
346 * than manually installed packages and essential packages, or \b
347 * NULL if an error occurs.
349 * \todo Is this the best place for this function? Perhaps the
350 * settings for mark-and-sweep should be stored in a single
353 virtual InRootSetFunc
*GetRootSetFunc();
355 /** \return \b true if the garbage collector should follow recommendations.
357 virtual bool MarkFollowsRecommends();
359 /** \return \b true if the garbage collector should follow suggestions.
361 virtual bool MarkFollowsSuggests();
363 /** \brief Update the Marked and Garbage fields of all packages.
365 * This routine is implicitly invoked after all state manipulators
366 * and when an ActionGroup is destroyed. It invokes #MarkRequired
367 * and #Sweep to do its dirty work.
369 * \param rootFunc A predicate that returns \b true for packages
370 * that should be added to the root set.
372 bool MarkAndSweep(InRootSetFunc
&rootFunc
)
374 return MarkRequired(rootFunc
) && Sweep();
379 std::auto_ptr
<InRootSetFunc
> f(GetRootSetFunc());
381 return MarkAndSweep(*f
.get());
386 /** \name State Manipulators
389 void MarkKeep(PkgIterator
const &Pkg
, bool Soft
= false,
390 bool FromUser
= true, unsigned long Depth
= 0);
391 void MarkDelete(PkgIterator
const &Pkg
, bool Purge
= false,
392 unsigned long Depth
= 0, bool FromUser
= true);
393 void MarkInstall(PkgIterator
const &Pkg
,bool AutoInst
= true,
394 unsigned long Depth
= 0, bool FromUser
= true,
395 bool ForceImportantDeps
= false);
397 void SetReInstall(PkgIterator
const &Pkg
,bool To
);
398 void SetCandidateVersion(VerIterator TargetVer
, bool const &Pseudo
= true);
400 /** Set the "is automatically installed" flag of Pkg. */
401 void MarkAuto(const PkgIterator
&Pkg
, bool Auto
);
404 /** \return \b true if it's OK for MarkInstall to install
407 * See the default implementation for a simple example how this
408 * method can be used.
409 * Overriding implementations should use the hold-state-flag to cache
410 * results from previous checks of this package - also it should
411 * be used if the default resolver implementation is also used to
412 * ensure that these packages are handled like "normal" dpkg holds.
414 * The parameters are the same as in the calling MarkInstall:
415 * \param Pkg the package that MarkInstall wants to install.
416 * \param AutoInst needs a previous MarkInstall this package?
417 * \param Depth recursive deep of this Marker call
418 * \param FromUser was the install requested by the user?
420 virtual bool IsInstallOk(const PkgIterator
&Pkg
,bool AutoInst
= true,
421 unsigned long Depth
= 0, bool FromUser
= true);
423 /** \return \b true if it's OK for MarkDelete to remove
426 * See the default implementation for a simple example how this
427 * method can be used.
428 * Overriding implementations should use the hold-state-flag to cache
429 * results from previous checks of this package - also it should
430 * be used if the default resolver implementation is also used to
431 * ensure that these packages are handled like "normal" dpkg holds.
433 * The parameters are the same as in the calling MarkDelete:
434 * \param Pkg the package that MarkDelete wants to remove.
435 * \param Purge should we purge instead of "only" remove?
436 * \param Depth recursive deep of this Marker call
437 * \param FromUser was the remove requested by the user?
439 virtual bool IsDeleteOk(const PkgIterator
&Pkg
,bool Purge
= false,
440 unsigned long Depth
= 0, bool FromUser
= true);
442 // read persistent states
443 bool readStateFile(OpProgress
*prog
);
444 bool writeStateFile(OpProgress
*prog
, bool InstalledOnly
=true);
447 inline signed long long UsrSize() {return iUsrSize
;};
448 inline unsigned long long DebSize() {return iDownloadSize
;};
449 inline unsigned long DelCount() {return iDelCount
;};
450 inline unsigned long KeepCount() {return iKeepCount
;};
451 inline unsigned long InstCount() {return iInstCount
;};
452 inline unsigned long BrokenCount() {return iBrokenCount
;};
453 inline unsigned long PolicyBrokenCount() {return iPolicyBrokenCount
;};
454 inline unsigned long BadCount() {return iBadCount
;};
456 bool Init(OpProgress
*Prog
);
457 // Generate all state information
458 void Update(OpProgress
*Prog
= 0);
460 pkgDepCache(pkgCache
*Cache
,Policy
*Plcy
= 0);
461 virtual ~pkgDepCache();
464 // Helper for Update(OpProgress) to remove pseudoinstalled arch all packages
465 bool RemovePseudoInstalledPkg(PkgIterator
&Pkg
, std::set
<unsigned long> &recheck
);
466 bool ReInstallPseudoForGroup(unsigned long const &Grp
, std::set
<unsigned long> &recheck
);
467 bool ReInstallPseudoForGroup(pkgCache::PkgIterator
const &P
, std::set
<unsigned long> &recheck
);