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