private-source.cc 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. bool CorrectRelTag = false;
  267. // See if we need to look for a specific release tag
  268. if (RelTag != "" && UserRequestedVerTag == "")
  269. {
  270. pkgCache::RlsFileIterator const Rls = GetReleaseFileForSourceRecord(CacheFile, SrcList, Parse);
  271. if (Rls.end() == false)
  272. {
  273. if ((Rls->Archive != 0 && RelTag == Rls.Archive()) ||
  274. (Rls->Codename != 0 && RelTag == Rls.Codename()))
  275. CorrectRelTag = true;
  276. }
  277. } else
  278. CorrectRelTag = true;
  279. // Ignore all versions which doesn't fit
  280. if (VerTag.empty() == false &&
  281. Cache->VS().CmpVersion(VerTag, Ver) != 0) // exact match
  282. continue;
  283. // Newer version or an exact match? Save the hit
  284. if (CorrectRelTag && (Last == 0 || Cache->VS().CmpVersion(Version,Ver) < 0)) {
  285. Last = Parse;
  286. Offset = Parse->Offset();
  287. Version = Ver;
  288. }
  289. // was the version check above an exact match?
  290. // If so, we don't need to look further
  291. if (VerTag.empty() == false && (VerTag == Ver))
  292. break;
  293. }
  294. if (UserRequestedVerTag == "" && Version != "" && RelTag != "")
  295. ioprintf(c1out, "Selected version '%s' (%s) for %s\n",
  296. Version.c_str(), RelTag.c_str(), Src.c_str());
  297. if (Last != 0 || VerTag.empty() == true)
  298. break;
  299. _error->Error(_("Can not find version '%s' of package '%s'"), VerTag.c_str(), TmpSrc.c_str());
  300. return 0;
  301. }
  302. if (Last == 0 || Last->Jump(Offset) == false)
  303. return 0;
  304. return Last;
  305. }
  306. /*}}}*/
  307. // DoSource - Fetch a source archive /*{{{*/
  308. // ---------------------------------------------------------------------
  309. /* Fetch souce packages */
  310. struct DscFile
  311. {
  312. std::string Package;
  313. std::string Version;
  314. std::string Dsc;
  315. };
  316. bool DoSource(CommandLine &CmdL)
  317. {
  318. CacheFile Cache;
  319. if (Cache.Open(false) == false)
  320. return false;
  321. if (CmdL.FileSize() <= 1)
  322. return _error->Error(_("Must specify at least one package to fetch source for"));
  323. // Read the source list
  324. if (Cache.BuildSourceList() == false)
  325. return false;
  326. pkgSourceList *List = Cache.GetSourceList();
  327. // Create the text record parsers
  328. pkgSrcRecords SrcRecs(*List);
  329. if (_error->PendingError() == true)
  330. return false;
  331. std::unique_ptr<DscFile[]> Dsc(new DscFile[CmdL.FileSize()]);
  332. // insert all downloaded uris into this set to avoid downloading them
  333. // twice
  334. std::set<std::string> queued;
  335. // Diff only mode only fetches .diff files
  336. bool const diffOnly = _config->FindB("APT::Get::Diff-Only", false);
  337. // Tar only mode only fetches .tar files
  338. bool const tarOnly = _config->FindB("APT::Get::Tar-Only", false);
  339. // Dsc only mode only fetches .dsc files
  340. bool const dscOnly = _config->FindB("APT::Get::Dsc-Only", false);
  341. // Load the requestd sources into the fetcher
  342. aptAcquireWithTextStatus Fetcher;
  343. unsigned J = 0;
  344. std::vector<std::string> UntrustedList;
  345. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  346. {
  347. std::string Src;
  348. pkgSrcRecords::Parser *Last = FindSrc(*I,SrcRecs,Src,Cache);
  349. if (Last == 0) {
  350. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  351. }
  352. if (Last->Index().IsTrusted() == false)
  353. UntrustedList.push_back(Src);
  354. std::string srec = Last->AsStr();
  355. std::string::size_type pos = srec.find("\nVcs-");
  356. while (pos != std::string::npos)
  357. {
  358. pos += strlen("\nVcs-");
  359. std::string vcs = srec.substr(pos,srec.find(":",pos)-pos);
  360. if(vcs == "Browser")
  361. {
  362. pos = srec.find("\nVcs-", pos);
  363. continue;
  364. }
  365. pos += vcs.length()+2;
  366. std::string::size_type epos = srec.find("\n", pos);
  367. std::string const uri = srec.substr(pos,epos-pos);
  368. ioprintf(c1out, _("NOTICE: '%s' packaging is maintained in "
  369. "the '%s' version control system at:\n"
  370. "%s\n"),
  371. Src.c_str(), vcs.c_str(), uri.c_str());
  372. std::string vcscmd;
  373. if (vcs == "Bzr")
  374. vcscmd = "bzr branch " + uri;
  375. else if (vcs == "Git")
  376. vcscmd = "git clone " + uri;
  377. if (vcscmd.empty() == false)
  378. ioprintf(c1out,_("Please use:\n%s\n"
  379. "to retrieve the latest (possibly unreleased) "
  380. "updates to the package.\n"),
  381. vcscmd.c_str());
  382. break;
  383. }
  384. // Back track
  385. std::vector<pkgSrcRecords::File2> Lst;
  386. if (Last->Files2(Lst) == false) {
  387. return false;
  388. }
  389. // Load them into the fetcher
  390. for (std::vector<pkgSrcRecords::File2>::const_iterator I = Lst.begin();
  391. I != Lst.end(); ++I)
  392. {
  393. // Try to guess what sort of file it is we are getting.
  394. if (I->Type == "dsc")
  395. {
  396. Dsc[J].Package = Last->Package();
  397. Dsc[J].Version = Last->Version();
  398. Dsc[J].Dsc = flNotDir(I->Path);
  399. }
  400. // Handle the only options so that multiple can be used at once
  401. if (diffOnly == true || tarOnly == true || dscOnly == true)
  402. {
  403. if ((diffOnly == true && I->Type == "diff") ||
  404. (tarOnly == true && I->Type == "tar") ||
  405. (dscOnly == true && I->Type == "dsc"))
  406. ; // Fine, we want this file downloaded
  407. else
  408. continue;
  409. }
  410. // don't download the same uri twice (should this be moved to
  411. // the fetcher interface itself?)
  412. if(queued.find(Last->Index().ArchiveURI(I->Path)) != queued.end())
  413. continue;
  414. queued.insert(Last->Index().ArchiveURI(I->Path));
  415. // check if we have a file with that md5 sum already localy
  416. std::string localFile = flNotDir(I->Path);
  417. if (FileExists(localFile) == true)
  418. if(I->Hashes.VerifyFile(localFile) == true)
  419. {
  420. ioprintf(c1out,_("Skipping already downloaded file '%s'\n"),
  421. localFile.c_str());
  422. continue;
  423. }
  424. // see if we have a hash (Acquire::ForceHash is the only way to have none)
  425. if (I->Hashes.usable() == false && _config->FindB("APT::Get::AllowUnauthenticated",false) == false)
  426. {
  427. ioprintf(c1out, "Skipping download of file '%s' as requested hashsum is not available for authentication\n",
  428. localFile.c_str());
  429. continue;
  430. }
  431. new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
  432. I->Hashes, I->FileSize, Last->Index().SourceInfo(*Last,*I), Src);
  433. }
  434. }
  435. // Display statistics
  436. unsigned long long FetchBytes = Fetcher.FetchNeeded();
  437. unsigned long long FetchPBytes = Fetcher.PartialPresent();
  438. unsigned long long DebBytes = Fetcher.TotalNeeded();
  439. if (CheckFreeSpaceBeforeDownload(".", (FetchBytes - FetchPBytes)) == false)
  440. return false;
  441. // Number of bytes
  442. if (DebBytes != FetchBytes)
  443. //TRANSLATOR: The required space between number and unit is already included
  444. // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
  445. ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
  446. SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
  447. else
  448. //TRANSLATOR: The required space between number and unit is already included
  449. // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
  450. ioprintf(c1out,_("Need to get %sB of source archives.\n"),
  451. SizeToStr(DebBytes).c_str());
  452. if (_config->FindB("APT::Get::Simulate",false) == true)
  453. {
  454. for (unsigned I = 0; I != J; I++)
  455. ioprintf(std::cout,_("Fetch source %s\n"),Dsc[I].Package.c_str());
  456. return true;
  457. }
  458. // Just print out the uris an exit if the --print-uris flag was used
  459. if (_config->FindB("APT::Get::Print-URIs") == true)
  460. {
  461. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  462. for (; I != Fetcher.UriEnd(); ++I)
  463. std::cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  464. I->Owner->FileSize << ' ' << I->Owner->HashSum() << std::endl;
  465. return true;
  466. }
  467. // check authentication status of the source as well
  468. if (UntrustedList.empty() == false && AuthPrompt(UntrustedList, false) == false)
  469. return false;
  470. // Run it
  471. bool Failed = false;
  472. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
  473. {
  474. return _error->Error(_("Failed to fetch some archives."));
  475. }
  476. if (_config->FindB("APT::Get::Download-only",false) == true)
  477. {
  478. c1out << _("Download complete and in download only mode") << std::endl;
  479. return true;
  480. }
  481. // Unpack the sources
  482. pid_t Process = ExecFork();
  483. if (Process == 0)
  484. {
  485. bool const fixBroken = _config->FindB("APT::Get::Fix-Broken", false);
  486. for (unsigned I = 0; I != J; ++I)
  487. {
  488. std::string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
  489. // Diff only mode only fetches .diff files
  490. if (_config->FindB("APT::Get::Diff-Only",false) == true ||
  491. _config->FindB("APT::Get::Tar-Only",false) == true ||
  492. Dsc[I].Dsc.empty() == true)
  493. continue;
  494. // See if the package is already unpacked
  495. struct stat Stat;
  496. if (fixBroken == false && stat(Dir.c_str(),&Stat) == 0 &&
  497. S_ISDIR(Stat.st_mode) != 0)
  498. {
  499. ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
  500. Dir.c_str());
  501. }
  502. else
  503. {
  504. // Call dpkg-source
  505. std::string const sourceopts = _config->Find("DPkg::Source-Options", "-x");
  506. std::string S;
  507. strprintf(S, "%s %s %s",
  508. _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
  509. sourceopts.c_str(), Dsc[I].Dsc.c_str());
  510. if (system(S.c_str()) != 0)
  511. {
  512. fprintf(stderr, _("Unpack command '%s' failed.\n"), S.c_str());
  513. fprintf(stderr, _("Check if the 'dpkg-dev' package is installed.\n"));
  514. _exit(1);
  515. }
  516. }
  517. // Try to compile it with dpkg-buildpackage
  518. if (_config->FindB("APT::Get::Compile",false) == true)
  519. {
  520. std::string buildopts = _config->Find("APT::Get::Host-Architecture");
  521. if (buildopts.empty() == false)
  522. buildopts = "-a" + buildopts + " ";
  523. // get all active build profiles
  524. std::string const profiles = APT::Configuration::getBuildProfilesString();
  525. if (profiles.empty() == false)
  526. buildopts.append(" -P").append(profiles).append(" ");
  527. buildopts.append(_config->Find("DPkg::Build-Options","-b -uc"));
  528. // Call dpkg-buildpackage
  529. std::string S;
  530. strprintf(S, "cd %s && %s %s",
  531. Dir.c_str(),
  532. _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
  533. buildopts.c_str());
  534. if (system(S.c_str()) != 0)
  535. {
  536. fprintf(stderr, _("Build command '%s' failed.\n"), S.c_str());
  537. _exit(1);
  538. }
  539. }
  540. }
  541. _exit(0);
  542. }
  543. return ExecWait(Process, "dpkg-source");
  544. }
  545. /*}}}*/
  546. // InstallBuildDepsLoop /*{{{*/
  547. static bool InstallBuildDepsLoop(CacheFile &Cache, std::string const &Src,
  548. std::vector<pkgSrcRecords::Parser::BuildDepRec> const &BuildDeps,
  549. bool const StripMultiArch, std::string const &hostArch)
  550. {
  551. // Install the requested packages
  552. std::vector <pkgSrcRecords::Parser::BuildDepRec>::const_iterator D;
  553. pkgProblemResolver Fix(Cache);
  554. bool skipAlternatives = false; // skip remaining alternatives in an or group
  555. for (D = BuildDeps.begin(); D != BuildDeps.end(); ++D)
  556. {
  557. bool hasAlternatives = (((*D).Op & pkgCache::Dep::Or) == pkgCache::Dep::Or);
  558. if (skipAlternatives == true)
  559. {
  560. /*
  561. * if there are alternatives, we've already picked one, so skip
  562. * the rest
  563. *
  564. * TODO: this means that if there's a build-dep on A|B and B is
  565. * installed, we'll still try to install A; more importantly,
  566. * if A is currently broken, we cannot go back and try B. To fix
  567. * this would require we do a Resolve cycle for each package we
  568. * add to the install list. Ugh
  569. */
  570. if (!hasAlternatives)
  571. skipAlternatives = false; // end of or group
  572. continue;
  573. }
  574. if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
  575. (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
  576. {
  577. pkgCache::GrpIterator Grp = Cache->FindGrp((*D).Package);
  578. // Build-conflicts on unknown packages are silently ignored
  579. if (Grp.end() == true)
  580. continue;
  581. for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
  582. {
  583. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  584. /*
  585. * Remove if we have an installed version that satisfies the
  586. * version criteria
  587. */
  588. if (IV.end() == false &&
  589. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  590. TryToInstallBuildDep(Pkg,Cache,Fix,true,false);
  591. }
  592. }
  593. else // BuildDep || BuildDepIndep
  594. {
  595. if (_config->FindB("Debug::BuildDeps",false) == true)
  596. std::cout << "Looking for " << (*D).Package << "...\n";
  597. pkgCache::PkgIterator Pkg;
  598. // Cross-Building?
  599. if (StripMultiArch == false && D->Type != pkgSrcRecords::Parser::BuildDependIndep)
  600. {
  601. size_t const colon = D->Package.find(":");
  602. if (colon != std::string::npos)
  603. {
  604. if (strcmp(D->Package.c_str() + colon, ":any") == 0 || strcmp(D->Package.c_str() + colon, ":native") == 0)
  605. Pkg = Cache->FindPkg(D->Package.substr(0,colon));
  606. else
  607. Pkg = Cache->FindPkg(D->Package);
  608. }
  609. else
  610. Pkg = Cache->FindPkg(D->Package, hostArch);
  611. // a bad version either is invalid or doesn't satify dependency
  612. #define BADVER(Ver) (Ver.end() == true || \
  613. (D->Version.empty() == false && \
  614. Cache->VS().CheckDep(Ver.VerStr(),D->Op,D->Version.c_str()) == false))
  615. APT::VersionList verlist;
  616. if (Pkg.end() == false)
  617. {
  618. pkgCache::VerIterator Ver = (*Cache)[Pkg].InstVerIter(*Cache);
  619. if (BADVER(Ver) == false)
  620. verlist.insert(Ver);
  621. Ver = (*Cache)[Pkg].CandidateVerIter(*Cache);
  622. if (BADVER(Ver) == false)
  623. verlist.insert(Ver);
  624. }
  625. if (verlist.empty() == true)
  626. {
  627. pkgCache::PkgIterator BuildPkg = Cache->FindPkg(D->Package, "native");
  628. if (BuildPkg.end() == false && Pkg != BuildPkg)
  629. {
  630. pkgCache::VerIterator Ver = (*Cache)[BuildPkg].InstVerIter(*Cache);
  631. if (BADVER(Ver) == false)
  632. verlist.insert(Ver);
  633. Ver = (*Cache)[BuildPkg].CandidateVerIter(*Cache);
  634. if (BADVER(Ver) == false)
  635. verlist.insert(Ver);
  636. }
  637. }
  638. #undef BADVER
  639. std::string forbidden;
  640. // We need to decide if host or build arch, so find a version we can look at
  641. APT::VersionList::const_iterator Ver = verlist.begin();
  642. for (; Ver != verlist.end(); ++Ver)
  643. {
  644. forbidden.clear();
  645. if (Ver->MultiArch == pkgCache::Version::No || Ver->MultiArch == pkgCache::Version::All)
  646. {
  647. if (colon == std::string::npos)
  648. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  649. else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
  650. forbidden = "Multi-Arch: no";
  651. else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
  652. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  653. }
  654. else if (Ver->MultiArch == pkgCache::Version::Same)
  655. {
  656. if (colon == std::string::npos)
  657. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  658. else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
  659. forbidden = "Multi-Arch: same";
  660. else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
  661. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  662. }
  663. else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
  664. {
  665. if (colon == std::string::npos)
  666. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  667. else if (strcmp(D->Package.c_str() + colon, ":any") == 0 ||
  668. strcmp(D->Package.c_str() + colon, ":native") == 0)
  669. forbidden = "Multi-Arch: foreign";
  670. }
  671. else if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
  672. {
  673. if (colon == std::string::npos)
  674. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  675. else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
  676. {
  677. // prefer any installed over preferred non-installed architectures
  678. pkgCache::GrpIterator Grp = Ver.ParentPkg().Group();
  679. // we don't check for version here as we are better of with upgrading than remove and install
  680. for (Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
  681. if (Pkg.CurrentVer().end() == false)
  682. break;
  683. if (Pkg.end() == true)
  684. Pkg = Grp.FindPreferredPkg(true);
  685. }
  686. else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
  687. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  688. }
  689. if (forbidden.empty() == false)
  690. {
  691. if (_config->FindB("Debug::BuildDeps",false) == true)
  692. std::cout << D->Package.substr(colon, std::string::npos) << " is not allowed from " << forbidden << " package " << (*D).Package << " (" << Ver.VerStr() << ")" << std::endl;
  693. continue;
  694. }
  695. //we found a good version
  696. break;
  697. }
  698. if (Ver == verlist.end())
  699. {
  700. if (_config->FindB("Debug::BuildDeps",false) == true)
  701. std::cout << " No multiarch info as we have no satisfying installed nor candidate for " << D->Package << " on build or host arch" << std::endl;
  702. if (forbidden.empty() == false)
  703. {
  704. if (hasAlternatives)
  705. continue;
  706. return _error->Error(_("%s dependency for %s can't be satisfied "
  707. "because %s is not allowed on '%s' packages"),
  708. pkgSrcRecords::Parser::BuildDepType(D->Type), Src.c_str(),
  709. D->Package.c_str(), forbidden.c_str());
  710. }
  711. }
  712. }
  713. else
  714. Pkg = Cache->FindPkg(D->Package);
  715. if (Pkg.end() == true || (Pkg->VersionList == 0 && Pkg->ProvidesList == 0))
  716. {
  717. if (_config->FindB("Debug::BuildDeps",false) == true)
  718. std::cout << " (not found)" << (*D).Package << std::endl;
  719. if (hasAlternatives)
  720. continue;
  721. return _error->Error(_("%s dependency for %s cannot be satisfied "
  722. "because the package %s cannot be found"),
  723. pkgSrcRecords::Parser::BuildDepType(D->Type), Src.c_str(),
  724. (*D).Package.c_str());
  725. }
  726. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  727. if (IV.end() == false)
  728. {
  729. if (_config->FindB("Debug::BuildDeps",false) == true)
  730. std::cout << " Is installed\n";
  731. if (D->Version.empty() == true ||
  732. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  733. {
  734. skipAlternatives = hasAlternatives;
  735. continue;
  736. }
  737. if (_config->FindB("Debug::BuildDeps",false) == true)
  738. std::cout << " ...but the installed version doesn't meet the version requirement\n";
  739. if (((*D).Op & pkgCache::Dep::LessEq) == pkgCache::Dep::LessEq)
  740. return _error->Error(_("Failed to satisfy %s dependency for %s: Installed package %s is too new"),
  741. pkgSrcRecords::Parser::BuildDepType(D->Type), Src.c_str(), Pkg.FullName(true).c_str());
  742. }
  743. // Only consider virtual packages if there is no versioned dependency
  744. if ((*D).Version.empty() == true)
  745. {
  746. /*
  747. * If this is a virtual package, we need to check the list of
  748. * packages that provide it and see if any of those are
  749. * installed
  750. */
  751. pkgCache::PrvIterator Prv = Pkg.ProvidesList();
  752. for (; Prv.end() != true; ++Prv)
  753. {
  754. if (_config->FindB("Debug::BuildDeps",false) == true)
  755. std::cout << " Checking provider " << Prv.OwnerPkg().FullName() << std::endl;
  756. if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
  757. break;
  758. }
  759. if (Prv.end() == false)
  760. {
  761. if (_config->FindB("Debug::BuildDeps",false) == true)
  762. std::cout << " Is provided by installed package " << Prv.OwnerPkg().FullName() << std::endl;
  763. skipAlternatives = hasAlternatives;
  764. continue;
  765. }
  766. }
  767. else // versioned dependency
  768. {
  769. pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
  770. if (CV.end() == true ||
  771. Cache->VS().CheckDep(CV.VerStr(),(*D).Op,(*D).Version.c_str()) == false)
  772. {
  773. if (hasAlternatives)
  774. continue;
  775. else if (CV.end() == false)
  776. return _error->Error(_("%s dependency for %s cannot be satisfied "
  777. "because candidate version of package %s "
  778. "can't satisfy version requirements"),
  779. pkgSrcRecords::Parser::BuildDepType(D->Type), Src.c_str(),
  780. D->Package.c_str());
  781. else
  782. return _error->Error(_("%s dependency for %s cannot be satisfied "
  783. "because package %s has no candidate version"),
  784. pkgSrcRecords::Parser::BuildDepType(D->Type), Src.c_str(),
  785. D->Package.c_str());
  786. }
  787. }
  788. if (TryToInstallBuildDep(Pkg,Cache,Fix,false,false,false) == true)
  789. {
  790. // We successfully installed something; skip remaining alternatives
  791. skipAlternatives = hasAlternatives;
  792. if(_config->FindB("APT::Get::Build-Dep-Automatic", false) == true)
  793. Cache->MarkAuto(Pkg, true);
  794. continue;
  795. }
  796. else if (hasAlternatives)
  797. {
  798. if (_config->FindB("Debug::BuildDeps",false) == true)
  799. std::cout << " Unsatisfiable, trying alternatives\n";
  800. continue;
  801. }
  802. else
  803. {
  804. return _error->Error(_("Failed to satisfy %s dependency for %s: %s"),
  805. pkgSrcRecords::Parser::BuildDepType(D->Type),
  806. Src.c_str(),
  807. (*D).Package.c_str());
  808. }
  809. }
  810. }
  811. if (Fix.Resolve(true) == false)
  812. _error->Discard();
  813. // Now we check the state of the packages,
  814. if (Cache->BrokenCount() != 0)
  815. {
  816. ShowBroken(std::cout, Cache, false);
  817. return _error->Error(_("Build-dependencies for %s could not be satisfied."), Src.c_str());
  818. }
  819. return true;
  820. }
  821. /*}}}*/
  822. // DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
  823. // ---------------------------------------------------------------------
  824. /* This function will look at the build depends list of the given source
  825. package and install the necessary packages to make it true, or fail. */
  826. static std::vector<pkgSrcRecords::Parser::BuildDepRec> GetBuildDeps(pkgSrcRecords::Parser * const Last,
  827. char const * const Src, bool const StripMultiArch, std::string const &hostArch)
  828. {
  829. std::vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
  830. // FIXME: Can't specify architecture to use for [wildcard] matching, so switch default arch temporary
  831. if (hostArch.empty() == false)
  832. {
  833. std::string nativeArch = _config->Find("APT::Architecture");
  834. _config->Set("APT::Architecture", hostArch);
  835. bool Success = Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch);
  836. _config->Set("APT::Architecture", nativeArch);
  837. if (Success == false)
  838. {
  839. _error->Error(_("Unable to get build-dependency information for %s"), Src);
  840. return {};
  841. }
  842. }
  843. else if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
  844. {
  845. _error->Error(_("Unable to get build-dependency information for %s"), Src);
  846. return {};
  847. }
  848. if (BuildDeps.empty() == true)
  849. ioprintf(c1out,_("%s has no build depends.\n"), Src);
  850. return BuildDeps;
  851. }
  852. bool DoBuildDep(CommandLine &CmdL)
  853. {
  854. CacheFile Cache;
  855. std::vector<char const *> VolatileCmdL;
  856. Cache.GetSourceList()->AddVolatileFiles(CmdL, &VolatileCmdL);
  857. _config->Set("APT::Install-Recommends", false);
  858. bool WantLock = _config->FindB("APT::Get::Print-URIs", false) == false;
  859. if (Cache.Open(WantLock) == false)
  860. return false;
  861. if (CmdL.FileSize() <= 1 && VolatileCmdL.empty())
  862. return _error->Error(_("Must specify at least one package to check builddeps for"));
  863. // Read the source list
  864. if (Cache.BuildSourceList() == false)
  865. return false;
  866. pkgSourceList *List = Cache.GetSourceList();
  867. // Create the text record parsers
  868. pkgSrcRecords SrcRecs(*List);
  869. if (_error->PendingError() == true)
  870. return false;
  871. bool StripMultiArch;
  872. std::string hostArch = _config->Find("APT::Get::Host-Architecture");
  873. if (hostArch.empty() == false)
  874. {
  875. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  876. if (std::find(archs.begin(), archs.end(), hostArch) == archs.end())
  877. return _error->Error(_("No architecture information available for %s. See apt.conf(5) APT::Architectures for setup"), hostArch.c_str());
  878. StripMultiArch = false;
  879. }
  880. else
  881. StripMultiArch = true;
  882. // deal with the build essentials first
  883. {
  884. std::vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
  885. Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
  886. if (Opts)
  887. Opts = Opts->Child;
  888. for (; Opts; Opts = Opts->Next)
  889. {
  890. if (Opts->Value.empty() == true)
  891. continue;
  892. pkgSrcRecords::Parser::BuildDepRec rec;
  893. rec.Package = Opts->Value;
  894. rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
  895. rec.Op = 0;
  896. BuildDeps.push_back(rec);
  897. }
  898. if (InstallBuildDepsLoop(Cache, "APT::Build-Essential", BuildDeps, StripMultiArch, hostArch) == false)
  899. return false;
  900. }
  901. // FIXME: Avoid volatile sources == cmdline assumption
  902. {
  903. auto const VolatileSources = List->GetVolatileFiles();
  904. if (VolatileSources.size() == VolatileCmdL.size())
  905. {
  906. for (size_t i = 0; i < VolatileSources.size(); ++i)
  907. {
  908. char const * const Src = VolatileCmdL[i];
  909. if (DirectoryExists(Src))
  910. ioprintf(c1out, _("Note, using directory '%s' to get the build dependencies\n"), Src);
  911. else
  912. ioprintf(c1out, _("Note, using file '%s' to get the build dependencies\n"), Src);
  913. std::unique_ptr<pkgSrcRecords::Parser> Last(VolatileSources[i]->CreateSrcParser());
  914. if (Last == nullptr)
  915. return _error->Error(_("Unable to find a source package for %s"), Src);
  916. auto const BuildDeps = GetBuildDeps(Last.get(), Src, StripMultiArch, hostArch);
  917. if (InstallBuildDepsLoop(Cache, Src, BuildDeps, StripMultiArch, hostArch) == false)
  918. return false;
  919. }
  920. }
  921. else
  922. _error->Error("Implementation error: Volatile sources (%lu) and commandline elements (%lu) do not match!", VolatileSources.size(), VolatileCmdL.size());
  923. }
  924. for (const char **I = CmdL.FileList + 1; *I != 0; ++I)
  925. {
  926. std::string Src;
  927. pkgSrcRecords::Parser * const Last = FindSrc(*I,SrcRecs,Src,Cache);
  928. if (Last == nullptr)
  929. return _error->Error(_("Unable to find a source package for %s"), *I);
  930. auto const BuildDeps = GetBuildDeps(Last, Src.c_str(), StripMultiArch, hostArch);
  931. if (InstallBuildDepsLoop(Cache, Src, BuildDeps, StripMultiArch, hostArch) == false)
  932. return false;
  933. }
  934. if (_error->PendingError() || InstallPackages(Cache, false, true) == false)
  935. return _error->Error(_("Failed to process build dependencies"));
  936. return true;
  937. }
  938. /*}}}*/