apt 3.0.3
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 <string_view>
50#include <utility>
51
52
53class OpProgress;
55namespace APT
56{
57template <class Container>
58class PackageContainer;
59using PackageVector = PackageContainer<std::vector<pkgCache::PkgIterator>>;
60} // namespace APT
61
62class APT_PUBLIC pkgDepCache : protected pkgCache::Namespace
63{
64 public:
65
67 class APT_PUBLIC InRootSetFunc
68 {
69 public:
70 virtual bool InRootSet(const pkgCache::PkgIterator &/*pkg*/) {return false;};
71 virtual ~InRootSetFunc() {};
72 };
73
74 private:
88 bool MarkRequired(InRootSetFunc &rootFunc);
89
99 bool Sweep();
100
101 public:
102
103 // These flags are used in DepState
104 enum DepFlags {DepNow = (1 << 0),DepInstall = (1 << 1),DepCVer = (1 << 2),
105 DepGNow = (1 << 3),DepGInstall = (1 << 4),DepGCVer = (1 << 5)};
106
107 // These flags are used in StateCache::DepState
108 enum DepStateFlags {DepNowPolicy = (1 << 0), DepNowMin = (1 << 1),
109 DepInstPolicy = (1 << 2), DepInstMin = (1 << 3),
110 DepCandPolicy = (1 << 4), DepCandMin = (1 << 5)};
111
112 // These flags are used in StateCache::iFlags
113 enum InternalFlags {AutoKept = (1 << 0), Purge = (1 << 1), ReInstall = (1 << 2), Protected = (1 << 3)};
114
115 enum VersionTypes {NowVersion, InstallVersion, CandidateVersion};
116 enum ModeList {ModeDelete = 0, ModeKeep = 1, ModeInstall = 2, ModeGarbage = 3};
117
141 class APT_PUBLIC ActionGroup
142 {
143 void * const d;
144 pkgDepCache &cache;
145
146 bool released;
147
149 APT_HIDDEN ActionGroup(const ActionGroup &other);
150 public:
159 explicit ActionGroup(pkgDepCache &cache);
160
165 void release();
166
172 virtual ~ActionGroup();
173 };
174
179 {
180 public:
181 DefaultRootSetFunc() : Configuration::MatchAgainstConfig("APT::NeverAutoRemove") {};
182 ~DefaultRootSetFunc() override = default;
183
184 bool InRootSet(const pkgCache::PkgIterator &pkg) override { return pkg.end() == false && Match(pkg.Name()); };
185 };
186
187 struct APT_PUBLIC StateCache
188 {
189 // text versions of the two version fields
190 const char *CandVersion;
191 const char *CurVersion;
192
193 // Pointer to the candidate install version.
194 Version *CandidateVer;
195
196 // Pointer to the install version.
197 Version *InstallVer;
198
199 // Copy of Package::Flags
200 unsigned short Flags;
201 unsigned short iFlags; // Internal flags
202
204 bool Marked;
205
213
214 // Various tree indicators
215 signed char Status; // -1,0,1,2
216 unsigned char Mode; // ModeList
217 unsigned char DepState; // DepState Flags
218
219 // Update of candidate version
220 void Update(PkgIterator Pkg,pkgCache &Cache);
221
222 // Various test members for the current status of the package
223 inline bool NewInstall() const {return Status == 2 && Mode == ModeInstall;};
224 inline bool Delete() const {return Mode == ModeDelete;};
225 inline bool Purge() const {return Delete() == true && (iFlags & pkgDepCache::Purge) == pkgDepCache::Purge; };
226 inline bool Keep() const {return Mode == ModeKeep;};
227 inline bool Protect() const {return (iFlags & Protected) == Protected;};
228 inline bool Upgrade() const {return Status > 0 && Mode == ModeInstall;};
229 inline bool Upgradable() const {return Status >= 1 && CandidateVer != NULL;};
230 inline bool Downgrade() const {return Status < 0 && Mode == ModeInstall;};
231 inline bool Held() const {return Status != 0 && Keep();};
232 inline bool NowBroken() const {return (DepState & DepNowMin) != DepNowMin;};
233 inline bool NowPolicyBroken() const {return (DepState & DepNowPolicy) != DepNowPolicy;};
234 inline bool InstBroken() const {return (DepState & DepInstMin) != DepInstMin;};
235 inline bool InstPolicyBroken() const {return (DepState & DepInstPolicy) != DepInstPolicy;};
236 inline bool Install() const {return Mode == ModeInstall;};
237 inline bool ReInstall() const {return Delete() == false && (iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall;};
238 inline VerIterator InstVerIter(pkgCache &Cache)
239 {return VerIterator(Cache,InstallVer);};
240 inline VerIterator CandidateVerIter(pkgCache &Cache)
241 {return VerIterator(Cache,CandidateVer);};
242 };
243
244 // Helper functions
245 void BuildGroupOrs(VerIterator const &V);
246 void UpdateVerState(PkgIterator const &Pkg);
247
248 // User Policy control
249 class APT_PUBLIC Policy
250 {
251 public:
252 Policy() {
253 InstallRecommends = _config->FindB("APT::Install-Recommends", false);
254 InstallSuggests = _config->FindB("APT::Install-Suggests", false);
255 }
256
257 virtual VerIterator GetCandidateVer(PkgIterator const &Pkg);
258 virtual bool IsImportantDep(DepIterator const &Dep) const;
259 virtual signed short GetPriority(PkgIterator const &Pkg);
260 virtual signed short GetPriority(VerIterator const &Ver, bool ConsiderFiles=true);
261 virtual signed short GetPriority(PkgFileIterator const &File);
262
263 virtual ~Policy() {};
264
265 private:
266 bool InstallRecommends;
267 bool InstallSuggests;
268 };
269
277 class APT_PUBLIC Transaction final
278 {
279 struct Private;
280 std::unique_ptr<Private> d;
281
282 public:
283 enum class Behavior
284 {
285 COMMIT,
286 ROLLBACK,
287 AUTO,
288 };
289
290 explicit Transaction(pkgDepCache &cache, Behavior behavior);
292 void commit();
294 void rollback();
301 void temporaryRollback();
303 ~Transaction();
304 };
305
306 private:
310 int group_level;
311
312 friend class ActionGroup;
313 friend class Transaction;
314
315 public:
316 int IncreaseActionGroupLevel();
317 int DecreaseActionGroupLevel();
318
319 protected:
320
321 // State information
322 pkgCache *Cache;
323 StateCache *PkgState;
324 unsigned char *DepState;
325
327 signed long long iUsrSize;
329 unsigned long long iDownloadSize;
330 unsigned long iInstCount;
331 unsigned long iDelCount;
332 unsigned long iKeepCount;
333 unsigned long iBrokenCount;
334 unsigned long iPolicyBrokenCount;
335 unsigned long iBadCount;
336
337 bool DebugMarker;
338 bool DebugAutoInstall;
339
340 Policy *delLocalPolicy; // For memory clean up..
341 Policy *LocalPolicy;
342
343 // Check for a matching provides
344 bool CheckDep(DepIterator const &Dep,int const Type,PkgIterator &Res);
345 inline bool CheckDep(DepIterator const &Dep,int const Type)
346 {
347 PkgIterator Res(*this,0);
348 return CheckDep(Dep,Type,Res);
349 }
350
351 // Computes state information for deps and versions (w/o storing)
352 unsigned char DependencyState(DepIterator const &D);
353 unsigned char VersionState(DepIterator D,unsigned char const Check,
354 unsigned char const SetMin,
355 unsigned char const SetPolicy) const;
356
357 // Recalculates various portions of the cache, call after changing something
358 void Update(DepIterator Dep); // Mostly internal
359 void Update(PkgIterator const &P);
360
361 // Count manipulators
362 void AddSizes(const PkgIterator &Pkg, bool const Invert = false);
363 inline void RemoveSizes(const PkgIterator &Pkg) {AddSizes(Pkg, true);};
364 void AddStates(const PkgIterator &Pkg, bool const Invert = false);
365 inline void RemoveStates(const PkgIterator &Pkg) {AddStates(Pkg,true);};
366
367 public:
368
369 // Legacy.. We look like a pkgCache
370 inline operator pkgCache &() {return *Cache;};
371 inline Header &Head() {return *Cache->HeaderP;};
372 inline GrpIterator GrpBegin() {return Cache->GrpBegin();};
373 inline PkgIterator PkgBegin() {return Cache->PkgBegin();};
374 inline GrpIterator FindGrp(std::string_view Name) {return Cache->FindGrp(Name);};
375 inline PkgIterator FindPkg(std::string_view Name) {return Cache->FindPkg(Name);};
376 inline PkgIterator FindPkg(std::string_view Name, std::string_view Arch) {return Cache->FindPkg(Name, Arch);};
377
378 inline pkgCache &GetCache() {return *Cache;};
379 inline pkgVersioningSystem &VS() {return *Cache->VS;};
380
381 inline bool IsImportantDep(DepIterator Dep) const {return LocalPolicy->IsImportantDep(Dep);};
382 inline Policy &GetPolicy() {return *LocalPolicy;};
383
384 // Accessors
385 inline StateCache &operator [](PkgIterator const &I) {return PkgState[I->ID];};
386 inline StateCache &operator [](PkgIterator const &I) const {return PkgState[I->ID];};
387 inline unsigned char &operator [](DepIterator const &I) {return DepState[I->ID];};
388 inline unsigned char const &operator [](DepIterator const &I) const {return DepState[I->ID];};
389
398 virtual InRootSetFunc *GetRootSetFunc();
399
401 InRootSetFunc *GetCachedRootSetFunc() APT_HIDDEN;
402
405 virtual bool MarkFollowsRecommends();
406
409 virtual bool MarkFollowsSuggests();
410
420 bool MarkAndSweep(InRootSetFunc &rootFunc);
421 bool MarkAndSweep();
422
427 bool PhasingApplied(PkgIterator Pkg) const;
428
431 // @{
432 bool MarkKeep(PkgIterator const &Pkg, bool Soft = false,
433 bool FromUser = true, unsigned long Depth = 0);
434 bool MarkDelete(PkgIterator const &Pkg, bool MarkPurge = false,
435 unsigned long Depth = 0, bool FromUser = true);
436 bool MarkInstall(PkgIterator const &Pkg,bool AutoInst = true,
437 unsigned long Depth = 0, bool FromUser = true,
438 bool ForceImportantDeps = false);
439 void MarkProtected(PkgIterator const &Pkg) { PkgState[Pkg->ID].iFlags |= Protected; };
440
441 void SetReInstall(PkgIterator const &Pkg,bool To);
442
451 pkgCache::VerIterator GetCandidateVersion(pkgCache::PkgIterator const &Pkg);
452 void SetCandidateVersion(VerIterator TargetVer);
453 bool SetCandidateRelease(pkgCache::VerIterator TargetVer,
454 std::string const &TargetRel);
469 bool SetCandidateRelease(pkgCache::VerIterator TargetVer,
470 std::string const &TargetRel,
471 std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > &Changed);
472
474 void MarkAuto(const PkgIterator &Pkg, bool Auto);
475 // @}
476
492 virtual bool IsInstallOk(const PkgIterator &Pkg,bool AutoInst = true,
493 unsigned long Depth = 0, bool FromUser = true);
494
507 virtual bool IsDeleteOk(const PkgIterator &Pkg,bool MarkPurge = false,
508 unsigned long Depth = 0, bool FromUser = true);
509
510 // read persistent states
511 bool readStateFile(OpProgress * const prog);
512 bool writeStateFile(OpProgress * const prog, bool const InstalledOnly=true);
513
514 // Size queries
515 inline signed long long UsrSize() {return iUsrSize;};
516 inline unsigned long long DebSize() {return iDownloadSize;};
517 inline unsigned long DelCount() {return iDelCount;};
518 inline unsigned long KeepCount() {return iKeepCount;};
519 inline unsigned long InstCount() {return iInstCount;};
520 unsigned long UpgradeCount();
521 inline unsigned long BrokenCount() {return iBrokenCount;};
522 inline unsigned long PolicyBrokenCount() {return iPolicyBrokenCount;};
523 inline unsigned long BadCount() {return iBadCount;};
524
525 bool Init(OpProgress * const Prog);
526 // Generate all state information
527 void Update(OpProgress * const Prog = 0);
528
529 pkgDepCache(pkgCache * const Cache,Policy * const Plcy = 0);
530 virtual ~pkgDepCache();
531
532 bool CheckConsistency(char const *const msgtag = "");
533#ifdef APT_COMPILING_APT
534 unsigned long long BootSize(bool initrdOnly);
535#endif
536 protected:
537 // methods call by IsInstallOk
538 bool IsInstallOkMultiArchSameVersionSynced(PkgIterator const &Pkg,
539 bool const AutoInst, unsigned long const Depth, bool const FromUser);
540 bool IsInstallOkDependenciesSatisfiableByCandidates(PkgIterator const &Pkg,
541 bool const AutoInst, unsigned long const Depth, bool const FromUser);
542
543 // methods call by IsDeleteOk
544 bool IsDeleteOkProtectInstallRequests(PkgIterator const &Pkg,
545 bool const rPurge, unsigned long const Depth, bool const FromUser);
546
547 private:
548 struct Private;
549 Private *const d;
550
551 APT_HIDDEN bool MarkInstall_StateChange(PkgIterator const &Pkg, bool AutoInst, bool FromUser);
552 APT_HIDDEN bool MarkInstall_DiscardInstall(PkgIterator const &Pkg);
553
554 APT_HIDDEN void PerformDependencyPass(OpProgress * const Prog);
555};
556
557#endif
match a string against a configurable list of patterns
Definition configuration.h:129
Definition progress.h:30
Definition pkgcache.h:852
Definition cacheiterators.h:47
Represents an active action group.
Definition depcache.h:142
Returns true for packages matching a regular expression in APT::NeverAutoRemove.
Definition depcache.h:179
An arbitrary predicate on packages.
Definition depcache.h:68
Definition depcache.h:250
virtual bool IsImportantDep(DepIterator const &Dep) const
Definition depcache.cc:2239
Perform changes to the depcache atomically.
Definition depcache.h:278
Definition depcache.h:63
unsigned long long iDownloadSize
Definition depcache.h:329
signed long long iUsrSize
Definition depcache.h:327
Definition version.h:27
pkgCache - Structure definitions for the cache file
Definition pkgcache.h:166
information for a single version of a package
Definition pkgcache.h:652
Definition depcache.h:188
bool Garbage
true if this package is unused and should be removed.
Definition depcache.h:212
bool Marked
true if this package can be reached from the root set.
Definition depcache.h:204
Definition depcache.cc:2669