* merged the install-recommends branch
[ntk/apt.git] / apt-pkg / depcache.h
1 // -*- mode: c++; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: depcache.h,v 1.14 2001/02/20 07:03:17 jgg Exp $
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 /*}}}*/
38 #ifndef PKGLIB_DEPCACHE_H
39 #define PKGLIB_DEPCACHE_H
40
41 #ifdef __GNUG__
42 #pragma interface "apt-pkg/depcache.h"
43 #endif
44
45 #include <apt-pkg/pkgcache.h>
46 #include <apt-pkg/progress.h>
47
48 #include <regex.h>
49
50 #include <vector>
51
52 class pkgDepCache : protected pkgCache::Namespace
53 {
54 public:
55
56 /** \brief An arbitrary predicate on packages. */
57 class InRootSetFunc
58 {
59 public:
60 virtual bool InRootSet(const pkgCache::PkgIterator &pkg) {return false;};
61 virtual ~InRootSetFunc() {};
62 };
63
64 private:
65 /** \brief Mark a single package and all its unmarked important
66 * dependencies during mark-and-sweep.
67 *
68 * Recursively invokes itself to mark all dependencies of the
69 * package.
70 *
71 * \param pkg The package to mark.
72 *
73 * \param ver The version of the package that is to be marked.
74 *
75 * \param follow_recommends If \b true, recommendations of the
76 * package will be recursively marked.
77 *
78 * \param follow_suggests If \b true, suggestions of the package
79 * will be recursively marked.
80 */
81 void MarkPackage(const pkgCache::PkgIterator &pkg,
82 const pkgCache::VerIterator &ver,
83 bool follow_recommends,
84 bool follow_suggests);
85
86 /** \brief Update the Marked field of all packages.
87 *
88 * Each package's StateCache::Marked field will be set to \b true
89 * if and only if it can be reached from the root set. By
90 * default, the root set consists of the set of manually installed
91 * or essential packages, but it can be extended using the
92 * parameter #rootFunc.
93 *
94 * \param rootFunc A callback that can be used to add extra
95 * packages to the root set.
96 *
97 * \return \b false if an error occured.
98 */
99 bool MarkRequired(InRootSetFunc &rootFunc);
100
101 /** \brief Set the StateCache::Garbage flag on all packages that
102 * should be removed.
103 *
104 * Packages that were not marked by the last call to #MarkRequired
105 * are tested to see whether they are actually garbage. If so,
106 * they are marked as such.
107 *
108 * \return \b false if an error occured.
109 */
110 bool Sweep();
111
112 public:
113
114 // These flags are used in DepState
115 enum DepFlags {DepNow = (1 << 0),DepInstall = (1 << 1),DepCVer = (1 << 2),
116 DepGNow = (1 << 3),DepGInstall = (1 << 4),DepGCVer = (1 << 5)};
117
118 // These flags are used in StateCache::DepState
119 enum DepStateFlags {DepNowPolicy = (1 << 0), DepNowMin = (1 << 1),
120 DepInstPolicy = (1 << 2), DepInstMin = (1 << 3),
121 DepCandPolicy = (1 << 4), DepCandMin = (1 << 5)};
122
123 // These flags are used in StateCache::iFlags
124 enum InternalFlags {AutoKept = (1 << 0), Purge = (1 << 1), ReInstall = (1 << 2)};
125
126 enum VersionTypes {NowVersion, InstallVersion, CandidateVersion};
127 enum ModeList {ModeDelete = 0, ModeKeep = 1, ModeInstall = 2};
128
129 /** \brief Represents an active action group.
130 *
131 * An action group is a group of actions that are currently being
132 * performed. While an active group is active, certain routine
133 * clean-up actions that would normally be performed after every
134 * cache operation are delayed until the action group is
135 * completed. This is necessary primarily to avoid inefficiencies
136 * when modifying a large number of packages at once.
137 *
138 * This class represents an active action group. Creating an
139 * instance will create an action group; destroying one will
140 * destroy the corresponding action group.
141 *
142 * The following operations are suppressed by this class:
143 *
144 * - Keeping the Marked and Garbage flags up to date.
145 *
146 * \note This can be used in the future to easily accumulate
147 * atomic actions for undo or to display "what apt did anyway";
148 * e.g., change the counter of how many action groups are active
149 * to a std::set of pointers to them and use those to store
150 * information about what happened in a group in the group.
151 */
152 class ActionGroup
153 {
154 pkgDepCache &cache;
155
156 bool released;
157
158 /** Action groups are noncopyable. */
159 ActionGroup(const ActionGroup &other);
160 public:
161 /** \brief Create a new ActionGroup.
162 *
163 * \param cache The cache that this ActionGroup should
164 * manipulate.
165 *
166 * As long as this object exists, no automatic cleanup
167 * operations will be undertaken.
168 */
169 ActionGroup(pkgDepCache &cache);
170
171 /** \brief Clean up the action group before it is destroyed.
172 *
173 * If it is destroyed later, no second cleanup wil be run.
174 */
175 void release();
176
177 /** \brief Destroy the action group.
178 *
179 * If this is the last action group, the automatic cache
180 * cleanup operations will be undertaken.
181 */
182 ~ActionGroup();
183 };
184
185 /** \brief Returns \b true for packages matching a regular
186 * expression in APT::NeverAutoRemove.
187 */
188 class DefaultRootSetFunc : public InRootSetFunc
189 {
190 std::vector<regex_t *> rootSetRegexp;
191 bool constructedSuccessfully;
192
193 public:
194 DefaultRootSetFunc();
195 ~DefaultRootSetFunc();
196
197 /** \return \b true if the class initialized successfully, \b
198 * false otherwise. Used to avoid throwing an exception, since
199 * APT classes generally don't.
200 */
201 bool wasConstructedSuccessfully() const { return constructedSuccessfully; }
202
203 bool InRootSet(const pkgCache::PkgIterator &pkg);
204 };
205
206 struct StateCache
207 {
208 // Epoch stripped text versions of the two version fields
209 const char *CandVersion;
210 const char *CurVersion;
211
212 // Pointer to the candidate install version.
213 Version *CandidateVer;
214
215 // Pointer to the install version.
216 Version *InstallVer;
217
218 // Copy of Package::Flags
219 unsigned short Flags;
220 unsigned short iFlags; // Internal flags
221
222 /** \brief \b true if this package can be reached from the root set. */
223 bool Marked;
224
225 /** \brief \b true if this package is unused and should be removed.
226 *
227 * This differs from !#Marked, because it is possible that some
228 * unreachable packages will be protected from becoming
229 * garbage.
230 */
231 bool Garbage;
232
233 // Various tree indicators
234 signed char Status; // -1,0,1,2
235 unsigned char Mode; // ModeList
236 unsigned char DepState; // DepState Flags
237
238 // Update of candidate version
239 const char *StripEpoch(const char *Ver);
240 void Update(PkgIterator Pkg,pkgCache &Cache);
241
242 // Various test members for the current status of the package
243 inline bool NewInstall() const {return Status == 2 && Mode == ModeInstall;};
244 inline bool Delete() const {return Mode == ModeDelete;};
245 inline bool Keep() const {return Mode == ModeKeep;};
246 inline bool Upgrade() const {return Status > 0 && Mode == ModeInstall;};
247 inline bool Upgradable() const {return Status >= 1;};
248 inline bool Downgrade() const {return Status < 0 && Mode == ModeInstall;};
249 inline bool Held() const {return Status != 0 && Keep();};
250 inline bool NowBroken() const {return (DepState & DepNowMin) != DepNowMin;};
251 inline bool NowPolicyBroken() const {return (DepState & DepNowPolicy) != DepNowPolicy;};
252 inline bool InstBroken() const {return (DepState & DepInstMin) != DepInstMin;};
253 inline bool InstPolicyBroken() const {return (DepState & DepInstPolicy) != DepInstPolicy;};
254 inline bool Install() const {return Mode == ModeInstall;};
255 inline VerIterator InstVerIter(pkgCache &Cache)
256 {return VerIterator(Cache,InstallVer);};
257 inline VerIterator CandidateVerIter(pkgCache &Cache)
258 {return VerIterator(Cache,CandidateVer);};
259 };
260
261 // Helper functions
262 void BuildGroupOrs(VerIterator const &V);
263 void UpdateVerState(PkgIterator Pkg);
264
265 // User Policy control
266 class Policy
267 {
268 public:
269
270 virtual VerIterator GetCandidateVer(PkgIterator Pkg);
271 virtual bool IsImportantDep(DepIterator Dep);
272
273 virtual ~Policy() {};
274 };
275
276 private:
277 /** The number of open "action groups"; certain post-action
278 * operations are suppressed if this number is > 0.
279 */
280 int group_level;
281
282 friend class ActionGroup;
283
284 protected:
285
286 // State information
287 pkgCache *Cache;
288 StateCache *PkgState;
289 unsigned char *DepState;
290
291 double iUsrSize;
292 double iDownloadSize;
293 unsigned long iInstCount;
294 unsigned long iDelCount;
295 unsigned long iKeepCount;
296 unsigned long iBrokenCount;
297 unsigned long iPolicyBrokenCount;
298 unsigned long iBadCount;
299
300 Policy *delLocalPolicy; // For memory clean up..
301 Policy *LocalPolicy;
302
303 // Check for a matching provides
304 bool CheckDep(DepIterator Dep,int Type,PkgIterator &Res);
305 inline bool CheckDep(DepIterator Dep,int Type)
306 {
307 PkgIterator Res(*this,0);
308 return CheckDep(Dep,Type,Res);
309 }
310
311 // Computes state information for deps and versions (w/o storing)
312 unsigned char DependencyState(DepIterator &D);
313 unsigned char VersionState(DepIterator D,unsigned char Check,
314 unsigned char SetMin,
315 unsigned char SetPolicy);
316
317 // Recalculates various portions of the cache, call after changing something
318 void Update(DepIterator Dep); // Mostly internal
319 void Update(PkgIterator const &P);
320
321 // Count manipulators
322 void AddSizes(const PkgIterator &Pkg,signed long Mult = 1);
323 inline void RemoveSizes(const PkgIterator &Pkg) {AddSizes(Pkg,-1);};
324 void AddStates(const PkgIterator &Pkg,int Add = 1);
325 inline void RemoveStates(const PkgIterator &Pkg) {AddStates(Pkg,-1);};
326
327 public:
328
329 // Legacy.. We look like a pkgCache
330 inline operator pkgCache &() {return *Cache;};
331 inline Header &Head() {return *Cache->HeaderP;};
332 inline PkgIterator PkgBegin() {return Cache->PkgBegin();};
333 inline PkgIterator FindPkg(string const &Name) {return Cache->FindPkg(Name);};
334
335 inline pkgCache &GetCache() {return *Cache;};
336 inline pkgVersioningSystem &VS() {return *Cache->VS;};
337
338 // Policy implementation
339 inline VerIterator GetCandidateVer(PkgIterator Pkg) {return LocalPolicy->GetCandidateVer(Pkg);};
340 inline bool IsImportantDep(DepIterator Dep) {return LocalPolicy->IsImportantDep(Dep);};
341 inline Policy &GetPolicy() {return *LocalPolicy;};
342
343 // Accessors
344 inline StateCache &operator [](PkgIterator const &I) {return PkgState[I->ID];};
345 inline unsigned char &operator [](DepIterator const &I) {return DepState[I->ID];};
346
347 /** \return A function identifying packages in the root set other
348 * than manually installed packages and essential packages, or \b
349 * NULL if an error occurs.
350 *
351 * \todo Is this the best place for this function? Perhaps the
352 * settings for mark-and-sweep should be stored in a single
353 * external class?
354 */
355 virtual InRootSetFunc *GetRootSetFunc();
356
357 /** \return \b true if the garbage collector should follow recommendations.
358 */
359 virtual bool MarkFollowsRecommends();
360
361 /** \return \b true if the garbage collector should follow suggestions.
362 */
363 virtual bool MarkFollowsSuggests();
364
365 /** \brief Update the Marked and Garbage fields of all packages.
366 *
367 * This routine is implicitly invoked after all state manipulators
368 * and when an ActionGroup is destroyed. It invokes #MarkRequired
369 * and #Sweep to do its dirty work.
370 *
371 * \param rootFunc A predicate that returns \b true for packages
372 * that should be added to the root set.
373 */
374 bool MarkAndSweep(InRootSetFunc &rootFunc)
375 {
376 return MarkRequired(rootFunc) && Sweep();
377 }
378
379 bool MarkAndSweep()
380 {
381 std::auto_ptr<InRootSetFunc> f(GetRootSetFunc());
382 if(f.get() != NULL)
383 return MarkAndSweep(*f.get());
384 else
385 return false;
386 }
387
388 /** \name State Manipulators
389 */
390 // @{
391 void MarkKeep(PkgIterator const &Pkg, bool Soft = false,
392 bool FromUser = true);
393 void MarkDelete(PkgIterator const &Pkg,bool Purge = false);
394 void MarkInstall(PkgIterator const &Pkg,bool AutoInst = true,
395 unsigned long Depth = 0, bool FromUser = true,
396 bool ForceImportantDeps = false);
397 void SetReInstall(PkgIterator const &Pkg,bool To);
398 void SetCandidateVersion(VerIterator TargetVer);
399
400 /** Set the "is automatically installed" flag of Pkg. */
401 void MarkAuto(const PkgIterator &Pkg, bool Auto);
402 // @}
403
404 // This is for debuging
405 void Update(OpProgress *Prog = 0);
406
407 // read persistent states
408 bool readStateFile(OpProgress *prog);
409 bool writeStateFile(OpProgress *prog);
410
411 // Size queries
412 inline double UsrSize() {return iUsrSize;};
413 inline double DebSize() {return iDownloadSize;};
414 inline unsigned long DelCount() {return iDelCount;};
415 inline unsigned long KeepCount() {return iKeepCount;};
416 inline unsigned long InstCount() {return iInstCount;};
417 inline unsigned long BrokenCount() {return iBrokenCount;};
418 inline unsigned long PolicyBrokenCount() {return iPolicyBrokenCount;};
419 inline unsigned long BadCount() {return iBadCount;};
420
421 bool Init(OpProgress *Prog);
422
423 pkgDepCache(pkgCache *Cache,Policy *Plcy = 0);
424 virtual ~pkgDepCache();
425 };
426
427 #endif