You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1659 lines
56 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-get.cc,v 1.156 2004/08/28 01:05:16 mdz Exp $
  4. /* ######################################################################
  5. apt-get - Cover for dpkg
  6. This is an allout cover for dpkg implementing a safer front end. It is
  7. based largely on libapt-pkg.
  8. The syntax is different,
  9. apt-get [opt] command [things]
  10. Where command is:
  11. update - Resyncronize the package files from their sources
  12. upgrade - Smart-Download the newest versions of all packages
  13. dselect-upgrade - Follows dselect's changes to the Status: field
  14. and installes new and removes old packages
  15. dist-upgrade - Powerful upgrader designed to handle the issues with
  16. a new distribution.
  17. install - Download and install a given package (by name, not by .deb)
  18. check - Update the package cache and check for broken packages
  19. clean - Erase the .debs downloaded to /var/cache/apt/archives and
  20. the partial dir too
  21. ##################################################################### */
  22. /*}}}*/
  23. // Include Files /*{{{*/
  24. #include <config.h>
  25. #include <apt-pkg/acquire-item.h>
  26. #include <apt-pkg/algorithms.h>
  27. #include <apt-pkg/aptconfiguration.h>
  28. #include <apt-pkg/cachefile.h>
  29. #include <apt-pkg/cacheset.h>
  30. #include <apt-pkg/clean.h>
  31. #include <apt-pkg/cmndline.h>
  32. #include <apt-pkg/debmetaindex.h>
  33. #include <apt-pkg/depcache.h>
  34. #include <apt-pkg/error.h>
  35. #include <apt-pkg/fileutl.h>
  36. #include <apt-pkg/indexfile.h>
  37. #include <apt-pkg/init.h>
  38. #include <apt-pkg/md5.h>
  39. #include <apt-pkg/metaindex.h>
  40. #include <apt-pkg/pkgrecords.h>
  41. #include <apt-pkg/pkgsystem.h>
  42. #include <apt-pkg/progress.h>
  43. #include <apt-pkg/sourcelist.h>
  44. #include <apt-pkg/srcrecords.h>
  45. #include <apt-pkg/strutl.h>
  46. #include <apt-pkg/version.h>
  47. #include <apt-pkg/acquire.h>
  48. #include <apt-pkg/configuration.h>
  49. #include <apt-pkg/macros.h>
  50. #include <apt-pkg/pkgcache.h>
  51. #include <apt-pkg/cacheiterators.h>
  52. #include <apt-pkg/upgrade.h>
  53. #include <apt-pkg/sptr.h>
  54. #include <apt-private/acqprogress.h>
  55. #include <apt-private/private-cacheset.h>
  56. #include <apt-private/private-cachefile.h>
  57. #include <apt-private/private-cmndline.h>
  58. #include <apt-private/private-download.h>
  59. #include <apt-private/private-install.h>
  60. #include <apt-private/private-main.h>
  61. #include <apt-private/private-moo.h>
  62. #include <apt-private/private-output.h>
  63. #include <apt-private/private-update.h>
  64. #include <apt-private/private-upgrade.h>
  65. #include <apt-private/private-utils.h>
  66. #include <errno.h>
  67. #include <signal.h>
  68. #include <stddef.h>
  69. #include <stdio.h>
  70. #include <stdlib.h>
  71. #include <string.h>
  72. #include <sys/ioctl.h>
  73. #include <sys/stat.h>
  74. #include <unistd.h>
  75. #include <pwd.h>
  76. #include <grp.h>
  77. #include <algorithm>
  78. #include <fstream>
  79. #include <iostream>
  80. #include <sstream>
  81. #include <set>
  82. #include <string>
  83. #include <vector>
  84. #include <apti18n.h>
  85. /*}}}*/
  86. using namespace std;
  87. // TryToInstallBuildDep - Try to install a single package /*{{{*/
  88. // ---------------------------------------------------------------------
  89. /* This used to be inlined in DoInstall, but with the advent of regex package
  90. name matching it was split out.. */
  91. static bool TryToInstallBuildDep(pkgCache::PkgIterator Pkg,pkgCacheFile &Cache,
  92. pkgProblemResolver &Fix,bool Remove,bool BrokenFix,
  93. bool AllowFail = true)
  94. {
  95. if (Cache[Pkg].CandidateVer == 0 && Pkg->ProvidesList != 0)
  96. {
  97. CacheSetHelperAPTGet helper(c1out);
  98. helper.showErrors(false);
  99. pkgCache::VerIterator Ver = helper.canNotFindNewestVer(Cache, Pkg);
  100. if (Ver.end() == false)
  101. Pkg = Ver.ParentPkg();
  102. else if (helper.showVirtualPackageErrors(Cache) == false)
  103. return AllowFail;
  104. }
  105. if (_config->FindB("Debug::BuildDeps",false) == true)
  106. {
  107. if (Remove == true)
  108. cout << " Trying to remove " << Pkg << endl;
  109. else
  110. cout << " Trying to install " << Pkg << endl;
  111. }
  112. if (Remove == true)
  113. {
  114. TryToRemove RemoveAction(Cache, &Fix);
  115. RemoveAction(Pkg.VersionList());
  116. } else if (Cache[Pkg].CandidateVer != 0) {
  117. TryToInstall InstallAction(Cache, &Fix, BrokenFix);
  118. InstallAction(Cache[Pkg].CandidateVerIter(Cache));
  119. InstallAction.doAutoInstall();
  120. } else
  121. return AllowFail;
  122. return true;
  123. }
  124. /*}}}*/
  125. // GetReleaseFileForSourceRecord - Return Suite for the given srcrecord /*{{{*/
  126. static pkgCache::RlsFileIterator GetReleaseFileForSourceRecord(CacheFile &CacheFile,
  127. pkgSourceList *SrcList, pkgSrcRecords::Parser *Parse)
  128. {
  129. // try to find release
  130. const pkgIndexFile& CurrentIndexFile = Parse->Index();
  131. for (pkgSourceList::const_iterator S = SrcList->begin();
  132. S != SrcList->end(); ++S)
  133. {
  134. vector<pkgIndexFile *> *Indexes = (*S)->GetIndexFiles();
  135. for (vector<pkgIndexFile *>::const_iterator IF = Indexes->begin();
  136. IF != Indexes->end(); ++IF)
  137. {
  138. if (&CurrentIndexFile == (*IF))
  139. return (*S)->FindInCache(CacheFile, false);
  140. }
  141. }
  142. return pkgCache::RlsFileIterator(CacheFile);
  143. }
  144. /*}}}*/
  145. // FindSrc - Find a source record /*{{{*/
  146. // ---------------------------------------------------------------------
  147. /* */
  148. static pkgSrcRecords::Parser *FindSrc(const char *Name,
  149. pkgSrcRecords &SrcRecs,string &Src,
  150. CacheFile &CacheFile)
  151. {
  152. string VerTag, UserRequestedVerTag;
  153. string ArchTag = "";
  154. string RelTag = _config->Find("APT::Default-Release");
  155. string TmpSrc = Name;
  156. pkgDepCache *Cache = CacheFile.GetDepCache();
  157. // extract release
  158. size_t found = TmpSrc.find_last_of("/");
  159. if (found != string::npos)
  160. {
  161. RelTag = TmpSrc.substr(found+1);
  162. TmpSrc = TmpSrc.substr(0,found);
  163. }
  164. // extract the version
  165. found = TmpSrc.find_last_of("=");
  166. if (found != string::npos)
  167. {
  168. VerTag = UserRequestedVerTag = TmpSrc.substr(found+1);
  169. TmpSrc = TmpSrc.substr(0,found);
  170. }
  171. // extract arch
  172. found = TmpSrc.find_last_of(":");
  173. if (found != string::npos)
  174. {
  175. ArchTag = TmpSrc.substr(found+1);
  176. TmpSrc = TmpSrc.substr(0,found);
  177. }
  178. /* Lookup the version of the package we would install if we were to
  179. install a version and determine the source package name, then look
  180. in the archive for a source package of the same name. */
  181. bool MatchSrcOnly = _config->FindB("APT::Get::Only-Source");
  182. pkgCache::PkgIterator Pkg;
  183. if (ArchTag != "")
  184. Pkg = Cache->FindPkg(TmpSrc, ArchTag);
  185. else
  186. Pkg = Cache->FindPkg(TmpSrc);
  187. // if we can't find a package but the user qualified with a arch,
  188. // error out here
  189. if (Pkg.end() && ArchTag != "")
  190. {
  191. Src = Name;
  192. _error->Error(_("Can not find a package for architecture '%s'"),
  193. ArchTag.c_str());
  194. return 0;
  195. }
  196. if (MatchSrcOnly == false && Pkg.end() == false)
  197. {
  198. if(VerTag != "" || RelTag != "" || ArchTag != "")
  199. {
  200. bool fuzzy = false;
  201. // we have a default release, try to locate the pkg. we do it like
  202. // this because GetCandidateVer() will not "downgrade", that means
  203. // "apt-get source -t stable apt" won't work on a unstable system
  204. for (pkgCache::VerIterator Ver = Pkg.VersionList();; ++Ver)
  205. {
  206. // try first only exact matches, later fuzzy matches
  207. if (Ver.end() == true)
  208. {
  209. if (fuzzy == true)
  210. break;
  211. fuzzy = true;
  212. Ver = Pkg.VersionList();
  213. // exit right away from the Pkg.VersionList() loop if we
  214. // don't have any versions
  215. if (Ver.end() == true)
  216. break;
  217. }
  218. // ignore arches that are not for us
  219. if (ArchTag != "" && Ver.Arch() != ArchTag)
  220. continue;
  221. // pick highest version for the arch unless the user wants
  222. // something else
  223. if (ArchTag != "" && VerTag == "" && RelTag == "")
  224. if(Cache->VS().CmpVersion(VerTag, Ver.VerStr()) < 0)
  225. VerTag = Ver.VerStr();
  226. // We match against a concrete version (or a part of this version)
  227. if (VerTag.empty() == false &&
  228. (fuzzy == true || Cache->VS().CmpVersion(VerTag, Ver.VerStr()) != 0) && // exact match
  229. (fuzzy == false || strncmp(VerTag.c_str(), Ver.VerStr(), VerTag.size()) != 0)) // fuzzy match
  230. continue;
  231. for (pkgCache::VerFileIterator VF = Ver.FileList();
  232. VF.end() == false; ++VF)
  233. {
  234. /* If this is the status file, and the current version is not the
  235. version in the status file (ie it is not installed, or somesuch)
  236. then it is not a candidate for installation, ever. This weeds
  237. out bogus entries that may be due to config-file states, or
  238. other. */
  239. if ((VF.File()->Flags & pkgCache::Flag::NotSource) ==
  240. pkgCache::Flag::NotSource && Pkg.CurrentVer() != Ver)
  241. continue;
  242. // or we match against a release
  243. if(VerTag.empty() == false ||
  244. (VF.File().Archive() != 0 && VF.File().Archive() == RelTag) ||
  245. (VF.File().Codename() != 0 && VF.File().Codename() == RelTag))
  246. {
  247. // the Version we have is possibly fuzzy or includes binUploads,
  248. // so we use the Version of the SourcePkg (empty if same as package)
  249. Src = Ver.SourcePkgName();
  250. VerTag = Ver.SourceVerStr();
  251. break;
  252. }
  253. }
  254. if (Src.empty() == false)
  255. break;
  256. }
  257. }
  258. if (Src == "" && ArchTag != "")
  259. {
  260. if (VerTag != "")
  261. _error->Error(_("Can not find a package '%s' with version '%s'"),
  262. Pkg.FullName().c_str(), VerTag.c_str());
  263. if (RelTag != "")
  264. _error->Error(_("Can not find a package '%s' with release '%s'"),
  265. Pkg.FullName().c_str(), RelTag.c_str());
  266. Src = Name;
  267. return 0;
  268. }
  269. if (Src.empty() == true)
  270. {
  271. // if we don't have found a fitting package yet so we will
  272. // choose a good candidate and proceed with that.
  273. // Maybe we will find a source later on with the right VerTag
  274. // or RelTag
  275. pkgCache::VerIterator Ver = Cache->GetCandidateVer(Pkg);
  276. if (Ver.end() == false)
  277. {
  278. if (strcmp(Ver.SourcePkgName(),Ver.ParentPkg().Name()) != 0)
  279. Src = Ver.SourcePkgName();
  280. if (VerTag.empty() == true && strcmp(Ver.SourceVerStr(),Ver.VerStr()) != 0)
  281. VerTag = Ver.SourceVerStr();
  282. }
  283. }
  284. }
  285. if (Src.empty() == true)
  286. {
  287. Src = TmpSrc;
  288. }
  289. else
  290. {
  291. /* if we have a source pkg name, make sure to only search
  292. for srcpkg names, otherwise apt gets confused if there
  293. is a binary package "pkg1" and a source package "pkg1"
  294. with the same name but that comes from different packages */
  295. MatchSrcOnly = true;
  296. if (Src != TmpSrc)
  297. {
  298. ioprintf(c1out, _("Picking '%s' as source package instead of '%s'\n"), Src.c_str(), TmpSrc.c_str());
  299. }
  300. }
  301. // The best hit
  302. pkgSrcRecords::Parser *Last = 0;
  303. unsigned long Offset = 0;
  304. string Version;
  305. pkgSourceList *SrcList = CacheFile.GetSourceList();
  306. /* Iterate over all of the hits, which includes the resulting
  307. binary packages in the search */
  308. pkgSrcRecords::Parser *Parse;
  309. while (true)
  310. {
  311. SrcRecs.Restart();
  312. while ((Parse = SrcRecs.Find(Src.c_str(), MatchSrcOnly)) != 0)
  313. {
  314. const string Ver = Parse->Version();
  315. // See if we need to look for a specific release tag
  316. if (RelTag != "" && UserRequestedVerTag == "")
  317. {
  318. pkgCache::RlsFileIterator const Rls = GetReleaseFileForSourceRecord(CacheFile, SrcList, Parse);
  319. if (Rls.end() == false)
  320. {
  321. if ((Rls->Archive != 0 && RelTag == Rls.Archive()) ||
  322. (Rls->Codename != 0 && RelTag == Rls.Codename()))
  323. {
  324. Last = Parse;
  325. Offset = Parse->Offset();
  326. Version = Ver;
  327. }
  328. }
  329. }
  330. // Ignore all versions which doesn't fit
  331. if (VerTag.empty() == false &&
  332. Cache->VS().CmpVersion(VerTag, Ver) != 0) // exact match
  333. continue;
  334. // Newer version or an exact match? Save the hit
  335. if (Last == 0 || Cache->VS().CmpVersion(Version,Ver) < 0) {
  336. Last = Parse;
  337. Offset = Parse->Offset();
  338. Version = Ver;
  339. }
  340. // was the version check above an exact match?
  341. // If so, we don't need to look further
  342. if (VerTag.empty() == false && (VerTag == Ver))
  343. break;
  344. }
  345. if (UserRequestedVerTag == "" && Version != "" && RelTag != "")
  346. ioprintf(c1out, "Selected version '%s' (%s) for %s\n",
  347. Version.c_str(), RelTag.c_str(), Src.c_str());
  348. if (Last != 0 || VerTag.empty() == true)
  349. break;
  350. _error->Error(_("Can not find version '%s' of package '%s'"), VerTag.c_str(), TmpSrc.c_str());
  351. return 0;
  352. }
  353. if (Last == 0 || Last->Jump(Offset) == false)
  354. return 0;
  355. return Last;
  356. }
  357. /*}}}*/
  358. /* mark packages as automatically/manually installed. {{{*/
  359. static bool DoMarkAuto(CommandLine &CmdL)
  360. {
  361. bool Action = true;
  362. int AutoMarkChanged = 0;
  363. OpTextProgress progress;
  364. CacheFile Cache;
  365. if (Cache.Open() == false)
  366. return false;
  367. if (strcasecmp(CmdL.FileList[0],"markauto") == 0)
  368. Action = true;
  369. else if (strcasecmp(CmdL.FileList[0],"unmarkauto") == 0)
  370. Action = false;
  371. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  372. {
  373. const char *S = *I;
  374. // Locate the package
  375. pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
  376. if (Pkg.end() == true) {
  377. return _error->Error(_("Couldn't find package %s"),S);
  378. }
  379. else
  380. {
  381. if (!Action)
  382. ioprintf(c1out,_("%s set to manually installed.\n"), Pkg.Name());
  383. else
  384. ioprintf(c1out,_("%s set to automatically installed.\n"),
  385. Pkg.Name());
  386. Cache->MarkAuto(Pkg,Action);
  387. AutoMarkChanged++;
  388. }
  389. }
  390. _error->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
  391. if (AutoMarkChanged && ! _config->FindB("APT::Get::Simulate",false))
  392. return Cache->writeStateFile(NULL);
  393. return false;
  394. }
  395. /*}}}*/
  396. // DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
  397. // ---------------------------------------------------------------------
  398. /* Follows dselect's selections */
  399. static bool DoDSelectUpgrade(CommandLine &)
  400. {
  401. CacheFile Cache;
  402. if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
  403. return false;
  404. pkgDepCache::ActionGroup group(Cache);
  405. // Install everything with the install flag set
  406. pkgCache::PkgIterator I = Cache->PkgBegin();
  407. for (;I.end() != true; ++I)
  408. {
  409. /* Install the package only if it is a new install, the autoupgrader
  410. will deal with the rest */
  411. if (I->SelectedState == pkgCache::State::Install)
  412. Cache->MarkInstall(I,false);
  413. }
  414. /* Now install their deps too, if we do this above then order of
  415. the status file is significant for | groups */
  416. for (I = Cache->PkgBegin();I.end() != true; ++I)
  417. {
  418. /* Install the package only if it is a new install, the autoupgrader
  419. will deal with the rest */
  420. if (I->SelectedState == pkgCache::State::Install)
  421. Cache->MarkInstall(I,true);
  422. }
  423. // Apply erasures now, they override everything else.
  424. for (I = Cache->PkgBegin();I.end() != true; ++I)
  425. {
  426. // Remove packages
  427. if (I->SelectedState == pkgCache::State::DeInstall ||
  428. I->SelectedState == pkgCache::State::Purge)
  429. Cache->MarkDelete(I,I->SelectedState == pkgCache::State::Purge);
  430. }
  431. /* Resolve any problems that dselect created, allupgrade cannot handle
  432. such things. We do so quite aggressively too.. */
  433. if (Cache->BrokenCount() != 0)
  434. {
  435. pkgProblemResolver Fix(Cache);
  436. // Hold back held packages.
  437. if (_config->FindB("APT::Ignore-Hold",false) == false)
  438. {
  439. for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; ++I)
  440. {
  441. if (I->SelectedState == pkgCache::State::Hold)
  442. {
  443. Fix.Protect(I);
  444. Cache->MarkKeep(I);
  445. }
  446. }
  447. }
  448. if (Fix.Resolve() == false)
  449. {
  450. ShowBroken(c1out,Cache,false);
  451. return _error->Error(_("Internal error, problem resolver broke stuff"));
  452. }
  453. }
  454. // Now upgrade everything
  455. if (APT::Upgrade::Upgrade(Cache, APT::Upgrade::FORBID_REMOVE_PACKAGES | APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES) == false)
  456. {
  457. ShowBroken(c1out,Cache,false);
  458. return _error->Error(_("Internal error, problem resolver broke stuff"));
  459. }
  460. return InstallPackages(Cache,false);
  461. }
  462. /*}}}*/
  463. // DoClean - Remove download archives /*{{{*/
  464. // ---------------------------------------------------------------------
  465. /* */
  466. static bool DoClean(CommandLine &)
  467. {
  468. std::string const archivedir = _config->FindDir("Dir::Cache::archives");
  469. std::string const listsdir = _config->FindDir("Dir::state::lists");
  470. if (_config->FindB("APT::Get::Simulate") == true)
  471. {
  472. std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
  473. std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
  474. cout << "Del " << archivedir << "* " << archivedir << "partial/*"<< endl
  475. << "Del " << listsdir << "partial/*" << endl
  476. << "Del " << pkgcache << " " << srcpkgcache << endl;
  477. return true;
  478. }
  479. pkgAcquire Fetcher;
  480. Fetcher.GetLock(archivedir);
  481. Fetcher.Clean(archivedir);
  482. Fetcher.Clean(archivedir + "partial/");
  483. Fetcher.GetLock(listsdir);
  484. Fetcher.Clean(listsdir + "partial/");
  485. pkgCacheFile::RemoveCaches();
  486. return true;
  487. }
  488. /*}}}*/
  489. // DoAutoClean - Smartly remove downloaded archives /*{{{*/
  490. // ---------------------------------------------------------------------
  491. /* This is similar to clean but it only purges things that cannot be
  492. downloaded, that is old versions of cached packages. */
  493. class LogCleaner : public pkgArchiveCleaner
  494. {
  495. protected:
  496. virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St) APT_OVERRIDE
  497. {
  498. c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << endl;
  499. if (_config->FindB("APT::Get::Simulate") == false)
  500. unlink(File);
  501. };
  502. };
  503. static bool DoAutoClean(CommandLine &)
  504. {
  505. // Lock the archive directory
  506. FileFd Lock;
  507. if (_config->FindB("Debug::NoLocking",false) == false)
  508. {
  509. int lock_fd = GetLock(_config->FindDir("Dir::Cache::Archives") + "lock");
  510. if (lock_fd < 0)
  511. return _error->Error(_("Unable to lock the download directory"));
  512. Lock.Fd(lock_fd);
  513. }
  514. CacheFile Cache;
  515. if (Cache.Open() == false)
  516. return false;
  517. LogCleaner Cleaner;
  518. return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
  519. Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
  520. }
  521. /*}}}*/
  522. // DoDownload - download a binary /*{{{*/
  523. // ---------------------------------------------------------------------
  524. static bool DoDownload(CommandLine &CmdL)
  525. {
  526. CacheFile Cache;
  527. if (Cache.ReadOnlyOpen() == false)
  528. return false;
  529. APT::CacheSetHelper helper;
  530. APT::VersionSet verset = APT::VersionSet::FromCommandLine(Cache,
  531. CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper);
  532. if (verset.empty() == true)
  533. return false;
  534. pkgRecords Recs(Cache);
  535. pkgSourceList *SrcList = Cache.GetSourceList();
  536. // reuse the usual acquire methods for deb files, but don't drop them into
  537. // the usual directories - keep everything in the current directory
  538. aptAcquireWithTextStatus Fetcher;
  539. std::vector<std::string> storefile(verset.size());
  540. std::string const cwd = SafeGetCWD();
  541. _config->Set("Dir::Cache::Archives", cwd);
  542. int i = 0;
  543. for (APT::VersionSet::const_iterator Ver = verset.begin();
  544. Ver != verset.end(); ++Ver, ++i)
  545. {
  546. pkgAcquire::Item *I = new pkgAcqArchive(&Fetcher, SrcList, &Recs, *Ver, storefile[i]);
  547. if (storefile[i].empty())
  548. continue;
  549. std::string const filename = cwd + flNotDir(storefile[i]);
  550. storefile[i].assign(filename);
  551. I->DestFile.assign(filename);
  552. }
  553. // Just print out the uris and exit if the --print-uris flag was used
  554. if (_config->FindB("APT::Get::Print-URIs") == true)
  555. {
  556. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  557. for (; I != Fetcher.UriEnd(); ++I)
  558. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  559. I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
  560. return true;
  561. }
  562. if (_error->PendingError() == true || CheckAuth(Fetcher, false) == false)
  563. return false;
  564. bool Failed = false;
  565. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false)
  566. return false;
  567. // copy files in local sources to the current directory
  568. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I)
  569. {
  570. std::string const filename = cwd + flNotDir((*I)->DestFile);
  571. if ((*I)->Local == true &&
  572. filename != (*I)->DestFile &&
  573. (*I)->Status == pkgAcquire::Item::StatDone)
  574. {
  575. std::ifstream src((*I)->DestFile.c_str(), std::ios::binary);
  576. std::ofstream dst(filename.c_str(), std::ios::binary);
  577. dst << src.rdbuf();
  578. }
  579. }
  580. return Failed == false;
  581. }
  582. /*}}}*/
  583. // DoCheck - Perform the check operation /*{{{*/
  584. // ---------------------------------------------------------------------
  585. /* Opening automatically checks the system, this command is mostly used
  586. for debugging */
  587. static bool DoCheck(CommandLine &)
  588. {
  589. CacheFile Cache;
  590. Cache.Open();
  591. Cache.CheckDeps();
  592. return true;
  593. }
  594. /*}}}*/
  595. // DoSource - Fetch a source archive /*{{{*/
  596. // ---------------------------------------------------------------------
  597. /* Fetch souce packages */
  598. struct DscFile
  599. {
  600. string Package;
  601. string Version;
  602. string Dsc;
  603. };
  604. static bool DoSource(CommandLine &CmdL)
  605. {
  606. CacheFile Cache;
  607. if (Cache.Open(false) == false)
  608. return false;
  609. if (CmdL.FileSize() <= 1)
  610. return _error->Error(_("Must specify at least one package to fetch source for"));
  611. // Read the source list
  612. if (Cache.BuildSourceList() == false)
  613. return false;
  614. pkgSourceList *List = Cache.GetSourceList();
  615. // Create the text record parsers
  616. pkgSrcRecords SrcRecs(*List);
  617. if (_error->PendingError() == true)
  618. return false;
  619. std::unique_ptr<DscFile[]> Dsc(new DscFile[CmdL.FileSize()]);
  620. // insert all downloaded uris into this set to avoid downloading them
  621. // twice
  622. set<string> queued;
  623. // Diff only mode only fetches .diff files
  624. bool const diffOnly = _config->FindB("APT::Get::Diff-Only", false);
  625. // Tar only mode only fetches .tar files
  626. bool const tarOnly = _config->FindB("APT::Get::Tar-Only", false);
  627. // Dsc only mode only fetches .dsc files
  628. bool const dscOnly = _config->FindB("APT::Get::Dsc-Only", false);
  629. // Load the requestd sources into the fetcher
  630. aptAcquireWithTextStatus Fetcher;
  631. unsigned J = 0;
  632. std::vector<std::string> UntrustedList;
  633. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  634. {
  635. string Src;
  636. pkgSrcRecords::Parser *Last = FindSrc(*I,SrcRecs,Src,Cache);
  637. if (Last == 0) {
  638. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  639. }
  640. if (Last->Index().IsTrusted() == false)
  641. UntrustedList.push_back(Src);
  642. string srec = Last->AsStr();
  643. string::size_type pos = srec.find("\nVcs-");
  644. while (pos != string::npos)
  645. {
  646. pos += strlen("\nVcs-");
  647. string vcs = srec.substr(pos,srec.find(":",pos)-pos);
  648. if(vcs == "Browser")
  649. {
  650. pos = srec.find("\nVcs-", pos);
  651. continue;
  652. }
  653. pos += vcs.length()+2;
  654. string::size_type epos = srec.find("\n", pos);
  655. string const uri = srec.substr(pos,epos-pos);
  656. ioprintf(c1out, _("NOTICE: '%s' packaging is maintained in "
  657. "the '%s' version control system at:\n"
  658. "%s\n"),
  659. Src.c_str(), vcs.c_str(), uri.c_str());
  660. std::string vcscmd;
  661. if (vcs == "Bzr")
  662. vcscmd = "bzr branch " + uri;
  663. else if (vcs == "Git")
  664. vcscmd = "git clone " + uri;
  665. if (vcscmd.empty() == false)
  666. ioprintf(c1out,_("Please use:\n%s\n"
  667. "to retrieve the latest (possibly unreleased) "
  668. "updates to the package.\n"),
  669. vcscmd.c_str());
  670. break;
  671. }
  672. // Back track
  673. vector<pkgSrcRecords::File2> Lst;
  674. if (Last->Files2(Lst) == false) {
  675. return false;
  676. }
  677. // Load them into the fetcher
  678. for (vector<pkgSrcRecords::File2>::const_iterator I = Lst.begin();
  679. I != Lst.end(); ++I)
  680. {
  681. // Try to guess what sort of file it is we are getting.
  682. if (I->Type == "dsc")
  683. {
  684. Dsc[J].Package = Last->Package();
  685. Dsc[J].Version = Last->Version();
  686. Dsc[J].Dsc = flNotDir(I->Path);
  687. }
  688. // Handle the only options so that multiple can be used at once
  689. if (diffOnly == true || tarOnly == true || dscOnly == true)
  690. {
  691. if ((diffOnly == true && I->Type == "diff") ||
  692. (tarOnly == true && I->Type == "tar") ||
  693. (dscOnly == true && I->Type == "dsc"))
  694. ; // Fine, we want this file downloaded
  695. else
  696. continue;
  697. }
  698. // don't download the same uri twice (should this be moved to
  699. // the fetcher interface itself?)
  700. if(queued.find(Last->Index().ArchiveURI(I->Path)) != queued.end())
  701. continue;
  702. queued.insert(Last->Index().ArchiveURI(I->Path));
  703. // check if we have a file with that md5 sum already localy
  704. std::string localFile = flNotDir(I->Path);
  705. if (FileExists(localFile) == true)
  706. if(I->Hashes.VerifyFile(localFile) == true)
  707. {
  708. ioprintf(c1out,_("Skipping already downloaded file '%s'\n"),
  709. localFile.c_str());
  710. continue;
  711. }
  712. // see if we have a hash (Acquire::ForceHash is the only way to have none)
  713. if (I->Hashes.usable() == false && _config->FindB("APT::Get::AllowUnauthenticated",false) == false)
  714. {
  715. ioprintf(c1out, "Skipping download of file '%s' as requested hashsum is not available for authentication\n",
  716. localFile.c_str());
  717. continue;
  718. }
  719. new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
  720. I->Hashes, I->FileSize, Last->Index().SourceInfo(*Last,*I), Src);
  721. }
  722. }
  723. // Display statistics
  724. unsigned long long FetchBytes = Fetcher.FetchNeeded();
  725. unsigned long long FetchPBytes = Fetcher.PartialPresent();
  726. unsigned long long DebBytes = Fetcher.TotalNeeded();
  727. if (CheckFreeSpaceBeforeDownload(".", (FetchBytes - FetchPBytes)) == false)
  728. return false;
  729. // Number of bytes
  730. if (DebBytes != FetchBytes)
  731. //TRANSLATOR: The required space between number and unit is already included
  732. // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
  733. ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
  734. SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
  735. else
  736. //TRANSLATOR: The required space between number and unit is already included
  737. // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
  738. ioprintf(c1out,_("Need to get %sB of source archives.\n"),
  739. SizeToStr(DebBytes).c_str());
  740. if (_config->FindB("APT::Get::Simulate",false) == true)
  741. {
  742. for (unsigned I = 0; I != J; I++)
  743. ioprintf(cout,_("Fetch source %s\n"),Dsc[I].Package.c_str());
  744. return true;
  745. }
  746. // Just print out the uris an exit if the --print-uris flag was used
  747. if (_config->FindB("APT::Get::Print-URIs") == true)
  748. {
  749. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  750. for (; I != Fetcher.UriEnd(); ++I)
  751. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  752. I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
  753. return true;
  754. }
  755. // check authentication status of the source as well
  756. if (UntrustedList.empty() == false && AuthPrompt(UntrustedList, false) == false)
  757. return false;
  758. // Run it
  759. bool Failed = false;
  760. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
  761. {
  762. return _error->Error(_("Failed to fetch some archives."));
  763. }
  764. if (_config->FindB("APT::Get::Download-only",false) == true)
  765. {
  766. c1out << _("Download complete and in download only mode") << endl;
  767. return true;
  768. }
  769. // Unpack the sources
  770. pid_t Process = ExecFork();
  771. if (Process == 0)
  772. {
  773. bool const fixBroken = _config->FindB("APT::Get::Fix-Broken", false);
  774. for (unsigned I = 0; I != J; ++I)
  775. {
  776. string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
  777. // Diff only mode only fetches .diff files
  778. if (_config->FindB("APT::Get::Diff-Only",false) == true ||
  779. _config->FindB("APT::Get::Tar-Only",false) == true ||
  780. Dsc[I].Dsc.empty() == true)
  781. continue;
  782. // See if the package is already unpacked
  783. struct stat Stat;
  784. if (fixBroken == false && stat(Dir.c_str(),&Stat) == 0 &&
  785. S_ISDIR(Stat.st_mode) != 0)
  786. {
  787. ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
  788. Dir.c_str());
  789. }
  790. else
  791. {
  792. // Call dpkg-source
  793. std::string const sourceopts = _config->Find("DPkg::Source-Options", "-x");
  794. std::string S;
  795. strprintf(S, "%s %s %s",
  796. _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
  797. sourceopts.c_str(), Dsc[I].Dsc.c_str());
  798. if (system(S.c_str()) != 0)
  799. {
  800. fprintf(stderr, _("Unpack command '%s' failed.\n"), S.c_str());
  801. fprintf(stderr, _("Check if the 'dpkg-dev' package is installed.\n"));
  802. _exit(1);
  803. }
  804. }
  805. // Try to compile it with dpkg-buildpackage
  806. if (_config->FindB("APT::Get::Compile",false) == true)
  807. {
  808. string buildopts = _config->Find("APT::Get::Host-Architecture");
  809. if (buildopts.empty() == false)
  810. buildopts = "-a" + buildopts + " ";
  811. // get all active build profiles
  812. std::string const profiles = APT::Configuration::getBuildProfilesString();
  813. if (profiles.empty() == false)
  814. buildopts.append(" -P").append(profiles).append(" ");
  815. buildopts.append(_config->Find("DPkg::Build-Options","-b -uc"));
  816. // Call dpkg-buildpackage
  817. std::string S;
  818. strprintf(S, "cd %s && %s %s",
  819. Dir.c_str(),
  820. _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
  821. buildopts.c_str());
  822. if (system(S.c_str()) != 0)
  823. {
  824. fprintf(stderr, _("Build command '%s' failed.\n"), S.c_str());
  825. _exit(1);
  826. }
  827. }
  828. }
  829. _exit(0);
  830. }
  831. return ExecWait(Process, "dpkg-source");
  832. }
  833. /*}}}*/
  834. // DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
  835. // ---------------------------------------------------------------------
  836. /* This function will look at the build depends list of the given source
  837. package and install the necessary packages to make it true, or fail. */
  838. static bool DoBuildDep(CommandLine &CmdL)
  839. {
  840. CacheFile Cache;
  841. _config->Set("APT::Install-Recommends", false);
  842. bool WantLock = _config->FindB("APT::Get::Print-URIs", false) == false;
  843. if (Cache.Open(WantLock) == false)
  844. return false;
  845. if (CmdL.FileSize() <= 1)
  846. return _error->Error(_("Must specify at least one package to check builddeps for"));
  847. // Read the source list
  848. if (Cache.BuildSourceList() == false)
  849. return false;
  850. pkgSourceList *List = Cache.GetSourceList();
  851. // Create the text record parsers
  852. pkgSrcRecords SrcRecs(*List);
  853. if (_error->PendingError() == true)
  854. return false;
  855. bool StripMultiArch;
  856. string hostArch = _config->Find("APT::Get::Host-Architecture");
  857. if (hostArch.empty() == false)
  858. {
  859. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  860. if (std::find(archs.begin(), archs.end(), hostArch) == archs.end())
  861. return _error->Error(_("No architecture information available for %s. See apt.conf(5) APT::Architectures for setup"), hostArch.c_str());
  862. StripMultiArch = false;
  863. }
  864. else
  865. StripMultiArch = true;
  866. unsigned J = 0;
  867. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  868. {
  869. string Src;
  870. pkgSrcRecords::Parser *Last = 0;
  871. std::unique_ptr<pkgSrcRecords::Parser> LastOwner;
  872. // an unpacked debian source tree
  873. using APT::String::Startswith;
  874. if ((Startswith(*I, "./") || Startswith(*I, "/")) &&
  875. DirectoryExists(*I))
  876. {
  877. ioprintf(c1out, _("Note, using directory '%s' to get the build dependencies\n"), *I);
  878. // FIXME: how can we make this more elegant?
  879. std::string TypeName = "Debian control file";
  880. pkgIndexFile::Type *Type = pkgIndexFile::Type::GetType(TypeName.c_str());
  881. if(Type != NULL)
  882. LastOwner.reset(Last = Type->CreateSrcPkgParser(*I));
  883. }
  884. // if its a local file (e.g. .dsc) use this
  885. else if (FileExists(*I))
  886. {
  887. ioprintf(c1out, _("Note, using file '%s' to get the build dependencies\n"), *I);
  888. // see if we can get a parser for this pkgIndexFile type
  889. string TypeName = "Debian " + flExtension(*I) + " file";
  890. pkgIndexFile::Type *Type = pkgIndexFile::Type::GetType(TypeName.c_str());
  891. if(Type != NULL)
  892. LastOwner.reset(Last = Type->CreateSrcPkgParser(*I));
  893. } else {
  894. // normal case, search the cache for the source file
  895. Last = FindSrc(*I,SrcRecs,Src,Cache);
  896. }
  897. if (Last == 0)
  898. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  899. // Process the build-dependencies
  900. vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
  901. // FIXME: Can't specify architecture to use for [wildcard] matching, so switch default arch temporary
  902. if (hostArch.empty() == false)
  903. {
  904. std::string nativeArch = _config->Find("APT::Architecture");
  905. _config->Set("APT::Architecture", hostArch);
  906. bool Success = Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch);
  907. _config->Set("APT::Architecture", nativeArch);
  908. if (Success == false)
  909. return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
  910. }
  911. else if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
  912. return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
  913. // Also ensure that build-essential packages are present
  914. Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
  915. if (Opts)
  916. Opts = Opts->Child;
  917. for (; Opts; Opts = Opts->Next)
  918. {
  919. if (Opts->Value.empty() == true)
  920. continue;
  921. pkgSrcRecords::Parser::BuildDepRec rec;
  922. rec.Package = Opts->Value;
  923. rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
  924. rec.Op = 0;
  925. BuildDeps.push_back(rec);
  926. }
  927. if (BuildDeps.empty() == true)
  928. {
  929. ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
  930. continue;
  931. }
  932. // Install the requested packages
  933. vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
  934. pkgProblemResolver Fix(Cache);
  935. bool skipAlternatives = false; // skip remaining alternatives in an or group
  936. for (D = BuildDeps.begin(); D != BuildDeps.end(); ++D)
  937. {
  938. bool hasAlternatives = (((*D).Op & pkgCache::Dep::Or) == pkgCache::Dep::Or);
  939. if (skipAlternatives == true)
  940. {
  941. /*
  942. * if there are alternatives, we've already picked one, so skip
  943. * the rest
  944. *
  945. * TODO: this means that if there's a build-dep on A|B and B is
  946. * installed, we'll still try to install A; more importantly,
  947. * if A is currently broken, we cannot go back and try B. To fix
  948. * this would require we do a Resolve cycle for each package we
  949. * add to the install list. Ugh
  950. */
  951. if (!hasAlternatives)
  952. skipAlternatives = false; // end of or group
  953. continue;
  954. }
  955. if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
  956. (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
  957. {
  958. pkgCache::GrpIterator Grp = Cache->FindGrp((*D).Package);
  959. // Build-conflicts on unknown packages are silently ignored
  960. if (Grp.end() == true)
  961. continue;
  962. for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
  963. {
  964. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  965. /*
  966. * Remove if we have an installed version that satisfies the
  967. * version criteria
  968. */
  969. if (IV.end() == false &&
  970. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  971. TryToInstallBuildDep(Pkg,Cache,Fix,true,false);
  972. }
  973. }
  974. else // BuildDep || BuildDepIndep
  975. {
  976. if (_config->FindB("Debug::BuildDeps",false) == true)
  977. cout << "Looking for " << (*D).Package << "...\n";
  978. pkgCache::PkgIterator Pkg;
  979. // Cross-Building?
  980. if (StripMultiArch == false && D->Type != pkgSrcRecords::Parser::BuildDependIndep)
  981. {
  982. size_t const colon = D->Package.find(":");
  983. if (colon != string::npos)
  984. {
  985. if (strcmp(D->Package.c_str() + colon, ":any") == 0 || strcmp(D->Package.c_str() + colon, ":native") == 0)
  986. Pkg = Cache->FindPkg(D->Package.substr(0,colon));
  987. else
  988. Pkg = Cache->FindPkg(D->Package);
  989. }
  990. else
  991. Pkg = Cache->FindPkg(D->Package, hostArch);
  992. // a bad version either is invalid or doesn't satify dependency
  993. #define BADVER(Ver) (Ver.end() == true || \
  994. (D->Version.empty() == false && \
  995. Cache->VS().CheckDep(Ver.VerStr(),D->Op,D->Version.c_str()) == false))
  996. APT::VersionList verlist;
  997. if (Pkg.end() == false)
  998. {
  999. pkgCache::VerIterator Ver = (*Cache)[Pkg].InstVerIter(*Cache);
  1000. if (BADVER(Ver) == false)
  1001. verlist.insert(Ver);
  1002. Ver = (*Cache)[Pkg].CandidateVerIter(*Cache);
  1003. if (BADVER(Ver) == false)
  1004. verlist.insert(Ver);
  1005. }
  1006. if (verlist.empty() == true)
  1007. {
  1008. pkgCache::PkgIterator BuildPkg = Cache->FindPkg(D->Package, "native");
  1009. if (BuildPkg.end() == false && Pkg != BuildPkg)
  1010. {
  1011. pkgCache::VerIterator Ver = (*Cache)[BuildPkg].InstVerIter(*Cache);
  1012. if (BADVER(Ver) == false)
  1013. verlist.insert(Ver);
  1014. Ver = (*Cache)[BuildPkg].CandidateVerIter(*Cache);
  1015. if (BADVER(Ver) == false)
  1016. verlist.insert(Ver);
  1017. }
  1018. }
  1019. #undef BADVER
  1020. string forbidden;
  1021. // We need to decide if host or build arch, so find a version we can look at
  1022. APT::VersionList::const_iterator Ver = verlist.begin();
  1023. for (; Ver != verlist.end(); ++Ver)
  1024. {
  1025. forbidden.clear();
  1026. if (Ver->MultiArch == pkgCache::Version::No || Ver->MultiArch == pkgCache::Version::All)
  1027. {
  1028. if (colon == string::npos)
  1029. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  1030. else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
  1031. forbidden = "Multi-Arch: no";
  1032. else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
  1033. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  1034. }
  1035. else if (Ver->MultiArch == pkgCache::Version::Same)
  1036. {
  1037. if (colon == string::npos)
  1038. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  1039. else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
  1040. forbidden = "Multi-Arch: same";
  1041. else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
  1042. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  1043. }
  1044. else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
  1045. {
  1046. if (colon == string::npos)
  1047. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  1048. else if (strcmp(D->Package.c_str() + colon, ":any") == 0 ||
  1049. strcmp(D->Package.c_str() + colon, ":native") == 0)
  1050. forbidden = "Multi-Arch: foreign";
  1051. }
  1052. else if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
  1053. {
  1054. if (colon == string::npos)
  1055. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  1056. else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
  1057. {
  1058. // prefer any installed over preferred non-installed architectures
  1059. pkgCache::GrpIterator Grp = Ver.ParentPkg().Group();
  1060. // we don't check for version here as we are better of with upgrading than remove and install
  1061. for (Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
  1062. if (Pkg.CurrentVer().end() == false)
  1063. break;
  1064. if (Pkg.end() == true)
  1065. Pkg = Grp.FindPreferredPkg(true);
  1066. }
  1067. else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
  1068. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  1069. }
  1070. if (forbidden.empty() == false)
  1071. {
  1072. if (_config->FindB("Debug::BuildDeps",false) == true)
  1073. cout << D->Package.substr(colon, string::npos) << " is not allowed from " << forbidden << " package " << (*D).Package << " (" << Ver.VerStr() << ")" << endl;
  1074. continue;
  1075. }
  1076. //we found a good version
  1077. break;
  1078. }
  1079. if (Ver == verlist.end())
  1080. {
  1081. if (_config->FindB("Debug::BuildDeps",false) == true)
  1082. cout << " No multiarch info as we have no satisfying installed nor candidate for " << D->Package << " on build or host arch" << endl;
  1083. if (forbidden.empty() == false)
  1084. {
  1085. if (hasAlternatives)
  1086. continue;
  1087. return _error->Error(_("%s dependency for %s can't be satisfied "
  1088. "because %s is not allowed on '%s' packages"),
  1089. Last->BuildDepType(D->Type), Src.c_str(),
  1090. D->Package.c_str(), forbidden.c_str());
  1091. }
  1092. }
  1093. }
  1094. else
  1095. Pkg = Cache->FindPkg(D->Package);
  1096. if (Pkg.end() == true || (Pkg->VersionList == 0 && Pkg->ProvidesList == 0))
  1097. {
  1098. if (_config->FindB("Debug::BuildDeps",false) == true)
  1099. cout << " (not found)" << (*D).Package << endl;
  1100. if (hasAlternatives)
  1101. continue;
  1102. return _error->Error(_("%s dependency for %s cannot be satisfied "
  1103. "because the package %s cannot be found"),
  1104. Last->BuildDepType((*D).Type),Src.c_str(),
  1105. (*D).Package.c_str());
  1106. }
  1107. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  1108. if (IV.end() == false)
  1109. {
  1110. if (_config->FindB("Debug::BuildDeps",false) == true)
  1111. cout << " Is installed\n";
  1112. if (D->Version.empty() == true ||
  1113. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  1114. {
  1115. skipAlternatives = hasAlternatives;
  1116. continue;
  1117. }
  1118. if (_config->FindB("Debug::BuildDeps",false) == true)
  1119. cout << " ...but the installed version doesn't meet the version requirement\n";
  1120. if (((*D).Op & pkgCache::Dep::LessEq) == pkgCache::Dep::LessEq)
  1121. return _error->Error(_("Failed to satisfy %s dependency for %s: Installed package %s is too new"),
  1122. Last->BuildDepType((*D).Type), Src.c_str(), Pkg.FullName(true).c_str());
  1123. }
  1124. // Only consider virtual packages if there is no versioned dependency
  1125. if ((*D).Version.empty() == true)
  1126. {
  1127. /*
  1128. * If this is a virtual package, we need to check the list of
  1129. * packages that provide it and see if any of those are
  1130. * installed
  1131. */
  1132. pkgCache::PrvIterator Prv = Pkg.ProvidesList();
  1133. for (; Prv.end() != true; ++Prv)
  1134. {
  1135. if (_config->FindB("Debug::BuildDeps",false) == true)
  1136. cout << " Checking provider " << Prv.OwnerPkg().FullName() << endl;
  1137. if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
  1138. break;
  1139. }
  1140. if (Prv.end() == false)
  1141. {
  1142. if (_config->FindB("Debug::BuildDeps",false) == true)
  1143. cout << " Is provided by installed package " << Prv.OwnerPkg().FullName() << endl;
  1144. skipAlternatives = hasAlternatives;
  1145. continue;
  1146. }
  1147. }
  1148. else // versioned dependency
  1149. {
  1150. pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
  1151. if (CV.end() == true ||
  1152. Cache->VS().CheckDep(CV.VerStr(),(*D).Op,(*D).Version.c_str()) == false)
  1153. {
  1154. if (hasAlternatives)
  1155. continue;
  1156. else if (CV.end() == false)
  1157. return _error->Error(_("%s dependency for %s cannot be satisfied "
  1158. "because candidate version of package %s "
  1159. "can't satisfy version requirements"),
  1160. Last->BuildDepType(D->Type), Src.c_str(),
  1161. D->Package.c_str());
  1162. else
  1163. return _error->Error(_("%s dependency for %s cannot be satisfied "
  1164. "because package %s has no candidate version"),
  1165. Last->BuildDepType(D->Type), Src.c_str(),
  1166. D->Package.c_str());
  1167. }
  1168. }
  1169. if (TryToInstallBuildDep(Pkg,Cache,Fix,false,false,false) == true)
  1170. {
  1171. // We successfully installed something; skip remaining alternatives
  1172. skipAlternatives = hasAlternatives;
  1173. if(_config->FindB("APT::Get::Build-Dep-Automatic", false) == true)
  1174. Cache->MarkAuto(Pkg, true);
  1175. continue;
  1176. }
  1177. else if (hasAlternatives)
  1178. {
  1179. if (_config->FindB("Debug::BuildDeps",false) == true)
  1180. cout << " Unsatisfiable, trying alternatives\n";
  1181. continue;
  1182. }
  1183. else
  1184. {
  1185. return _error->Error(_("Failed to satisfy %s dependency for %s: %s"),
  1186. Last->BuildDepType((*D).Type),
  1187. Src.c_str(),
  1188. (*D).Package.c_str());
  1189. }
  1190. }
  1191. }
  1192. if (Fix.Resolve(true) == false)
  1193. _error->Discard();
  1194. // Now we check the state of the packages,
  1195. if (Cache->BrokenCount() != 0)
  1196. {
  1197. ShowBroken(cout, Cache, false);
  1198. return _error->Error(_("Build-dependencies for %s could not be satisfied."),*I);
  1199. }
  1200. }
  1201. if (InstallPackages(Cache, false, true) == false)
  1202. return _error->Error(_("Failed to process build dependencies"));
  1203. return true;
  1204. }
  1205. /*}}}*/
  1206. // DoChangelog - Get changelog from the command line /*{{{*/
  1207. static bool DoChangelog(CommandLine &CmdL)
  1208. {
  1209. CacheFile Cache;
  1210. if (Cache.ReadOnlyOpen() == false)
  1211. return false;
  1212. APT::CacheSetHelper helper;
  1213. APT::VersionList verset = APT::VersionList::FromCommandLine(Cache,
  1214. CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper);
  1215. if (verset.empty() == true)
  1216. return false;
  1217. bool const downOnly = _config->FindB("APT::Get::Download-Only", false);
  1218. bool const printOnly = _config->FindB("APT::Get::Print-URIs", false);
  1219. aptAcquireWithTextStatus Fetcher;
  1220. for (APT::VersionList::const_iterator Ver = verset.begin();
  1221. Ver != verset.end();
  1222. ++Ver)
  1223. {
  1224. if (printOnly)
  1225. new pkgAcqChangelog(&Fetcher, Ver, "/dev/null");
  1226. else if (downOnly)
  1227. new pkgAcqChangelog(&Fetcher, Ver, ".");
  1228. else
  1229. new pkgAcqChangelog(&Fetcher, Ver);
  1230. }
  1231. if (printOnly == false)
  1232. {
  1233. bool Failed = false;
  1234. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
  1235. return false;
  1236. }
  1237. if (downOnly == false || printOnly == true)
  1238. {
  1239. bool Failed = false;
  1240. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I)
  1241. {
  1242. if (printOnly)
  1243. {
  1244. if ((*I)->ErrorText.empty() == false)
  1245. {
  1246. Failed = true;
  1247. _error->Error("%s", (*I)->ErrorText.c_str());
  1248. }
  1249. else
  1250. cout << '\'' << (*I)->DescURI() << "' " << flNotDir((*I)->DestFile) << std::endl;
  1251. }
  1252. else
  1253. DisplayFileInPager((*I)->DestFile);
  1254. }
  1255. return Failed == false;
  1256. }
  1257. return true;
  1258. }
  1259. /*}}}*/
  1260. // DoIndexTargets - Lists all IndexTargets /*{{{*/
  1261. static std::string format_key(std::string key)
  1262. {
  1263. // deb822 is case-insensitive, but the human eye prefers candy
  1264. std::transform(key.begin(), key.end(), key.begin(), ::tolower);
  1265. key[0] = ::toupper(key[0]);
  1266. size_t found = key.find("_uri");
  1267. if (found != std::string::npos)
  1268. key.replace(found, 4, "-URI");
  1269. while ((found = key.find('_')) != std::string::npos)
  1270. {
  1271. key[found] = '-';
  1272. key[found + 1] = ::toupper(key[found + 1]);
  1273. }
  1274. return key;
  1275. }
  1276. static bool DoIndexTargets(CommandLine &CmdL)
  1277. {
  1278. pkgCacheFile CacheFile;
  1279. pkgSourceList *SrcList = CacheFile.GetSourceList();
  1280. if (SrcList == NULL)
  1281. return false;
  1282. std::string const Format = _config->Find("APT::Get::IndexTargets::Format");
  1283. bool const ReleaseInfo = _config->FindB("APT::Get::IndexTargets::ReleaseInfo", true);
  1284. bool Filtered = CmdL.FileSize() > 1;
  1285. for (pkgSourceList::const_iterator S = SrcList->begin(); S != SrcList->end(); ++S)
  1286. {
  1287. std::vector<IndexTarget> const targets = (*S)->GetIndexTargets();
  1288. std::map<std::string, string> AddOptions;
  1289. if (ReleaseInfo)
  1290. {
  1291. AddOptions.insert(std::make_pair("TRUSTED", ((*S)->IsTrusted() ? "yes" : "no")));
  1292. pkgCache &Cache = *CacheFile.GetPkgCache();
  1293. pkgCache::RlsFileIterator const RlsFile = (*S)->FindInCache(Cache, false);
  1294. if (RlsFile.end())
  1295. continue;
  1296. #define APT_RELEASE(X,Y) if (RlsFile.Y() != NULL) AddOptions.insert(std::make_pair(X, RlsFile.Y()))
  1297. APT_RELEASE("CODENAME", Codename);
  1298. APT_RELEASE("SUITE", Archive);
  1299. APT_RELEASE("VERSION", Version);
  1300. APT_RELEASE("ORIGIN", Origin);
  1301. APT_RELEASE("LABEL", Label);
  1302. #undef APT_RELEASE
  1303. }
  1304. for (std::vector<IndexTarget>::const_iterator T = targets.begin(); T != targets.end(); ++T)
  1305. {
  1306. std::string filename = T->Option(ReleaseInfo ? IndexTarget::EXISTING_FILENAME : IndexTarget::FILENAME);
  1307. if (filename.empty())
  1308. continue;
  1309. std::ostringstream stanza;
  1310. if (Filtered || Format.empty())
  1311. {
  1312. stanza << "MetaKey: " << T->MetaKey << "\n"
  1313. << "ShortDesc: " << T->ShortDesc << "\n"
  1314. << "Description: " << T->Description << "\n"
  1315. << "URI: " << T->URI << "\n"
  1316. << "Filename: " << filename << "\n"
  1317. << "Optional: " << (T->IsOptional ? "yes" : "no") << "\n"
  1318. << "KeepCompressed: " << (T->KeepCompressed ? "yes" : "no") << "\n";
  1319. for (std::map<std::string,std::string>::const_iterator O = AddOptions.begin(); O != AddOptions.end(); ++O)
  1320. stanza << format_key(O->first) << ": " << O->second << "\n";
  1321. for (std::map<std::string,std::string>::const_iterator O = T->Options.begin(); O != T->Options.end(); ++O)
  1322. {
  1323. if (O->first == "PDIFFS")
  1324. stanza << "PDiffs: " << O->second << "\n";
  1325. else if (O->first == "COMPRESSIONTYPES")
  1326. stanza << "CompressionTypes: " << O->second << "\n";
  1327. else if (O->first == "DEFAULTENABLED")
  1328. stanza << "DefaultEnabled: " << O->second << "\n";
  1329. else
  1330. stanza << format_key(O->first) << ": " << O->second << "\n";
  1331. }
  1332. stanza << "\n";
  1333. if (Filtered)
  1334. {
  1335. // that is a bit crude, but good enough for now
  1336. bool found = true;
  1337. std::string haystack = std::string("\n") + stanza.str() + "\n";
  1338. std::transform(haystack.begin(), haystack.end(), haystack.begin(), ::tolower);
  1339. size_t const filesize = CmdL.FileSize() - 1;
  1340. for (size_t i = 0; i != filesize; ++i)
  1341. {
  1342. std::string needle = std::string("\n") + CmdL.FileList[i + 1] + "\n";
  1343. std::transform(needle.begin(), needle.end(), needle.begin(), ::tolower);
  1344. if (haystack.find(needle) != std::string::npos)
  1345. continue;
  1346. found = false;
  1347. break;
  1348. }
  1349. if (found == false)
  1350. continue;
  1351. }
  1352. }
  1353. if (Format.empty())
  1354. cout << stanza.str();
  1355. else
  1356. {
  1357. std::string out = SubstVar(Format, "$(FILENAME)", filename);
  1358. out = T->Format(out);
  1359. for (std::map<std::string,std::string>::const_iterator O = AddOptions.begin(); O != AddOptions.end(); ++O)
  1360. out = SubstVar(out, std::string("$(") + O->first + ")", O->second);
  1361. cout << out << std::endl;
  1362. }
  1363. }
  1364. }
  1365. return true;
  1366. }
  1367. /*}}}*/
  1368. // ShowHelp - Show a help screen /*{{{*/
  1369. bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
  1370. {
  1371. ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
  1372. if (_config->FindB("version") == true)
  1373. {
  1374. cout << _("Supported modules:") << endl;
  1375. for (unsigned I = 0; I != pkgVersioningSystem::GlobalListLen; I++)
  1376. {
  1377. pkgVersioningSystem *VS = pkgVersioningSystem::GlobalList[I];
  1378. if (_system != 0 && _system->VS == VS)
  1379. cout << '*';
  1380. else
  1381. cout << ' ';
  1382. cout << "Ver: " << VS->Label << endl;
  1383. /* Print out all the packaging systems that will work with
  1384. this VS */
  1385. for (unsigned J = 0; J != pkgSystem::GlobalListLen; J++)
  1386. {
  1387. pkgSystem *Sys = pkgSystem::GlobalList[J];
  1388. if (_system == Sys)
  1389. cout << '*';
  1390. else
  1391. cout << ' ';
  1392. if (Sys->VS->TestCompatibility(*VS) == true)
  1393. cout << "Pkg: " << Sys->Label << " (Priority " << Sys->Score(*_config) << ")" << endl;
  1394. }
  1395. }
  1396. for (unsigned I = 0; I != pkgSourceList::Type::GlobalListLen; I++)
  1397. {
  1398. pkgSourceList::Type *Type = pkgSourceList::Type::GlobalList[I];
  1399. cout << " S.L: '" << Type->Name << "' " << Type->Label << endl;
  1400. }
  1401. for (unsigned I = 0; I != pkgIndexFile::Type::GlobalListLen; I++)
  1402. {
  1403. pkgIndexFile::Type *Type = pkgIndexFile::Type::GlobalList[I];
  1404. cout << " Idx: " << Type->Label << endl;
  1405. }
  1406. return true;
  1407. }
  1408. std::cout <<
  1409. _("Usage: apt-get [options] command\n"
  1410. " apt-get [options] install|remove pkg1 [pkg2 ...]\n"
  1411. " apt-get [options] source pkg1 [pkg2 ...]\n"
  1412. "\n"
  1413. "apt-get is a simple command line interface for downloading and\n"
  1414. "installing packages. The most frequently used commands are update\n"
  1415. "and install.\n")
  1416. << std::endl
  1417. << _("Commands:") << std::endl;
  1418. for (; Cmds->Handler != nullptr; ++Cmds)
  1419. {
  1420. if (Cmds->Help == nullptr)
  1421. continue;
  1422. std::cout << " " << Cmds->Match << " - " << Cmds->Help << std::endl;
  1423. }
  1424. std::cout << std::endl <<
  1425. _("Options:\n"
  1426. " -h This help text.\n"
  1427. " -q Loggable output - no progress indicator\n"
  1428. " -qq No output except for errors\n"
  1429. " -d Download only - do NOT install or unpack archives\n"
  1430. " -s No-act. Perform ordering simulation\n"
  1431. " -y Assume Yes to all queries and do not prompt\n"
  1432. " -f Attempt to correct a system with broken dependencies in place\n"
  1433. " -m Attempt to continue if archives are unlocatable\n"
  1434. " -u Show a list of upgraded packages as well\n"
  1435. " -b Build the source package after fetching it\n"
  1436. " -V Show verbose version numbers\n"
  1437. " -c=? Read this configuration file\n"
  1438. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
  1439. "See the apt-get(8), sources.list(5) and apt.conf(5) manual\n"
  1440. "pages for more information and options.\n"
  1441. " This APT has Super Cow Powers.\n");
  1442. return true;
  1443. }
  1444. /*}}}*/
  1445. std::vector<CommandLine::DispatchWithHelp> GetCommands() /*{{{*/
  1446. {
  1447. return {
  1448. {"update", &DoUpdate, _("Retrieve new lists of packages")},
  1449. {"upgrade", &DoUpgrade, _("Perform an upgrade")},
  1450. {"install", &DoInstall, _("Install new packages (pkg is libc6 not libc6.deb)")},
  1451. {"remove", &DoInstall, _("Remove packages")},
  1452. {"purge", &DoInstall, _("Remove packages and config files")},
  1453. {"autoremove", &DoInstall, _("Remove automatically all unused packages")},
  1454. {"auto-remove", &DoInstall, nullptr},
  1455. {"markauto", &DoMarkAuto, nullptr},
  1456. {"unmarkauto", &DoMarkAuto, nullptr},
  1457. {"dist-upgrade", &DoDistUpgrade, _("Distribution upgrade, see apt-get(8)")},
  1458. {"full-upgrade", &DoDistUpgrade, nullptr},
  1459. {"dselect-upgrade", &DoDSelectUpgrade, _("Follow dselect selections")},
  1460. {"build-dep", &DoBuildDep, _("Configure build-dependencies for source packages")},
  1461. {"clean", &DoClean, _("Erase downloaded archive files")},
  1462. {"autoclean", &DoAutoClean, _("Erase old downloaded archive files")},
  1463. {"auto-clean", &DoAutoClean, nullptr},
  1464. {"check", &DoCheck, _("Verify that there are no broken dependencies")},
  1465. {"source", &DoSource, _("Download source archives")},
  1466. {"download", &DoDownload, _("Download the binary package into the current directory")},
  1467. {"changelog", &DoChangelog, _("Download and display the changelog for the given package")},
  1468. {"indextargets", &DoIndexTargets, nullptr},
  1469. {"moo", &DoMoo, nullptr},
  1470. {nullptr, nullptr, nullptr}
  1471. };
  1472. }
  1473. /*}}}*/
  1474. int main(int argc,const char *argv[]) /*{{{*/
  1475. {
  1476. InitLocale();
  1477. // Parse the command line and initialize the package library
  1478. CommandLine CmdL;
  1479. auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_GET, &_config, &_system, argc, argv);
  1480. InitSignals();
  1481. InitOutput();
  1482. CheckIfSimulateMode(CmdL);
  1483. return DispatchCommandLine(CmdL, Cmds);
  1484. }
  1485. /*}}}*/