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