+#ifdef _DIRENT_HAVE_D_TYPE
+ if (unlikely(Ent->d_type != DT_LNK && Ent->d_type != DT_UNKNOWN))
+ continue;
+#endif
+ if (unlikely(unlinkat(dfd, Ent->d_name, 0) != 0))
+ break;
+ }
+ closedir(D);
+ rmdir(tmpdir);
+ }
+ free(tmpdir);
+}
+ /*}}}*/
+
+// DPkgPM::Go - Run the sequence /*{{{*/
+// ---------------------------------------------------------------------
+/* This globs the operations and calls dpkg
+ *
+ * If it is called with a progress object apt will report the install
+ * progress to this object. It maps the dpkg states a package goes
+ * through to human readable (and i10n-able)
+ * names and calculates a percentage for each step.
+ */
+static bool ItemIsEssential(pkgDPkgPM::Item const &I)
+{
+ static auto const cachegen = _config->Find("pkgCacheGen::Essential");
+ if (cachegen == "none" || cachegen == "native")
+ return true;
+ if (unlikely(I.Pkg.end()))
+ return true;
+ return (I.Pkg->Flags & pkgCache::Flag::Essential) != 0;
+}
+bool pkgDPkgPM::ExpandPendingCalls(std::vector<Item> &List, pkgDepCache &Cache)
+{
+ {
+ std::unordered_set<decltype(pkgCache::Package::ID)> alreadyRemoved;
+ for (auto && I : List)
+ if (I.Op == Item::Remove || I.Op == Item::Purge)
+ alreadyRemoved.insert(I.Pkg->ID);
+ std::remove_reference<decltype(List)>::type AppendList;
+ for (auto Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
+ if (Cache[Pkg].Delete() && alreadyRemoved.insert(Pkg->ID).second == true)
+ AppendList.emplace_back(Cache[Pkg].Purge() ? Item::Purge : Item::Remove, Pkg);
+ std::move(AppendList.begin(), AppendList.end(), std::back_inserter(List));
+ }
+ {
+ std::unordered_set<decltype(pkgCache::Package::ID)> alreadyConfigured;
+ for (auto && I : List)
+ if (I.Op == Item::Configure)
+ alreadyConfigured.insert(I.Pkg->ID);
+ std::remove_reference<decltype(List)>::type AppendList;
+ for (auto && I : List)
+ if (I.Op == Item::Install && alreadyConfigured.insert(I.Pkg->ID).second == true)
+ AppendList.emplace_back(Item::Configure, I.Pkg);
+ for (auto Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
+ if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure && alreadyConfigured.insert(Pkg->ID).second == true)
+ AppendList.emplace_back(Item::Configure, Pkg);
+ std::move(AppendList.begin(), AppendList.end(), std::back_inserter(List));
+ }
+ return true;
+}
+bool pkgDPkgPM::Go(APT::Progress::PackageManager *progress)
+{
+ // explicitely remove&configure everything for hookscripts and progress building
+ // we need them only temporarily through, so keep the length and erase afterwards
+ decltype(List)::const_iterator::difference_type explicitIdx =
+ std::distance(List.cbegin(), List.cend());
+ ExpandPendingCalls(List, Cache);
+
+ /* if dpkg told us that it has already done everything to the package we wanted it to do,
+ we shouldn't ask it for "more" later. That can e.g. happen if packages without conffiles
+ are purged as they will have pass through the purge states on remove already */
+ auto const StripAlreadyDoneFrom = [&](APT::VersionVector & Pending) {
+ Pending.erase(std::remove_if(Pending.begin(), Pending.end(), [&](pkgCache::VerIterator const &Ver) {
+ auto const PN = Ver.ParentPkg().FullName();
+ auto const POD = PackageOpsDone.find(PN);
+ if (POD == PackageOpsDone.end())
+ return false;
+ return PackageOps[PN].size() <= POD->second;
+ }), Pending.end());
+ };
+
+ pkgPackageManager::SigINTStop = false;
+ d->progress = progress;
+
+ // Generate the base argument list for dpkg
+ std::vector<std::string> const sArgs = debSystem::GetDpkgBaseCommand();
+ std::vector<const char *> Args(sArgs.size(), NULL);
+ std::transform(sArgs.begin(), sArgs.end(), Args.begin(),
+ [](std::string const &s) { return s.c_str(); });
+ unsigned long long const StartSize = std::accumulate(sArgs.begin(), sArgs.end(), 0llu,
+ [](unsigned long long const i, std::string const &s) { return i + s.length(); });
+ size_t const BaseArgs = Args.size();
+
+ fd_set rfds;
+ struct timespec tv;
+
+ // try to figure out the max environment size
+ int OSArgMax = sysconf(_SC_ARG_MAX);
+ if(OSArgMax < 0)
+ OSArgMax = 32*1024;
+ OSArgMax -= EnvironmentSize() - 2*1024;
+ unsigned int const MaxArgBytes = _config->FindI("Dpkg::MaxArgBytes", OSArgMax);
+ bool const NoTriggers = _config->FindB("DPkg::NoTriggers", true);
+
+ if (RunScripts("DPkg::Pre-Invoke") == false)
+ return false;
+
+ if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false)
+ return false;
+
+ auto const noopDPkgInvocation = _config->FindB("Debug::pkgDPkgPM",false);
+ // store auto-bits as they are supposed to be after dpkg is run
+ if (noopDPkgInvocation == false)
+ Cache.writeStateFile(NULL);
+
+ bool dpkg_recursive_install = _config->FindB("dpkg::install::recursive", false);
+ if (_config->FindB("dpkg::install::recursive::force", false) == false)
+ {
+ // dpkg uses a sorted treewalk since that version which enables the workaround to work
+ auto const dpkgpkg = Cache.FindPkg("dpkg");
+ if (likely(dpkgpkg.end() == false && dpkgpkg->CurrentVer != 0))
+ dpkg_recursive_install = Cache.VS().CmpVersion("1.18.5", dpkgpkg.CurrentVer().VerStr()) <= 0;
+ }
+ // no point in doing this dance for a handful of packages only
+ unsigned int const dpkg_recursive_install_min = _config->FindB("dpkg::install::recursive::minimum", 5);
+ // FIXME: workaround for dpkg bug, see our ./test-bug-740843-versioned-up-down-breaks test
+ bool const dpkg_recursive_install_numbered = _config->FindB("dpkg::install::recursive::numbered", true);
+
+ // for the progress
+ BuildPackagesProgressMap();
+
+ APT::StateChanges approvedStates;
+ if (_config->FindB("dpkg::selection::remove::approved", true))
+ {
+ for (auto && I : List)
+ if (I.Op == Item::Purge)
+ approvedStates.Purge(FindToBeRemovedVersion(I.Pkg));
+ else if (I.Op == Item::Remove)
+ approvedStates.Remove(FindToBeRemovedVersion(I.Pkg));
+ }
+
+ // 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());