private-download.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Include Files /*{{{*/
  2. #include <config.h>
  3. #include <apt-pkg/acquire.h>
  4. #include <apt-pkg/acquire-item.h>
  5. #include <apt-pkg/configuration.h>
  6. #include <apt-pkg/error.h>
  7. #include <apt-pkg/fileutl.h>
  8. #include <apt-pkg/strutl.h>
  9. #include <apt-private/private-output.h>
  10. #include <apt-private/private-download.h>
  11. #include <fstream>
  12. #include <string>
  13. #include <vector>
  14. #include <unistd.h>
  15. #include <sys/types.h>
  16. #include <pwd.h>
  17. #include <fcntl.h>
  18. #include <sys/vfs.h>
  19. #include <sys/statvfs.h>
  20. #include <errno.h>
  21. #include <apti18n.h>
  22. /*}}}*/
  23. // CheckAuth - check if each download comes form a trusted source /*{{{*/
  24. bool CheckAuth(pkgAcquire& Fetcher, bool const PromptUser)
  25. {
  26. std::vector<std::string> UntrustedList;
  27. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd(); ++I)
  28. if (!(*I)->IsTrusted())
  29. UntrustedList.push_back((*I)->ShortDesc());
  30. if (UntrustedList.empty())
  31. return true;
  32. return AuthPrompt(UntrustedList, PromptUser);
  33. }
  34. bool AuthPrompt(std::vector<std::string> const &UntrustedList, bool const PromptUser)
  35. {
  36. ShowList(c2out,_("WARNING: The following packages cannot be authenticated!"), UntrustedList,
  37. [](std::string const&) { return true; },
  38. [](std::string const&str) { return str; },
  39. [](std::string const&) { return ""; });
  40. if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true)
  41. {
  42. c2out << _("Authentication warning overridden.\n");
  43. return true;
  44. }
  45. if (PromptUser == false)
  46. return _error->Error(_("Some packages could not be authenticated"));
  47. if (_config->FindI("quiet",0) < 2
  48. && _config->FindB("APT::Get::Assume-Yes",false) == false)
  49. {
  50. c2out << _("Install these packages without verification?") << std::flush;
  51. if (!YnPrompt(false))
  52. return _error->Error(_("Some packages could not be authenticated"));
  53. return true;
  54. }
  55. else if (_config->FindB("APT::Get::Force-Yes",false) == true) {
  56. _error->Warning(_("--force-yes is deprecated, use one of the options starting with --allow instead."));
  57. return true;
  58. }
  59. return _error->Error(_("There were unauthenticated packages and -y was used without --allow-unauthenticated"));
  60. }
  61. /*}}}*/
  62. bool AcquireRun(pkgAcquire &Fetcher, int const PulseInterval, bool * const Failure, bool * const TransientNetworkFailure)/*{{{*/
  63. {
  64. pkgAcquire::RunResult res;
  65. if(PulseInterval > 0)
  66. res = Fetcher.Run(PulseInterval);
  67. else
  68. res = Fetcher.Run();
  69. if (res == pkgAcquire::Failed)
  70. return false;
  71. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin();
  72. I != Fetcher.ItemsEnd(); ++I)
  73. {
  74. if ((*I)->Status == pkgAcquire::Item::StatDone &&
  75. (*I)->Complete == true)
  76. continue;
  77. if (TransientNetworkFailure != NULL && (*I)->Status == pkgAcquire::Item::StatIdle)
  78. {
  79. *TransientNetworkFailure = true;
  80. continue;
  81. }
  82. ::URI uri((*I)->DescURI());
  83. uri.User.clear();
  84. uri.Password.clear();
  85. std::string descUri = std::string(uri);
  86. _error->Error(_("Failed to fetch %s %s\n"), descUri.c_str(),
  87. (*I)->ErrorText.c_str());
  88. if (Failure != NULL)
  89. *Failure = true;
  90. }
  91. return true;
  92. }
  93. /*}}}*/
  94. bool CheckFreeSpaceBeforeDownload(std::string const &Dir, unsigned long long FetchBytes)/*{{{*/
  95. {
  96. uint32_t const RAMFS_MAGIC = 0x858458f6;
  97. /* Check for enough free space, but only if we are actually going to
  98. download */
  99. if (_config->FindB("APT::Get::Print-URIs", false) == true ||
  100. _config->FindB("APT::Get::Download", true) == false)
  101. return true;
  102. struct statvfs Buf;
  103. if (statvfs(Dir.c_str(),&Buf) != 0) {
  104. if (errno == EOVERFLOW)
  105. return _error->WarningE("statvfs",_("Couldn't determine free space in %s"),
  106. Dir.c_str());
  107. else
  108. return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
  109. Dir.c_str());
  110. }
  111. else
  112. {
  113. unsigned long long const FreeBlocks = _config->Find("APT::Sandbox::User").empty() ? Buf.f_bfree : Buf.f_bavail;
  114. if (FreeBlocks < (FetchBytes / Buf.f_bsize))
  115. {
  116. struct statfs Stat;
  117. if (statfs(Dir.c_str(),&Stat) != 0
  118. #if HAVE_STRUCT_STATFS_F_TYPE
  119. || Stat.f_type != RAMFS_MAGIC
  120. #endif
  121. )
  122. return _error->Error(_("You don't have enough free space in %s."),
  123. Dir.c_str());
  124. }
  125. }
  126. return true;
  127. }
  128. /*}}}*/