private-source.cc 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. // DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
  547. // ---------------------------------------------------------------------
  548. /* This function will look at the build depends list of the given source
  549. package and install the necessary packages to make it true, or fail. */
  550. bool DoBuildDep(CommandLine &CmdL)
  551. {
  552. CacheFile Cache;
  553. _config->Set("APT::Install-Recommends", false);
  554. bool WantLock = _config->FindB("APT::Get::Print-URIs", false) == false;
  555. if (Cache.Open(WantLock) == false)
  556. return false;
  557. if (CmdL.FileSize() <= 1)
  558. return _error->Error(_("Must specify at least one package to check builddeps for"));
  559. // Read the source list
  560. if (Cache.BuildSourceList() == false)
  561. return false;
  562. pkgSourceList *List = Cache.GetSourceList();
  563. // Create the text record parsers
  564. pkgSrcRecords SrcRecs(*List);
  565. if (_error->PendingError() == true)
  566. return false;
  567. bool StripMultiArch;
  568. std::string hostArch = _config->Find("APT::Get::Host-Architecture");
  569. if (hostArch.empty() == false)
  570. {
  571. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  572. if (std::find(archs.begin(), archs.end(), hostArch) == archs.end())
  573. return _error->Error(_("No architecture information available for %s. See apt.conf(5) APT::Architectures for setup"), hostArch.c_str());
  574. StripMultiArch = false;
  575. }
  576. else
  577. StripMultiArch = true;
  578. unsigned J = 0;
  579. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  580. {
  581. std::string Src;
  582. pkgSrcRecords::Parser *Last = 0;
  583. std::unique_ptr<pkgSrcRecords::Parser> LastOwner;
  584. // an unpacked debian source tree
  585. using APT::String::Startswith;
  586. if ((Startswith(*I, "./") || Startswith(*I, "/")) &&
  587. DirectoryExists(*I))
  588. {
  589. ioprintf(c1out, _("Note, using directory '%s' to get the build dependencies\n"), *I);
  590. // FIXME: how can we make this more elegant?
  591. std::string TypeName = "Debian control file";
  592. pkgIndexFile::Type *Type = pkgIndexFile::Type::GetType(TypeName.c_str());
  593. if(Type != NULL)
  594. LastOwner.reset(Last = Type->CreateSrcPkgParser(*I));
  595. }
  596. // if its a local file (e.g. .dsc) use this
  597. else if (FileExists(*I))
  598. {
  599. ioprintf(c1out, _("Note, using file '%s' to get the build dependencies\n"), *I);
  600. // see if we can get a parser for this pkgIndexFile type
  601. std::string TypeName = "Debian " + flExtension(*I) + " file";
  602. pkgIndexFile::Type *Type = pkgIndexFile::Type::GetType(TypeName.c_str());
  603. if(Type != NULL)
  604. LastOwner.reset(Last = Type->CreateSrcPkgParser(*I));
  605. } else {
  606. // normal case, search the cache for the source file
  607. Last = FindSrc(*I,SrcRecs,Src,Cache);
  608. }
  609. if (Last == 0)
  610. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  611. // Process the build-dependencies
  612. std::vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
  613. // FIXME: Can't specify architecture to use for [wildcard] matching, so switch default arch temporary
  614. if (hostArch.empty() == false)
  615. {
  616. std::string nativeArch = _config->Find("APT::Architecture");
  617. _config->Set("APT::Architecture", hostArch);
  618. bool Success = Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch);
  619. _config->Set("APT::Architecture", nativeArch);
  620. if (Success == false)
  621. return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
  622. }
  623. else if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
  624. return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
  625. // Also ensure that build-essential packages are present
  626. Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
  627. if (Opts)
  628. Opts = Opts->Child;
  629. for (; Opts; Opts = Opts->Next)
  630. {
  631. if (Opts->Value.empty() == true)
  632. continue;
  633. pkgSrcRecords::Parser::BuildDepRec rec;
  634. rec.Package = Opts->Value;
  635. rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
  636. rec.Op = 0;
  637. BuildDeps.push_back(rec);
  638. }
  639. if (BuildDeps.empty() == true)
  640. {
  641. ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
  642. continue;
  643. }
  644. // Install the requested packages
  645. std::vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
  646. pkgProblemResolver Fix(Cache);
  647. bool skipAlternatives = false; // skip remaining alternatives in an or group
  648. for (D = BuildDeps.begin(); D != BuildDeps.end(); ++D)
  649. {
  650. bool hasAlternatives = (((*D).Op & pkgCache::Dep::Or) == pkgCache::Dep::Or);
  651. if (skipAlternatives == true)
  652. {
  653. /*
  654. * if there are alternatives, we've already picked one, so skip
  655. * the rest
  656. *
  657. * TODO: this means that if there's a build-dep on A|B and B is
  658. * installed, we'll still try to install A; more importantly,
  659. * if A is currently broken, we cannot go back and try B. To fix
  660. * this would require we do a Resolve cycle for each package we
  661. * add to the install list. Ugh
  662. */
  663. if (!hasAlternatives)
  664. skipAlternatives = false; // end of or group
  665. continue;
  666. }
  667. if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
  668. (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
  669. {
  670. pkgCache::GrpIterator Grp = Cache->FindGrp((*D).Package);
  671. // Build-conflicts on unknown packages are silently ignored
  672. if (Grp.end() == true)
  673. continue;
  674. for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
  675. {
  676. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  677. /*
  678. * Remove if we have an installed version that satisfies the
  679. * version criteria
  680. */
  681. if (IV.end() == false &&
  682. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  683. TryToInstallBuildDep(Pkg,Cache,Fix,true,false);
  684. }
  685. }
  686. else // BuildDep || BuildDepIndep
  687. {
  688. if (_config->FindB("Debug::BuildDeps",false) == true)
  689. std::cout << "Looking for " << (*D).Package << "...\n";
  690. pkgCache::PkgIterator Pkg;
  691. // Cross-Building?
  692. if (StripMultiArch == false && D->Type != pkgSrcRecords::Parser::BuildDependIndep)
  693. {
  694. size_t const colon = D->Package.find(":");
  695. if (colon != std::string::npos)
  696. {
  697. if (strcmp(D->Package.c_str() + colon, ":any") == 0 || strcmp(D->Package.c_str() + colon, ":native") == 0)
  698. Pkg = Cache->FindPkg(D->Package.substr(0,colon));
  699. else
  700. Pkg = Cache->FindPkg(D->Package);
  701. }
  702. else
  703. Pkg = Cache->FindPkg(D->Package, hostArch);
  704. // a bad version either is invalid or doesn't satify dependency
  705. #define BADVER(Ver) (Ver.end() == true || \
  706. (D->Version.empty() == false && \
  707. Cache->VS().CheckDep(Ver.VerStr(),D->Op,D->Version.c_str()) == false))
  708. APT::VersionList verlist;
  709. if (Pkg.end() == false)
  710. {
  711. pkgCache::VerIterator Ver = (*Cache)[Pkg].InstVerIter(*Cache);
  712. if (BADVER(Ver) == false)
  713. verlist.insert(Ver);
  714. Ver = (*Cache)[Pkg].CandidateVerIter(*Cache);
  715. if (BADVER(Ver) == false)
  716. verlist.insert(Ver);
  717. }
  718. if (verlist.empty() == true)
  719. {
  720. pkgCache::PkgIterator BuildPkg = Cache->FindPkg(D->Package, "native");
  721. if (BuildPkg.end() == false && Pkg != BuildPkg)
  722. {
  723. pkgCache::VerIterator Ver = (*Cache)[BuildPkg].InstVerIter(*Cache);
  724. if (BADVER(Ver) == false)
  725. verlist.insert(Ver);
  726. Ver = (*Cache)[BuildPkg].CandidateVerIter(*Cache);
  727. if (BADVER(Ver) == false)
  728. verlist.insert(Ver);
  729. }
  730. }
  731. #undef BADVER
  732. std::string forbidden;
  733. // We need to decide if host or build arch, so find a version we can look at
  734. APT::VersionList::const_iterator Ver = verlist.begin();
  735. for (; Ver != verlist.end(); ++Ver)
  736. {
  737. forbidden.clear();
  738. if (Ver->MultiArch == pkgCache::Version::No || Ver->MultiArch == pkgCache::Version::All)
  739. {
  740. if (colon == std::string::npos)
  741. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  742. else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
  743. forbidden = "Multi-Arch: no";
  744. else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
  745. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  746. }
  747. else if (Ver->MultiArch == pkgCache::Version::Same)
  748. {
  749. if (colon == std::string::npos)
  750. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  751. else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
  752. forbidden = "Multi-Arch: same";
  753. else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
  754. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  755. }
  756. else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
  757. {
  758. if (colon == std::string::npos)
  759. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  760. else if (strcmp(D->Package.c_str() + colon, ":any") == 0 ||
  761. strcmp(D->Package.c_str() + colon, ":native") == 0)
  762. forbidden = "Multi-Arch: foreign";
  763. }
  764. else if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
  765. {
  766. if (colon == std::string::npos)
  767. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  768. else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
  769. {
  770. // prefer any installed over preferred non-installed architectures
  771. pkgCache::GrpIterator Grp = Ver.ParentPkg().Group();
  772. // we don't check for version here as we are better of with upgrading than remove and install
  773. for (Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
  774. if (Pkg.CurrentVer().end() == false)
  775. break;
  776. if (Pkg.end() == true)
  777. Pkg = Grp.FindPreferredPkg(true);
  778. }
  779. else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
  780. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  781. }
  782. if (forbidden.empty() == false)
  783. {
  784. if (_config->FindB("Debug::BuildDeps",false) == true)
  785. std::cout << D->Package.substr(colon, std::string::npos) << " is not allowed from " << forbidden << " package " << (*D).Package << " (" << Ver.VerStr() << ")" << std::endl;
  786. continue;
  787. }
  788. //we found a good version
  789. break;
  790. }
  791. if (Ver == verlist.end())
  792. {
  793. if (_config->FindB("Debug::BuildDeps",false) == true)
  794. std::cout << " No multiarch info as we have no satisfying installed nor candidate for " << D->Package << " on build or host arch" << std::endl;
  795. if (forbidden.empty() == false)
  796. {
  797. if (hasAlternatives)
  798. continue;
  799. return _error->Error(_("%s dependency for %s can't be satisfied "
  800. "because %s is not allowed on '%s' packages"),
  801. Last->BuildDepType(D->Type), Src.c_str(),
  802. D->Package.c_str(), forbidden.c_str());
  803. }
  804. }
  805. }
  806. else
  807. Pkg = Cache->FindPkg(D->Package);
  808. if (Pkg.end() == true || (Pkg->VersionList == 0 && Pkg->ProvidesList == 0))
  809. {
  810. if (_config->FindB("Debug::BuildDeps",false) == true)
  811. std::cout << " (not found)" << (*D).Package << std::endl;
  812. if (hasAlternatives)
  813. continue;
  814. return _error->Error(_("%s dependency for %s cannot be satisfied "
  815. "because the package %s cannot be found"),
  816. Last->BuildDepType((*D).Type),Src.c_str(),
  817. (*D).Package.c_str());
  818. }
  819. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  820. if (IV.end() == false)
  821. {
  822. if (_config->FindB("Debug::BuildDeps",false) == true)
  823. std::cout << " Is installed\n";
  824. if (D->Version.empty() == true ||
  825. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  826. {
  827. skipAlternatives = hasAlternatives;
  828. continue;
  829. }
  830. if (_config->FindB("Debug::BuildDeps",false) == true)
  831. std::cout << " ...but the installed version doesn't meet the version requirement\n";
  832. if (((*D).Op & pkgCache::Dep::LessEq) == pkgCache::Dep::LessEq)
  833. return _error->Error(_("Failed to satisfy %s dependency for %s: Installed package %s is too new"),
  834. Last->BuildDepType((*D).Type), Src.c_str(), Pkg.FullName(true).c_str());
  835. }
  836. // Only consider virtual packages if there is no versioned dependency
  837. if ((*D).Version.empty() == true)
  838. {
  839. /*
  840. * If this is a virtual package, we need to check the list of
  841. * packages that provide it and see if any of those are
  842. * installed
  843. */
  844. pkgCache::PrvIterator Prv = Pkg.ProvidesList();
  845. for (; Prv.end() != true; ++Prv)
  846. {
  847. if (_config->FindB("Debug::BuildDeps",false) == true)
  848. std::cout << " Checking provider " << Prv.OwnerPkg().FullName() << std::endl;
  849. if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
  850. break;
  851. }
  852. if (Prv.end() == false)
  853. {
  854. if (_config->FindB("Debug::BuildDeps",false) == true)
  855. std::cout << " Is provided by installed package " << Prv.OwnerPkg().FullName() << std::endl;
  856. skipAlternatives = hasAlternatives;
  857. continue;
  858. }
  859. }
  860. else // versioned dependency
  861. {
  862. pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
  863. if (CV.end() == true ||
  864. Cache->VS().CheckDep(CV.VerStr(),(*D).Op,(*D).Version.c_str()) == false)
  865. {
  866. if (hasAlternatives)
  867. continue;
  868. else if (CV.end() == false)
  869. return _error->Error(_("%s dependency for %s cannot be satisfied "
  870. "because candidate version of package %s "
  871. "can't satisfy version requirements"),
  872. Last->BuildDepType(D->Type), Src.c_str(),
  873. D->Package.c_str());
  874. else
  875. return _error->Error(_("%s dependency for %s cannot be satisfied "
  876. "because package %s has no candidate version"),
  877. Last->BuildDepType(D->Type), Src.c_str(),
  878. D->Package.c_str());
  879. }
  880. }
  881. if (TryToInstallBuildDep(Pkg,Cache,Fix,false,false,false) == true)
  882. {
  883. // We successfully installed something; skip remaining alternatives
  884. skipAlternatives = hasAlternatives;
  885. if(_config->FindB("APT::Get::Build-Dep-Automatic", false) == true)
  886. Cache->MarkAuto(Pkg, true);
  887. continue;
  888. }
  889. else if (hasAlternatives)
  890. {
  891. if (_config->FindB("Debug::BuildDeps",false) == true)
  892. std::cout << " Unsatisfiable, trying alternatives\n";
  893. continue;
  894. }
  895. else
  896. {
  897. return _error->Error(_("Failed to satisfy %s dependency for %s: %s"),
  898. Last->BuildDepType((*D).Type),
  899. Src.c_str(),
  900. (*D).Package.c_str());
  901. }
  902. }
  903. }
  904. if (Fix.Resolve(true) == false)
  905. _error->Discard();
  906. // Now we check the state of the packages,
  907. if (Cache->BrokenCount() != 0)
  908. {
  909. ShowBroken(std::cout, Cache, false);
  910. return _error->Error(_("Build-dependencies for %s could not be satisfied."),*I);
  911. }
  912. }
  913. if (InstallPackages(Cache, false, true) == false)
  914. return _error->Error(_("Failed to process build dependencies"));
  915. return true;
  916. }
  917. /*}}}*/