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.
 
 
 
 
 
 

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