From: David Kalnischkies <david@kalnischkies.de>
Date: Sat, 4 Jun 2016 16:45:01 +0000 (+0200)
Subject: edsp: use an ID mapping for the internal solver
X-Git-Tag: 1.3_exp2~20
X-Git-Url: https://git.saurik.com/apt.git/commitdiff_plain/307d9eb2d13ee59191b86ffec2f36ba3fffc5c20

edsp: use an ID mapping for the internal solver

Currently an EDSP solver gets send basically all versions which means
the absolute count is the same, but that might not be true forever (and
with the skipping of rc-only versions it kinda is already) and even if
it were true, segfaulting on bad input seems wrong.
---

diff --git a/apt-pkg/edsp.cc b/apt-pkg/edsp.cc
index 2aaffcfc7..f8925072e 100644
--- a/apt-pkg/edsp.cc
+++ b/apt-pkg/edsp.cc
@@ -17,6 +17,7 @@
 #include <apt-pkg/edsp.h>
 #include <apt-pkg/tagfile.h>
 #include <apt-pkg/strutl.h>
+#include <apt-pkg/pkgsystem.h>
 
 #include <ctype.h>
 #include <stddef.h>
@@ -878,20 +879,20 @@ bool EDSP::WriteSolution(pkgDepCache &Cache, FILE* output)
    {
       if (Cache[Pkg].Delete() == true)
       {
-	 fprintf(output, "Remove: %d\n", Pkg.CurrentVer()->ID);
+	 fprintf(output, "Remove: %d\n", _system->GetVersionMapping(Pkg.CurrentVer()->ID));
 	 if (Debug == true)
 	    fprintf(output, "Package: %s\nVersion: %s\n", Pkg.FullName().c_str(), Pkg.CurrentVer().VerStr());
       }
       else if (Cache[Pkg].NewInstall() == true || Cache[Pkg].Upgrade() == true)
       {
 	 pkgCache::VerIterator const CandVer = Cache.GetCandidateVersion(Pkg);
-	 fprintf(output, "Install: %d\n", CandVer->ID);
+	 fprintf(output, "Install: %d\n", _system->GetVersionMapping(CandVer->ID));
 	 if (Debug == true)
 	    fprintf(output, "Package: %s\nVersion: %s\n", Pkg.FullName().c_str(), CandVer.VerStr());
       }
       else if (Cache[Pkg].Garbage == true)
       {
-	 fprintf(output, "Autoremove: %d\n", Pkg.CurrentVer()->ID);
+	 fprintf(output, "Autoremove: %d\n", _system->GetVersionMapping(Pkg.CurrentVer()->ID));
 	 if (Debug == true)
 	    fprintf(output, "Package: %s\nVersion: %s\n", Pkg.FullName().c_str(), Pkg.CurrentVer().VerStr());
       }
@@ -910,11 +911,11 @@ bool EDSP::WriteSolution(pkgDepCache &Cache, FileFd &output)
    {
       std::string action;
       if (Cache[Pkg].Delete() == true)
-	 WriteOkay(Okay, output, "Remove: ", Pkg.CurrentVer()->ID, "\n");
+	 WriteOkay(Okay, output, "Remove: ", _system->GetVersionMapping(Pkg.CurrentVer()->ID), "\n");
       else if (Cache[Pkg].NewInstall() == true || Cache[Pkg].Upgrade() == true)
-	 WriteOkay(Okay, output, "Install: ", Cache.GetCandidateVersion(Pkg)->ID, "\n");
+	 WriteOkay(Okay, output, "Install: ", _system->GetVersionMapping(Cache.GetCandidateVersion(Pkg)->ID), "\n");
       else if (Cache[Pkg].Garbage == true)
-	 WriteOkay(Okay, output, "Autoremove: ", Pkg.CurrentVer()->ID, "\n");
+	 WriteOkay(Okay, output, "Autoremove: ", _system->GetVersionMapping(Pkg.CurrentVer()->ID), "\n");
       else
 	 continue;
 
diff --git a/apt-pkg/edsp/edsplistparser.cc b/apt-pkg/edsp/edsplistparser.cc
index 82799205e..f35000e0e 100644
--- a/apt-pkg/edsp/edsplistparser.cc
+++ b/apt-pkg/edsp/edsplistparser.cc
@@ -19,6 +19,7 @@
 #include <apt-pkg/cacheiterators.h>
 #include <apt-pkg/tagfile.h>
 #include <apt-pkg/fileutl.h>
+#include <apt-pkg/pkgsystem.h>
 
 									/*}}}*/
 
@@ -47,7 +48,7 @@ edspListParser::edspListParser(FileFd *File) : debListParser(File), d(new edspLi
 // ListParser::NewVersion - Fill in the version structure		/*{{{*/
 bool edspListParser::NewVersion(pkgCache::VerIterator &Ver)
 {
-   Ver->ID = Section.FindI("APT-ID", Ver->ID);
+   _system->SetVersionMapping(Ver->ID, Section.FindI("APT-ID", Ver->ID));
    return debListParser::NewVersion(Ver);
 }
 									/*}}}*/
diff --git a/apt-pkg/pkgsystem.cc b/apt-pkg/pkgsystem.cc
index 530150221..b1c6cc1ea 100644
--- a/apt-pkg/pkgsystem.cc
+++ b/apt-pkg/pkgsystem.cc
@@ -16,6 +16,7 @@
 #include <apt-pkg/pkgsystem.h>
 #include <apt-pkg/macros.h>
 
+#include <map>
 #include <cassert>
 #include <cstring>
 									/*}}}*/
@@ -25,11 +26,19 @@ static pkgSystem *SysList[10];
 pkgSystem **pkgSystem::GlobalList = SysList;
 unsigned long pkgSystem::GlobalListLen = 0;
 
+class APT_HIDDEN pkgSystemPrivate					/*{{{*/
+{
+public:
+   typedef decltype(pkgCache::Version::ID) idtype;
+   std::map<idtype,idtype> idmap;
+   pkgSystemPrivate() {}
+};
+									/*}}}*/
 // System::pkgSystem - Constructor					/*{{{*/
 // ---------------------------------------------------------------------
 /* Add it to the global list.. */
 pkgSystem::pkgSystem(char const * const label, pkgVersioningSystem * const vs) :
-   Label(label), VS(vs), d(NULL)
+   Label(label), VS(vs), d(new pkgSystemPrivate())
 {
    assert(GlobalListLen < sizeof(SysList)/sizeof(*SysList));
    SysList[GlobalListLen] = this;
@@ -63,5 +72,18 @@ std::vector<std::string> pkgSystem::ArchitecturesSupported() const	/*{{{*/
    return {};
 }
 									/*}}}*/
+// pkgSystem::Set/GetVersionMapping - for internal/external communcation/*{{{*/
+void pkgSystem::SetVersionMapping(map_id_t const in, map_id_t const out)
+{
+   if (in == out)
+      return;
+   d->idmap.emplace(in, out);
+}
+map_id_t pkgSystem::GetVersionMapping(map_id_t const in) const
+{
+   auto const o = d->idmap.find(in);
+   return (o == d->idmap.end()) ? in : o->second;
+}
+									/*}}}*/
 
 pkgSystem::~pkgSystem() {}
diff --git a/apt-pkg/pkgsystem.h b/apt-pkg/pkgsystem.h
index 55bdeec70..affaa5c9a 100644
--- a/apt-pkg/pkgsystem.h
+++ b/apt-pkg/pkgsystem.h
@@ -51,6 +51,7 @@ class pkgVersioningSystem;
 class Configuration;
 class pkgIndexFile;
 
+class pkgSystemPrivate;
 class pkgSystem
 {
    public:
@@ -114,10 +115,13 @@ class pkgSystem
     */
    std::vector<std::string> ArchitecturesSupported() const;
 
+   APT_HIDDEN void SetVersionMapping(map_id_t const in, map_id_t const out);
+   APT_HIDDEN map_id_t GetVersionMapping(map_id_t const in) const;
+
    pkgSystem(char const * const Label, pkgVersioningSystem * const VS);
    virtual ~pkgSystem();
    private:
-   void * const d;
+   pkgSystemPrivate * const d;
 };
 
 // The environment we are operating in.
diff --git a/test/integration/test-external-dependency-solver-protocol b/test/integration/test-external-dependency-solver-protocol
index 6b76fd55c..4b13edc09 100755
--- a/test/integration/test-external-dependency-solver-protocol
+++ b/test/integration/test-external-dependency-solver-protocol
@@ -153,6 +153,11 @@ else
 	cat solver.result
 	msgfail
 fi
+testsuccess grep '^APT-ID: 1$' "$APT_EDSP_DUMP_FILENAME"
+sed -i -e 's#^APT-ID: 1$#APT-ID: 10000#' "$APT_EDSP_DUMP_FILENAME"
+cat "$APT_EDSP_DUMP_FILENAME" | aptinternalsolver > solver.result 2>&1 || true
+testsuccessequal 'Message: Done
+' tail -n2 solver.result
 rm -f "$APT_EDSP_DUMP_FILENAME"
 
 testsuccess aptinternalsolver scenario