+ // Skip removes if we install another architecture of this package soon (crossgrade)
+ // We can't just skip them all the time as it could be an ordering requirement [of another package]
+ if ((approvedStates.Remove().empty() == false || approvedStates.Purge().empty() == false) &&
+ _config->FindB("dpkg::remove::crossgrade::implicit", true) == true)
+ {
+ std::unordered_set<decltype(pkgCache::Package::ID)> crossgraded;
+ std::vector<std::pair<Item*, std::string>> toCrossgrade;
+ auto const PlanedEnd = std::next(List.begin(), explicitIdx);
+ for (auto I = List.begin(); I != PlanedEnd; ++I)
+ {
+ if (I->Op != Item::Remove && I->Op != Item::Purge)
+ continue;
+
+ auto const Grp = I->Pkg.Group();
+ size_t installedInstances = 0;
+ for (auto Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
+ if (Pkg->CurrentVer != 0 || Cache[Pkg].Install())
+ ++installedInstances;
+ if (installedInstances == 2)
+ {
+ auto const FirstInstall = std::find_if_not(I, List.end(),
+ [](Item const &i) { return i.Op == Item::Remove || i.Op == Item::Purge; });
+ auto const LastInstall = std::find_if_not(FirstInstall, List.end(),
+ [](Item const &i) { return i.Op == Item::Install; });
+ auto const crosser = std::find_if(FirstInstall, LastInstall,
+ [&I](Item const &i) { return i.Pkg->Group == I->Pkg->Group; });
+ if (crosser != LastInstall)
+ {
+ crossgraded.insert(I->Pkg->ID);
+ toCrossgrade.emplace_back(&(*I), crosser->Pkg.FullName());
+ }
+ }
+ }
+ for (auto I = PlanedEnd; I != List.end(); ++I)
+ {
+ if (I->Op != Item::Remove && I->Op != Item::Purge)
+ continue;
+
+ auto const Grp = I->Pkg.Group();
+ for (auto Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
+ {
+ if (Pkg == I->Pkg || Cache[Pkg].Install() == false)
+ continue;
+ toCrossgrade.emplace_back(&(*I), Pkg.FullName());
+ break;
+ }
+ }
+ for (auto C : toCrossgrade)
+ {
+ // we never do purges on packages which are crossgraded, even if "requested"
+ if (C.first->Op == Item::Purge)
+ {
+ C.first->Op = Item::Remove; // crossgrades should never be purged
+ auto && Purges = approvedStates.Purge();
+ auto const Ver = std::find_if(
+#if __GNUC__ >= 5 || (__GNUC_MINOR__ >= 9 && __GNUC__ >= 4)
+ Purges.cbegin(), Purges.cend(),
+#else
+ Purges.begin(), Purges.end(),
+#endif
+ [&C](pkgCache::VerIterator const &V) { return V.ParentPkg() == C.first->Pkg; });
+ approvedStates.Remove(*Ver);
+ Purges.erase(Ver);
+ auto && RemOp = PackageOps[C.first->Pkg.FullName()];
+ if (RemOp.size() == 5)
+ {
+ RemOp.erase(std::next(RemOp.begin(), 3), RemOp.end());
+ PackagesTotal -= 2;
+ }
+ else
+ _error->Warning("Unexpected amount of planned ops for package %s: %lu", C.first->Pkg.FullName().c_str(), RemOp.size());
+ }
+ }
+ if (crossgraded.empty() == false)
+ {
+ auto const oldsize = List.size();
+ List.erase(std::remove_if(List.begin(), PlanedEnd,
+ [&crossgraded](Item const &i){
+ return (i.Op == Item::Remove || i.Op == Item::Purge) &&
+ crossgraded.find(i.Pkg->ID) != crossgraded.end();
+ }), PlanedEnd);
+ explicitIdx -= (oldsize - List.size());
+ }
+ }
+
+ APT::StateChanges currentStates;
+ if (_config->FindB("dpkg::selection::current::saveandrestore", true))
+ {
+ for (auto Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
+ if (Pkg->CurrentVer == 0)
+ continue;
+ else if (Pkg->SelectedState == pkgCache::State::Purge)
+ currentStates.Purge(FindToBeRemovedVersion(Pkg));
+ else if (Pkg->SelectedState == pkgCache::State::DeInstall)
+ currentStates.Remove(FindToBeRemovedVersion(Pkg));
+ if (currentStates.empty() == false)
+ {
+ APT::StateChanges cleanStates;
+ for (auto && P: currentStates.Remove())
+ cleanStates.Install(P);
+ for (auto && P: currentStates.Purge())
+ cleanStates.Install(P);
+ if (cleanStates.Save(false) == false)
+ return _error->Error("Couldn't clean the currently selected dpkg states");
+ }
+ }
+
+ if (_config->FindB("dpkg::selection::remove::approved", true))
+ {
+ if (approvedStates.Save(false) == false)
+ {
+ _error->Error("Couldn't record the approved state changes as dpkg selection states");
+ if (currentStates.Save(false) == false)
+ _error->Error("Couldn't restore dpkg selection states which were present before this interaction!");
+ return false;
+ }
+
+ List.erase(std::next(List.begin(), explicitIdx), List.end());
+
+ std::vector<bool> toBeRemoved(Cache.Head().PackageCount, false);
+ for (auto && I: approvedStates.Remove())
+ toBeRemoved[I.ParentPkg()->ID] = true;
+ for (auto && I: approvedStates.Purge())
+ toBeRemoved[I.ParentPkg()->ID] = true;
+
+ for (auto && I: List)
+ if (I.Op == Item::Remove || I.Op == Item::Purge)
+ toBeRemoved[I.Pkg->ID] = false;
+
+ bool const RemovePending = std::find(toBeRemoved.begin(), toBeRemoved.end(), true) != toBeRemoved.end();
+ bool const PurgePending = approvedStates.Purge().empty() == false;
+ if (RemovePending != false || PurgePending != false)
+ List.emplace_back(Item::ConfigurePending, pkgCache::PkgIterator());
+ if (RemovePending)
+ List.emplace_back(Item::RemovePending, pkgCache::PkgIterator());
+ if (PurgePending)
+ List.emplace_back(Item::PurgePending, pkgCache::PkgIterator());
+
+ // support subpressing of triggers processing for special
+ // cases like d-i that runs the triggers handling manually
+ if (_config->FindB("DPkg::ConfigurePending", true))
+ List.emplace_back(Item::ConfigurePending, pkgCache::PkgIterator());
+ }
+ bool const TriggersPending = _config->FindB("DPkg::TriggersPending", false);
+