apt 2.9.16 commandline package manager

depcache.h
1// -*- mode: c++; mode: fold -*-
2// Description /*{{{*/
3/* ######################################################################
4
5 DepCache - Dependency Extension data for the cache
6
7 This class stores the cache data and a set of extension structures for
8 monitoring the current state of all the packages. It also generates and
9 caches the 'install' state of many things. This refers to the state of the
10 package after an install has been run.
11
12 The StateCache::State field can be -1,0,1,2 which is <,=,>,no current.
13 StateCache::Mode is which of the 3 fields is active.
14
15 This structure is important to support the readonly status of the cache
16 file. When the data is saved the cache will be refereshed from our
17 internal rep and written to disk. Then the actual persistent data
18 files will be put on the disk.
19
20 Each dependency is compared against 3 target versions to produce to
21 3 dependency results.
22 Now - Compared using the Currently install version
23 Install - Compared using the install version (final state)
24 CVer - (Candidate Version) Compared using the Candidate Version
25 The candidate and now results are used to decide whether a package
26 should be automatically installed or if it should be left alone.
27
28 Remember, the Candidate Version is selected based on the distribution
29 settings for the Package. The Install Version is selected based on the
30 state (Delete, Keep, Install) field and can be either the Current Version
31 or the Candidate version.
32
33 The Candidate version is what is shown the 'Install Version' field.
34
35 ##################################################################### */
36 /*}}}*/
37#ifndef PKGLIB_DEPCACHE_H
38#define PKGLIB_DEPCACHE_H
39
40#include <apt-pkg/configuration.h>
41#include <apt-pkg/macros.h>
42#include <apt-pkg/pkgcache.h>
43
44#include <cstddef>
45
46#include <list>
47#include <memory>
48#include <string>
49#include <utility>
50
51
52 class OpProgress;
53 class pkgVersioningSystem;
54 namespace APT
55{
56 template <class Container>
57 class PackageContainer;
58 using PackageVector = PackageContainer<std::vector<pkgCache::PkgIterator>>;
59} // namespace APT
60
61 class APT_PUBLIC pkgDepCache : protected pkgCache::Namespace
62{
63 public:
64
66 class APT_PUBLIC InRootSetFunc
67 {
68 public:
69 virtual bool InRootSet(const pkgCache::PkgIterator &/*pkg*/) {return false;};
70 virtual ~InRootSetFunc() {};
71 };
72
73 private:
87 bool MarkRequired(InRootSetFunc &rootFunc);
88
98 bool Sweep();
99
100 public:
101
102 // These flags are used in DepState
103 enum DepFlags {DepNow = (1 << 0),DepInstall = (1 << 1),DepCVer = (1 << 2),
104 DepGNow = (1 << 3),DepGInstall = (1 << 4),DepGCVer = (1 << 5)};
105
106 // These flags are used in StateCache::DepState
107 enum DepStateFlags {DepNowPolicy = (1 << 0), DepNowMin = (1 << 1),
108 DepInstPolicy = (1 << 2), DepInstMin = (1 << 3),
109 DepCandPolicy = (1 << 4), DepCandMin = (1 << 5)};
110
111 // These flags are used in StateCache::iFlags
112 enum InternalFlags {AutoKept = (1 << 0), Purge = (1 << 1), ReInstall = (1 << 2), Protected = (1 << 3)};
113
114 enum VersionTypes {NowVersion, InstallVersion, CandidateVersion};
115 enum ModeList {ModeDelete = 0, ModeKeep = 1, ModeInstall = 2, ModeGarbage = 3};
116
140 class APT_PUBLIC ActionGroup
141 {
142 void * const d;
143 pkgDepCache &cache;
144
145 bool released;
146
148 APT_HIDDEN ActionGroup(const ActionGroup &other);
149 public:
158 explicit ActionGroup(pkgDepCache &cache);
159
164 void release();
165
171 virtual ~ActionGroup();
172 };
173
177 class APT_PUBLIC DefaultRootSetFunc : public InRootSetFunc, public Configuration::MatchAgainstConfig
178 {
179 public:
180 DefaultRootSetFunc() : Configuration::MatchAgainstConfig("APT::NeverAutoRemove") {};
181 virtual ~DefaultRootSetFunc() {};
182
183 bool InRootSet(const pkgCache::PkgIterator &pkg) APT_OVERRIDE { return pkg.end() == false && Match(pkg.Name()); };
184 };
185
186 struct APT_PUBLIC StateCache
187 {
188 // text versions of the two version fields
189 const char *CandVersion;
190 const char *CurVersion;
191
192 // Pointer to the candidate install version.
193 Version *CandidateVer;
194
195 // Pointer to the install version.
196 Version *InstallVer;
197
198 // Copy of Package::Flags
199 unsigned short Flags;
200 unsigned short iFlags; // Internal flags
201
203 bool Marked;
204
211 bool Garbage;
212
213 // Various tree indicators
214 signed char Status; // -1,0,1,2
215 unsigned char Mode; // ModeList
216 unsigned char DepState; // DepState Flags
217
218 // Update of candidate version
219 void Update(PkgIterator Pkg,pkgCache &Cache);
220
221 // Various test members for the current status of the package
222 inline bool NewInstall() const {return Status == 2 && Mode == ModeInstall;};
223 inline bool Delete() const {return Mode == ModeDelete;};
224 inline bool Purge() const {return Delete() == true && (iFlags & pkgDepCache::Purge) == pkgDepCache::Purge; };
225 inline bool Keep() const {return Mode == ModeKeep;};
226 inline bool Protect() const {return (iFlags & Protected) == Protected;};
227 inline bool Upgrade() const {return Status > 0 && Mode == ModeInstall;};
228 inline bool Upgradable() const {return Status >= 1 && CandidateVer != NULL;};
229 inline bool Downgrade() const {return Status < 0 && Mode == ModeInstall;};
230 inline bool Held() const {return Status != 0 && Keep();};
231 inline bool NowBroken() const {return (DepState & DepNowMin) != DepNowMin;};
232 inline bool NowPolicyBroken() const {return (DepState & DepNowPolicy) != DepNowPolicy;};
233 inline bool InstBroken() const {return (DepState & DepInstMin) != DepInstMin;};
234 inline bool InstPolicyBroken() const {return (DepState & DepInstPolicy) != DepInstPolicy;};
235 inline bool Install() const {return Mode == ModeInstall;};
236 inline bool ReInstall() const {return Delete() == false && (iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall;};
237 inline VerIterator InstVerIter(pkgCache &Cache)
238 {return VerIterator(Cache,InstallVer);};
239 inline VerIterator CandidateVerIter(pkgCache &Cache)
240 {return VerIterator(Cache,CandidateVer);};
241 };
242
243 // Helper functions
244 void BuildGroupOrs(VerIterator const &V);
245 void UpdateVerState(PkgIterator const &Pkg);
246
247 // User Policy control
248 class APT_PUBLIC Policy
249 {
250 public:
251 Policy() {
252 InstallRecommends = _config->FindB("APT::Install-Recommends", false);
253 InstallSuggests = _config->FindB("APT::Install-Suggests", false);
254 }
255
256 virtual VerIterator GetCandidateVer(PkgIterator const &Pkg);
257 virtual bool IsImportantDep(DepIterator const &Dep) const;
258 virtual signed short GetPriority(PkgIterator const &Pkg);
259 virtual signed short GetPriority(VerIterator const &Ver, bool ConsiderFiles=true);
260 virtual signed short GetPriority(PkgFileIterator const &File);
261
262 virtual ~Policy() {};
263
264 private:
265 bool InstallRecommends;
266 bool InstallSuggests;
267 };
268
269 private:
273 int group_level;
274
275 friend class ActionGroup;
276 public:
277 int IncreaseActionGroupLevel();
278 int DecreaseActionGroupLevel();
279
280 protected:
281
282 // State information
283 pkgCache *Cache;
284 StateCache *PkgState;
285 unsigned char *DepState;
286
288 signed long long iUsrSize;
290 unsigned long long iDownloadSize;
291 unsigned long iInstCount;
292 unsigned long iDelCount;
293 unsigned long iKeepCount;
294 unsigned long iBrokenCount;
295 unsigned long iPolicyBrokenCount;
296 unsigned long iBadCount;
297
298 bool DebugMarker;
299 bool DebugAutoInstall;
300
301 Policy *delLocalPolicy; // For memory clean up..
302 Policy *LocalPolicy;
303
304 // Check for a matching provides
305 bool CheckDep(DepIterator const &Dep,int const Type,PkgIterator &Res);
306 inline bool CheckDep(DepIterator const &Dep,int const Type)
307 {
308 PkgIterator Res(*this,0);
309 return CheckDep(Dep,Type,Res);
310 }
311
312 // Computes state information for deps and versions (w/o storing)
313 unsigned char DependencyState(DepIterator const &D);
314 unsigned char VersionState(DepIterator D,unsigned char const Check,
315 unsigned char const SetMin,
316 unsigned char const SetPolicy) const;
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
323 void AddSizes(const PkgIterator &Pkg, bool const Invert = false);
324 inline void RemoveSizes(const PkgIterator &Pkg) {AddSizes(Pkg, true);};
325 void AddStates(const PkgIterator &Pkg, bool const Invert = false);
326 inline void RemoveStates(const PkgIterator &Pkg) {AddStates(Pkg,true);};
327
328 public:
329
330 // Legacy.. We look like a pkgCache
331 inline operator pkgCache &() {return *Cache;};
332 inline Header &Head() {return *Cache->HeaderP;};
333 inline GrpIterator GrpBegin() {return Cache->GrpBegin();};
334 inline PkgIterator PkgBegin() {return Cache->PkgBegin();};
335 inline GrpIterator FindGrp(APT::StringView Name) {return Cache->FindGrp(Name);};
336 inline PkgIterator FindPkg(APT::StringView Name) {return Cache->FindPkg(Name);};
337 inline PkgIterator FindPkg(APT::StringView Name, APT::StringView Arch) {return Cache->FindPkg(Name, Arch);};
338
339 inline pkgCache &GetCache() {return *Cache;};
340 inline pkgVersioningSystem &VS() {return *Cache->VS;};
341
342 inline bool IsImportantDep(DepIterator Dep) const {return LocalPolicy->IsImportantDep(Dep);};
343 inline Policy &GetPolicy() {return *LocalPolicy;};
344
345 // Accessors
346 inline StateCache &operator [](PkgIterator const &I) {return PkgState[I->ID];};
347 inline StateCache &operator [](PkgIterator const &I) const {return PkgState[I->ID];};
348 inline unsigned char &operator [](DepIterator const &I) {return DepState[I->ID];};
349 inline unsigned char const &operator [](DepIterator const &I) const {return DepState[I->ID];};
350
359 virtual InRootSetFunc *GetRootSetFunc();
360
362 InRootSetFunc *GetCachedRootSetFunc() APT_HIDDEN;
363
366 virtual bool MarkFollowsRecommends();
367
370 virtual bool MarkFollowsSuggests();
371
381 bool MarkAndSweep(InRootSetFunc &rootFunc);
382 bool MarkAndSweep();
383
388 bool PhasingApplied(PkgIterator Pkg) const;
389
392 // @{
393 bool MarkKeep(PkgIterator const &Pkg, bool Soft = false,
394 bool FromUser = true, unsigned long Depth = 0);
395 bool MarkDelete(PkgIterator const &Pkg, bool MarkPurge = false,
396 unsigned long Depth = 0, bool FromUser = true);
397 bool MarkInstall(PkgIterator const &Pkg,bool AutoInst = true,
398 unsigned long Depth = 0, bool FromUser = true,
399 bool ForceImportantDeps = false);
400 void MarkProtected(PkgIterator const &Pkg) { PkgState[Pkg->ID].iFlags |= Protected; };
401
402 void SetReInstall(PkgIterator const &Pkg,bool To);
403
412 pkgCache::VerIterator GetCandidateVersion(pkgCache::PkgIterator const &Pkg);
413 void SetCandidateVersion(VerIterator TargetVer);
414 bool SetCandidateRelease(pkgCache::VerIterator TargetVer,
415 std::string const &TargetRel);
430 bool SetCandidateRelease(pkgCache::VerIterator TargetVer,
431 std::string const &TargetRel,
432 std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > &Changed);
433
435 void MarkAuto(const PkgIterator &Pkg, bool Auto);
436 // @}
437
453 virtual bool IsInstallOk(const PkgIterator &Pkg,bool AutoInst = true,
454 unsigned long Depth = 0, bool FromUser = true);
455
468 virtual bool IsDeleteOk(const PkgIterator &Pkg,bool MarkPurge = false,
469 unsigned long Depth = 0, bool FromUser = true);
470
471 // read persistent states
472 bool readStateFile(OpProgress * const prog);
473 bool writeStateFile(OpProgress * const prog, bool const InstalledOnly=true);
474
475 // Size queries
476 inline signed long long UsrSize() {return iUsrSize;};
477 inline unsigned long long DebSize() {return iDownloadSize;};
478 inline unsigned long DelCount() {return iDelCount;};
479 inline unsigned long KeepCount() {return iKeepCount;};
480 inline unsigned long InstCount() {return iInstCount;};
481 inline unsigned long BrokenCount() {return iBrokenCount;};
482 inline unsigned long PolicyBrokenCount() {return iPolicyBrokenCount;};
483 inline unsigned long BadCount() {return iBadCount;};
484
485 bool Init(OpProgress * const Prog);
486 // Generate all state information
487 void Update(OpProgress * const Prog = 0);
488
489 pkgDepCache(pkgCache * const Cache,Policy * const Plcy = 0);
490 virtual ~pkgDepCache();
491
492 bool CheckConsistency(char const *const msgtag = "");
493#ifdef APT_COMPILING_APT
494 double BootSize(bool initrdOnly);
495#endif
496 protected:
497 // methods call by IsInstallOk
498 bool IsInstallOkMultiArchSameVersionSynced(PkgIterator const &Pkg,
499 bool const AutoInst, unsigned long const Depth, bool const FromUser);
500 bool IsInstallOkDependenciesSatisfiableByCandidates(PkgIterator const &Pkg,
501 bool const AutoInst, unsigned long const Depth, bool const FromUser);
502
503 // methods call by IsDeleteOk
504 bool IsDeleteOkProtectInstallRequests(PkgIterator const &Pkg,
505 bool const rPurge, unsigned long const Depth, bool const FromUser);
506
507 private:
508 struct Private;
509 Private *const d;
510
511 APT_HIDDEN bool MarkInstall_StateChange(PkgIterator const &Pkg, bool AutoInst, bool FromUser);
512 APT_HIDDEN bool MarkInstall_DiscardInstall(PkgIterator const &Pkg);
513
514 APT_HIDDEN void PerformDependencyPass(OpProgress * const Prog);
515};
516
517#endif
APT::PackageContainer
Definition depcache.h:57
APT::StringView
Simple subset of std::string_view from C++17.
Definition string_view.h:35
Configuration::MatchAgainstConfig
match a string against a configurable list of patterns
Definition configuration.h:125
OpProgress
Definition progress.h:30
pkgCache::DepIterator
Definition cacheiterators.h:299
pkgCache::Namespace
Definition pkgcache.h:826
pkgCache::PkgFileIterator
Definition cacheiterators.h:454
pkgCache::PkgIterator
Definition cacheiterators.h:147
pkgCache::VerIterator
Definition cacheiterators.h:193
pkgCache
Definition pkgcache.h:126
pkgDepCache::ActionGroup
Represents an active action group.
Definition depcache.h:141
pkgDepCache::DefaultRootSetFunc
Returns true for packages matching a regular expression in APT::NeverAutoRemove.
Definition depcache.h:178
pkgDepCache::InRootSetFunc
An arbitrary predicate on packages.
Definition depcache.h:67
pkgDepCache::Policy
Definition depcache.h:249
pkgDepCache::Policy::IsImportantDep
virtual bool IsImportantDep(DepIterator const &Dep) const
Definition depcache.cc:2265
pkgDepCache
Definition depcache.h:62
pkgDepCache::iDownloadSize
unsigned long long iDownloadSize
Definition depcache.h:290
pkgDepCache::iUsrSize
signed long long iUsrSize
Definition depcache.h:288
pkgVersioningSystem
Definition version.h:27
pkgcache.h
pkgCache - Structure definitions for the cache file
pkgCache::Dep
Definition pkgcache.h:164
pkgCache::Version
information for a single version of a package
Definition pkgcache.h:625
pkgDepCache::StateCache
Definition depcache.h:187
pkgDepCache::StateCache::Garbage
bool Garbage
true if this package is unused and should be removed.
Definition depcache.h:211
pkgDepCache::StateCache::Marked
bool Marked
true if this package can be reached from the root set.
Definition depcache.h:203