From 6089a4b17c61ef30b2efc00e270b0907f51f352a Mon Sep 17 00:00:00 2001
From: Julian Andres Klode <julian.klode@canonical.com>
Date: Mon, 6 May 2019 12:22:42 +0200
Subject: Use debDebFile to get control file instead of dpkg-deb

---
 apt-pkg/deb/debindexfile.cc               | 43 ++++++++++++-------------------
 test/integration/test-apt-get-install-deb |  6 ++---
 2 files changed, 19 insertions(+), 30 deletions(-)

diff --git a/apt-pkg/deb/debindexfile.cc b/apt-pkg/deb/debindexfile.cc
index 25e0a3312..279f35a38 100644
--- a/apt-pkg/deb/debindexfile.cc
+++ b/apt-pkg/deb/debindexfile.cc
@@ -10,7 +10,9 @@
 // Include Files							/*{{{*/
 #include <config.h>
 
+#include <apti18n.h>
 #include <apt-pkg/configuration.h>
+#include <apt-pkg/debfile.h>
 #include <apt-pkg/debindexfile.h>
 #include <apt-pkg/deblistparser.h>
 #include <apt-pkg/debrecords.h>
@@ -172,36 +174,23 @@ bool debDebPkgFileIndex::GetContent(std::ostream &content, std::string const &de
    if (stat(debfile.c_str(), &Buf) != 0)
       return false;
 
-   // get the control data out of the deb file via dpkg-deb -I
-   std::string dpkg = _config->Find("Dir::Bin::dpkg","dpkg-deb");
-   std::vector<const char *> Args;
-   Args.push_back(dpkg.c_str());
-   Args.push_back("-I");
-   Args.push_back(debfile.c_str());
-   Args.push_back("control");
-   Args.push_back(NULL);
-   FileFd PipeFd;
-   pid_t Child;
-   if(Popen((const char**)&Args[0], PipeFd, Child, FileFd::ReadOnly) == false)
-      return _error->Error("Popen failed");
+   FileFd debFd(debfile, FileFd::ReadOnly);
+   debDebFile deb(debFd);
+   debDebFile::MemControlExtract extractor("control");
 
-   std::string line;
-   bool first_line_seen = false;
-   while (PipeFd.ReadLine(line))
-   {
-      if (first_line_seen == false)
-      {
-	 if (line.empty())
-	    continue;
-	 first_line_seen = true;
-      }
-      else if (line.empty())
-	 break;
-      content << line << "\n";
-   }
+   if (not extractor.Read(deb))
+      return _error->Error(_("Could not read meta data from %s"), debfile.c_str());
+
+   // trim off newlines
+   while (extractor.Control[extractor.Length] == '\n')
+      extractor.Control[extractor.Length--] = '\0';
+   const char *Control = extractor.Control;
+   while (isspace_ascii(Control[0]))
+      Control++;
+
+   content << Control;
    content << "Filename: " << debfile << "\n";
    content << "Size: " << std::to_string(Buf.st_size) << "\n";
-   ExecWait(Child, "Popen");
 
    return true;
 }
diff --git a/test/integration/test-apt-get-install-deb b/test/integration/test-apt-get-install-deb
index 844f1d7c5..002c1aef8 100755
--- a/test/integration/test-apt-get-install-deb
+++ b/test/integration/test-apt-get-install-deb
@@ -22,9 +22,9 @@ done
 mv foo.rpm foo.deb
 for exe in apt aptget; do
 	for cmd in install remove purge upgrade full-upgrade; do
-		testfailuremsg "E: Sub-process Popen returned an error code (2)
-E: Encountered a section with no Package: header
-E: Problem with MergeList ${TMPWORKINGDIRECTORY}/foo.deb
+		testfailuremsg "E: Invalid archive signature
+E: Internal error, could not locate member control.tar.{zstlz4gzxzbz2lzma}
+E: Could not read meta data from ${TMPWORKINGDIRECTORY}/foo.deb
 E: The package lists or status file could not be parsed or opened." $exe $cmd ./foo.deb
 	done
 done
-- 
cgit v1.2.3