]>
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 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
42 #include <apt-pkg/pkgcache.h>
43 #include <apt-pkg/progress.h>
49 class pkgDepCache
: protected pkgCache::Namespace
53 /** \brief An arbitrary predicate on packages. */
57 virtual bool InRootSet(const pkgCache::PkgIterator
&pkg
) {return false;};
58 virtual ~InRootSetFunc() {};
62 /** \brief Mark a single package and all its unmarked important
63 * dependencies during mark-and-sweep.
65 * Recursively invokes itself to mark all dependencies of the
68 * \param pkg The package to mark.
70 * \param ver The version of the package that is to be marked.
72 * \param follow_recommends If \b true, recommendations of the
73 * package will be recursively marked.
75 * \param follow_suggests If \b true, suggestions of the package
76 * will be recursively marked.
78 void MarkPackage(const pkgCache::PkgIterator
&pkg
,
79 const pkgCache::VerIterator
&ver
,
80 bool follow_recommends
,
81 bool follow_suggests
);
83 /** \brief Update the Marked field of all packages.
85 * Each package's StateCache::Marked field will be set to \b true
86 * if and only if it can be reached from the root set. By
87 * default, the root set consists of the set of manually installed
88 * or essential packages, but it can be extended using the
89 * parameter #rootFunc.
91 * \param rootFunc A callback that can be used to add extra
92 * packages to the root set.
94 * \return \b false if an error occured.
96 bool MarkRequired(InRootSetFunc
&rootFunc
);
98 /** \brief Set the StateCache::Garbage flag on all packages that
101 * Packages that were not marked by the last call to #MarkRequired
102 * are tested to see whether they are actually garbage. If so,
103 * they are marked as such.
105 * \return \b false if an error occured.
111 // These flags are used in DepState
112 enum DepFlags
{DepNow
= (1 << 0),DepInstall
= (1 << 1),DepCVer
= (1 << 2),
113 DepGNow
= (1 << 3),DepGInstall
= (1 << 4),DepGCVer
= (1 << 5)};
115 // These flags are used in StateCache::DepState
116 enum DepStateFlags
{DepNowPolicy
= (1 << 0), DepNowMin
= (1 << 1),
117 DepInstPolicy
= (1 << 2), DepInstMin
= (1 << 3),
118 DepCandPolicy
= (1 << 4), DepCandMin
= (1 << 5)};
120 // These flags are used in StateCache::iFlags
121 enum InternalFlags
{AutoKept
= (1 << 0), Purge
= (1 << 1), ReInstall
= (1 << 2)};
123 enum VersionTypes
{NowVersion
, InstallVersion
, CandidateVersion
};
124 enum ModeList
{ModeDelete
= 0, ModeKeep
= 1, ModeInstall
= 2};
126 /** \brief Represents an active action group.
128 * An action group is a group of actions that are currently being
129 * performed. While an active group is active, certain routine
130 * clean-up actions that would normally be performed after every
131 * cache operation are delayed until the action group is
132 * completed. This is necessary primarily to avoid inefficiencies
133 * when modifying a large number of packages at once.
135 * This class represents an active action group. Creating an
136 * instance will create an action group; destroying one will
137 * destroy the corresponding action group.
139 * The following operations are suppressed by this class:
141 * - Keeping the Marked and Garbage flags up to date.
143 * \note This can be used in the future to easily accumulate
144 * atomic actions for undo or to display "what apt did anyway";
145 * e.g., change the counter of how many action groups are active
146 * to a std::set of pointers to them and use those to store
147 * information about what happened in a group in the group.
155 /** Action groups are noncopyable. */
156 ActionGroup(const ActionGroup
&other
);
158 /** \brief Create a new ActionGroup.
160 * \param cache The cache that this ActionGroup should
163 * As long as this object exists, no automatic cleanup
164 * operations will be undertaken.
166 ActionGroup(pkgDepCache
&cache
);
168 /** \brief Clean up the action group before it is destroyed.
170 * If it is destroyed later, no second cleanup wil be run.
174 /** \brief Destroy the action group.
176 * If this is the last action group, the automatic cache
177 * cleanup operations will be undertaken.
182 /** \brief Returns \b true for packages matching a regular
183 * expression in APT::NeverAutoRemove.
185 class DefaultRootSetFunc
: public InRootSetFunc
187 std::vector
<regex_t
*> rootSetRegexp
;
188 bool constructedSuccessfully
;
191 DefaultRootSetFunc();
192 ~DefaultRootSetFunc();
194 /** \return \b true if the class initialized successfully, \b
195 * false otherwise. Used to avoid throwing an exception, since
196 * APT classes generally don't.
198 bool wasConstructedSuccessfully() const { return constructedSuccessfully
; }
200 bool InRootSet(const pkgCache::PkgIterator
&pkg
);
205 // Epoch stripped text versions of the two version fields
206 const char *CandVersion
;
207 const char *CurVersion
;
209 // Pointer to the candidate install version.
210 Version
*CandidateVer
;
212 // Pointer to the install version.
215 // Copy of Package::Flags
216 unsigned short Flags
;
217 unsigned short iFlags
; // Internal flags
219 /** \brief \b true if this package can be reached from the root set. */
222 /** \brief \b true if this package is unused and should be removed.
224 * This differs from !#Marked, because it is possible that some
225 * unreachable packages will be protected from becoming
230 // Various tree indicators
231 signed char Status
; // -1,0,1,2
232 unsigned char Mode
; // ModeList
233 unsigned char DepState
; // DepState Flags
235 // Update of candidate version
236 const char *StripEpoch(const char *Ver
);
237 void Update(PkgIterator Pkg
,pkgCache
&Cache
);
239 // Various test members for the current status of the package
240 inline bool NewInstall() const {return Status
== 2 && Mode
== ModeInstall
;};
241 inline bool Delete() const {return Mode
== ModeDelete
;};
242 inline bool Keep() const {return Mode
== ModeKeep
;};
243 inline bool Upgrade() const {return Status
> 0 && Mode
== ModeInstall
;};
244 inline bool Upgradable() const {return Status
>= 1;};
245 inline bool Downgrade() const {return Status
< 0 && Mode
== ModeInstall
;};
246 inline bool Held() const {return Status
!= 0 && Keep();};
247 inline bool NowBroken() const {return (DepState
& DepNowMin
) != DepNowMin
;};
248 inline bool NowPolicyBroken() const {return (DepState
& DepNowPolicy
) != DepNowPolicy
;};
249 inline bool InstBroken() const {return (DepState
& DepInstMin
) != DepInstMin
;};
250 inline bool InstPolicyBroken() const {return (DepState
& DepInstPolicy
) != DepInstPolicy
;};
251 inline bool Install() const {return Mode
== ModeInstall
;};
252 inline VerIterator
InstVerIter(pkgCache
&Cache
)
253 {return VerIterator(Cache
,InstallVer
);};
254 inline VerIterator
CandidateVerIter(pkgCache
&Cache
)
255 {return VerIterator(Cache
,CandidateVer
);};
259 void BuildGroupOrs(VerIterator
const &V
);
260 void UpdateVerState(PkgIterator Pkg
);
262 // User Policy control
267 virtual VerIterator
GetCandidateVer(PkgIterator Pkg
);
268 virtual bool IsImportantDep(DepIterator Dep
);
270 virtual ~Policy() {};
274 /** The number of open "action groups"; certain post-action
275 * operations are suppressed if this number is > 0.
279 friend class ActionGroup
;
285 StateCache
*PkgState
;
286 unsigned char *DepState
;
289 double iDownloadSize
;
290 unsigned long iInstCount
;
291 unsigned long iDelCount
;
292 unsigned long iKeepCount
;
293 unsigned long iBrokenCount
;
294 unsigned long iPolicyBrokenCount
;
295 unsigned long iBadCount
;
297 Policy
*delLocalPolicy
; // For memory clean up..
300 // Check for a matching provides
301 bool CheckDep(DepIterator Dep
,int Type
,PkgIterator
&Res
);
302 inline bool CheckDep(DepIterator Dep
,int Type
)
304 PkgIterator
Res(*this,0);
305 return CheckDep(Dep
,Type
,Res
);
308 // Computes state information for deps and versions (w/o storing)
309 unsigned char DependencyState(DepIterator
&D
);
310 unsigned char VersionState(DepIterator D
,unsigned char Check
,
311 unsigned char SetMin
,
312 unsigned char SetPolicy
);
314 // Recalculates various portions of the cache, call after changing something
315 void Update(DepIterator Dep
); // Mostly internal
316 void Update(PkgIterator
const &P
);
318 // Count manipulators
319 void AddSizes(const PkgIterator
&Pkg
,signed long Mult
= 1);
320 inline void RemoveSizes(const PkgIterator
&Pkg
) {AddSizes(Pkg
,-1);};
321 void AddStates(const PkgIterator
&Pkg
,int Add
= 1);
322 inline void RemoveStates(const PkgIterator
&Pkg
) {AddStates(Pkg
,-1);};
326 // Legacy.. We look like a pkgCache
327 inline operator pkgCache
&() {return *Cache
;};
328 inline Header
&Head() {return *Cache
->HeaderP
;};
329 inline PkgIterator
PkgBegin() {return Cache
->PkgBegin();};
330 inline PkgIterator
FindPkg(string
const &Name
) {return Cache
->FindPkg(Name
);};
332 inline pkgCache
&GetCache() {return *Cache
;};
333 inline pkgVersioningSystem
&VS() {return *Cache
->VS
;};
335 // Policy implementation
336 inline VerIterator
GetCandidateVer(PkgIterator Pkg
) {return LocalPolicy
->GetCandidateVer(Pkg
);};
337 inline bool IsImportantDep(DepIterator Dep
) {return LocalPolicy
->IsImportantDep(Dep
);};
338 inline Policy
&GetPolicy() {return *LocalPolicy
;};
341 inline StateCache
&operator [](PkgIterator
const &I
) {return PkgState
[I
->ID
];};
342 inline unsigned char &operator [](DepIterator
const &I
) {return DepState
[I
->ID
];};
344 /** \return A function identifying packages in the root set other
345 * than manually installed packages and essential packages, or \b
346 * NULL if an error occurs.
348 * \todo Is this the best place for this function? Perhaps the
349 * settings for mark-and-sweep should be stored in a single
352 virtual InRootSetFunc
*GetRootSetFunc();
354 /** \return \b true if the garbage collector should follow recommendations.
356 virtual bool MarkFollowsRecommends();
358 /** \return \b true if the garbage collector should follow suggestions.
360 virtual bool MarkFollowsSuggests();
362 /** \brief Update the Marked and Garbage fields of all packages.
364 * This routine is implicitly invoked after all state manipulators
365 * and when an ActionGroup is destroyed. It invokes #MarkRequired
366 * and #Sweep to do its dirty work.
368 * \param rootFunc A predicate that returns \b true for packages
369 * that should be added to the root set.
371 bool MarkAndSweep(InRootSetFunc
&rootFunc
)
373 return MarkRequired(rootFunc
) && Sweep();
378 std::auto_ptr
<InRootSetFunc
> f(GetRootSetFunc());
380 return MarkAndSweep(*f
.get());
385 /** \name State Manipulators
388 void MarkKeep(PkgIterator
const &Pkg
, bool Soft
= false,
389 bool FromUser
= true);
390 void MarkDelete(PkgIterator
const &Pkg
,bool Purge
= false);
391 void MarkInstall(PkgIterator
const &Pkg
,bool AutoInst
= true,
392 unsigned long Depth
= 0, bool FromUser
= true,
393 bool ForceImportantDeps
= false);
394 void SetReInstall(PkgIterator
const &Pkg
,bool To
);
395 void SetCandidateVersion(VerIterator TargetVer
);
397 /** Set the "is automatically installed" flag of Pkg. */
398 void MarkAuto(const PkgIterator
&Pkg
, bool Auto
);
401 // This is for debuging
402 void Update(OpProgress
*Prog
= 0);
404 // read persistent states
405 bool readStateFile(OpProgress
*prog
);
406 bool writeStateFile(OpProgress
*prog
);
409 inline double UsrSize() {return iUsrSize
;};
410 inline double DebSize() {return iDownloadSize
;};
411 inline unsigned long DelCount() {return iDelCount
;};
412 inline unsigned long KeepCount() {return iKeepCount
;};
413 inline unsigned long InstCount() {return iInstCount
;};
414 inline unsigned long BrokenCount() {return iBrokenCount
;};
415 inline unsigned long PolicyBrokenCount() {return iPolicyBrokenCount
;};
416 inline unsigned long BadCount() {return iBadCount
;};
418 bool Init(OpProgress
*Prog
);
420 pkgDepCache(pkgCache
*Cache
,Policy
*Plcy
= 0);
421 virtual ~pkgDepCache();