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.
 
 
 
 
 
 

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