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