From a8352c2859a6f84b36fa5cd0af89231cb656b1ce Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Mon, 12 Feb 2024 14:48:48 +0100 Subject: Add public phased update API This moves the functions of the PhasedUpgrader class into various other classes so they can be publicly exposed. This introduces three new functions: pkgDepCache::PhasingApplied() tells you whether phasing should be applied to the package. pkgProblemResolver::KeepPhasedUpdates() keeps back updates that have phasing applied. pkgCache::VerIterator::IsSecurityUpdate() determines whether this version contains security fixes. --- apt-pkg/depcache.cc | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) (limited to 'apt-pkg/depcache.cc') diff --git a/apt-pkg/depcache.cc b/apt-pkg/depcache.cc index 322bf1bbe..c965781a4 100644 --- a/apt-pkg/depcache.cc +++ b/apt-pkg/depcache.cc @@ -30,11 +30,12 @@ #include #include -#include -#include #include #include +#include +#include #include +#include #include #include #include @@ -141,6 +142,7 @@ struct pkgDepCache::Private { std::unique_ptr inRootSetFunc; std::unique_ptr IsAVersionedKernelPackage, IsProtectedKernelPackage; + std::string machineID; }; pkgDepCache::pkgDepCache(pkgCache *const pCache, Policy *const Plcy) : group_level(0), Cache(pCache), PkgState(0), DepState(0), iUsrSize(0), iDownloadSize(0), iInstCount(0), iDelCount(0), iKeepCount(0), @@ -148,6 +150,7 @@ pkgDepCache::pkgDepCache(pkgCache *const pCache, Policy *const Plcy) : group_lev { DebugMarker = _config->FindB("Debug::pkgDepCache::Marker", false); DebugAutoInstall = _config->FindB("Debug::pkgDepCache::AutoInstall", false); + d->machineID = APT::Configuration::getMachineID(); delLocalPolicy = 0; LocalPolicy = Plcy; if (LocalPolicy == 0) @@ -2562,3 +2565,51 @@ bool pkgDepCache::MarkAndSweep() return false; } /*}}}*/ + +// DepCache::PhasingApplied /*{{{*/ +// Check if this version is a phased update that should be ignored, not considering whether +// it is a security update. +static bool IsIgnoredPhasedUpdate(std::string machineID, pkgCache::VerIterator const &Ver) +{ + if (_config->FindB("APT::Get::Phase-Policy", false)) + return false; + + // The order and fallbacks for the always/never checks come from update-manager and exist + // to preserve compatibility. + if (_config->FindB("APT::Get::Always-Include-Phased-Updates", + _config->FindB("Update-Manager::Always-Include-Phased-Updates", false))) + return false; + + if (_config->FindB("APT::Get::Never-Include-Phased-Updates", + _config->FindB("Update-Manager::Never-Include-Phased-Updates", false))) + return true; + + if (machineID.empty() // no machine-id + || getenv("SOURCE_DATE_EPOCH") != nullptr // reproducible build - always include + || APT::Configuration::isChroot()) + return false; + + std::string seedStr = std::string(Ver.SourcePkgName()) + "-" + Ver.SourceVerStr() + "-" + machineID; + std::seed_seq seed(seedStr.begin(), seedStr.end()); + std::minstd_rand rand(seed); + std::uniform_int_distribution dist(0, 100); + + return dist(rand) > Ver.PhasedUpdatePercentage(); +} + +bool pkgDepCache::PhasingApplied(pkgCache::PkgIterator Pkg) const +{ + if (Pkg->CurrentVer == 0) + return false; + if ((*this)[Pkg].CandidateVer == 0) + return false; + if ((*this)[Pkg].CandidateVerIter(*Cache).PhasedUpdatePercentage() == 100) + return false; + if ((*this)[Pkg].CandidateVerIter(*Cache).IsSecurityUpdate()) + return false; + if (!IsIgnoredPhasedUpdate(d->machineID, (*this)[Pkg].CandidateVerIter(*Cache))) + return false; + + return true; +} + /*}}}*/ -- cgit v1.2.3-70-g09d2