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