depcache.h
// -*- mode: c++; mode: fold -*-
// Description /*{{{*/
/* ######################################################################
DepCache - Dependency Extension data for the cache
This class stores the cache data and a set of extension structures for
monitoring the current state of all the packages. It also generates and
caches the 'install' state of many things. This refers to the state of the
package after an install has been run.
The StateCache::State field can be -1,0,1,2 which is <,=,>,no current.
StateCache::Mode is which of the 3 fields is active.
This structure is important to support the readonly status of the cache
file. When the data is saved the cache will be refereshed from our
internal rep and written to disk. Then the actual persistent data
files will be put on the disk.
Each dependency is compared against 3 target versions to produce to
3 dependency results.
Now - Compared using the Currently install version
Install - Compared using the install version (final state)
CVer - (Candidate Version) Compared using the Candidate Version
The candidate and now results are used to decide whether a package
should be automatically installed or if it should be left alone.
Remember, the Candidate Version is selected based on the distribution
settings for the Package. The Install Version is selected based on the
state (Delete, Keep, Install) field and can be either the Current Version
or the Candidate version.
The Candidate version is what is shown the 'Install Version' field.
##################################################################### */
/*}}}*/
#ifndef PKGLIB_DEPCACHE_H
#define PKGLIB_DEPCACHE_H
#include <apt-pkg/configuration.h>
#include <apt-pkg/macros.h>
#include <apt-pkg/pkgcache.h>
#include <cstddef>
#include <list>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
class OpProgress;
class pkgVersioningSystem;
namespace APT
{
template <class Container>
class PackageContainer;
using PackageVector = PackageContainer<std::vector<pkgCache::PkgIterator>>;
} // namespace APT
class APT_PUBLIC pkgDepCache : protected pkgCache::Namespace
{
public:
class APT_PUBLIC InRootSetFunc
{
public:
virtual bool InRootSet(const pkgCache::PkgIterator &/*pkg*/) {return false;};
virtual ~InRootSetFunc() {};
};
private:
bool MarkRequired(InRootSetFunc &rootFunc);
bool Sweep();
public:
// These flags are used in DepState
enum DepFlags {DepNow = (1 << 0),DepInstall = (1 << 1),DepCVer = (1 << 2),
DepGNow = (1 << 3),DepGInstall = (1 << 4),DepGCVer = (1 << 5)};
// These flags are used in StateCache::DepState
enum DepStateFlags {DepNowPolicy = (1 << 0), DepNowMin = (1 << 1),
DepInstPolicy = (1 << 2), DepInstMin = (1 << 3),
DepCandPolicy = (1 << 4), DepCandMin = (1 << 5)};
// These flags are used in StateCache::iFlags
enum InternalFlags {AutoKept = (1 << 0), Purge = (1 << 1), ReInstall = (1 << 2), Protected = (1 << 3)};
enum VersionTypes {NowVersion, InstallVersion, CandidateVersion};
enum ModeList {ModeDelete = 0, ModeKeep = 1, ModeInstall = 2, ModeGarbage = 3};
class APT_PUBLIC ActionGroup
{
void * const d;
pkgDepCache &cache;
bool released;
APT_HIDDEN ActionGroup(const ActionGroup &other);
public:
explicit ActionGroup(pkgDepCache &cache);
void release();
virtual ~ActionGroup();
};
class APT_PUBLIC DefaultRootSetFunc : public InRootSetFunc, public Configuration::MatchAgainstConfig
{
public:
DefaultRootSetFunc() : Configuration::MatchAgainstConfig("APT::NeverAutoRemove") {};
~DefaultRootSetFunc() override = default;
bool InRootSet(const pkgCache::PkgIterator &pkg) override { return pkg.end() == false && Match(pkg.Name()); };
};
struct APT_PUBLIC StateCache
{
// text versions of the two version fields
const char *CandVersion;
const char *CurVersion;
// Pointer to the candidate install version.
Version *CandidateVer;
// Pointer to the install version.
Version *InstallVer;
// Copy of Package::Flags
unsigned short Flags;
unsigned short iFlags; // Internal flags
bool Marked;
bool Garbage;
// Various tree indicators
signed char Status; // -1,0,1,2
unsigned char Mode; // ModeList
unsigned char DepState; // DepState Flags
// Update of candidate version
void Update(PkgIterator Pkg,pkgCache &Cache);
// Various test members for the current status of the package
inline bool NewInstall() const {return Status == 2 && Mode == ModeInstall;};
inline bool Delete() const {return Mode == ModeDelete;};
inline bool Purge() const {return Delete() == true && (iFlags & pkgDepCache::Purge) == pkgDepCache::Purge; };
inline bool Keep() const {return Mode == ModeKeep;};
inline bool Protect() const {return (iFlags & Protected) == Protected;};
inline bool Upgrade() const {return Status > 0 && Mode == ModeInstall;};
inline bool Upgradable() const {return Status >= 1 && CandidateVer != NULL;};
inline bool Downgrade() const {return Status < 0 && Mode == ModeInstall;};
inline bool Held() const {return Status != 0 && Keep();};
inline bool NowBroken() const {return (DepState & DepNowMin) != DepNowMin;};
inline bool NowPolicyBroken() const {return (DepState & DepNowPolicy) != DepNowPolicy;};
inline bool InstBroken() const {return (DepState & DepInstMin) != DepInstMin;};
inline bool InstPolicyBroken() const {return (DepState & DepInstPolicy) != DepInstPolicy;};
inline bool Install() const {return Mode == ModeInstall;};
inline bool ReInstall() const {return Delete() == false && (iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall;};
inline VerIterator InstVerIter(pkgCache &Cache)
{return VerIterator(Cache,InstallVer);};
inline VerIterator CandidateVerIter(pkgCache &Cache)
{return VerIterator(Cache,CandidateVer);};
};
// Helper functions
void BuildGroupOrs(VerIterator const &V);
void UpdateVerState(PkgIterator const &Pkg);
// User Policy control
class APT_PUBLIC Policy
{
public:
Policy() {
InstallRecommends = _config->FindB("APT::Install-Recommends", false);
InstallSuggests = _config->FindB("APT::Install-Suggests", false);
}
virtual VerIterator GetCandidateVer(PkgIterator const &Pkg);
virtual bool IsImportantDep(DepIterator const &Dep) const;
virtual signed short GetPriority(PkgIterator const &Pkg);
virtual signed short GetPriority(VerIterator const &Ver, bool ConsiderFiles=true);
virtual signed short GetPriority(PkgFileIterator const &File);
virtual ~Policy() {};
private:
bool InstallRecommends;
bool InstallSuggests;
};
class APT_PUBLIC Transaction final
{
struct Private;
std::unique_ptr<Private> d;
public:
enum class Behavior
{
COMMIT,
ROLLBACK,
AUTO,
};
explicit Transaction(pkgDepCache &cache, Behavior behavior);
void commit();
void rollback();
void temporaryRollback();
~Transaction();
};
private:
int group_level;
friend class ActionGroup;
friend class Transaction;
public:
int IncreaseActionGroupLevel();
int DecreaseActionGroupLevel();
protected:
// State information
pkgCache *Cache;
StateCache *PkgState;
unsigned char *DepState;
signed long long iUsrSize;
unsigned long long iDownloadSize;
unsigned long iInstCount;
unsigned long iDelCount;
unsigned long iKeepCount;
unsigned long iBrokenCount;
unsigned long iPolicyBrokenCount;
unsigned long iBadCount;
bool DebugMarker;
bool DebugAutoInstall;
Policy *delLocalPolicy; // For memory clean up..
Policy *LocalPolicy;
// Check for a matching provides
bool CheckDep(DepIterator const &Dep,int const Type,PkgIterator &Res);
inline bool CheckDep(DepIterator const &Dep,int const Type)
{
PkgIterator Res(*this,0);
return CheckDep(Dep,Type,Res);
}
// Computes state information for deps and versions (w/o storing)
unsigned char DependencyState(DepIterator const &D);
unsigned char VersionState(DepIterator D,unsigned char const Check,
unsigned char const SetMin,
unsigned char const SetPolicy) const;
// Recalculates various portions of the cache, call after changing something
void Update(DepIterator Dep); // Mostly internal
void Update(PkgIterator const &P);
// Count manipulators
void AddSizes(const PkgIterator &Pkg, bool const Invert = false);
inline void RemoveSizes(const PkgIterator &Pkg) {AddSizes(Pkg, true);};
void AddStates(const PkgIterator &Pkg, bool const Invert = false);
inline void RemoveStates(const PkgIterator &Pkg) {AddStates(Pkg,true);};
public:
// Legacy.. We look like a pkgCache
inline operator pkgCache &() {return *Cache;};
inline Header &Head() {return *Cache->HeaderP;};
inline GrpIterator GrpBegin() {return Cache->GrpBegin();};
inline PkgIterator PkgBegin() {return Cache->PkgBegin();};
inline GrpIterator FindGrp(std::string_view Name) {return Cache->FindGrp(Name);};
inline PkgIterator FindPkg(std::string_view Name) {return Cache->FindPkg(Name);};
inline PkgIterator FindPkg(std::string_view Name, std::string_view Arch) {return Cache->FindPkg(Name, Arch);};
inline pkgCache &GetCache() {return *Cache;};
inline pkgVersioningSystem &VS() {return *Cache->VS;};
inline bool IsImportantDep(DepIterator Dep) const {return LocalPolicy->IsImportantDep(Dep);};
inline Policy &GetPolicy() {return *LocalPolicy;};
// Accessors
inline StateCache &operator [](PkgIterator const &I) {return PkgState[I->ID];};
inline StateCache &operator [](PkgIterator const &I) const {return PkgState[I->ID];};
inline unsigned char &operator [](DepIterator const &I) {return DepState[I->ID];};
inline unsigned char const &operator [](DepIterator const &I) const {return DepState[I->ID];};
virtual InRootSetFunc *GetRootSetFunc();
InRootSetFunc *GetCachedRootSetFunc() APT_HIDDEN;
virtual bool MarkFollowsRecommends();
virtual bool MarkFollowsSuggests();
bool MarkAndSweep(InRootSetFunc &rootFunc);
bool MarkAndSweep();
bool PhasingApplied(PkgIterator Pkg) const;
// @{
bool MarkKeep(PkgIterator const &Pkg, bool Soft = false,
bool FromUser = true, unsigned long Depth = 0);
bool MarkDelete(PkgIterator const &Pkg, bool MarkPurge = false,
unsigned long Depth = 0, bool FromUser = true);
bool MarkInstall(PkgIterator const &Pkg,bool AutoInst = true,
unsigned long Depth = 0, bool FromUser = true,
bool ForceImportantDeps = false);
void MarkProtected(PkgIterator const &Pkg) { PkgState[Pkg->ID].iFlags |= Protected; };
void SetReInstall(PkgIterator const &Pkg,bool To);
pkgCache::VerIterator GetCandidateVersion(pkgCache::PkgIterator const &Pkg);
void SetCandidateVersion(VerIterator TargetVer);
bool SetCandidateRelease(pkgCache::VerIterator TargetVer,
std::string const &TargetRel);
bool SetCandidateRelease(pkgCache::VerIterator TargetVer,
std::string const &TargetRel,
std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > &Changed);
void MarkAuto(const PkgIterator &Pkg, bool Auto);
// @}
virtual bool IsInstallOk(const PkgIterator &Pkg,bool AutoInst = true,
unsigned long Depth = 0, bool FromUser = true);
virtual bool IsDeleteOk(const PkgIterator &Pkg,bool MarkPurge = false,
unsigned long Depth = 0, bool FromUser = true);
// read persistent states
bool readStateFile(OpProgress * const prog);
bool writeStateFile(OpProgress * const prog, bool const InstalledOnly=true);
// Size queries
inline signed long long UsrSize() {return iUsrSize;};
inline unsigned long long DebSize() {return iDownloadSize;};
inline unsigned long DelCount() {return iDelCount;};
inline unsigned long KeepCount() {return iKeepCount;};
inline unsigned long InstCount() {return iInstCount;};
unsigned long UpgradeCount();
inline unsigned long BrokenCount() {return iBrokenCount;};
inline unsigned long PolicyBrokenCount() {return iPolicyBrokenCount;};
inline unsigned long BadCount() {return iBadCount;};
bool Init(OpProgress * const Prog);
// Generate all state information
void Update(OpProgress * const Prog = 0);
pkgDepCache(pkgCache * const Cache,Policy * const Plcy = 0);
virtual ~pkgDepCache();
bool CheckConsistency(char const *const msgtag = "");
#ifdef APT_COMPILING_APT
unsigned long long BootSize(bool initrdOnly);
#endif
protected:
// methods call by IsInstallOk
bool IsInstallOkMultiArchSameVersionSynced(PkgIterator const &Pkg,
bool const AutoInst, unsigned long const Depth, bool const FromUser);
bool IsInstallOkDependenciesSatisfiableByCandidates(PkgIterator const &Pkg,
bool const AutoInst, unsigned long const Depth, bool const FromUser);
// methods call by IsDeleteOk
bool IsDeleteOkProtectInstallRequests(PkgIterator const &Pkg,
bool const rPurge, unsigned long const Depth, bool const FromUser);
private:
struct Private;
Private *const d;
APT_HIDDEN bool MarkInstall_StateChange(PkgIterator const &Pkg, bool AutoInst, bool FromUser);
APT_HIDDEN bool MarkInstall_DiscardInstall(PkgIterator const &Pkg);
APT_HIDDEN void PerformDependencyPass(OpProgress * const Prog);
};
#endif
APT::PackageContainer
Definition cacheset.h:347
Configuration::MatchAgainstConfig
match a string against a configurable list of patterns
Definition configuration.h:130
Configuration::MatchAgainstConfig::Match
bool Match(char const *str) const
Returns true for a string matching one of the patterns.
Definition configuration.cc:1228
OpProgress
Definition progress.h:30
pkgCache::Namespace
Definition pkgcache.h:853
pkgCache::PkgIterator
Definition cacheiterators.h:147
pkgCache
Definition pkgcache.h:126
pkgDepCache::ActionGroup
Represents an active action group.
Definition depcache.h:142
pkgDepCache::ActionGroup::release
void release()
Clean up the action group before it is destroyed.
Definition depcache.cc:78
pkgDepCache::InRootSetFunc
An arbitrary predicate on packages.
Definition depcache.h:68
pkgDepCache::Policy
Definition depcache.h:250
pkgDepCache::Policy::IsImportantDep
virtual bool IsImportantDep(DepIterator const &Dep) const
Definition depcache.cc:2241
pkgDepCache::Transaction
Perform changes to the depcache atomically.
Definition depcache.h:278
pkgDepCache::Transaction::rollback
void rollback()
Rollback the transaction immediately.
Definition depcache.cc:2723
pkgDepCache::Transaction::temporaryRollback
void temporaryRollback()
Like rollback, but can be called multiple times.
Definition depcache.cc:2713
pkgDepCache::Transaction::commit
void commit()
Commit the transaction immediately.
Definition depcache.cc:2718
pkgDepCache::iDownloadSize
unsigned long long iDownloadSize
Definition depcache.h:329
pkgDepCache::iUsrSize
signed long long iUsrSize
Definition depcache.h:327
pkgVersioningSystem
Definition version.h:27
pkgcache.h
pkgCache - Structure definitions for the cache file
pkgDepCache::StateCache
Definition depcache.h:188
pkgDepCache::StateCache::Garbage
bool Garbage
true if this package is unused and should be removed.
Definition depcache.h:212
pkgDepCache::StateCache::Marked
bool Marked
true if this package can be reached from the root set.
Definition depcache.h:204
pkgDepCache::Transaction::Private
Definition depcache.cc:2671