+ 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;
+
+ if (std::find(toBeRemoved.begin(), toBeRemoved.end(), true) != toBeRemoved.end())
+ List.emplace_back(Item::RemovePending, pkgCache::PkgIterator());
+ if (approvedStates.Purge().empty() == false)
+ 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());