summaryrefslogtreecommitdiff
path: root/apt-private
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2015-10-26 19:30:44 +0100
committerDavid Kalnischkies <david@kalnischkies.de>2015-11-04 18:04:04 +0100
commit9055d5e68bd09f31df00e45bd14cb599ba3735e5 (patch)
tree0cab141c3e7fdf5de5e9940dd394b9dbf0503c62 /apt-private
parent41d39345bdc31cb9b8be057cd678aa2890830437 (diff)
hidden support more apt-get/apt-cache commands in apt
apt is supposed to be a user-friendly interface, so while these commands are usually poweruser material and therefore do not need to be shown in general introduction manpages/help messages its of no use to not allow users to use them. This includes clean, autoclean, build-dep, source, download, changelog, depends, rdepends and showsrc – it doesn't include more non-interactive commands like dump or xvcg as those are usually used by scripts if at all. Closes: 778234, 780700, 781237
Diffstat (limited to 'apt-private')
-rw-r--r--apt-private/private-depends.cc152
-rw-r--r--apt-private/private-depends.h11
-rw-r--r--apt-private/private-download.cc203
-rw-r--r--apt-private/private-download.h8
-rw-r--r--apt-private/private-show.cc181
-rw-r--r--apt-private/private-show.h2
-rw-r--r--apt-private/private-source.cc1028
-rw-r--r--apt-private/private-source.h11
8 files changed, 1595 insertions, 1 deletions
diff --git a/apt-private/private-depends.cc b/apt-private/private-depends.cc
new file mode 100644
index 000000000..955f3dc67
--- /dev/null
+++ b/apt-private/private-depends.cc
@@ -0,0 +1,152 @@
+// Include Files /*{{{*/
+#include<config.h>
+
+#include <apt-pkg/algorithms.h>
+#include <apt-pkg/cachefile.h>
+#include <apt-pkg/cacheiterators.h>
+#include <apt-pkg/cacheset.h>
+#include <apt-pkg/configuration.h>
+#include <apt-pkg/cmndline.h>
+#include <apt-pkg/error.h>
+#include <apt-pkg/pkgcache.h>
+
+#include <apt-private/private-cacheset.h>
+#include <apt-private/private-depends.h>
+
+#include <iostream>
+#include <string>
+#include <vector>
+
+#include <stddef.h>
+
+#include <apti18n.h>
+ /*}}}*/
+
+// ShowDepends - Helper for printing out a dependency tree /*{{{*/
+static bool ShowDepends(CommandLine &CmdL, bool const RevDepends)
+{
+ pkgCacheFile CacheFile;
+ pkgCache *Cache = CacheFile.GetPkgCache();
+ if (unlikely(Cache == NULL))
+ return false;
+
+ CacheSetHelperVirtuals helper(false);
+ APT::VersionList verset = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper);
+ if (verset.empty() == true && helper.virtualPkgs.empty() == true)
+ return _error->Error(_("No packages found"));
+ std::vector<bool> Shown(Cache->Head().PackageCount);
+
+ bool const Recurse = _config->FindB("APT::Cache::RecurseDepends", false);
+ bool const Installed = _config->FindB("APT::Cache::Installed", false);
+ bool const Important = _config->FindB("APT::Cache::Important", false);
+ bool const ShowDepType = _config->FindB("APT::Cache::ShowDependencyType", RevDepends == false);
+ bool const ShowVersion = _config->FindB("APT::Cache::ShowVersion", false);
+ bool const ShowPreDepends = _config->FindB("APT::Cache::ShowPre-Depends", true);
+ bool const ShowDepends = _config->FindB("APT::Cache::ShowDepends", true);
+ bool const ShowRecommends = _config->FindB("APT::Cache::ShowRecommends", Important == false);
+ bool const ShowSuggests = _config->FindB("APT::Cache::ShowSuggests", Important == false);
+ bool const ShowReplaces = _config->FindB("APT::Cache::ShowReplaces", Important == false);
+ bool const ShowConflicts = _config->FindB("APT::Cache::ShowConflicts", Important == false);
+ bool const ShowBreaks = _config->FindB("APT::Cache::ShowBreaks", Important == false);
+ bool const ShowEnhances = _config->FindB("APT::Cache::ShowEnhances", Important == false);
+ bool const ShowOnlyFirstOr = _config->FindB("APT::Cache::ShowOnlyFirstOr", false);
+ bool const ShowImplicit = _config->FindB("APT::Cache::ShowImplicit", false);
+
+ while (verset.empty() != true)
+ {
+ pkgCache::VerIterator Ver = *verset.begin();
+ verset.erase(verset.begin());
+ pkgCache::PkgIterator Pkg = Ver.ParentPkg();
+ Shown[Pkg->ID] = true;
+
+ std::cout << Pkg.FullName(true) << std::endl;
+
+ if (RevDepends == true)
+ std::cout << "Reverse Depends:" << std::endl;
+ for (pkgCache::DepIterator D = RevDepends ? Pkg.RevDependsList() : Ver.DependsList();
+ D.end() == false; ++D)
+ {
+ switch (D->Type) {
+ case pkgCache::Dep::PreDepends: if (!ShowPreDepends) continue; break;
+ case pkgCache::Dep::Depends: if (!ShowDepends) continue; break;
+ case pkgCache::Dep::Recommends: if (!ShowRecommends) continue; break;
+ case pkgCache::Dep::Suggests: if (!ShowSuggests) continue; break;
+ case pkgCache::Dep::Replaces: if (!ShowReplaces) continue; break;
+ case pkgCache::Dep::Conflicts: if (!ShowConflicts) continue; break;
+ case pkgCache::Dep::DpkgBreaks: if (!ShowBreaks) continue; break;
+ case pkgCache::Dep::Enhances: if (!ShowEnhances) continue; break;
+ }
+ if (ShowImplicit == false && D.IsImplicit())
+ continue;
+
+ pkgCache::PkgIterator Trg = RevDepends ? D.ParentPkg() : D.TargetPkg();
+
+ if((Installed && Trg->CurrentVer != 0) || !Installed)
+ {
+
+ if ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or && ShowOnlyFirstOr == false)
+ std::cout << " |";
+ else
+ std::cout << " ";
+
+ // Show the package
+ if (ShowDepType == true)
+ std::cout << D.DepType() << ": ";
+ if (Trg->VersionList == 0)
+ std::cout << "<" << Trg.FullName(true) << ">";
+ else
+ std::cout << Trg.FullName(true);
+ if (ShowVersion == true && D->Version != 0)
+ std::cout << " (" << pkgCache::CompTypeDeb(D->CompareOp) << ' ' << D.TargetVer() << ')';
+ std::cout << std::endl;
+
+ if (Recurse == true && Shown[Trg->ID] == false)
+ {
+ Shown[Trg->ID] = true;
+ verset.insert(APT::VersionSet::FromPackage(CacheFile, Trg, APT::CacheSetHelper::CANDIDATE, helper));
+ }
+
+ }
+
+ // Display all solutions
+ std::unique_ptr<pkgCache::Version *[]> List(D.AllTargets());
+ pkgPrioSortList(*Cache,List.get());
+ for (pkgCache::Version **I = List.get(); *I != 0; I++)
+ {
+ pkgCache::VerIterator V(*Cache,*I);
+ if (V != Cache->VerP + V.ParentPkg()->VersionList ||
+ V->ParentPkg == D->Package)
+ continue;
+ std::cout << " " << V.ParentPkg().FullName(true) << std::endl;
+
+ if (Recurse == true && Shown[V.ParentPkg()->ID] == false)
+ {
+ Shown[V.ParentPkg()->ID] = true;
+ verset.insert(APT::VersionSet::FromPackage(CacheFile, V.ParentPkg(), APT::CacheSetHelper::CANDIDATE, helper));
+ }
+ }
+
+ if (ShowOnlyFirstOr == true)
+ while ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or) ++D;
+ }
+ }
+
+ for (APT::PackageSet::const_iterator Pkg = helper.virtualPkgs.begin();
+ Pkg != helper.virtualPkgs.end(); ++Pkg)
+ std::cout << '<' << Pkg.FullName(true) << '>' << std::endl;
+
+ return true;
+}
+ /*}}}*/
+// Depends - Print out a dependency tree /*{{{*/
+bool Depends(CommandLine &CmdL)
+{
+ return ShowDepends(CmdL, false);
+}
+ /*}}}*/
+// RDepends - Print out a reverse dependency tree /*{{{*/
+bool RDepends(CommandLine &CmdL)
+{
+ return ShowDepends(CmdL, true);
+}
+ /*}}}*/
diff --git a/apt-private/private-depends.h b/apt-private/private-depends.h
new file mode 100644
index 000000000..e9f703dc7
--- /dev/null
+++ b/apt-private/private-depends.h
@@ -0,0 +1,11 @@
+#ifndef APT_PRIVATE_DEPENDS_H
+#define APT_PRIVATE_DEPENDS_H
+
+#include <apt-pkg/macros.h>
+
+class CommandLine;
+
+APT_PUBLIC bool Depends(CommandLine &CmdL);
+APT_PUBLIC bool RDepends(CommandLine &CmdL);
+
+#endif
diff --git a/apt-private/private-download.cc b/apt-private/private-download.cc
index 96d44b1c5..4894c72bf 100644
--- a/apt-private/private-download.cc
+++ b/apt-private/private-download.cc
@@ -3,13 +3,18 @@
#include <apt-pkg/acquire.h>
#include <apt-pkg/acquire-item.h>
+#include <apt-pkg/cacheset.h>
+#include <apt-pkg/cmndline.h>
+#include <apt-pkg/clean.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/error.h>
#include <apt-pkg/fileutl.h>
#include <apt-pkg/strutl.h>
-#include <apt-private/private-output.h>
+#include <apt-private/private-cachefile.h>
#include <apt-private/private-download.h>
+#include <apt-private/private-output.h>
+#include <apt-private/private-utils.h>
#include <apt-private/acqprogress.h>
#include <fstream>
@@ -155,3 +160,199 @@ aptAcquireWithTextStatus::aptAcquireWithTextStatus() : pkgAcquire::pkgAcquire(),
{
SetLog(&Stat);
}
+
+// DoDownload - download a binary /*{{{*/
+bool DoDownload(CommandLine &CmdL)
+{
+ CacheFile Cache;
+ if (Cache.ReadOnlyOpen() == false)
+ return false;
+
+ APT::CacheSetHelper helper;
+ APT::VersionSet verset = APT::VersionSet::FromCommandLine(Cache,
+ CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper);
+
+ if (verset.empty() == true)
+ return false;
+
+ pkgRecords Recs(Cache);
+ pkgSourceList *SrcList = Cache.GetSourceList();
+
+ // reuse the usual acquire methods for deb files, but don't drop them into
+ // the usual directories - keep everything in the current directory
+ aptAcquireWithTextStatus Fetcher;
+ std::vector<std::string> storefile(verset.size());
+ std::string const cwd = SafeGetCWD();
+ _config->Set("Dir::Cache::Archives", cwd);
+ int i = 0;
+ for (APT::VersionSet::const_iterator Ver = verset.begin();
+ Ver != verset.end(); ++Ver, ++i)
+ {
+ pkgAcquire::Item *I = new pkgAcqArchive(&Fetcher, SrcList, &Recs, *Ver, storefile[i]);
+ if (storefile[i].empty())
+ continue;
+ std::string const filename = cwd + flNotDir(storefile[i]);
+ storefile[i].assign(filename);
+ I->DestFile.assign(filename);
+ }
+
+ // Just print out the uris and exit if the --print-uris flag was used
+ if (_config->FindB("APT::Get::Print-URIs") == true)
+ {
+ pkgAcquire::UriIterator I = Fetcher.UriBegin();
+ for (; I != Fetcher.UriEnd(); ++I)
+ std::cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
+ I->Owner->FileSize << ' ' << I->Owner->HashSum() << std::endl;
+ return true;
+ }
+
+ if (_error->PendingError() == true || CheckAuth(Fetcher, false) == false)
+ return false;
+
+ bool Failed = false;
+ if (AcquireRun(Fetcher, 0, &Failed, NULL) == false)
+ return false;
+
+ // copy files in local sources to the current directory
+ for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I)
+ {
+ std::string const filename = cwd + flNotDir((*I)->DestFile);
+ if ((*I)->Local == true &&
+ filename != (*I)->DestFile &&
+ (*I)->Status == pkgAcquire::Item::StatDone)
+ {
+ std::ifstream src((*I)->DestFile.c_str(), std::ios::binary);
+ std::ofstream dst(filename.c_str(), std::ios::binary);
+ dst << src.rdbuf();
+ }
+ }
+ return Failed == false;
+}
+ /*}}}*/
+// DoChangelog - Get changelog from the command line /*{{{*/
+bool DoChangelog(CommandLine &CmdL)
+{
+ CacheFile Cache;
+ if (Cache.ReadOnlyOpen() == false)
+ return false;
+
+ APT::CacheSetHelper helper;
+ APT::VersionList verset = APT::VersionList::FromCommandLine(Cache,
+ CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper);
+ if (verset.empty() == true)
+ return false;
+
+ bool const downOnly = _config->FindB("APT::Get::Download-Only", false);
+ bool const printOnly = _config->FindB("APT::Get::Print-URIs", false);
+
+ aptAcquireWithTextStatus Fetcher;
+ for (APT::VersionList::const_iterator Ver = verset.begin();
+ Ver != verset.end();
+ ++Ver)
+ {
+ if (printOnly)
+ new pkgAcqChangelog(&Fetcher, Ver, "/dev/null");
+ else if (downOnly)
+ new pkgAcqChangelog(&Fetcher, Ver, ".");
+ else
+ new pkgAcqChangelog(&Fetcher, Ver);
+ }
+
+ if (printOnly == false)
+ {
+ bool Failed = false;
+ if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
+ return false;
+ }
+
+ if (downOnly == false || printOnly == true)
+ {
+ bool Failed = false;
+ for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I)
+ {
+ if (printOnly)
+ {
+ if ((*I)->ErrorText.empty() == false)
+ {
+ Failed = true;
+ _error->Error("%s", (*I)->ErrorText.c_str());
+ }
+ else
+ std::cout << '\'' << (*I)->DescURI() << "' " << flNotDir((*I)->DestFile) << std::endl;
+ }
+ else
+ DisplayFileInPager((*I)->DestFile);
+ }
+ return Failed == false;
+ }
+
+ return true;
+}
+ /*}}}*/
+
+// DoClean - Remove download archives /*{{{*/
+bool DoClean(CommandLine &)
+{
+ std::string const archivedir = _config->FindDir("Dir::Cache::archives");
+ std::string const listsdir = _config->FindDir("Dir::state::lists");
+
+ if (_config->FindB("APT::Get::Simulate") == true)
+ {
+ std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
+ std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
+ std::cout << "Del " << archivedir << "* " << archivedir << "partial/*"<< std::endl
+ << "Del " << listsdir << "partial/*" << std::endl
+ << "Del " << pkgcache << " " << srcpkgcache << std::endl;
+ return true;
+ }
+
+ pkgAcquire Fetcher;
+ Fetcher.GetLock(archivedir);
+ Fetcher.Clean(archivedir);
+ Fetcher.Clean(archivedir + "partial/");
+
+ Fetcher.GetLock(listsdir);
+ Fetcher.Clean(listsdir + "partial/");
+
+ pkgCacheFile::RemoveCaches();
+
+ return true;
+}
+ /*}}}*/
+// DoAutoClean - Smartly remove downloaded archives /*{{{*/
+// ---------------------------------------------------------------------
+/* This is similar to clean but it only purges things that cannot be
+ downloaded, that is old versions of cached packages. */
+ class LogCleaner : public pkgArchiveCleaner
+{
+ protected:
+ virtual void Erase(const char *File, std::string Pkg, std::string Ver,struct stat &St) APT_OVERRIDE
+ {
+ c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << std::endl;
+
+ if (_config->FindB("APT::Get::Simulate") == false)
+ unlink(File);
+ };
+};
+bool DoAutoClean(CommandLine &)
+{
+ // Lock the archive directory
+ FileFd Lock;
+ if (_config->FindB("Debug::NoLocking",false) == false)
+ {
+ int lock_fd = GetLock(_config->FindDir("Dir::Cache::Archives") + "lock");
+ if (lock_fd < 0)
+ return _error->Error(_("Unable to lock the download directory"));
+ Lock.Fd(lock_fd);
+ }
+
+ CacheFile Cache;
+ if (Cache.Open() == false)
+ return false;
+
+ LogCleaner Cleaner;
+
+ return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
+ Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
+}
+ /*}}}*/
diff --git a/apt-private/private-download.h b/apt-private/private-download.h
index 7df8f284a..587165166 100644
--- a/apt-private/private-download.h
+++ b/apt-private/private-download.h
@@ -27,4 +27,12 @@ public:
aptAcquireWithTextStatus();
};
+class CommandLine;
+
+APT_PUBLIC bool DoDownload(CommandLine &CmdL);
+APT_PUBLIC bool DoChangelog(CommandLine &CmdL);
+
+APT_PUBLIC bool DoClean(CommandLine &CmdL);
+APT_PUBLIC bool DoAutoClean(CommandLine &CmdL);
+
#endif
diff --git a/apt-private/private-show.cc b/apt-private/private-show.cc
index 1bf712684..aaa4268c6 100644
--- a/apt-private/private-show.cc
+++ b/apt-private/private-show.cc
@@ -17,6 +17,7 @@
#include <apt-pkg/depcache.h>
#include <apt-pkg/macros.h>
#include <apt-pkg/pkgcache.h>
+#include <apt-pkg/policy.h>
#include <apt-private/private-cacheset.h>
#include <apt-private/private-output.h>
@@ -297,3 +298,183 @@ bool ShowPackage(CommandLine &CmdL) /*{{{*/
return true;
}
/*}}}*/
+bool ShowSrcPackage(CommandLine &CmdL) /*{{{*/
+{
+ pkgCacheFile CacheFile;
+ pkgSourceList *List = CacheFile.GetSourceList();
+ if (unlikely(List == NULL))
+ return false;
+
+ // Create the text record parsers
+ pkgSrcRecords SrcRecs(*List);
+ if (_error->PendingError() == true)
+ return false;
+
+ bool found = false;
+ for (const char **I = CmdL.FileList + 1; *I != 0; I++)
+ {
+ SrcRecs.Restart();
+
+ pkgSrcRecords::Parser *Parse;
+ bool found_this = false;
+ while ((Parse = SrcRecs.Find(*I,false)) != 0) {
+ // SrcRecs.Find() will find both binary and source names
+ if (_config->FindB("APT::Cache::Only-Source", false) == true)
+ if (Parse->Package() != *I)
+ continue;
+ std::cout << Parse->AsStr() << std::endl;;
+ found = true;
+ found_this = true;
+ }
+ if (found_this == false) {
+ _error->Warning(_("Unable to locate package %s"),*I);
+ continue;
+ }
+ }
+ if (found == false)
+ _error->Notice(_("No packages found"));
+ return true;
+}
+ /*}}}*/
+// Policy - Show the results of the preferences file /*{{{*/
+bool Policy(CommandLine &CmdL)
+{
+ pkgCacheFile CacheFile;
+ pkgCache *Cache = CacheFile.GetPkgCache();
+ pkgPolicy *Plcy = CacheFile.GetPolicy();
+ pkgSourceList *SrcList = CacheFile.GetSourceList();
+ if (unlikely(Cache == NULL || Plcy == NULL || SrcList == NULL))
+ return false;
+
+ bool OldPolicy = _config->FindI("APT::Policy", 1) < 1;
+
+ // Print out all of the package files
+ if (CmdL.FileList[1] == 0)
+ {
+ std::cout << _("Package files:") << std::endl;
+ for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F.end() == false; ++F)
+ {
+ if (F.Flagged(pkgCache::Flag::NoPackages))
+ continue;
+ // Locate the associated index files so we can derive a description
+ pkgIndexFile *Indx;
+ if (SrcList->FindIndex(F,Indx) == false &&
+ _system->FindIndex(F,Indx) == false)
+ return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
+
+ printf("%4i %s\n",
+ Plcy->GetPriority(F),Indx->Describe(true).c_str());
+
+ // Print the reference information for the package
+ std::string Str = F.RelStr();
+ if (Str.empty() == false)
+ printf(" release %s\n",F.RelStr().c_str());
+ if (F.Site() != 0 && F.Site()[0] != 0)
+ printf(" origin %s\n",F.Site());
+ }
+
+ // Show any packages have explicit pins
+ std::cout << _("Pinned packages:") << std::endl;
+ pkgCache::PkgIterator I = Cache->PkgBegin();
+ for (;I.end() != true; ++I)
+ {
+ // Old code for debugging
+ if (OldPolicy)
+ {
+ if (Plcy->GetPriority(I) == 0)
+ continue;
+
+ // Print the package name and the version we are forcing to
+ std::cout << " " << I.FullName(true) << " -> ";
+
+ pkgCache::VerIterator V = Plcy->GetMatch(I);
+ if (V.end() == true)
+ std::cout << _("(not found)") << std::endl;
+ else
+ std::cout << V.VerStr() << std::endl;
+
+ continue;
+ }
+ // New code
+ for (pkgCache::VerIterator V = I.VersionList(); !V.end(); V++) {
+ auto Prio = Plcy->GetPriority(V, false);
+ if (Prio == 0)
+ continue;
+
+ std::cout << " ";
+ // Print the package name and the version we are forcing to
+ ioprintf(std::cout, _("%s -> %s with priority %d\n"), I.FullName(true).c_str(), V.VerStr(), Prio);
+ }
+ }
+ return true;
+ }
+
+ char const * const msgInstalled = _(" Installed: ");
+ char const * const msgCandidate = _(" Candidate: ");
+ short const InstalledLessCandidate =
+ mbstowcs(NULL, msgInstalled, 0) - mbstowcs(NULL, msgCandidate, 0);
+ short const deepInstalled =
+ (InstalledLessCandidate < 0 ? (InstalledLessCandidate*-1) : 0) - 1;
+ short const deepCandidate =
+ (InstalledLessCandidate > 0 ? (InstalledLessCandidate) : 0) - 1;
+
+ // Print out detailed information for each package
+ APT::CacheSetHelper helper(true, GlobalError::NOTICE);
+ APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
+ for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
+ {
+ std::cout << Pkg.FullName(true) << ":" << std::endl;
+
+ // Installed version
+ std::cout << msgInstalled << OutputInDepth(deepInstalled, " ");
+ if (Pkg->CurrentVer == 0)
+ std::cout << _("(none)") << std::endl;
+ else
+ std::cout << Pkg.CurrentVer().VerStr() << std::endl;
+
+ // Candidate Version
+ std::cout << msgCandidate << OutputInDepth(deepCandidate, " ");
+ pkgCache::VerIterator V = Plcy->GetCandidateVer(Pkg);
+ if (V.end() == true)
+ std::cout << _("(none)") << std::endl;
+ else
+ std::cout << V.VerStr() << std::endl;
+
+ // Pinned version
+ if (OldPolicy && Plcy->GetPriority(Pkg) != 0)
+ {
+ std::cout << _(" Package pin: ");
+ V = Plcy->GetMatch(Pkg);
+ if (V.end() == true)
+ std::cout << _("(not found)") << std::endl;
+ else
+ std::cout << V.VerStr() << std::endl;
+ }
+
+ // Show the priority tables
+ std::cout << _(" Version table:") << std::endl;
+ for (V = Pkg.VersionList(); V.end() == false; ++V)
+ {
+ if (Pkg.CurrentVer() == V)
+ std::cout << " *** " << V.VerStr();
+ else
+ std::cout << " " << V.VerStr();
+ if (_config->FindI("APT::Policy", 1) < 1)
+ std::cout << " " << Plcy->GetPriority(Pkg) << std::endl;
+ else
+ std::cout << " " << Plcy->GetPriority(V) << std::endl;
+ for (pkgCache::VerFileIterator VF = V.FileList(); VF.end() == false; ++VF)
+ {
+ // Locate the associated index files so we can derive a description
+ pkgIndexFile *Indx;
+ if (SrcList->FindIndex(VF.File(),Indx) == false &&
+ _system->FindIndex(VF.File(),Indx) == false)
+ return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
+ printf(" %4i %s\n",Plcy->GetPriority(VF.File()),
+ Indx->Describe(true).c_str());
+ }
+ }
+ }
+ return true;
+}
+ /*}}}*/
diff --git a/apt-private/private-show.h b/apt-private/private-show.h
index b38c65f2f..80871b817 100644
--- a/apt-private/private-show.h
+++ b/apt-private/private-show.h
@@ -11,5 +11,7 @@ class pkgCacheFile;
APT_PUBLIC bool ShowPackage(CommandLine &CmdL);
APT_PUBLIC bool DisplayRecordV1(pkgCacheFile &CacheFile, pkgCache::VerIterator const &V, std::ostream &out);
+APT_PUBLIC bool ShowSrcPackage(CommandLine &CmdL);
+APT_PUBLIC bool Policy(CommandLine &CmdL);
#endif
diff --git a/apt-private/private-source.cc b/apt-private/private-source.cc
new file mode 100644
index 000000000..c7e5e4257
--- /dev/null
+++ b/apt-private/private-source.cc
@@ -0,0 +1,1028 @@
+// Include Files /*{{{*/
+#include <config.h>
+
+#include <apt-pkg/acquire-item.h>
+#include <apt-pkg/acquire.h>
+#include <apt-pkg/algorithms.h>
+#include <apt-pkg/aptconfiguration.h>
+#include <apt-pkg/cachefile.h>
+#include <apt-pkg/cacheiterators.h>
+#include <apt-pkg/cacheset.h>
+#include <apt-pkg/cmndline.h>
+#include <apt-pkg/configuration.h>
+#include <apt-pkg/depcache.h>
+#include <apt-pkg/error.h>
+#include <apt-pkg/fileutl.h>
+#include <apt-pkg/hashes.h>
+#include <apt-pkg/indexfile.h>
+#include <apt-pkg/metaindex.h>
+#include <apt-pkg/pkgcache.h>
+#include <apt-pkg/sourcelist.h>
+#include <apt-pkg/srcrecords.h>
+#include <apt-pkg/strutl.h>
+#include <apt-pkg/version.h>
+
+#include <apt-private/private-cachefile.h>
+#include <apt-private/private-cacheset.h>
+#include <apt-private/private-download.h>
+#include <apt-private/private-install.h>
+#include <apt-private/private-source.h>
+
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <iostream>
+#include <set>
+#include <string>
+#include <vector>
+
+#include <apti18n.h>
+ /*}}}*/
+
+// TryToInstallBuildDep - Try to install a single package /*{{{*/
+// ---------------------------------------------------------------------
+/* This used to be inlined in DoInstall, but with the advent of regex package
+ name matching it was split out.. */
+static bool TryToInstallBuildDep(pkgCache::PkgIterator Pkg,pkgCacheFile &Cache,
+ pkgProblemResolver &Fix,bool Remove,bool BrokenFix,
+ bool AllowFail = true)
+{
+ if (Cache[Pkg].CandidateVer == 0 && Pkg->ProvidesList != 0)
+ {
+ CacheSetHelperAPTGet helper(c1out);
+ helper.showErrors(false);
+ pkgCache::VerIterator Ver = helper.canNotFindNewestVer(Cache, Pkg);
+ if (Ver.end() == false)
+ Pkg = Ver.ParentPkg();
+ else if (helper.showVirtualPackageErrors(Cache) == false)
+ return AllowFail;
+ }
+
+ if (_config->FindB("Debug::BuildDeps",false) == true)
+ {
+ if (Remove == true)
+ std::cout << " Trying to remove " << Pkg << std::endl;
+ else
+ std::cout << " Trying to install " << Pkg << std::endl;
+ }
+
+ if (Remove == true)
+ {
+ TryToRemove RemoveAction(Cache, &Fix);
+ RemoveAction(Pkg.VersionList());
+ } else if (Cache[Pkg].CandidateVer != 0) {
+ TryToInstall InstallAction(Cache, &Fix, BrokenFix);
+ InstallAction(Cache[Pkg].CandidateVerIter(Cache));
+ InstallAction.doAutoInstall();
+ } else
+ return AllowFail;
+
+ return true;
+}
+ /*}}}*/
+// GetReleaseFileForSourceRecord - Return Suite for the given srcrecord /*{{{*/
+static pkgCache::RlsFileIterator GetReleaseFileForSourceRecord(CacheFile &CacheFile,
+ pkgSourceList *SrcList, pkgSrcRecords::Parser *Parse)
+{
+ // try to find release
+ const pkgIndexFile& CurrentIndexFile = Parse->Index();
+
+ for (pkgSourceList::const_iterator S = SrcList->begin();
+ S != SrcList->end(); ++S)
+ {
+ std::vector<pkgIndexFile *> *Indexes = (*S)->GetIndexFiles();
+ for (std::vector<pkgIndexFile *>::const_iterator IF = Indexes->begin();
+ IF != Indexes->end(); ++IF)
+ {
+ if (&CurrentIndexFile == (*IF))
+ return (*S)->FindInCache(CacheFile, false);
+ }
+ }
+ return pkgCache::RlsFileIterator(CacheFile);
+}
+ /*}}}*/
+// FindSrc - Find a source record /*{{{*/
+static pkgSrcRecords::Parser *FindSrc(const char *Name,
+ pkgSrcRecords &SrcRecs,std::string &Src,
+ CacheFile &CacheFile)
+{
+ std::string VerTag, UserRequestedVerTag;
+ std::string ArchTag = "";
+ std::string RelTag = _config->Find("APT::Default-Release");
+ std::string TmpSrc = Name;
+ pkgDepCache *Cache = CacheFile.GetDepCache();
+
+ // extract release
+ size_t found = TmpSrc.find_last_of("/");
+ if (found != std::string::npos)
+ {
+ RelTag = TmpSrc.substr(found+1);
+ TmpSrc = TmpSrc.substr(0,found);
+ }
+ // extract the version
+ found = TmpSrc.find_last_of("=");
+ if (found != std::string::npos)
+ {
+ VerTag = UserRequestedVerTag = TmpSrc.substr(found+1);
+ TmpSrc = TmpSrc.substr(0,found);
+ }
+ // extract arch
+ found = TmpSrc.find_last_of(":");
+ if (found != std::string::npos)
+ {
+ ArchTag = TmpSrc.substr(found+1);
+ TmpSrc = TmpSrc.substr(0,found);
+ }
+
+ /* Lookup the version of the package we would install if we were to
+ install a version and determine the source package name, then look
+ in the archive for a source package of the same name. */
+ bool MatchSrcOnly = _config->FindB("APT::Get::Only-Source");
+ pkgCache::PkgIterator Pkg;
+ if (ArchTag != "")
+ Pkg = Cache->FindPkg(TmpSrc, ArchTag);
+ else
+ Pkg = Cache->FindPkg(TmpSrc);
+
+ // if we can't find a package but the user qualified with a arch,
+ // error out here
+ if (Pkg.end() && ArchTag != "")
+ {
+ Src = Name;
+ _error->Error(_("Can not find a package for architecture '%s'"),
+ ArchTag.c_str());
+ return 0;
+ }
+
+ if (MatchSrcOnly == false && Pkg.end() == false)
+ {
+ if(VerTag != "" || RelTag != "" || ArchTag != "")
+ {
+ bool fuzzy = false;
+ // we have a default release, try to locate the pkg. we do it like
+ // this because GetCandidateVer() will not "downgrade", that means
+ // "apt-get source -t stable apt" won't work on a unstable system
+ for (pkgCache::VerIterator Ver = Pkg.VersionList();; ++Ver)
+ {
+ // try first only exact matches, later fuzzy matches
+ if (Ver.end() == true)
+ {
+ if (fuzzy == true)
+ break;
+ fuzzy = true;
+ Ver = Pkg.VersionList();
+ // exit right away from the Pkg.VersionList() loop if we
+ // don't have any versions
+ if (Ver.end() == true)
+ break;
+ }
+
+ // ignore arches that are not for us
+ if (ArchTag != "" && Ver.Arch() != ArchTag)
+ continue;
+
+ // pick highest version for the arch unless the user wants
+ // something else
+ if (ArchTag != "" && VerTag == "" && RelTag == "")
+ if(Cache->VS().CmpVersion(VerTag, Ver.VerStr()) < 0)
+ VerTag = Ver.VerStr();
+
+ // We match against a concrete version (or a part of this version)
+ if (VerTag.empty() == false &&
+ (fuzzy == true || Cache->VS().CmpVersion(VerTag, Ver.VerStr()) != 0) && // exact match
+ (fuzzy == false || strncmp(VerTag.c_str(), Ver.VerStr(), VerTag.size()) != 0)) // fuzzy match
+ continue;
+
+ for (pkgCache::VerFileIterator VF = Ver.FileList();
+ VF.end() == false; ++VF)
+ {
+ /* If this is the status file, and the current version is not the
+ version in the status file (ie it is not installed, or somesuch)
+ then it is not a candidate for installation, ever. This weeds
+ out bogus entries that may be due to config-file states, or
+ other. */
+ if ((VF.File()->Flags & pkgCache::Flag::NotSource) ==
+ pkgCache::Flag::NotSource && Pkg.CurrentVer() != Ver)
+ continue;
+
+ // or we match against a release
+ if(VerTag.empty() == false ||
+ (VF.File().Archive() != 0 && VF.File().Archive() == RelTag) ||
+ (VF.File().Codename() != 0 && VF.File().Codename() == RelTag))
+ {
+ // the Version we have is possibly fuzzy or includes binUploads,
+ // so we use the Version of the SourcePkg (empty if same as package)
+ Src = Ver.SourcePkgName();
+ VerTag = Ver.SourceVerStr();
+ break;
+ }
+ }
+ if (Src.empty() == false)
+ break;
+ }
+ }
+
+ if (Src == "" && ArchTag != "")
+ {
+ if (VerTag != "")
+ _error->Error(_("Can not find a package '%s' with version '%s'"),
+ Pkg.FullName().c_str(), VerTag.c_str());
+ if (RelTag != "")
+ _error->Error(_("Can not find a package '%s' with release '%s'"),
+ Pkg.FullName().c_str(), RelTag.c_str());
+ Src = Name;
+ return 0;
+ }
+
+
+ if (Src.empty() == true)
+ {
+ // if we don't have found a fitting package yet so we will
+ // choose a good candidate and proceed with that.
+ // Maybe we will find a source later on with the right VerTag
+ // or RelTag
+ pkgCache::VerIterator Ver = Cache->GetCandidateVer(Pkg);
+ if (Ver.end() == false)
+ {
+ if (strcmp(Ver.SourcePkgName(),Ver.ParentPkg().Name()) != 0)
+ Src = Ver.SourcePkgName();
+ if (VerTag.empty() == true && strcmp(Ver.SourceVerStr(),Ver.VerStr()) != 0)
+ VerTag = Ver.SourceVerStr();
+ }
+ }
+ }
+
+ if (Src.empty() == true)
+ {
+ Src = TmpSrc;
+ }
+ else
+ {
+ /* if we have a source pkg name, make sure to only search
+ for srcpkg names, otherwise apt gets confused if there
+ is a binary package "pkg1" and a source package "pkg1"
+ with the same name but that comes from different packages */
+ MatchSrcOnly = true;
+ if (Src != TmpSrc)
+ {
+ ioprintf(c1out, _("Picking '%s' as source package instead of '%s'\n"), Src.c_str(), TmpSrc.c_str());
+ }
+ }
+
+ // The best hit
+ pkgSrcRecords::Parser *Last = 0;
+ unsigned long Offset = 0;
+ std::string Version;
+ pkgSourceList *SrcList = CacheFile.GetSourceList();
+
+ /* Iterate over all of the hits, which includes the resulting
+ binary packages in the search */
+ pkgSrcRecords::Parser *Parse;
+ while (true)
+ {
+ SrcRecs.Restart();
+ while ((Parse = SrcRecs.Find(Src.c_str(), MatchSrcOnly)) != 0)
+ {
+ const std::string Ver = Parse->Version();
+
+ // See if we need to look for a specific release tag
+ if (RelTag != "" && UserRequestedVerTag == "")
+ {
+ pkgCache::RlsFileIterator const Rls = GetReleaseFileForSourceRecord(CacheFile, SrcList, Parse);
+ if (Rls.end() == false)
+ {
+ if ((Rls->Archive != 0 && RelTag == Rls.Archive()) ||
+ (Rls->Codename != 0 && RelTag == Rls.Codename()))
+ {
+ Last = Parse;
+ Offset = Parse->Offset();
+ Version = Ver;
+ }
+ }
+ }
+
+ // Ignore all versions which doesn't fit
+ if (VerTag.empty() == false &&
+ Cache->VS().CmpVersion(VerTag, Ver) != 0) // exact match
+ continue;
+
+ // Newer version or an exact match? Save the hit
+ if (Last == 0 || Cache->VS().CmpVersion(Version,Ver) < 0) {
+ Last = Parse;
+ Offset = Parse->Offset();
+ Version = Ver;
+ }
+
+ // was the version check above an exact match?
+ // If so, we don't need to look further
+ if (VerTag.empty() == false && (VerTag == Ver))
+ break;
+ }
+ if (UserRequestedVerTag == "" && Version != "" && RelTag != "")
+ ioprintf(c1out, "Selected version '%s' (%s) for %s\n",
+ Version.c_str(), RelTag.c_str(), Src.c_str());
+
+ if (Last != 0 || VerTag.empty() == true)
+ break;
+ _error->Error(_("Can not find version '%s' of package '%s'"), VerTag.c_str(), TmpSrc.c_str());
+ return 0;
+ }
+
+ if (Last == 0 || Last->Jump(Offset) == false)
+ return 0;
+
+ return Last;
+}
+ /*}}}*/
+// DoSource - Fetch a source archive /*{{{*/
+// ---------------------------------------------------------------------
+/* Fetch souce packages */
+struct DscFile
+{
+ std::string Package;
+ std::string Version;
+ std::string Dsc;
+};
+bool DoSource(CommandLine &CmdL)
+{
+ CacheFile Cache;
+ if (Cache.Open(false) == false)
+ return false;
+
+ if (CmdL.FileSize() <= 1)
+ return _error->Error(_("Must specify at least one package to fetch source for"));
+
+ // Read the source list
+ if (Cache.BuildSourceList() == false)
+ return false;
+ pkgSourceList *List = Cache.GetSourceList();
+
+ // Create the text record parsers
+ pkgSrcRecords SrcRecs(*List);
+ if (_error->PendingError() == true)
+ return false;
+
+ std::unique_ptr<DscFile[]> Dsc(new DscFile[CmdL.FileSize()]);
+
+ // insert all downloaded uris into this set to avoid downloading them
+ // twice
+ std::set<std::string> queued;
+
+ // Diff only mode only fetches .diff files
+ bool const diffOnly = _config->FindB("APT::Get::Diff-Only", false);
+ // Tar only mode only fetches .tar files
+ bool const tarOnly = _config->FindB("APT::Get::Tar-Only", false);
+ // Dsc only mode only fetches .dsc files
+ bool const dscOnly = _config->FindB("APT::Get::Dsc-Only", false);
+
+ // Load the requestd sources into the fetcher
+ aptAcquireWithTextStatus Fetcher;
+ unsigned J = 0;
+ std::vector<std::string> UntrustedList;
+ for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
+ {
+ std::string Src;
+ pkgSrcRecords::Parser *Last = FindSrc(*I,SrcRecs,Src,Cache);
+ if (Last == 0) {
+ return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
+ }
+
+ if (Last->Index().IsTrusted() == false)
+ UntrustedList.push_back(Src);
+
+ std::string srec = Last->AsStr();
+ std::string::size_type pos = srec.find("\nVcs-");
+ while (pos != std::string::npos)
+ {
+ pos += strlen("\nVcs-");
+ std::string vcs = srec.substr(pos,srec.find(":",pos)-pos);
+ if(vcs == "Browser")
+ {
+ pos = srec.find("\nVcs-", pos);
+ continue;
+ }
+ pos += vcs.length()+2;
+ std::string::size_type epos = srec.find("\n", pos);
+ std::string const uri = srec.substr(pos,epos-pos);
+ ioprintf(c1out, _("NOTICE: '%s' packaging is maintained in "
+ "the '%s' version control system at:\n"
+ "%s\n"),
+ Src.c_str(), vcs.c_str(), uri.c_str());
+ std::string vcscmd;
+ if (vcs == "Bzr")
+ vcscmd = "bzr branch " + uri;
+ else if (vcs == "Git")
+ vcscmd = "git clone " + uri;
+
+ if (vcscmd.empty() == false)
+ ioprintf(c1out,_("Please use:\n%s\n"
+ "to retrieve the latest (possibly unreleased) "
+ "updates to the package.\n"),
+ vcscmd.c_str());
+ break;
+ }
+
+ // Back track
+ std::vector<pkgSrcRecords::File2> Lst;
+ if (Last->Files2(Lst) == false) {
+ return false;
+ }
+
+ // Load them into the fetcher
+ for (std::vector<pkgSrcRecords::File2>::const_iterator I = Lst.begin();
+ I != Lst.end(); ++I)
+ {
+ // Try to guess what sort of file it is we are getting.
+ if (I->Type == "dsc")
+ {
+ Dsc[J].Package = Last->Package();
+ Dsc[J].Version = Last->Version();
+ Dsc[J].Dsc = flNotDir(I->Path);
+ }
+
+ // Handle the only options so that multiple can be used at once
+ if (diffOnly == true || tarOnly == true || dscOnly == true)
+ {
+ if ((diffOnly == true && I->Type == "diff") ||
+ (tarOnly == true && I->Type == "tar") ||
+ (dscOnly == true && I->Type == "dsc"))
+ ; // Fine, we want this file downloaded
+ else
+ continue;
+ }
+
+ // don't download the same uri twice (should this be moved to
+ // the fetcher interface itself?)
+ if(queued.find(Last->Index().ArchiveURI(I->Path)) != queued.end())
+ continue;
+ queued.insert(Last->Index().ArchiveURI(I->Path));
+
+ // check if we have a file with that md5 sum already localy
+ std::string localFile = flNotDir(I->Path);
+ if (FileExists(localFile) == true)
+ if(I->Hashes.VerifyFile(localFile) == true)
+ {
+ ioprintf(c1out,_("Skipping already downloaded file '%s'\n"),
+ localFile.c_str());
+ continue;
+ }
+
+ // see if we have a hash (Acquire::ForceHash is the only way to have none)
+ if (I->Hashes.usable() == false && _config->FindB("APT::Get::AllowUnauthenticated",false) == false)
+ {
+ ioprintf(c1out, "Skipping download of file '%s' as requested hashsum is not available for authentication\n",
+ localFile.c_str());
+ continue;
+ }
+
+ new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
+ I->Hashes, I->FileSize, Last->Index().SourceInfo(*Last,*I), Src);
+ }
+ }
+
+ // Display statistics
+ unsigned long long FetchBytes = Fetcher.FetchNeeded();
+ unsigned long long FetchPBytes = Fetcher.PartialPresent();
+ unsigned long long DebBytes = Fetcher.TotalNeeded();
+
+ if (CheckFreeSpaceBeforeDownload(".", (FetchBytes - FetchPBytes)) == false)
+ return false;
+
+ // Number of bytes
+ if (DebBytes != FetchBytes)
+ //TRANSLATOR: The required space between number and unit is already included
+ // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
+ ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
+ SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
+ else
+ //TRANSLATOR: The required space between number and unit is already included
+ // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
+ ioprintf(c1out,_("Need to get %sB of source archives.\n"),
+ SizeToStr(DebBytes).c_str());
+
+ if (_config->FindB("APT::Get::Simulate",false) == true)
+ {
+ for (unsigned I = 0; I != J; I++)
+ ioprintf(std::cout,_("Fetch source %s\n"),Dsc[I].Package.c_str());
+ return true;
+ }
+
+ // Just print out the uris an exit if the --print-uris flag was used
+ if (_config->FindB("APT::Get::Print-URIs") == true)
+ {
+ pkgAcquire::UriIterator I = Fetcher.UriBegin();
+ for (; I != Fetcher.UriEnd(); ++I)
+ std::cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
+ I->Owner->FileSize << ' ' << I->Owner->HashSum() << std::endl;
+ return true;
+ }
+
+ // check authentication status of the source as well
+ if (UntrustedList.empty() == false && AuthPrompt(UntrustedList, false) == false)
+ return false;
+
+ // Run it
+ bool Failed = false;
+ if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
+ {
+ return _error->Error(_("Failed to fetch some archives."));
+ }
+
+ if (_config->FindB("APT::Get::Download-only",false) == true)
+ {
+ c1out << _("Download complete and in download only mode") << std::endl;
+ return true;
+ }
+
+ // Unpack the sources
+ pid_t Process = ExecFork();
+
+ if (Process == 0)
+ {
+ bool const fixBroken = _config->FindB("APT::Get::Fix-Broken", false);
+ for (unsigned I = 0; I != J; ++I)
+ {
+ std::string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
+
+ // Diff only mode only fetches .diff files
+ if (_config->FindB("APT::Get::Diff-Only",false) == true ||
+ _config->FindB("APT::Get::Tar-Only",false) == true ||
+ Dsc[I].Dsc.empty() == true)
+ continue;
+
+ // See if the package is already unpacked
+ struct stat Stat;
+ if (fixBroken == false && stat(Dir.c_str(),&Stat) == 0 &&
+ S_ISDIR(Stat.st_mode) != 0)
+ {
+ ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
+ Dir.c_str());
+ }
+ else
+ {
+ // Call dpkg-source
+ std::string const sourceopts = _config->Find("DPkg::Source-Options", "-x");
+ std::string S;
+ strprintf(S, "%s %s %s",
+ _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
+ sourceopts.c_str(), Dsc[I].Dsc.c_str());
+ if (system(S.c_str()) != 0)
+ {
+ fprintf(stderr, _("Unpack command '%s' failed.\n"), S.c_str());
+ fprintf(stderr, _("Check if the 'dpkg-dev' package is installed.\n"));
+ _exit(1);
+ }
+ }
+
+ // Try to compile it with dpkg-buildpackage
+ if (_config->FindB("APT::Get::Compile",false) == true)
+ {
+ std::string buildopts = _config->Find("APT::Get::Host-Architecture");
+ if (buildopts.empty() == false)
+ buildopts = "-a" + buildopts + " ";
+
+ // get all active build profiles
+ std::string const profiles = APT::Configuration::getBuildProfilesString();
+ if (profiles.empty() == false)
+ buildopts.append(" -P").append(profiles).append(" ");
+
+ buildopts.append(_config->Find("DPkg::Build-Options","-b -uc"));
+
+ // Call dpkg-buildpackage
+ std::string S;
+ strprintf(S, "cd %s && %s %s",
+ Dir.c_str(),
+ _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
+ buildopts.c_str());
+
+ if (system(S.c_str()) != 0)
+ {
+ fprintf(stderr, _("Build command '%s' failed.\n"), S.c_str());
+ _exit(1);
+ }
+ }
+ }
+
+ _exit(0);
+ }
+
+ return ExecWait(Process, "dpkg-source");
+}
+ /*}}}*/
+// DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
+// ---------------------------------------------------------------------
+/* This function will look at the build depends list of the given source
+ package and install the necessary packages to make it true, or fail. */
+bool DoBuildDep(CommandLine &CmdL)
+{
+ CacheFile Cache;
+
+ _config->Set("APT::Install-Recommends", false);
+
+ bool WantLock = _config->FindB("APT::Get::Print-URIs", false) == false;
+
+ if (Cache.Open(WantLock) == false)
+ return false;
+
+ if (CmdL.FileSize() <= 1)
+ return _error->Error(_("Must specify at least one package to check builddeps for"));
+
+ // Read the source list
+ if (Cache.BuildSourceList() == false)
+ return false;
+ pkgSourceList *List = Cache.GetSourceList();
+
+ // Create the text record parsers
+ pkgSrcRecords SrcRecs(*List);
+ if (_error->PendingError() == true)
+ return false;
+
+ bool StripMultiArch;
+ std::string hostArch = _config->Find("APT::Get::Host-Architecture");
+ if (hostArch.empty() == false)
+ {
+ std::vector<std::string> archs = APT::Configuration::getArchitectures();
+ if (std::find(archs.begin(), archs.end(), hostArch) == archs.end())
+ return _error->Error(_("No architecture information available for %s. See apt.conf(5) APT::Architectures for setup"), hostArch.c_str());
+ StripMultiArch = false;
+ }
+ else
+ StripMultiArch = true;
+
+ unsigned J = 0;
+ for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
+ {
+ std::string Src;
+ pkgSrcRecords::Parser *Last = 0;
+ std::unique_ptr<pkgSrcRecords::Parser> LastOwner;
+
+ // an unpacked debian source tree
+ using APT::String::Startswith;
+ if ((Startswith(*I, "./") || Startswith(*I, "/")) &&
+ DirectoryExists(*I))
+ {
+ ioprintf(c1out, _("Note, using directory '%s' to get the build dependencies\n"), *I);
+ // FIXME: how can we make this more elegant?
+ std::string TypeName = "Debian control file";
+ pkgIndexFile::Type *Type = pkgIndexFile::Type::GetType(TypeName.c_str());
+ if(Type != NULL)
+ LastOwner.reset(Last = Type->CreateSrcPkgParser(*I));
+ }
+ // if its a local file (e.g. .dsc) use this
+ else if (FileExists(*I))
+ {
+ ioprintf(c1out, _("Note, using file '%s' to get the build dependencies\n"), *I);
+
+ // see if we can get a parser for this pkgIndexFile type
+ std::string TypeName = "Debian " + flExtension(*I) + " file";
+ pkgIndexFile::Type *Type = pkgIndexFile::Type::GetType(TypeName.c_str());
+ if(Type != NULL)
+ LastOwner.reset(Last = Type->CreateSrcPkgParser(*I));
+ } else {
+ // normal case, search the cache for the source file
+ Last = FindSrc(*I,SrcRecs,Src,Cache);
+ }
+
+ if (Last == 0)
+ return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
+
+ // Process the build-dependencies
+ std::vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
+ // FIXME: Can't specify architecture to use for [wildcard] matching, so switch default arch temporary
+ if (hostArch.empty() == false)
+ {
+ std::string nativeArch = _config->Find("APT::Architecture");
+ _config->Set("APT::Architecture", hostArch);
+ bool Success = Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch);
+ _config->Set("APT::Architecture", nativeArch);
+ if (Success == false)
+ return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
+ }
+ else if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
+ return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
+
+ // Also ensure that build-essential packages are present
+ Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
+ if (Opts)
+ Opts = Opts->Child;
+ for (; Opts; Opts = Opts->Next)
+ {
+ if (Opts->Value.empty() == true)
+ continue;
+
+ pkgSrcRecords::Parser::BuildDepRec rec;
+ rec.Package = Opts->Value;
+ rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
+ rec.Op = 0;
+ BuildDeps.push_back(rec);
+ }
+
+ if (BuildDeps.empty() == true)
+ {
+ ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
+ continue;
+ }
+
+ // Install the requested packages
+ std::vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
+ pkgProblemResolver Fix(Cache);
+ bool skipAlternatives = false; // skip remaining alternatives in an or group
+ for (D = BuildDeps.begin(); D != BuildDeps.end(); ++D)
+ {
+ bool hasAlternatives = (((*D).Op & pkgCache::Dep::Or) == pkgCache::Dep::Or);
+
+ if (skipAlternatives == true)
+ {
+ /*
+ * if there are alternatives, we've already picked one, so skip
+ * the rest
+ *
+ * TODO: this means that if there's a build-dep on A|B and B is
+ * installed, we'll still try to install A; more importantly,
+ * if A is currently broken, we cannot go back and try B. To fix
+ * this would require we do a Resolve cycle for each package we
+ * add to the install list. Ugh
+ */
+ if (!hasAlternatives)
+ skipAlternatives = false; // end of or group
+ continue;
+ }
+
+ if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
+ (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
+ {
+ pkgCache::GrpIterator Grp = Cache->FindGrp((*D).Package);
+ // Build-conflicts on unknown packages are silently ignored
+ if (Grp.end() == true)
+ continue;
+
+ for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
+ {
+ pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
+ /*
+ * Remove if we have an installed version that satisfies the
+ * version criteria
+ */
+ if (IV.end() == false &&
+ Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
+ TryToInstallBuildDep(Pkg,Cache,Fix,true,false);
+ }
+ }
+ else // BuildDep || BuildDepIndep
+ {
+ if (_config->FindB("Debug::BuildDeps",false) == true)
+ std::cout << "Looking for " << (*D).Package << "...\n";
+
+ pkgCache::PkgIterator Pkg;
+
+ // Cross-Building?
+ if (StripMultiArch == false && D->Type != pkgSrcRecords::Parser::BuildDependIndep)
+ {
+ size_t const colon = D->Package.find(":");
+ if (colon != std::string::npos)
+ {
+ if (strcmp(D->Package.c_str() + colon, ":any") == 0 || strcmp(D->Package.c_str() + colon, ":native") == 0)
+ Pkg = Cache->FindPkg(D->Package.substr(0,colon));
+ else
+ Pkg = Cache->FindPkg(D->Package);
+ }
+ else
+ Pkg = Cache->FindPkg(D->Package, hostArch);
+
+ // a bad version either is invalid or doesn't satify dependency
+#define BADVER(Ver) (Ver.end() == true || \
+ (D->Version.empty() == false && \
+ Cache->VS().CheckDep(Ver.VerStr(),D->Op,D->Version.c_str()) == false))
+
+ APT::VersionList verlist;
+ if (Pkg.end() == false)
+ {
+ pkgCache::VerIterator Ver = (*Cache)[Pkg].InstVerIter(*Cache);
+ if (BADVER(Ver) == false)
+ verlist.insert(Ver);
+ Ver = (*Cache)[Pkg].CandidateVerIter(*Cache);
+ if (BADVER(Ver) == false)
+ verlist.insert(Ver);
+ }
+ if (verlist.empty() == true)
+ {
+ pkgCache::PkgIterator BuildPkg = Cache->FindPkg(D->Package, "native");
+ if (BuildPkg.end() == false && Pkg != BuildPkg)
+ {
+ pkgCache::VerIterator Ver = (*Cache)[BuildPkg].InstVerIter(*Cache);
+ if (BADVER(Ver) == false)
+ verlist.insert(Ver);
+ Ver = (*Cache)[BuildPkg].CandidateVerIter(*Cache);
+ if (BADVER(Ver) == false)
+ verlist.insert(Ver);
+ }
+ }
+#undef BADVER
+
+ std::string forbidden;
+ // We need to decide if host or build arch, so find a version we can look at
+ APT::VersionList::const_iterator Ver = verlist.begin();
+ for (; Ver != verlist.end(); ++Ver)
+ {
+ forbidden.clear();
+ if (Ver->MultiArch == pkgCache::Version::No || Ver->MultiArch == pkgCache::Version::All)
+ {
+ if (colon == std::string::npos)
+ Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
+ else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
+ forbidden = "Multi-Arch: no";
+ else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
+ Pkg = Ver.ParentPkg().Group().FindPkg("native");
+ }
+ else if (Ver->MultiArch == pkgCache::Version::Same)
+ {
+ if (colon == std::string::npos)
+ Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
+ else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
+ forbidden = "Multi-Arch: same";
+ else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
+ Pkg = Ver.ParentPkg().Group().FindPkg("native");
+ }
+ else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
+ {
+ if (colon == std::string::npos)
+ Pkg = Ver.ParentPkg().Group().FindPkg("native");
+ else if (strcmp(D->Package.c_str() + colon, ":any") == 0 ||
+ strcmp(D->Package.c_str() + colon, ":native") == 0)
+ forbidden = "Multi-Arch: foreign";
+ }
+ else if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
+ {
+ if (colon == std::string::npos)
+ Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
+ else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
+ {
+ // prefer any installed over preferred non-installed architectures
+ pkgCache::GrpIterator Grp = Ver.ParentPkg().Group();
+ // we don't check for version here as we are better of with upgrading than remove and install
+ for (Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
+ if (Pkg.CurrentVer().end() == false)
+ break;
+ if (Pkg.end() == true)
+ Pkg = Grp.FindPreferredPkg(true);
+ }
+ else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
+ Pkg = Ver.ParentPkg().Group().FindPkg("native");
+ }
+
+ if (forbidden.empty() == false)
+ {
+ if (_config->FindB("Debug::BuildDeps",false) == true)
+ std::cout << D->Package.substr(colon, std::string::npos) << " is not allowed from " << forbidden << " package " << (*D).Package << " (" << Ver.VerStr() << ")" << std::endl;
+ continue;
+ }
+
+ //we found a good version
+ break;
+ }
+ if (Ver == verlist.end())
+ {
+ if (_config->FindB("Debug::BuildDeps",false) == true)
+ std::cout << " No multiarch info as we have no satisfying installed nor candidate for " << D->Package << " on build or host arch" << std::endl;
+
+ if (forbidden.empty() == false)
+ {
+ if (hasAlternatives)
+ continue;
+ return _error->Error(_("%s dependency for %s can't be satisfied "
+ "because %s is not allowed on '%s' packages"),
+ Last->BuildDepType(D->Type), Src.c_str(),
+ D->Package.c_str(), forbidden.c_str());
+ }
+ }
+ }
+ else
+ Pkg = Cache->FindPkg(D->Package);
+
+ if (Pkg.end() == true || (Pkg->VersionList == 0 && Pkg->ProvidesList == 0))
+ {
+ if (_config->FindB("Debug::BuildDeps",false) == true)
+ std::cout << " (not found)" << (*D).Package << std::endl;
+
+ if (hasAlternatives)
+ continue;
+
+ return _error->Error(_("%s dependency for %s cannot be satisfied "
+ "because the package %s cannot be found"),
+ Last->BuildDepType((*D).Type),Src.c_str(),
+ (*D).Package.c_str());
+ }
+
+ pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
+ if (IV.end() == false)
+ {
+ if (_config->FindB("Debug::BuildDeps",false) == true)
+ std::cout << " Is installed\n";
+
+ if (D->Version.empty() == true ||
+ Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
+ {
+ skipAlternatives = hasAlternatives;
+ continue;
+ }
+
+ if (_config->FindB("Debug::BuildDeps",false) == true)
+ std::cout << " ...but the installed version doesn't meet the version requirement\n";
+
+ if (((*D).Op & pkgCache::Dep::LessEq) == pkgCache::Dep::LessEq)
+ return _error->Error(_("Failed to satisfy %s dependency for %s: Installed package %s is too new"),
+ Last->BuildDepType((*D).Type), Src.c_str(), Pkg.FullName(true).c_str());
+ }
+
+ // Only consider virtual packages if there is no versioned dependency
+ if ((*D).Version.empty() == true)
+ {
+ /*
+ * If this is a virtual package, we need to check the list of
+ * packages that provide it and see if any of those are
+ * installed
+ */
+ pkgCache::PrvIterator Prv = Pkg.ProvidesList();
+ for (; Prv.end() != true; ++Prv)
+ {
+ if (_config->FindB("Debug::BuildDeps",false) == true)
+ std::cout << " Checking provider " << Prv.OwnerPkg().FullName() << std::endl;
+
+ if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
+ break;
+ }
+
+ if (Prv.end() == false)
+ {
+ if (_config->FindB("Debug::BuildDeps",false) == true)
+ std::cout << " Is provided by installed package " << Prv.OwnerPkg().FullName() << std::endl;
+ skipAlternatives = hasAlternatives;
+ continue;
+ }
+ }
+ else // versioned dependency
+ {
+ pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
+ if (CV.end() == true ||
+ Cache->VS().CheckDep(CV.VerStr(),(*D).Op,(*D).Version.c_str()) == false)
+ {
+ if (hasAlternatives)
+ continue;
+ else if (CV.end() == false)
+ return _error->Error(_("%s dependency for %s cannot be satisfied "
+ "because candidate version of package %s "
+ "can't satisfy version requirements"),
+ Last->BuildDepType(D->Type), Src.c_str(),
+ D->Package.c_str());
+ else
+ return _error->Error(_("%s dependency for %s cannot be satisfied "
+ "because package %s has no candidate version"),
+ Last->BuildDepType(D->Type), Src.c_str(),
+ D->Package.c_str());
+ }
+ }
+
+ if (TryToInstallBuildDep(Pkg,Cache,Fix,false,false,false) == true)
+ {
+ // We successfully installed something; skip remaining alternatives
+ skipAlternatives = hasAlternatives;
+ if(_config->FindB("APT::Get::Build-Dep-Automatic", false) == true)
+ Cache->MarkAuto(Pkg, true);
+ continue;
+ }
+ else if (hasAlternatives)
+ {
+ if (_config->FindB("Debug::BuildDeps",false) == true)
+ std::cout << " Unsatisfiable, trying alternatives\n";
+ continue;
+ }
+ else
+ {
+ return _error->Error(_("Failed to satisfy %s dependency for %s: %s"),
+ Last->BuildDepType((*D).Type),
+ Src.c_str(),
+ (*D).Package.c_str());
+ }
+ }
+ }
+
+ if (Fix.Resolve(true) == false)
+ _error->Discard();
+
+ // Now we check the state of the packages,
+ if (Cache->BrokenCount() != 0)
+ {
+ ShowBroken(std::cout, Cache, false);
+ return _error->Error(_("Build-dependencies for %s could not be satisfied."),*I);
+ }
+ }
+
+ if (InstallPackages(Cache, false, true) == false)
+ return _error->Error(_("Failed to process build dependencies"));
+ return true;
+}
+ /*}}}*/
diff --git a/apt-private/private-source.h b/apt-private/private-source.h
new file mode 100644
index 000000000..d00c53eb1
--- /dev/null
+++ b/apt-private/private-source.h
@@ -0,0 +1,11 @@
+#ifndef APT_PRIVATE_SOURCE_H
+#define APT_PRIVATE_SOURCE_H
+
+#include <apt-pkg/macros.h>
+
+class CommandLine;
+
+APT_PUBLIC bool DoSource(CommandLine &CmdL);
+APT_PUBLIC bool DoBuildDep(CommandLine &CmdL);
+
+#endif