From 8aec002f1d3eb70423c760a68a9825a92bba7b2a Mon Sep 17 00:00:00 2001
From: David Kalnischkies <kalnischkies@gmail.com>
Date: Sat, 26 Mar 2011 01:31:32 +0100
Subject: [PATCH] * apt-pkg/aptconfiguration.cc:   - use dpkg
 --print-foreign-architectures to get multiarch configuration     if non is
 specified with APT::Architectures (Closes: #612958)

---
 apt-pkg/aptconfiguration.cc                   | 33 ++++++++++-
 debian/changelog                              |  5 +-
 .../test-bug-612958-use-dpkg-multiarch-config | 59 +++++++++++++++++++
 3 files changed, 95 insertions(+), 2 deletions(-)
 create mode 100755 test/integration/test-bug-612958-use-dpkg-multiarch-config

diff --git a/apt-pkg/aptconfiguration.cc b/apt-pkg/aptconfiguration.cc
index b23e12acb..14ee09e0d 100644
--- a/apt-pkg/aptconfiguration.cc
+++ b/apt-pkg/aptconfiguration.cc
@@ -17,6 +17,7 @@
 
 #include <sys/types.h>
 #include <dirent.h>
+#include <stdio.h>
 
 #include <algorithm>
 #include <string>
@@ -314,11 +315,41 @@ std::vector<std::string> const Configuration::getArchitectures(bool const &Cache
 	if (likely(Cached == true) && archs.empty() == false)
 		return archs;
 
-	archs = _config->FindVector("APT::Architectures");
 	string const arch = _config->Find("APT::Architecture");
+	archs = _config->FindVector("APT::Architectures");
+
 	if (unlikely(arch.empty() == true))
 		return archs;
 
+	// FIXME: It is a bit unclean to have debian specific code here…
+	if (archs.empty() == true) {
+		archs.push_back(arch);
+		string dpkgcall = _config->Find("Dir::Bin::dpkg", "dpkg");
+		std::vector<string> const dpkgoptions = _config->FindVector("DPkg::options");
+		for (std::vector<string>::const_iterator o = dpkgoptions.begin();
+		     o != dpkgoptions.end(); ++o)
+			dpkgcall.append(" ").append(*o);
+		dpkgcall.append(" --print-foreign-architectures 2> /dev/null");
+		FILE *dpkg = popen(dpkgcall.c_str(), "r");
+		char buf[1024];
+		if(dpkg != NULL) {
+			if (fgets(buf, sizeof(buf), dpkg) != NULL) {
+				char* arch = strtok(buf, " ");
+				while (arch != NULL) {
+					for (; isspace(*arch) != 0; ++arch);
+					if (arch != '\0') {
+						char const* archend = arch;
+						for (; isspace(*archend) == 0 && *archend != '\0'; ++archend);
+						archs.push_back(string(arch, (archend - arch)));
+					}
+					arch = strtok(NULL, " ");
+				}
+			}
+			pclose(dpkg);
+		}
+		return archs;
+	}
+
 	if (archs.empty() == true ||
 	    std::find(archs.begin(), archs.end(), arch) == archs.end())
 		archs.push_back(arch);
diff --git a/debian/changelog b/debian/changelog
index b0bec1ba2..1c2cd9da7 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -21,8 +21,11 @@ apt (0.8.13.1) UNRELEASED; urgency=low
   * apt-pkg/cacheiterator.h:
     - return "all" instead of native architecture without breaking the abi
       (too much) by extending enum instead of using bitflags (LP: #733741)
+  * apt-pkg/aptconfiguration.cc:
+    - use dpkg --print-foreign-architectures to get multiarch configuration
+      if non is specified with APT::Architectures (Closes: #612958)
 
- -- David Kalnischkies <kalnischkies@gmail.com>  Fri, 25 Mar 2011 22:07:59 +0100
+ -- David Kalnischkies <kalnischkies@gmail.com>  Sat, 26 Mar 2011 01:28:38 +0100
 
 apt (0.8.13) unstable; urgency=low
 
diff --git a/test/integration/test-bug-612958-use-dpkg-multiarch-config b/test/integration/test-bug-612958-use-dpkg-multiarch-config
new file mode 100755
index 000000000..4f31c3953
--- /dev/null
+++ b/test/integration/test-bug-612958-use-dpkg-multiarch-config
@@ -0,0 +1,59 @@
+#!/bin/sh
+set -e
+
+TESTDIR=$(readlink -f $(dirname $0))
+. $TESTDIR/framework
+setupenvironment
+configarchitecture 'i386'
+setupaptarchive
+
+insertinstalledpackage 'libapt' 'i386' '1.0'
+insertinstalledpackage 'libapt' 'amd64' '1.0'
+insertinstalledpackage 'libapt' 'armel' '1.0'
+
+testpass() {
+	rm rootdir/var/cache/apt/*.bin
+	msgtest 'Test architecture handling' "$1 with $2"
+	aptcache show libapt:$2 2> /dev/null > /dev/null && msgpass || msgfail
+}
+
+testfail() {
+	rm rootdir/var/cache/apt/*.bin
+	msgtest 'Test architecture handling' "$1 with $2"
+	aptcache show libapt:$2 2> /dev/null > /dev/null && msgfail || msgpass
+}
+
+testpass 'no config' 'i386'
+testfail 'no config' 'amd64'
+testfail 'no config' 'armel'
+
+CONFFILE=rootdir/etc/apt/apt.conf.d/99multiarch
+echo '#clear APT::Architectures;' >> $CONFFILE
+echo 'APT::Architectures:: "i386";' >> $CONFFILE
+echo 'APT::Architectures:: "amd64";' >> $CONFFILE
+
+testpass 'apt config' 'i386'
+testpass 'apt config' 'amd64'
+testfail 'apt config' 'armel'
+
+echo 'APT::Architectures:: "armel";' >> $CONFFILE
+
+testpass 'apt config' 'armel'
+
+rm $CONFFILE
+
+echo '#clear APT::Architectures;' >> $CONFFILE
+echo 'Dir::Bin::dpkg "./dpkg-printer";' >> $CONFFILE
+
+echo '#! /bin/sh
+echo "amd64"' > ./dpkg-printer
+chmod +x ./dpkg-printer
+
+testpass 'dpkg config' 'i386'
+testpass 'dpkg config' 'amd64'
+testfail 'dpkg config' 'armel'
+
+echo '#! /bin/sh
+echo "amd64 armel"' > ./dpkg-printer
+
+testpass 'dpkg config' 'armel'
-- 
2.47.2