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/upgrade.cc | 118 +++++------------------------------------------------ 1 file changed, 10 insertions(+), 108 deletions(-) (limited to 'apt-pkg/upgrade.cc') diff --git a/apt-pkg/upgrade.cc b/apt-pkg/upgrade.cc index c7e566a55..fad47838c 100644 --- a/apt-pkg/upgrade.cc +++ b/apt-pkg/upgrade.cc @@ -18,101 +18,6 @@ #include /*}}}*/ -struct PhasedUpgrader -{ - std::string machineID; - bool isChroot; - - PhasedUpgrader() - { - machineID = APT::Configuration::getMachineID(); - } - - // See if this version is a security update. This also checks, for installed packages, - // if any of the previous versions is a security update - bool IsSecurityUpdate(pkgCache::VerIterator const &Ver) - { - auto Pkg = Ver.ParentPkg(); - auto Installed = Pkg.CurrentVer(); - - auto OtherVer = Pkg.VersionList(); - - // Advance to first version < our version - while (OtherVer->ID != Ver->ID) - ++OtherVer; - - // Iterate over all versions < our version - for (; !OtherVer.end() && (Installed.end() || OtherVer->ID != Installed->ID); OtherVer++) - { - for (auto PF = OtherVer.FileList(); !PF.end(); PF++) - if (PF.File() && PF.File().Archive() != nullptr && APT::String::Endswith(PF.File().Archive(), "-security")) - return true; - } - return false; - } - - // Check if this version is a phased update that should be ignored - bool IsIgnoredPhasedUpdate(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 ShouldKeep(pkgDepCache &Cache, pkgCache::PkgIterator Pkg) - { - if (Pkg->CurrentVer == 0) - return false; - if (Cache[Pkg].CandidateVer == 0) - return false; - if (Cache[Pkg].CandidateVerIter(Cache).PhasedUpdatePercentage() == 100) - return false; - if (IsSecurityUpdate(Cache[Pkg].CandidateVerIter(Cache))) - return false; - if (!IsIgnoredPhasedUpdate(Cache[Pkg].CandidateVerIter(Cache))) - return false; - - return true; - } - - // Hold back upgrades to phased versions of already installed packages, unless - // they are security updates - void HoldBackIgnoredPhasedUpdates(pkgDepCache &Cache, pkgProblemResolver *Fix) - { - for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I) - { - if (not ShouldKeep(Cache, I)) - continue; - - Cache.MarkKeep(I, false, false); - Cache.MarkProtected(I); - if (Fix != nullptr) - Fix->Protect(I); - } - } -}; - // DistUpgrade - Distribution upgrade /*{{{*/ // --------------------------------------------------------------------- /* This autoinstalls every package and then force installs every @@ -132,14 +37,13 @@ static bool pkgDistUpgrade(pkgDepCache &Cache, OpProgress * const Progress) Progress->OverallProgress(0, 100, 1, _("Calculating upgrade")); pkgDepCache::ActionGroup group(Cache); - PhasedUpgrader phasedUpgrader; /* Upgrade all installed packages first without autoinst to help the resolver in versioned or-groups to upgrade the old solver instead of installing a new one (if the old solver is not the first one [anymore]) */ for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I) { - if (phasedUpgrader.ShouldKeep(Cache, I)) + if (Cache.PhasingApplied(I)) continue; if (I->CurrentVer != 0) Cache.MarkInstall(I, false, 0, false); @@ -152,7 +56,7 @@ static bool pkgDistUpgrade(pkgDepCache &Cache, OpProgress * const Progress) for the installation */ for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I) { - if (phasedUpgrader.ShouldKeep(Cache, I)) + if (Cache.PhasingApplied(I)) continue; if (I->CurrentVer != 0) Cache.MarkInstall(I, true, 0, false); @@ -184,7 +88,7 @@ static bool pkgDistUpgrade(pkgDepCache &Cache, OpProgress * const Progress) if (isEssential == false || instEssential == true) continue; pkgCache::PkgIterator P = G.FindPreferredPkg(); - if (phasedUpgrader.ShouldKeep(Cache, P)) + if (Cache.PhasingApplied(P)) continue; Cache.MarkInstall(P, true, 0, false); } @@ -192,7 +96,7 @@ static bool pkgDistUpgrade(pkgDepCache &Cache, OpProgress * const Progress) else if (essential != "none") for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I) { - if (phasedUpgrader.ShouldKeep(Cache, I)) + if (Cache.PhasingApplied(I)) continue; if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential) Cache.MarkInstall(I, true, 0, false); @@ -205,7 +109,7 @@ static bool pkgDistUpgrade(pkgDepCache &Cache, OpProgress * const Progress) conflict resolution on them all. */ for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I) { - if (phasedUpgrader.ShouldKeep(Cache, I)) + if (Cache.PhasingApplied(I)) continue; if (I->CurrentVer != 0) Cache.MarkInstall(I, false, 0, false); @@ -245,7 +149,7 @@ static bool pkgDistUpgrade(pkgDepCache &Cache, OpProgress * const Progress) for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I) if (Cache[I].InstPolicyBroken()) FixPhasing.AllowBrokenPolicy(I); - PhasedUpgrader().HoldBackIgnoredPhasedUpdates(Cache, &FixPhasing); + FixPhasing.KeepPhasedUpdates(); success = FixPhasing.ResolveByKeepInternal(); } @@ -267,7 +171,6 @@ static bool pkgAllUpgradeNoNewPackages(pkgDepCache &Cache, OpProgress * const Pr pkgDepCache::ActionGroup group(Cache); pkgProblemResolver Fix(&Cache); - PhasedUpgrader phasedUpgrader; // Upgrade all installed packages for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I) { @@ -278,7 +181,7 @@ static bool pkgAllUpgradeNoNewPackages(pkgDepCache &Cache, OpProgress * const Pr if (I->SelectedState == pkgCache::State::Hold) continue; - if (phasedUpgrader.ShouldKeep(Cache, I)) + if (Cache.PhasingApplied(I)) continue; if (I->CurrentVer != 0 && Cache[I].InstallVer != 0) @@ -288,7 +191,7 @@ static bool pkgAllUpgradeNoNewPackages(pkgDepCache &Cache, OpProgress * const Pr if (Progress != NULL) Progress->Progress(50); - phasedUpgrader.HoldBackIgnoredPhasedUpdates(Cache, &Fix); + Fix.KeepPhasedUpdates(); // resolve remaining issues via keep bool const success = Fix.ResolveByKeepInternal(); @@ -316,7 +219,6 @@ static bool pkgAllUpgradeWithNewPackages(pkgDepCache &Cache, OpProgress * const pkgDepCache::ActionGroup group(Cache); pkgProblemResolver Fix(&Cache); - PhasedUpgrader phasedUpgrader; // provide the initial set of stuff we want to upgrade by marking // all upgradable packages for upgrade @@ -327,7 +229,7 @@ static bool pkgAllUpgradeWithNewPackages(pkgDepCache &Cache, OpProgress * const if (_config->FindB("APT::Ignore-Hold",false) == false) if (I->SelectedState == pkgCache::State::Hold) continue; - if (phasedUpgrader.ShouldKeep(Cache, I)) + if (Cache.PhasingApplied(I)) continue; Cache.MarkInstall(I, false, 0, false); @@ -353,7 +255,7 @@ static bool pkgAllUpgradeWithNewPackages(pkgDepCache &Cache, OpProgress * const if (Progress != NULL) Progress->Progress(60); - phasedUpgrader.HoldBackIgnoredPhasedUpdates(Cache, &Fix); + Fix.KeepPhasedUpdates(); // resolve remaining issues via keep bool const success = Fix.ResolveByKeepInternal(); -- cgit v1.2.3-70-g09d2