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.
 
 
 
 
 
 

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