]> git.saurik.com Git - apt.git/blob - apt-private/private-source.cc
c45af6651f1dbe9ae0fad00589e77c403506eb62
[apt.git] / apt-private / private-source.cc
1 // Include Files /*{{{*/
2 #include <config.h>
3
4 #include <apt-pkg/acquire-item.h>
5 #include <apt-pkg/acquire.h>
6 #include <apt-pkg/algorithms.h>
7 #include <apt-pkg/aptconfiguration.h>
8 #include <apt-pkg/cachefile.h>
9 #include <apt-pkg/cacheiterators.h>
10 #include <apt-pkg/cacheset.h>
11 #include <apt-pkg/cmndline.h>
12 #include <apt-pkg/configuration.h>
13 #include <apt-pkg/depcache.h>
14 #include <apt-pkg/error.h>
15 #include <apt-pkg/fileutl.h>
16 #include <apt-pkg/hashes.h>
17 #include <apt-pkg/indexfile.h>
18 #include <apt-pkg/metaindex.h>
19 #include <apt-pkg/pkgcache.h>
20 #include <apt-pkg/sourcelist.h>
21 #include <apt-pkg/srcrecords.h>
22 #include <apt-pkg/strutl.h>
23 #include <apt-pkg/version.h>
24 #include <apt-pkg/policy.h>
25
26 #include <apt-private/private-cachefile.h>
27 #include <apt-private/private-cacheset.h>
28 #include <apt-private/private-download.h>
29 #include <apt-private/private-install.h>
30 #include <apt-private/private-source.h>
31
32 #include <apt-pkg/debindexfile.h>
33
34 #include <stddef.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40
41 #include <iostream>
42 #include <sstream>
43 #include <set>
44 #include <string>
45 #include <vector>
46
47 #include <apti18n.h>
48 /*}}}*/
49
50 // GetReleaseFileForSourceRecord - Return Suite for the given srcrecord /*{{{*/
51 static pkgCache::RlsFileIterator GetReleaseFileForSourceRecord(CacheFile &CacheFile,
52 pkgSourceList const * const SrcList, pkgSrcRecords::Parser const * const Parse)
53 {
54 // try to find release
55 const pkgIndexFile& CurrentIndexFile = Parse->Index();
56
57 for (pkgSourceList::const_iterator S = SrcList->begin();
58 S != SrcList->end(); ++S)
59 {
60 std::vector<pkgIndexFile *> *Indexes = (*S)->GetIndexFiles();
61 for (std::vector<pkgIndexFile *>::const_iterator IF = Indexes->begin();
62 IF != Indexes->end(); ++IF)
63 {
64 if (&CurrentIndexFile == (*IF))
65 return (*S)->FindInCache(CacheFile, false);
66 }
67 }
68 return pkgCache::RlsFileIterator(CacheFile);
69 }
70 /*}}}*/
71 // FindSrc - Find a source record /*{{{*/
72 static pkgSrcRecords::Parser *FindSrc(const char *Name,
73 pkgSrcRecords &SrcRecs,std::string &Src,
74 CacheFile &Cache)
75 {
76 if (Cache.BuildCaches(false) == false)
77 return nullptr;
78 std::string VerTag, UserRequestedVerTag;
79 std::string ArchTag = "";
80 std::string RelTag = _config->Find("APT::Default-Release");
81 std::string TmpSrc = Name;
82
83 // extract release
84 size_t found = TmpSrc.find_last_of("/");
85 if (found != std::string::npos)
86 {
87 RelTag = TmpSrc.substr(found+1);
88 TmpSrc = TmpSrc.substr(0,found);
89 }
90 // extract the version
91 found = TmpSrc.find_last_of("=");
92 if (found != std::string::npos)
93 {
94 VerTag = UserRequestedVerTag = TmpSrc.substr(found+1);
95 TmpSrc = TmpSrc.substr(0,found);
96 }
97 // extract arch
98 found = TmpSrc.find_last_of(":");
99 if (found != std::string::npos)
100 {
101 ArchTag = TmpSrc.substr(found+1);
102 TmpSrc = TmpSrc.substr(0,found);
103 }
104
105 /* Lookup the version of the package we would install if we were to
106 install a version and determine the source package name, then look
107 in the archive for a source package of the same name. */
108 bool MatchSrcOnly = _config->FindB("APT::Get::Only-Source");
109 pkgCache::PkgIterator Pkg;
110 if (ArchTag != "")
111 Pkg = Cache.GetPkgCache()->FindPkg(TmpSrc, ArchTag);
112 else
113 Pkg = Cache.GetPkgCache()->FindPkg(TmpSrc);
114
115 // if we can't find a package but the user qualified with a arch,
116 // error out here
117 if (Pkg.end() && ArchTag != "")
118 {
119 Src = Name;
120 _error->Error(_("Can not find a package for architecture '%s'"),
121 ArchTag.c_str());
122 return 0;
123 }
124
125 if (MatchSrcOnly == false && Pkg.end() == false)
126 {
127 if(VerTag != "" || RelTag != "" || ArchTag != "")
128 {
129 bool fuzzy = false;
130 // we have a default release, try to locate the pkg. we do it like
131 // this because GetCandidateVer() will not "downgrade", that means
132 // "apt-get source -t stable apt" won't work on a unstable system
133 for (pkgCache::VerIterator Ver = Pkg.VersionList();; ++Ver)
134 {
135 // try first only exact matches, later fuzzy matches
136 if (Ver.end() == true)
137 {
138 if (fuzzy == true)
139 break;
140 fuzzy = true;
141 Ver = Pkg.VersionList();
142 // exit right away from the Pkg.VersionList() loop if we
143 // don't have any versions
144 if (Ver.end() == true)
145 break;
146 }
147
148 // ignore arches that are not for us
149 if (ArchTag != "" && Ver.Arch() != ArchTag)
150 continue;
151
152 // pick highest version for the arch unless the user wants
153 // something else
154 if (ArchTag != "" && VerTag == "" && RelTag == "")
155 if(Cache->VS().CmpVersion(VerTag, Ver.VerStr()) < 0)
156 VerTag = Ver.VerStr();
157
158 // We match against a concrete version (or a part of this version)
159 if (VerTag.empty() == false &&
160 (fuzzy == true || Cache->VS().CmpVersion(VerTag, Ver.VerStr()) != 0) && // exact match
161 (fuzzy == false || strncmp(VerTag.c_str(), Ver.VerStr(), VerTag.size()) != 0)) // fuzzy match
162 continue;
163
164 for (pkgCache::VerFileIterator VF = Ver.FileList();
165 VF.end() == false; ++VF)
166 {
167 /* If this is the status file, and the current version is not the
168 version in the status file (ie it is not installed, or somesuch)
169 then it is not a candidate for installation, ever. This weeds
170 out bogus entries that may be due to config-file states, or
171 other. */
172 if ((VF.File()->Flags & pkgCache::Flag::NotSource) ==
173 pkgCache::Flag::NotSource && Pkg.CurrentVer() != Ver)
174 continue;
175
176 // or we match against a release
177 if(VerTag.empty() == false ||
178 (VF.File().Archive() != 0 && VF.File().Archive() == RelTag) ||
179 (VF.File().Codename() != 0 && VF.File().Codename() == RelTag))
180 {
181 // the Version we have is possibly fuzzy or includes binUploads,
182 // so we use the Version of the SourcePkg (empty if same as package)
183 Src = Ver.SourcePkgName();
184 VerTag = Ver.SourceVerStr();
185 break;
186 }
187 }
188 if (Src.empty() == false)
189 break;
190 }
191 }
192
193 if (Src.empty() == true && ArchTag.empty() == false)
194 {
195 if (VerTag.empty() == false)
196 _error->Error(_("Can not find a package '%s' with version '%s'"),
197 Pkg.FullName().c_str(), VerTag.c_str());
198 if (RelTag.empty() == false)
199 _error->Error(_("Can not find a package '%s' with release '%s'"),
200 Pkg.FullName().c_str(), RelTag.c_str());
201 Src = Name;
202 return 0;
203 }
204
205
206 if (Src.empty() == true)
207 {
208 // if we don't have found a fitting package yet so we will
209 // choose a good candidate and proceed with that.
210 // Maybe we will find a source later on with the right VerTag
211 // or RelTag
212 if (Cache.BuildPolicy() == false)
213 return nullptr;
214 pkgPolicy * Policy = dynamic_cast<pkgPolicy*>(Cache.GetPolicy());
215 if (Policy == nullptr)
216 {
217 _error->Fatal("Implementation error: dynamic up-casting policy engine failed in FindSrc!");
218 return nullptr;
219 }
220 pkgCache::VerIterator const Ver = Policy->GetCandidateVer(Pkg);
221 if (Ver.end() == false)
222 {
223 if (strcmp(Ver.SourcePkgName(),Ver.ParentPkg().Name()) != 0)
224 Src = Ver.SourcePkgName();
225 if (VerTag.empty() == true && strcmp(Ver.SourceVerStr(),Ver.VerStr()) != 0)
226 VerTag = Ver.SourceVerStr();
227 }
228 }
229 }
230
231 if (Src.empty() == true)
232 {
233 Src = TmpSrc;
234 }
235 else
236 {
237 /* if we have a source pkg name, make sure to only search
238 for srcpkg names, otherwise apt gets confused if there
239 is a binary package "pkg1" and a source package "pkg1"
240 with the same name but that comes from different packages */
241 MatchSrcOnly = true;
242 if (Src != TmpSrc)
243 {
244 ioprintf(c1out, _("Picking '%s' as source package instead of '%s'\n"), Src.c_str(), TmpSrc.c_str());
245 }
246 }
247
248 // The best hit
249 pkgSrcRecords::Parser *Last = 0;
250 unsigned long Offset = 0;
251 std::string Version;
252 pkgSourceList const * const SrcList = Cache.GetSourceList();
253
254 /* Iterate over all of the hits, which includes the resulting
255 binary packages in the search */
256 pkgSrcRecords::Parser *Parse;
257 while (true)
258 {
259 SrcRecs.Restart();
260 while ((Parse = SrcRecs.Find(Src.c_str(), MatchSrcOnly)) != 0)
261 {
262 const std::string Ver = Parse->Version();
263
264 // See if we need to look for a specific release tag
265 if (RelTag.empty() == false && UserRequestedVerTag.empty() == true)
266 {
267 pkgCache::RlsFileIterator const Rls = GetReleaseFileForSourceRecord(Cache, SrcList, Parse);
268 if (Rls.end() == false)
269 {
270 if ((Rls->Archive != 0 && RelTag != Rls.Archive()) &&
271 (Rls->Codename != 0 && RelTag != Rls.Codename()))
272 continue;
273 }
274 }
275
276 // Ignore all versions which doesn't fit
277 if (VerTag.empty() == false &&
278 Cache->VS().CmpVersion(VerTag, Ver) != 0) // exact match
279 continue;
280
281 // Newer version or an exact match? Save the hit
282 if (Last == 0 || Cache->VS().CmpVersion(Version,Ver) < 0) {
283 Last = Parse;
284 Offset = Parse->Offset();
285 Version = Ver;
286 }
287
288 // was the version check above an exact match?
289 // If so, we don't need to look further
290 if (VerTag.empty() == false && (VerTag == Ver))
291 break;
292 }
293 if (UserRequestedVerTag == "" && Version != "" && RelTag != "")
294 ioprintf(c1out, "Selected version '%s' (%s) for %s\n",
295 Version.c_str(), RelTag.c_str(), Src.c_str());
296
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
303 if (Last == 0 || Last->Jump(Offset) == false)
304 return 0;
305
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
324 if (CmdL.FileSize() <= 1)
325 return _error->Error(_("Must specify at least one package to fetch source for"));
326
327 // Read the source list
328 if (Cache.BuildSourceList() == false)
329 return false;
330 pkgSourceList *List = Cache.GetSourceList();
331
332 // Create the text record parsers
333 pkgSrcRecords SrcRecs(*List);
334 if (_error->PendingError() == true)
335 return false;
336
337 std::unique_ptr<DscFile[]> Dsc(new DscFile[CmdL.FileSize()]);
338
339 // insert all downloaded uris into this set to avoid downloading them
340 // twice
341 std::set<std::string> queued;
342
343 // Diff only mode only fetches .diff files
344 bool const diffOnly = _config->FindB("APT::Get::Diff-Only", false);
345 // Tar only mode only fetches .tar files
346 bool const tarOnly = _config->FindB("APT::Get::Tar-Only", false);
347 // Dsc only mode only fetches .dsc files
348 bool const dscOnly = _config->FindB("APT::Get::Dsc-Only", false);
349
350 // Load the requestd sources into the fetcher
351 aptAcquireWithTextStatus Fetcher;
352 unsigned J = 0;
353 std::vector<std::string> UntrustedList;
354 for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
355 {
356 std::string Src;
357 pkgSrcRecords::Parser *Last = FindSrc(*I,SrcRecs,Src,Cache);
358 if (Last == 0) {
359 return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
360 }
361
362 if (Last->Index().IsTrusted() == false)
363 UntrustedList.push_back(Src);
364
365 std::string srec = Last->AsStr();
366 std::string::size_type pos = srec.find("\nVcs-");
367 while (pos != std::string::npos)
368 {
369 pos += strlen("\nVcs-");
370 std::string vcs = srec.substr(pos,srec.find(":",pos)-pos);
371 if(vcs == "Browser")
372 {
373 pos = srec.find("\nVcs-", pos);
374 continue;
375 }
376 pos += vcs.length()+2;
377 std::string::size_type epos = srec.find("\n", pos);
378 std::string const uri = srec.substr(pos,epos-pos);
379 ioprintf(c1out, _("NOTICE: '%s' packaging is maintained in "
380 "the '%s' version control system at:\n"
381 "%s\n"),
382 Src.c_str(), vcs.c_str(), uri.c_str());
383 std::string vcscmd;
384 if (vcs == "Bzr")
385 vcscmd = "bzr branch " + uri;
386 else if (vcs == "Git")
387 vcscmd = "git clone " + uri;
388
389 if (vcscmd.empty() == false)
390 ioprintf(c1out,_("Please use:\n%s\n"
391 "to retrieve the latest (possibly unreleased) "
392 "updates to the package.\n"),
393 vcscmd.c_str());
394 break;
395 }
396
397 // Back track
398 std::vector<pkgSrcRecords::File2> Lst;
399 if (Last->Files2(Lst) == false) {
400 return false;
401 }
402
403 // Load them into the fetcher
404 for (std::vector<pkgSrcRecords::File2>::const_iterator I = Lst.begin();
405 I != Lst.end(); ++I)
406 {
407 // Try to guess what sort of file it is we are getting.
408 if (I->Type == "dsc")
409 {
410 Dsc[J].Package = Last->Package();
411 Dsc[J].Version = Last->Version();
412 Dsc[J].Dsc = flNotDir(I->Path);
413 }
414
415 // Handle the only options so that multiple can be used at once
416 if (diffOnly == true || tarOnly == true || dscOnly == true)
417 {
418 if ((diffOnly == true && I->Type == "diff") ||
419 (tarOnly == true && I->Type == "tar") ||
420 (dscOnly == true && I->Type == "dsc"))
421 ; // Fine, we want this file downloaded
422 else
423 continue;
424 }
425
426 // don't download the same uri twice (should this be moved to
427 // the fetcher interface itself?)
428 if(queued.find(Last->Index().ArchiveURI(I->Path)) != queued.end())
429 continue;
430 queued.insert(Last->Index().ArchiveURI(I->Path));
431
432 // check if we have a file with that md5 sum already localy
433 std::string localFile = flNotDir(I->Path);
434 if (FileExists(localFile) == true)
435 if(I->Hashes.VerifyFile(localFile) == true)
436 {
437 ioprintf(c1out,_("Skipping already downloaded file '%s'\n"),
438 localFile.c_str());
439 continue;
440 }
441
442 // see if we have a hash (Acquire::ForceHash is the only way to have none)
443 if (I->Hashes.usable() == false && _config->FindB("APT::Get::AllowUnauthenticated",false) == false)
444 {
445 ioprintf(c1out, "Skipping download of file '%s' as requested hashsum is not available for authentication\n",
446 localFile.c_str());
447 continue;
448 }
449
450 new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
451 I->Hashes, I->FileSize, Last->Index().SourceInfo(*Last,*I), Src);
452 }
453 }
454
455 // Display statistics
456 unsigned long long FetchBytes = Fetcher.FetchNeeded();
457 unsigned long long FetchPBytes = Fetcher.PartialPresent();
458 unsigned long long DebBytes = Fetcher.TotalNeeded();
459
460 if (CheckFreeSpaceBeforeDownload(".", (FetchBytes - FetchPBytes)) == false)
461 return false;
462
463 // Number of bytes
464 if (DebBytes != FetchBytes)
465 //TRANSLATOR: The required space between number and unit is already included
466 // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
467 ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
468 SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
469 else
470 //TRANSLATOR: The required space between number and unit is already included
471 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
472 ioprintf(c1out,_("Need to get %sB of source archives.\n"),
473 SizeToStr(DebBytes).c_str());
474
475 if (_config->FindB("APT::Get::Simulate",false) == true)
476 {
477 for (unsigned I = 0; I != J; I++)
478 ioprintf(std::cout,_("Fetch source %s\n"),Dsc[I].Package.c_str());
479 return true;
480 }
481
482 // Just print out the uris an exit if the --print-uris flag was used
483 if (_config->FindB("APT::Get::Print-URIs") == true)
484 {
485 pkgAcquire::UriIterator I = Fetcher.UriBegin();
486 for (; I != Fetcher.UriEnd(); ++I)
487 std::cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
488 I->Owner->FileSize << ' ' << I->Owner->HashSum() << std::endl;
489 return true;
490 }
491
492 // check authentication status of the source as well
493 if (UntrustedList.empty() == false && AuthPrompt(UntrustedList, false) == false)
494 return false;
495
496 // Run it
497 bool Failed = false;
498 if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
499 {
500 return _error->Error(_("Failed to fetch some archives."));
501 }
502
503 if (_config->FindB("APT::Get::Download-only",false) == true)
504 {
505 c1out << _("Download complete and in download only mode") << std::endl;
506 return true;
507 }
508
509 // Unpack the sources
510 pid_t Process = ExecFork();
511
512 if (Process == 0)
513 {
514 bool const fixBroken = _config->FindB("APT::Get::Fix-Broken", false);
515 for (unsigned I = 0; I != J; ++I)
516 {
517 std::string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
518
519 // Diff only mode only fetches .diff files
520 if (_config->FindB("APT::Get::Diff-Only",false) == true ||
521 _config->FindB("APT::Get::Tar-Only",false) == true ||
522 Dsc[I].Dsc.empty() == true)
523 continue;
524
525 // See if the package is already unpacked
526 struct stat Stat;
527 if (fixBroken == false && stat(Dir.c_str(),&Stat) == 0 &&
528 S_ISDIR(Stat.st_mode) != 0)
529 {
530 ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
531 Dir.c_str());
532 }
533 else
534 {
535 // Call dpkg-source
536 std::string const sourceopts = _config->Find("DPkg::Source-Options", "-x");
537 std::string S;
538 strprintf(S, "%s %s %s",
539 _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
540 sourceopts.c_str(), Dsc[I].Dsc.c_str());
541 if (system(S.c_str()) != 0)
542 {
543 fprintf(stderr, _("Unpack command '%s' failed.\n"), S.c_str());
544 fprintf(stderr, _("Check if the 'dpkg-dev' package is installed.\n"));
545 _exit(1);
546 }
547 }
548
549 // Try to compile it with dpkg-buildpackage
550 if (_config->FindB("APT::Get::Compile",false) == true)
551 {
552 std::string buildopts = _config->Find("APT::Get::Host-Architecture");
553 if (buildopts.empty() == false)
554 buildopts = "-a" + buildopts + " ";
555
556 // get all active build profiles
557 std::string const profiles = APT::Configuration::getBuildProfilesString();
558 if (profiles.empty() == false)
559 buildopts.append(" -P").append(profiles).append(" ");
560
561 buildopts.append(_config->Find("DPkg::Build-Options","-b -uc"));
562
563 // Call dpkg-buildpackage
564 std::string S;
565 strprintf(S, "cd %s && %s %s",
566 Dir.c_str(),
567 _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
568 buildopts.c_str());
569
570 if (system(S.c_str()) != 0)
571 {
572 fprintf(stderr, _("Build command '%s' failed.\n"), S.c_str());
573 _exit(1);
574 }
575 }
576 }
577
578 _exit(0);
579 }
580
581 return ExecWait(Process, "dpkg-source");
582 }
583 /*}}}*/
584 // DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
585 // ---------------------------------------------------------------------
586 /* This function will look at the build depends list of the given source
587 package and install the necessary packages to make it true, or fail. */
588 static std::vector<pkgSrcRecords::Parser::BuildDepRec> GetBuildDeps(pkgSrcRecords::Parser * const Last,
589 char const * const Src, bool const StripMultiArch, std::string const &hostArch)
590 {
591 std::vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
592 // FIXME: Can't specify architecture to use for [wildcard] matching, so switch default arch temporary
593 if (hostArch.empty() == false)
594 {
595 std::string nativeArch = _config->Find("APT::Architecture");
596 _config->Set("APT::Architecture", hostArch);
597 bool Success = Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch);
598 _config->Set("APT::Architecture", nativeArch);
599 if (Success == false)
600 {
601 _error->Error(_("Unable to get build-dependency information for %s"), Src);
602 return {};
603 }
604 }
605 else if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
606 {
607 _error->Error(_("Unable to get build-dependency information for %s"), Src);
608 return {};
609 }
610
611 if (BuildDeps.empty() == true)
612 ioprintf(c1out,_("%s has no build depends.\n"), Src);
613
614 return BuildDeps;
615 }
616 static void WriteBuildDependencyPackage(std::ostringstream &buildDepsPkgFile,
617 std::string const &PkgName, std::string const &Arch,
618 std::vector<pkgSrcRecords::Parser::BuildDepRec> const &Dependencies)
619 {
620 buildDepsPkgFile << "Package: " << PkgName << "\n"
621 << "Architecture: " << Arch << "\n"
622 << "Version: 1\n";
623
624 std::string depends, conflicts;
625 for (auto const &dep: Dependencies)
626 {
627 std::string * type;
628 if (dep.Type == pkgSrcRecords::Parser::BuildConflict || dep.Type == pkgSrcRecords::Parser::BuildConflictIndep)
629 type = &conflicts;
630 else
631 type = &depends;
632
633 type->append(" ").append(dep.Package);
634 if (dep.Version.empty() == false)
635 type->append(" (").append(pkgCache::CompTypeDeb(dep.Op)).append(" ").append(dep.Version).append(")");
636 if ((dep.Op & pkgCache::Dep::Or) == pkgCache::Dep::Or)
637 {
638 type->append("\n |");
639 }
640 else
641 type->append(",\n");
642 }
643 if (depends.empty() == false)
644 buildDepsPkgFile << "Depends:\n" << depends;
645 if (conflicts.empty() == false)
646 buildDepsPkgFile << "Conflicts:\n" << conflicts;
647 buildDepsPkgFile << "\n";
648 }
649 bool DoBuildDep(CommandLine &CmdL)
650 {
651 CacheFile Cache;
652 std::vector<char const *> VolatileCmdL;
653 Cache.GetSourceList()->AddVolatileFiles(CmdL, &VolatileCmdL);
654
655 _config->Set("APT::Install-Recommends", false);
656
657 if (CmdL.FileSize() <= 1 && VolatileCmdL.empty())
658 return _error->Error(_("Must specify at least one package to check builddeps for"));
659
660 bool StripMultiArch;
661 std::string hostArch = _config->Find("APT::Get::Host-Architecture");
662 if (hostArch.empty() == false)
663 {
664 std::vector<std::string> archs = APT::Configuration::getArchitectures();
665 if (std::find(archs.begin(), archs.end(), hostArch) == archs.end())
666 return _error->Error(_("No architecture information available for %s. See apt.conf(5) APT::Architectures for setup"), hostArch.c_str());
667 StripMultiArch = false;
668 }
669 else
670 StripMultiArch = true;
671
672 std::ostringstream buildDepsPkgFile;
673 std::vector<std::pair<std::string,std::string>> pseudoPkgs;
674 // deal with the build essentials first
675 {
676 std::vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
677 Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
678 if (Opts)
679 Opts = Opts->Child;
680 for (; Opts; Opts = Opts->Next)
681 {
682 if (Opts->Value.empty() == true)
683 continue;
684
685 pkgSrcRecords::Parser::BuildDepRec rec;
686 rec.Package = Opts->Value;
687 rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
688 rec.Op = 0;
689 BuildDeps.push_back(rec);
690 }
691 std::string const pseudo = "builddeps:essentials";
692 std::string const nativeArch = _config->Find("APT::Architecture");
693 WriteBuildDependencyPackage(buildDepsPkgFile, pseudo, nativeArch, BuildDeps);
694 pseudoPkgs.emplace_back(pseudo, nativeArch);
695 }
696
697 // Read the source list
698 if (Cache.BuildSourceList() == false)
699 return false;
700 pkgSourceList *List = Cache.GetSourceList();
701 std::string const pseudoArch = hostArch.empty() ? _config->Find("APT::Architecture") : hostArch;
702
703 // FIXME: Avoid volatile sources == cmdline assumption
704 {
705 auto const VolatileSources = List->GetVolatileFiles();
706 if (VolatileSources.size() == VolatileCmdL.size())
707 {
708 for (size_t i = 0; i < VolatileSources.size(); ++i)
709 {
710 char const * const Src = VolatileCmdL[i];
711 if (DirectoryExists(Src))
712 ioprintf(c1out, _("Note, using directory '%s' to get the build dependencies\n"), Src);
713 else
714 ioprintf(c1out, _("Note, using file '%s' to get the build dependencies\n"), Src);
715 std::unique_ptr<pkgSrcRecords::Parser> Last(VolatileSources[i]->CreateSrcParser());
716 if (Last == nullptr)
717 return _error->Error(_("Unable to find a source package for %s"), Src);
718
719 std::string const pseudo = std::string("builddeps:") + Src;
720 WriteBuildDependencyPackage(buildDepsPkgFile, pseudo, pseudoArch,
721 GetBuildDeps(Last.get(), Src, StripMultiArch, hostArch));
722 pseudoPkgs.emplace_back(pseudo, pseudoArch);
723 }
724 }
725 else
726 return _error->Error("Implementation error: Volatile sources (%lu) and"
727 "commandline elements (%lu) do not match!", VolatileSources.size(),
728 VolatileCmdL.size());
729 }
730
731 if (CmdL.FileList[1] != 0)
732 {
733 // Create the text record parsers
734 pkgSrcRecords SrcRecs(*List);
735 if (_error->PendingError() == true)
736 return false;
737 for (const char **I = CmdL.FileList + 1; *I != 0; ++I)
738 {
739 std::string Src;
740 pkgSrcRecords::Parser * const Last = FindSrc(*I,SrcRecs,Src,Cache);
741 if (Last == nullptr)
742 return _error->Error(_("Unable to find a source package for %s"), *I);
743
744 std::string const pseudo = std::string("builddeps:") + Src;
745 WriteBuildDependencyPackage(buildDepsPkgFile, pseudo, pseudoArch,
746 GetBuildDeps(Last, Src.c_str(), StripMultiArch, hostArch));
747 pseudoPkgs.emplace_back(pseudo, pseudoArch);
748 }
749 }
750
751 Cache.AddIndexFile(new debStringPackageIndex(buildDepsPkgFile.str()));
752
753 bool WantLock = _config->FindB("APT::Get::Print-URIs", false) == false;
754 if (Cache.Open(WantLock) == false)
755 return false;
756 pkgProblemResolver Fix(Cache.GetDepCache());
757
758 APT::PackageVector removeAgain;
759 {
760 pkgDepCache::ActionGroup group(Cache);
761 TryToInstall InstallAction(Cache, &Fix, false);
762 for (auto const &pkg: pseudoPkgs)
763 {
764 pkgCache::PkgIterator const Pkg = Cache->FindPkg(pkg.first, pkg.second);
765 if (Pkg.end())
766 continue;
767 Cache->SetCandidateVersion(Pkg.VersionList());
768 InstallAction(Cache[Pkg].CandidateVerIter(Cache));
769 removeAgain.push_back(Pkg);
770 }
771 InstallAction.doAutoInstall();
772
773 OpTextProgress Progress(*_config);
774 bool const resolver_fail = Fix.Resolve(true, &Progress);
775 if (resolver_fail == false && Cache->BrokenCount() == 0)
776 return false;
777 if (CheckNothingBroken(Cache) == false)
778 return false;
779 }
780 if (DoAutomaticRemove(Cache) == false)
781 return false;
782 {
783 pkgDepCache::ActionGroup group(Cache);
784 for (auto const &pkg: removeAgain)
785 Cache->MarkDelete(pkg, false, 0, true);
786 }
787
788 pseudoPkgs.clear();
789 if (_error->PendingError() || InstallPackages(Cache, false, true) == false)
790 return _error->Error(_("Failed to process build dependencies"));
791 return true;
792 }
793 /*}}}*/