private-source.cc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. // Include Files /*{{{*/
  2. #include <config.h>
  3. #include <apt-pkg/acquire-item.h>
  4. #include <apt-pkg/acquire.h>
  5. #include <apt-pkg/algorithms.h>
  6. #include <apt-pkg/aptconfiguration.h>
  7. #include <apt-pkg/cachefile.h>
  8. #include <apt-pkg/cacheiterators.h>
  9. #include <apt-pkg/cacheset.h>
  10. #include <apt-pkg/cmndline.h>
  11. #include <apt-pkg/configuration.h>
  12. #include <apt-pkg/depcache.h>
  13. #include <apt-pkg/error.h>
  14. #include <apt-pkg/fileutl.h>
  15. #include <apt-pkg/hashes.h>
  16. #include <apt-pkg/indexfile.h>
  17. #include <apt-pkg/metaindex.h>
  18. #include <apt-pkg/pkgcache.h>
  19. #include <apt-pkg/sourcelist.h>
  20. #include <apt-pkg/srcrecords.h>
  21. #include <apt-pkg/strutl.h>
  22. #include <apt-pkg/version.h>
  23. #include <apt-private/private-cachefile.h>
  24. #include <apt-private/private-cacheset.h>
  25. #include <apt-private/private-download.h>
  26. #include <apt-private/private-install.h>
  27. #include <apt-private/private-source.h>
  28. #include <stddef.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <sys/stat.h>
  33. #include <unistd.h>
  34. #include <iostream>
  35. #include <set>
  36. #include <string>
  37. #include <vector>
  38. #include <apti18n.h>
  39. /*}}}*/
  40. // TryToInstallBuildDep - Try to install a single package /*{{{*/
  41. // ---------------------------------------------------------------------
  42. /* This used to be inlined in DoInstall, but with the advent of regex package
  43. name matching it was split out.. */
  44. static bool TryToInstallBuildDep(pkgCache::PkgIterator Pkg,pkgCacheFile &Cache,
  45. pkgProblemResolver &Fix,bool Remove,bool BrokenFix,
  46. bool AllowFail = true)
  47. {
  48. if (Cache[Pkg].CandidateVer == 0 && Pkg->ProvidesList != 0)
  49. {
  50. CacheSetHelperAPTGet helper(c1out);
  51. helper.showErrors(false);
  52. pkgCache::VerIterator Ver = helper.canNotFindNewestVer(Cache, Pkg);
  53. if (Ver.end() == false)
  54. Pkg = Ver.ParentPkg();
  55. else if (helper.showVirtualPackageErrors(Cache) == false)
  56. return AllowFail;
  57. }
  58. if (_config->FindB("Debug::BuildDeps",false) == true)
  59. {
  60. if (Remove == true)
  61. std::cout << " Trying to remove " << Pkg << std::endl;
  62. else
  63. std::cout << " Trying to install " << Pkg << std::endl;
  64. }
  65. if (Remove == true)
  66. {
  67. TryToRemove RemoveAction(Cache, &Fix);
  68. RemoveAction(Pkg.VersionList());
  69. } else if (Cache[Pkg].CandidateVer != 0) {
  70. TryToInstall InstallAction(Cache, &Fix, BrokenFix);
  71. InstallAction(Cache[Pkg].CandidateVerIter(Cache));
  72. InstallAction.doAutoInstall();
  73. } else
  74. return AllowFail;
  75. return true;
  76. }
  77. /*}}}*/
  78. // GetReleaseFileForSourceRecord - Return Suite for the given srcrecord /*{{{*/
  79. static pkgCache::RlsFileIterator GetReleaseFileForSourceRecord(CacheFile &CacheFile,
  80. pkgSourceList *SrcList, pkgSrcRecords::Parser *Parse)
  81. {
  82. // try to find release
  83. const pkgIndexFile& CurrentIndexFile = Parse->Index();
  84. for (pkgSourceList::const_iterator S = SrcList->begin();
  85. S != SrcList->end(); ++S)
  86. {
  87. std::vector<pkgIndexFile *> *Indexes = (*S)->GetIndexFiles();
  88. for (std::vector<pkgIndexFile *>::const_iterator IF = Indexes->begin();
  89. IF != Indexes->end(); ++IF)
  90. {
  91. if (&CurrentIndexFile == (*IF))
  92. return (*S)->FindInCache(CacheFile, false);
  93. }
  94. }
  95. return pkgCache::RlsFileIterator(CacheFile);
  96. }
  97. /*}}}*/
  98. // FindSrc - Find a source record /*{{{*/
  99. static pkgSrcRecords::Parser *FindSrc(const char *Name,
  100. pkgSrcRecords &SrcRecs,std::string &Src,
  101. CacheFile &CacheFile)
  102. {
  103. std::string VerTag, UserRequestedVerTag;
  104. std::string ArchTag = "";
  105. std::string RelTag = _config->Find("APT::Default-Release");
  106. std::string TmpSrc = Name;
  107. pkgDepCache *Cache = CacheFile.GetDepCache();
  108. // extract release
  109. size_t found = TmpSrc.find_last_of("/");
  110. if (found != std::string::npos)
  111. {
  112. RelTag = TmpSrc.substr(found+1);
  113. TmpSrc = TmpSrc.substr(0,found);
  114. }
  115. // extract the version
  116. found = TmpSrc.find_last_of("=");
  117. if (found != std::string::npos)
  118. {
  119. VerTag = UserRequestedVerTag = TmpSrc.substr(found+1);
  120. TmpSrc = TmpSrc.substr(0,found);
  121. }
  122. // extract arch
  123. found = TmpSrc.find_last_of(":");
  124. if (found != std::string::npos)
  125. {
  126. ArchTag = TmpSrc.substr(found+1);
  127. TmpSrc = TmpSrc.substr(0,found);
  128. }
  129. /* Lookup the version of the package we would install if we were to
  130. install a version and determine the source package name, then look
  131. in the archive for a source package of the same name. */
  132. bool MatchSrcOnly = _config->FindB("APT::Get::Only-Source");
  133. pkgCache::PkgIterator Pkg;
  134. if (ArchTag != "")
  135. Pkg = Cache->FindPkg(TmpSrc, ArchTag);
  136. else
  137. Pkg = Cache->FindPkg(TmpSrc);
  138. // if we can't find a package but the user qualified with a arch,
  139. // error out here
  140. if (Pkg.end() && ArchTag != "")
  141. {
  142. Src = Name;
  143. _error->Error(_("Can not find a package for architecture '%s'"),
  144. ArchTag.c_str());
  145. return 0;
  146. }
  147. if (MatchSrcOnly == false && Pkg.end() == false)
  148. {
  149. if(VerTag != "" || RelTag != "" || ArchTag != "")
  150. {
  151. bool fuzzy = false;
  152. // we have a default release, try to locate the pkg. we do it like
  153. // this because GetCandidateVer() will not "downgrade", that means
  154. // "apt-get source -t stable apt" won't work on a unstable system
  155. for (pkgCache::VerIterator Ver = Pkg.VersionList();; ++Ver)
  156. {
  157. // try first only exact matches, later fuzzy matches
  158. if (Ver.end() == true)
  159. {
  160. if (fuzzy == true)
  161. break;
  162. fuzzy = true;
  163. Ver = Pkg.VersionList();
  164. // exit right away from the Pkg.VersionList() loop if we
  165. // don't have any versions
  166. if (Ver.end() == true)
  167. break;
  168. }
  169. // ignore arches that are not for us
  170. if (ArchTag != "" && Ver.Arch() != ArchTag)
  171. continue;
  172. // pick highest version for the arch unless the user wants
  173. // something else
  174. if (ArchTag != "" && VerTag == "" && RelTag == "")
  175. if(Cache->VS().CmpVersion(VerTag, Ver.VerStr()) < 0)
  176. VerTag = Ver.VerStr();
  177. // We match against a concrete version (or a part of this version)
  178. if (VerTag.empty() == false &&
  179. (fuzzy == true || Cache->VS().CmpVersion(VerTag, Ver.VerStr()) != 0) && // exact match
  180. (fuzzy == false || strncmp(VerTag.c_str(), Ver.VerStr(), VerTag.size()) != 0)) // fuzzy match
  181. continue;
  182. for (pkgCache::VerFileIterator VF = Ver.FileList();
  183. VF.end() == false; ++VF)
  184. {
  185. /* If this is the status file, and the current version is not the
  186. version in the status file (ie it is not installed, or somesuch)
  187. then it is not a candidate for installation, ever. This weeds
  188. out bogus entries that may be due to config-file states, or
  189. other. */
  190. if ((VF.File()->Flags & pkgCache::Flag::NotSource) ==
  191. pkgCache::Flag::NotSource && Pkg.CurrentVer() != Ver)
  192. continue;
  193. // or we match against a release
  194. if(VerTag.empty() == false ||
  195. (VF.File().Archive() != 0 && VF.File().Archive() == RelTag) ||
  196. (VF.File().Codename() != 0 && VF.File().Codename() == RelTag))
  197. {
  198. // the Version we have is possibly fuzzy or includes binUploads,
  199. // so we use the Version of the SourcePkg (empty if same as package)
  200. Src = Ver.SourcePkgName();
  201. VerTag = Ver.SourceVerStr();
  202. break;
  203. }
  204. }
  205. if (Src.empty() == false)
  206. break;
  207. }
  208. }
  209. if (Src == "" && ArchTag != "")
  210. {
  211. if (VerTag != "")
  212. _error->Error(_("Can not find a package '%s' with version '%s'"),
  213. Pkg.FullName().c_str(), VerTag.c_str());
  214. if (RelTag != "")
  215. _error->Error(_("Can not find a package '%s' with release '%s'"),
  216. Pkg.FullName().c_str(), RelTag.c_str());
  217. Src = Name;
  218. return 0;
  219. }
  220. if (Src.empty() == true)
  221. {
  222. // if we don't have found a fitting package yet so we will
  223. // choose a good candidate and proceed with that.
  224. // Maybe we will find a source later on with the right VerTag
  225. // or RelTag
  226. pkgCache::VerIterator const Ver = Cache->GetCandidateVersion(Pkg);
  227. if (Ver.end() == false)
  228. {
  229. if (strcmp(Ver.SourcePkgName(),Ver.ParentPkg().Name()) != 0)
  230. Src = Ver.SourcePkgName();
  231. if (VerTag.empty() == true && strcmp(Ver.SourceVerStr(),Ver.VerStr()) != 0)
  232. VerTag = Ver.SourceVerStr();
  233. }
  234. }
  235. }
  236. if (Src.empty() == true)
  237. {
  238. Src = TmpSrc;
  239. }
  240. else
  241. {
  242. /* if we have a source pkg name, make sure to only search
  243. for srcpkg names, otherwise apt gets confused if there
  244. is a binary package "pkg1" and a source package "pkg1"
  245. with the same name but that comes from different packages */
  246. MatchSrcOnly = true;
  247. if (Src != TmpSrc)
  248. {
  249. ioprintf(c1out, _("Picking '%s' as source package instead of '%s'\n"), Src.c_str(), TmpSrc.c_str());
  250. }
  251. }
  252. // The best hit
  253. pkgSrcRecords::Parser *Last = 0;
  254. unsigned long Offset = 0;
  255. std::string Version;
  256. pkgSourceList *SrcList = CacheFile.GetSourceList();
  257. /* Iterate over all of the hits, which includes the resulting
  258. binary packages in the search */
  259. pkgSrcRecords::Parser *Parse;
  260. while (true)
  261. {
  262. SrcRecs.Restart();
  263. while ((Parse = SrcRecs.Find(Src.c_str(), MatchSrcOnly)) != 0)
  264. {
  265. const std::string Ver = Parse->Version();
  266. // See if we need to look for a specific release tag
  267. if (RelTag != "" && UserRequestedVerTag == "")
  268. {
  269. pkgCache::RlsFileIterator const Rls = GetReleaseFileForSourceRecord(CacheFile, SrcList, Parse);
  270. if (Rls.end() == false)
  271. {
  272. if ((Rls->Archive != 0 && RelTag == Rls.Archive()) ||
  273. (Rls->Codename != 0 && RelTag == Rls.Codename()))
  274. {
  275. Last = Parse;
  276. Offset = Parse->Offset();
  277. Version = Ver;
  278. }
  279. }
  280. }
  281. // Ignore all versions which doesn't fit
  282. if (VerTag.empty() == false &&
  283. Cache->VS().CmpVersion(VerTag, Ver) != 0) // exact match
  284. continue;
  285. // Newer version or an exact match? Save the hit
  286. if (Last == 0 || Cache->VS().CmpVersion(Version,Ver) < 0) {
  287. Last = Parse;
  288. Offset = Parse->Offset();
  289. Version = Ver;
  290. }
  291. // was the version check above an exact match?
  292. // If so, we don't need to look further
  293. if (VerTag.empty() == false && (VerTag == Ver))
  294. break;
  295. }
  296. if (UserRequestedVerTag == "" && Version != "" && RelTag != "")
  297. ioprintf(c1out, "Selected version '%s' (%s) for %s\n",
  298. Version.c_str(), RelTag.c_str(), Src.c_str());
  299. if (Last != 0 || VerTag.empty() == true)
  300. break;
  301. _error->Error(_("Can not find version '%s' of package '%s'"), VerTag.c_str(), TmpSrc.c_str());
  302. return 0;
  303. }
  304. if (Last == 0 || Last->Jump(Offset) == false)
  305. return 0;
  306. return Last;
  307. }
  308. /*}}}*/
  309. // DoSource - Fetch a source archive /*{{{*/
  310. // ---------------------------------------------------------------------
  311. /* Fetch souce packages */
  312. struct DscFile
  313. {
  314. std::string Package;
  315. std::string Version;
  316. std::string Dsc;
  317. };
  318. bool DoSource(CommandLine &CmdL)
  319. {
  320. CacheFile Cache;
  321. if (Cache.Open(false) == false)
  322. return false;
  323. if (CmdL.FileSize() <= 1)
  324. return _error->Error(_("Must specify at least one package to fetch source for"));
  325. // Read the source list
  326. if (Cache.BuildSourceList() == false)
  327. return false;
  328. pkgSourceList *List = Cache.GetSourceList();
  329. // Create the text record parsers
  330. pkgSrcRecords SrcRecs(*List);
  331. if (_error->PendingError() == true)
  332. return false;
  333. std::unique_ptr<DscFile[]> Dsc(new DscFile[CmdL.FileSize()]);
  334. // insert all downloaded uris into this set to avoid downloading them
  335. // twice
  336. std::set<std::string> queued;
  337. // Diff only mode only fetches .diff files
  338. bool const diffOnly = _config->FindB("APT::Get::Diff-Only", false);
  339. // Tar only mode only fetches .tar files
  340. bool const tarOnly = _config->FindB("APT::Get::Tar-Only", false);
  341. // Dsc only mode only fetches .dsc files
  342. bool const dscOnly = _config->FindB("APT::Get::Dsc-Only", false);
  343. // Load the requestd sources into the fetcher
  344. aptAcquireWithTextStatus Fetcher;
  345. unsigned J = 0;
  346. std::vector<std::string> UntrustedList;
  347. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  348. {
  349. std::string Src;
  350. pkgSrcRecords::Parser *Last = FindSrc(*I,SrcRecs,Src,Cache);
  351. if (Last == 0) {
  352. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  353. }
  354. if (Last->Index().IsTrusted() == false)
  355. UntrustedList.push_back(Src);
  356. std::string srec = Last->AsStr();
  357. std::string::size_type pos = srec.find("\nVcs-");
  358. while (pos != std::string::npos)
  359. {
  360. pos += strlen("\nVcs-");
  361. std::string vcs = srec.substr(pos,srec.find(":",pos)-pos);
  362. if(vcs == "Browser")
  363. {
  364. pos = srec.find("\nVcs-", pos);
  365. continue;
  366. }
  367. pos += vcs.length()+2;
  368. std::string::size_type epos = srec.find("\n", pos);
  369. std::string const uri = srec.substr(pos,epos-pos);
  370. ioprintf(c1out, _("NOTICE: '%s' packaging is maintained in "
  371. "the '%s' version control system at:\n"
  372. "%s\n"),
  373. Src.c_str(), vcs.c_str(), uri.c_str());
  374. std::string vcscmd;
  375. if (vcs == "Bzr")
  376. vcscmd = "bzr branch " + uri;
  377. else if (vcs == "Git")
  378. vcscmd = "git clone " + uri;
  379. if (vcscmd.empty() == false)
  380. ioprintf(c1out,_("Please use:\n%s\n"
  381. "to retrieve the latest (possibly unreleased) "
  382. "updates to the package.\n"),
  383. vcscmd.c_str());
  384. break;
  385. }
  386. // Back track
  387. std::vector<pkgSrcRecords::File2> Lst;
  388. if (Last->Files2(Lst) == false) {
  389. return false;
  390. }
  391. // Load them into the fetcher
  392. for (std::vector<pkgSrcRecords::File2>::const_iterator I = Lst.begin();
  393. I != Lst.end(); ++I)
  394. {
  395. // Try to guess what sort of file it is we are getting.
  396. if (I->Type == "dsc")
  397. {
  398. Dsc[J].Package = Last->Package();
  399. Dsc[J].Version = Last->Version();
  400. Dsc[J].Dsc = flNotDir(I->Path);
  401. }
  402. // Handle the only options so that multiple can be used at once
  403. if (diffOnly == true || tarOnly == true || dscOnly == true)
  404. {
  405. if ((diffOnly == true && I->Type == "diff") ||
  406. (tarOnly == true && I->Type == "tar") ||
  407. (dscOnly == true && I->Type == "dsc"))
  408. ; // Fine, we want this file downloaded
  409. else
  410. continue;
  411. }
  412. // don't download the same uri twice (should this be moved to
  413. // the fetcher interface itself?)
  414. if(queued.find(Last->Index().ArchiveURI(I->Path)) != queued.end())
  415. continue;
  416. queued.insert(Last->Index().ArchiveURI(I->Path));
  417. // check if we have a file with that md5 sum already localy
  418. std::string localFile = flNotDir(I->Path);
  419. if (FileExists(localFile) == true)
  420. if(I->Hashes.VerifyFile(localFile) == true)
  421. {
  422. ioprintf(c1out,_("Skipping already downloaded file '%s'\n"),
  423. localFile.c_str());
  424. continue;
  425. }
  426. // see if we have a hash (Acquire::ForceHash is the only way to have none)
  427. if (I->Hashes.usable() == false && _config->FindB("APT::Get::AllowUnauthenticated",false) == false)
  428. {
  429. ioprintf(c1out, "Skipping download of file '%s' as requested hashsum is not available for authentication\n",
  430. localFile.c_str());
  431. continue;
  432. }
  433. new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
  434. I->Hashes, I->FileSize, Last->Index().SourceInfo(*Last,*I), Src);
  435. }
  436. }
  437. // Display statistics
  438. unsigned long long FetchBytes = Fetcher.FetchNeeded();
  439. unsigned long long FetchPBytes = Fetcher.PartialPresent();
  440. unsigned long long DebBytes = Fetcher.TotalNeeded();
  441. if (CheckFreeSpaceBeforeDownload(".", (FetchBytes - FetchPBytes)) == false)
  442. return false;
  443. // Number of bytes
  444. if (DebBytes != FetchBytes)
  445. //TRANSLATOR: The required space between number and unit is already included
  446. // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
  447. ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
  448. SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
  449. else
  450. //TRANSLATOR: The required space between number and unit is already included
  451. // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
  452. ioprintf(c1out,_("Need to get %sB of source archives.\n"),
  453. SizeToStr(DebBytes).c_str());
  454. if (_config->FindB("APT::Get::Simulate",false) == true)
  455. {
  456. for (unsigned I = 0; I != J; I++)
  457. ioprintf(std::cout,_("Fetch source %s\n"),Dsc[I].Package.c_str());
  458. return true;
  459. }
  460. // Just print out the uris an exit if the --print-uris flag was used
  461. if (_config->FindB("APT::Get::Print-URIs") == true)
  462. {
  463. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  464. for (; I != Fetcher.UriEnd(); ++I)
  465. std::cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  466. I->Owner->FileSize << ' ' << I->Owner->HashSum() << std::endl;
  467. return true;
  468. }
  469. // check authentication status of the source as well
  470. if (UntrustedList.empty() == false && AuthPrompt(UntrustedList, false) == false)
  471. return false;
  472. // Run it
  473. bool Failed = false;
  474. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
  475. {
  476. return _error->Error(_("Failed to fetch some archives."));
  477. }
  478. if (_config->FindB("APT::Get::Download-only",false) == true)
  479. {
  480. c1out << _("Download complete and in download only mode") << std::endl;
  481. return true;
  482. }
  483. // Unpack the sources
  484. pid_t Process = ExecFork();
  485. if (Process == 0)
  486. {
  487. bool const fixBroken = _config->FindB("APT::Get::Fix-Broken", false);
  488. for (unsigned I = 0; I != J; ++I)
  489. {
  490. std::string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
  491. // Diff only mode only fetches .diff files
  492. if (_config->FindB("APT::Get::Diff-Only",false) == true ||
  493. _config->FindB("APT::Get::Tar-Only",false) == true ||
  494. Dsc[I].Dsc.empty() == true)
  495. continue;
  496. // See if the package is already unpacked
  497. struct stat Stat;
  498. if (fixBroken == false && stat(Dir.c_str(),&Stat) == 0 &&
  499. S_ISDIR(Stat.st_mode) != 0)
  500. {
  501. ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
  502. Dir.c_str());
  503. }
  504. else
  505. {
  506. // Call dpkg-source
  507. std::string const sourceopts = _config->Find("DPkg::Source-Options", "-x");
  508. std::string S;
  509. strprintf(S, "%s %s %s",
  510. _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
  511. sourceopts.c_str(), Dsc[I].Dsc.c_str());
  512. if (system(S.c_str()) != 0)
  513. {
  514. fprintf(stderr, _("Unpack command '%s' failed.\n"), S.c_str());
  515. fprintf(stderr, _("Check if the 'dpkg-dev' package is installed.\n"));
  516. _exit(1);
  517. }
  518. }
  519. // Try to compile it with dpkg-buildpackage
  520. if (_config->FindB("APT::Get::Compile",false) == true)
  521. {
  522. std::string buildopts = _config->Find("APT::Get::Host-Architecture");
  523. if (buildopts.empty() == false)
  524. buildopts = "-a" + buildopts + " ";
  525. // get all active build profiles
  526. std::string const profiles = APT::Configuration::getBuildProfilesString();
  527. if (profiles.empty() == false)
  528. buildopts.append(" -P").append(profiles).append(" ");
  529. buildopts.append(_config->Find("DPkg::Build-Options","-b -uc"));
  530. // Call dpkg-buildpackage
  531. std::string S;
  532. strprintf(S, "cd %s && %s %s",
  533. Dir.c_str(),
  534. _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
  535. buildopts.c_str());
  536. if (system(S.c_str()) != 0)
  537. {
  538. fprintf(stderr, _("Build command '%s' failed.\n"), S.c_str());
  539. _exit(1);
  540. }
  541. }
  542. }
  543. _exit(0);
  544. }
  545. return ExecWait(Process, "dpkg-source");
  546. }
  547. /*}}}*/
  548. // DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
  549. // ---------------------------------------------------------------------
  550. /* This function will look at the build depends list of the given source
  551. package and install the necessary packages to make it true, or fail. */
  552. bool DoBuildDep(CommandLine &CmdL)
  553. {
  554. CacheFile Cache;
  555. _config->Set("APT::Install-Recommends", false);
  556. bool WantLock = _config->FindB("APT::Get::Print-URIs", false) == false;
  557. if (Cache.Open(WantLock) == false)
  558. return false;
  559. if (CmdL.FileSize() <= 1)
  560. return _error->Error(_("Must specify at least one package to check builddeps for"));
  561. // Read the source list
  562. if (Cache.BuildSourceList() == false)
  563. return false;
  564. pkgSourceList *List = Cache.GetSourceList();
  565. // Create the text record parsers
  566. pkgSrcRecords SrcRecs(*List);
  567. if (_error->PendingError() == true)
  568. return false;
  569. bool StripMultiArch;
  570. std::string hostArch = _config->Find("APT::Get::Host-Architecture");
  571. if (hostArch.empty() == false)
  572. {
  573. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  574. if (std::find(archs.begin(), archs.end(), hostArch) == archs.end())
  575. return _error->Error(_("No architecture information available for %s. See apt.conf(5) APT::Architectures for setup"), hostArch.c_str());
  576. StripMultiArch = false;
  577. }
  578. else
  579. StripMultiArch = true;
  580. unsigned J = 0;
  581. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  582. {
  583. std::string Src;
  584. pkgSrcRecords::Parser *Last = 0;
  585. std::unique_ptr<pkgSrcRecords::Parser> LastOwner;
  586. // an unpacked debian source tree
  587. using APT::String::Startswith;
  588. if ((Startswith(*I, "./") || Startswith(*I, "/")) &&
  589. DirectoryExists(*I))
  590. {
  591. ioprintf(c1out, _("Note, using directory '%s' to get the build dependencies\n"), *I);
  592. // FIXME: how can we make this more elegant?
  593. std::string TypeName = "Debian control file";
  594. pkgIndexFile::Type *Type = pkgIndexFile::Type::GetType(TypeName.c_str());
  595. if(Type != NULL)
  596. LastOwner.reset(Last = Type->CreateSrcPkgParser(*I));
  597. }
  598. // if its a local file (e.g. .dsc) use this
  599. else if (FileExists(*I))
  600. {
  601. ioprintf(c1out, _("Note, using file '%s' to get the build dependencies\n"), *I);
  602. // see if we can get a parser for this pkgIndexFile type
  603. std::string TypeName = "Debian " + flExtension(*I) + " file";
  604. pkgIndexFile::Type *Type = pkgIndexFile::Type::GetType(TypeName.c_str());
  605. if(Type != NULL)
  606. LastOwner.reset(Last = Type->CreateSrcPkgParser(*I));
  607. } else {
  608. // normal case, search the cache for the source file
  609. Last = FindSrc(*I,SrcRecs,Src,Cache);
  610. }
  611. if (Last == 0)
  612. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  613. // Process the build-dependencies
  614. std::vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
  615. // FIXME: Can't specify architecture to use for [wildcard] matching, so switch default arch temporary
  616. if (hostArch.empty() == false)
  617. {
  618. std::string nativeArch = _config->Find("APT::Architecture");
  619. _config->Set("APT::Architecture", hostArch);
  620. bool Success = Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch);
  621. _config->Set("APT::Architecture", nativeArch);
  622. if (Success == false)
  623. return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
  624. }
  625. else if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
  626. return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
  627. // Also ensure that build-essential packages are present
  628. Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
  629. if (Opts)
  630. Opts = Opts->Child;
  631. for (; Opts; Opts = Opts->Next)
  632. {
  633. if (Opts->Value.empty() == true)
  634. continue;
  635. pkgSrcRecords::Parser::BuildDepRec rec;
  636. rec.Package = Opts->Value;
  637. rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
  638. rec.Op = 0;
  639. BuildDeps.push_back(rec);
  640. }
  641. if (BuildDeps.empty() == true)
  642. {
  643. ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
  644. continue;
  645. }
  646. // Install the requested packages
  647. std::vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
  648. pkgProblemResolver Fix(Cache);
  649. bool skipAlternatives = false; // skip remaining alternatives in an or group
  650. for (D = BuildDeps.begin(); D != BuildDeps.end(); ++D)
  651. {
  652. bool hasAlternatives = (((*D).Op & pkgCache::Dep::Or) == pkgCache::Dep::Or);
  653. if (skipAlternatives == true)
  654. {
  655. /*
  656. * if there are alternatives, we've already picked one, so skip
  657. * the rest
  658. *
  659. * TODO: this means that if there's a build-dep on A|B and B is
  660. * installed, we'll still try to install A; more importantly,
  661. * if A is currently broken, we cannot go back and try B. To fix
  662. * this would require we do a Resolve cycle for each package we
  663. * add to the install list. Ugh
  664. */
  665. if (!hasAlternatives)
  666. skipAlternatives = false; // end of or group
  667. continue;
  668. }
  669. if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
  670. (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
  671. {
  672. pkgCache::GrpIterator Grp = Cache->FindGrp((*D).Package);
  673. // Build-conflicts on unknown packages are silently ignored
  674. if (Grp.end() == true)
  675. continue;
  676. for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
  677. {
  678. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  679. /*
  680. * Remove if we have an installed version that satisfies the
  681. * version criteria
  682. */
  683. if (IV.end() == false &&
  684. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  685. TryToInstallBuildDep(Pkg,Cache,Fix,true,false);
  686. }
  687. }
  688. else // BuildDep || BuildDepIndep
  689. {
  690. if (_config->FindB("Debug::BuildDeps",false) == true)
  691. std::cout << "Looking for " << (*D).Package << "...\n";
  692. pkgCache::PkgIterator Pkg;
  693. // Cross-Building?
  694. if (StripMultiArch == false && D->Type != pkgSrcRecords::Parser::BuildDependIndep)
  695. {
  696. size_t const colon = D->Package.find(":");
  697. if (colon != std::string::npos)
  698. {
  699. if (strcmp(D->Package.c_str() + colon, ":any") == 0 || strcmp(D->Package.c_str() + colon, ":native") == 0)
  700. Pkg = Cache->FindPkg(D->Package.substr(0,colon));
  701. else
  702. Pkg = Cache->FindPkg(D->Package);
  703. }
  704. else
  705. Pkg = Cache->FindPkg(D->Package, hostArch);
  706. // a bad version either is invalid or doesn't satify dependency
  707. #define BADVER(Ver) (Ver.end() == true || \
  708. (D->Version.empty() == false && \
  709. Cache->VS().CheckDep(Ver.VerStr(),D->Op,D->Version.c_str()) == false))
  710. APT::VersionList verlist;
  711. if (Pkg.end() == false)
  712. {
  713. pkgCache::VerIterator Ver = (*Cache)[Pkg].InstVerIter(*Cache);
  714. if (BADVER(Ver) == false)
  715. verlist.insert(Ver);
  716. Ver = (*Cache)[Pkg].CandidateVerIter(*Cache);
  717. if (BADVER(Ver) == false)
  718. verlist.insert(Ver);
  719. }
  720. if (verlist.empty() == true)
  721. {
  722. pkgCache::PkgIterator BuildPkg = Cache->FindPkg(D->Package, "native");
  723. if (BuildPkg.end() == false && Pkg != BuildPkg)
  724. {
  725. pkgCache::VerIterator Ver = (*Cache)[BuildPkg].InstVerIter(*Cache);
  726. if (BADVER(Ver) == false)
  727. verlist.insert(Ver);
  728. Ver = (*Cache)[BuildPkg].CandidateVerIter(*Cache);
  729. if (BADVER(Ver) == false)
  730. verlist.insert(Ver);
  731. }
  732. }
  733. #undef BADVER
  734. std::string forbidden;
  735. // We need to decide if host or build arch, so find a version we can look at
  736. APT::VersionList::const_iterator Ver = verlist.begin();
  737. for (; Ver != verlist.end(); ++Ver)
  738. {
  739. forbidden.clear();
  740. if (Ver->MultiArch == pkgCache::Version::No || Ver->MultiArch == pkgCache::Version::All)
  741. {
  742. if (colon == std::string::npos)
  743. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  744. else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
  745. forbidden = "Multi-Arch: no";
  746. else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
  747. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  748. }
  749. else if (Ver->MultiArch == pkgCache::Version::Same)
  750. {
  751. if (colon == std::string::npos)
  752. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  753. else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
  754. forbidden = "Multi-Arch: same";
  755. else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
  756. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  757. }
  758. else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
  759. {
  760. if (colon == std::string::npos)
  761. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  762. else if (strcmp(D->Package.c_str() + colon, ":any") == 0 ||
  763. strcmp(D->Package.c_str() + colon, ":native") == 0)
  764. forbidden = "Multi-Arch: foreign";
  765. }
  766. else if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
  767. {
  768. if (colon == std::string::npos)
  769. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  770. else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
  771. {
  772. // prefer any installed over preferred non-installed architectures
  773. pkgCache::GrpIterator Grp = Ver.ParentPkg().Group();
  774. // we don't check for version here as we are better of with upgrading than remove and install
  775. for (Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
  776. if (Pkg.CurrentVer().end() == false)
  777. break;
  778. if (Pkg.end() == true)
  779. Pkg = Grp.FindPreferredPkg(true);
  780. }
  781. else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
  782. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  783. }
  784. if (forbidden.empty() == false)
  785. {
  786. if (_config->FindB("Debug::BuildDeps",false) == true)
  787. std::cout << D->Package.substr(colon, std::string::npos) << " is not allowed from " << forbidden << " package " << (*D).Package << " (" << Ver.VerStr() << ")" << std::endl;
  788. continue;
  789. }
  790. //we found a good version
  791. break;
  792. }
  793. if (Ver == verlist.end())
  794. {
  795. if (_config->FindB("Debug::BuildDeps",false) == true)
  796. std::cout << " No multiarch info as we have no satisfying installed nor candidate for " << D->Package << " on build or host arch" << std::endl;
  797. if (forbidden.empty() == false)
  798. {
  799. if (hasAlternatives)
  800. continue;
  801. return _error->Error(_("%s dependency for %s can't be satisfied "
  802. "because %s is not allowed on '%s' packages"),
  803. Last->BuildDepType(D->Type), Src.c_str(),
  804. D->Package.c_str(), forbidden.c_str());
  805. }
  806. }
  807. }
  808. else
  809. Pkg = Cache->FindPkg(D->Package);
  810. if (Pkg.end() == true || (Pkg->VersionList == 0 && Pkg->ProvidesList == 0))
  811. {
  812. if (_config->FindB("Debug::BuildDeps",false) == true)
  813. std::cout << " (not found)" << (*D).Package << std::endl;
  814. if (hasAlternatives)
  815. continue;
  816. return _error->Error(_("%s dependency for %s cannot be satisfied "
  817. "because the package %s cannot be found"),
  818. Last->BuildDepType((*D).Type),Src.c_str(),
  819. (*D).Package.c_str());
  820. }
  821. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  822. if (IV.end() == false)
  823. {
  824. if (_config->FindB("Debug::BuildDeps",false) == true)
  825. std::cout << " Is installed\n";
  826. if (D->Version.empty() == true ||
  827. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  828. {
  829. skipAlternatives = hasAlternatives;
  830. continue;
  831. }
  832. if (_config->FindB("Debug::BuildDeps",false) == true)
  833. std::cout << " ...but the installed version doesn't meet the version requirement\n";
  834. if (((*D).Op & pkgCache::Dep::LessEq) == pkgCache::Dep::LessEq)
  835. return _error->Error(_("Failed to satisfy %s dependency for %s: Installed package %s is too new"),
  836. Last->BuildDepType((*D).Type), Src.c_str(), Pkg.FullName(true).c_str());
  837. }
  838. // Only consider virtual packages if there is no versioned dependency
  839. if ((*D).Version.empty() == true)
  840. {
  841. /*
  842. * If this is a virtual package, we need to check the list of
  843. * packages that provide it and see if any of those are
  844. * installed
  845. */
  846. pkgCache::PrvIterator Prv = Pkg.ProvidesList();
  847. for (; Prv.end() != true; ++Prv)
  848. {
  849. if (_config->FindB("Debug::BuildDeps",false) == true)
  850. std::cout << " Checking provider " << Prv.OwnerPkg().FullName() << std::endl;
  851. if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
  852. break;
  853. }
  854. if (Prv.end() == false)
  855. {
  856. if (_config->FindB("Debug::BuildDeps",false) == true)
  857. std::cout << " Is provided by installed package " << Prv.OwnerPkg().FullName() << std::endl;
  858. skipAlternatives = hasAlternatives;
  859. continue;
  860. }
  861. }
  862. else // versioned dependency
  863. {
  864. pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
  865. if (CV.end() == true ||
  866. Cache->VS().CheckDep(CV.VerStr(),(*D).Op,(*D).Version.c_str()) == false)
  867. {
  868. if (hasAlternatives)
  869. continue;
  870. else if (CV.end() == false)
  871. return _error->Error(_("%s dependency for %s cannot be satisfied "
  872. "because candidate version of package %s "
  873. "can't satisfy version requirements"),
  874. Last->BuildDepType(D->Type), Src.c_str(),
  875. D->Package.c_str());
  876. else
  877. return _error->Error(_("%s dependency for %s cannot be satisfied "
  878. "because package %s has no candidate version"),
  879. Last->BuildDepType(D->Type), Src.c_str(),
  880. D->Package.c_str());
  881. }
  882. }
  883. if (TryToInstallBuildDep(Pkg,Cache,Fix,false,false,false) == true)
  884. {
  885. // We successfully installed something; skip remaining alternatives
  886. skipAlternatives = hasAlternatives;
  887. if(_config->FindB("APT::Get::Build-Dep-Automatic", false) == true)
  888. Cache->MarkAuto(Pkg, true);
  889. continue;
  890. }
  891. else if (hasAlternatives)
  892. {
  893. if (_config->FindB("Debug::BuildDeps",false) == true)
  894. std::cout << " Unsatisfiable, trying alternatives\n";
  895. continue;
  896. }
  897. else
  898. {
  899. return _error->Error(_("Failed to satisfy %s dependency for %s: %s"),
  900. Last->BuildDepType((*D).Type),
  901. Src.c_str(),
  902. (*D).Package.c_str());
  903. }
  904. }
  905. }
  906. if (Fix.Resolve(true) == false)
  907. _error->Discard();
  908. // Now we check the state of the packages,
  909. if (Cache->BrokenCount() != 0)
  910. {
  911. ShowBroken(std::cout, Cache, false);
  912. return _error->Error(_("Build-dependencies for %s could not be satisfied."),*I);
  913. }
  914. }
  915. if (InstallPackages(Cache, false, true) == false)
  916. return _error->Error(_("Failed to process build dependencies"));
  917. return true;
  918. }
  919. /*}}}*/