private-download.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // Include Files /*{{{*/
  2. #include <config.h>
  3. #include <apt-pkg/acquire.h>
  4. #include <apt-pkg/acquire-item.h>
  5. #include <apt-pkg/cacheset.h>
  6. #include <apt-pkg/cmndline.h>
  7. #include <apt-pkg/clean.h>
  8. #include <apt-pkg/configuration.h>
  9. #include <apt-pkg/error.h>
  10. #include <apt-pkg/fileutl.h>
  11. #include <apt-pkg/strutl.h>
  12. #include <apt-private/private-cachefile.h>
  13. #include <apt-private/private-download.h>
  14. #include <apt-private/private-output.h>
  15. #include <apt-private/private-utils.h>
  16. #include <apt-private/acqprogress.h>
  17. #include <fstream>
  18. #include <string>
  19. #include <vector>
  20. #include <unistd.h>
  21. #include <sys/types.h>
  22. #include <pwd.h>
  23. #include <fcntl.h>
  24. #include <sys/vfs.h>
  25. #include <sys/statvfs.h>
  26. #include <errno.h>
  27. #include <apti18n.h>
  28. /*}}}*/
  29. // CheckAuth - check if each download comes form a trusted source /*{{{*/
  30. bool CheckAuth(pkgAcquire& Fetcher, bool const PromptUser)
  31. {
  32. std::vector<std::string> UntrustedList;
  33. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd(); ++I)
  34. if (!(*I)->IsTrusted())
  35. UntrustedList.push_back((*I)->ShortDesc());
  36. if (UntrustedList.empty())
  37. return true;
  38. return AuthPrompt(UntrustedList, PromptUser);
  39. }
  40. /*}}}*/
  41. bool AuthPrompt(std::vector<std::string> const &UntrustedList, bool const PromptUser)/*{{{*/
  42. {
  43. ShowList(c2out,_("WARNING: The following packages cannot be authenticated!"), UntrustedList,
  44. [](std::string const&) { return true; },
  45. [](std::string const&str) { return str; },
  46. [](std::string const&) { return ""; });
  47. if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true)
  48. {
  49. c2out << _("Authentication warning overridden.\n");
  50. return true;
  51. }
  52. if (PromptUser == false)
  53. return _error->Error(_("Some packages could not be authenticated"));
  54. if (_config->FindI("quiet",0) < 2
  55. && _config->FindB("APT::Get::Assume-Yes",false) == false)
  56. {
  57. c2out << _("Install these packages without verification?") << std::flush;
  58. if (!YnPrompt(false))
  59. return _error->Error(_("Some packages could not be authenticated"));
  60. return true;
  61. }
  62. else if (_config->FindB("APT::Get::Force-Yes",false) == true) {
  63. _error->Warning(_("--force-yes is deprecated, use one of the options starting with --allow instead."));
  64. return true;
  65. }
  66. return _error->Error(_("There were unauthenticated packages and -y was used without --allow-unauthenticated"));
  67. }
  68. /*}}}*/
  69. bool AcquireRun(pkgAcquire &Fetcher, int const PulseInterval, bool * const Failure, bool * const TransientNetworkFailure)/*{{{*/
  70. {
  71. pkgAcquire::RunResult res;
  72. if(PulseInterval > 0)
  73. res = Fetcher.Run(PulseInterval);
  74. else
  75. res = Fetcher.Run();
  76. if (res == pkgAcquire::Failed)
  77. return false;
  78. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin();
  79. I != Fetcher.ItemsEnd(); ++I)
  80. {
  81. if ((*I)->Status == pkgAcquire::Item::StatDone &&
  82. (*I)->Complete == true)
  83. continue;
  84. if (TransientNetworkFailure != NULL && (*I)->Status == pkgAcquire::Item::StatIdle)
  85. {
  86. *TransientNetworkFailure = true;
  87. continue;
  88. }
  89. ::URI uri((*I)->DescURI());
  90. uri.User.clear();
  91. uri.Password.clear();
  92. std::string descUri = std::string(uri);
  93. _error->Error(_("Failed to fetch %s %s\n"), descUri.c_str(),
  94. (*I)->ErrorText.c_str());
  95. if (Failure != NULL)
  96. *Failure = true;
  97. }
  98. return true;
  99. }
  100. /*}}}*/
  101. bool CheckFreeSpaceBeforeDownload(std::string const &Dir, unsigned long long FetchBytes)/*{{{*/
  102. {
  103. uint32_t const RAMFS_MAGIC = 0x858458f6;
  104. /* Check for enough free space, but only if we are actually going to
  105. download */
  106. if (_config->FindB("APT::Get::Print-URIs", false) == true ||
  107. _config->FindB("APT::Get::Download", true) == false)
  108. return true;
  109. struct statvfs Buf;
  110. if (statvfs(Dir.c_str(),&Buf) != 0) {
  111. if (errno == EOVERFLOW)
  112. return _error->WarningE("statvfs",_("Couldn't determine free space in %s"),
  113. Dir.c_str());
  114. else
  115. return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
  116. Dir.c_str());
  117. }
  118. else
  119. {
  120. unsigned long long const FreeBlocks = _config->Find("APT::Sandbox::User").empty() ? Buf.f_bfree : Buf.f_bavail;
  121. if (FreeBlocks < (FetchBytes / Buf.f_bsize))
  122. {
  123. struct statfs Stat;
  124. if (statfs(Dir.c_str(),&Stat) != 0
  125. #if HAVE_STRUCT_STATFS_F_TYPE
  126. || Stat.f_type != RAMFS_MAGIC
  127. #endif
  128. )
  129. return _error->Error(_("You don't have enough free space in %s."),
  130. Dir.c_str());
  131. }
  132. }
  133. return true;
  134. }
  135. /*}}}*/
  136. aptAcquireWithTextStatus::aptAcquireWithTextStatus() : pkgAcquire::pkgAcquire(),
  137. Stat(std::cout, ScreenWidth, _config->FindI("quiet",0))
  138. {
  139. SetLog(&Stat);
  140. }
  141. // DoDownload - download a binary /*{{{*/
  142. bool DoDownload(CommandLine &CmdL)
  143. {
  144. CacheFile Cache;
  145. if (Cache.ReadOnlyOpen() == false)
  146. return false;
  147. APT::CacheSetHelper helper;
  148. APT::VersionSet verset = APT::VersionSet::FromCommandLine(Cache,
  149. CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper);
  150. if (verset.empty() == true)
  151. return false;
  152. pkgRecords Recs(Cache);
  153. pkgSourceList *SrcList = Cache.GetSourceList();
  154. // reuse the usual acquire methods for deb files, but don't drop them into
  155. // the usual directories - keep everything in the current directory
  156. aptAcquireWithTextStatus Fetcher;
  157. std::vector<std::string> storefile(verset.size());
  158. std::string const cwd = SafeGetCWD();
  159. _config->Set("Dir::Cache::Archives", cwd);
  160. int i = 0;
  161. for (APT::VersionSet::const_iterator Ver = verset.begin();
  162. Ver != verset.end(); ++Ver, ++i)
  163. {
  164. pkgAcquire::Item *I = new pkgAcqArchive(&Fetcher, SrcList, &Recs, *Ver, storefile[i]);
  165. if (storefile[i].empty())
  166. continue;
  167. std::string const filename = cwd + flNotDir(storefile[i]);
  168. storefile[i].assign(filename);
  169. I->DestFile.assign(filename);
  170. }
  171. // Just print out the uris and exit if the --print-uris flag was used
  172. if (_config->FindB("APT::Get::Print-URIs") == true)
  173. {
  174. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  175. for (; I != Fetcher.UriEnd(); ++I)
  176. std::cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  177. I->Owner->FileSize << ' ' << I->Owner->HashSum() << std::endl;
  178. return true;
  179. }
  180. if (_error->PendingError() == true || CheckAuth(Fetcher, false) == false)
  181. return false;
  182. bool Failed = false;
  183. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false)
  184. return false;
  185. // copy files in local sources to the current directory
  186. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I)
  187. {
  188. std::string const filename = cwd + flNotDir((*I)->DestFile);
  189. if ((*I)->Local == true &&
  190. filename != (*I)->DestFile &&
  191. (*I)->Status == pkgAcquire::Item::StatDone)
  192. {
  193. std::ifstream src((*I)->DestFile.c_str(), std::ios::binary);
  194. std::ofstream dst(filename.c_str(), std::ios::binary);
  195. dst << src.rdbuf();
  196. }
  197. }
  198. return Failed == false;
  199. }
  200. /*}}}*/
  201. // DoChangelog - Get changelog from the command line /*{{{*/
  202. bool DoChangelog(CommandLine &CmdL)
  203. {
  204. CacheFile Cache;
  205. if (Cache.ReadOnlyOpen() == false)
  206. return false;
  207. APT::CacheSetHelper helper;
  208. APT::VersionList verset = APT::VersionList::FromCommandLine(Cache,
  209. CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper);
  210. if (verset.empty() == true)
  211. return false;
  212. bool const downOnly = _config->FindB("APT::Get::Download-Only", false);
  213. bool const printOnly = _config->FindB("APT::Get::Print-URIs", false);
  214. aptAcquireWithTextStatus Fetcher;
  215. for (APT::VersionList::const_iterator Ver = verset.begin();
  216. Ver != verset.end();
  217. ++Ver)
  218. {
  219. if (printOnly)
  220. new pkgAcqChangelog(&Fetcher, Ver, "/dev/null");
  221. else if (downOnly)
  222. new pkgAcqChangelog(&Fetcher, Ver, ".");
  223. else
  224. new pkgAcqChangelog(&Fetcher, Ver);
  225. }
  226. if (printOnly == false)
  227. {
  228. bool Failed = false;
  229. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
  230. return false;
  231. }
  232. if (downOnly == false || printOnly == true)
  233. {
  234. bool Failed = false;
  235. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I)
  236. {
  237. if (printOnly)
  238. {
  239. if ((*I)->ErrorText.empty() == false)
  240. {
  241. Failed = true;
  242. _error->Error("%s", (*I)->ErrorText.c_str());
  243. }
  244. else
  245. std::cout << '\'' << (*I)->DescURI() << "' " << flNotDir((*I)->DestFile) << std::endl;
  246. }
  247. else
  248. DisplayFileInPager((*I)->DestFile);
  249. }
  250. return Failed == false;
  251. }
  252. return true;
  253. }
  254. /*}}}*/
  255. // DoClean - Remove download archives /*{{{*/
  256. bool DoClean(CommandLine &)
  257. {
  258. std::string const archivedir = _config->FindDir("Dir::Cache::archives");
  259. std::string const listsdir = _config->FindDir("Dir::state::lists");
  260. if (_config->FindB("APT::Get::Simulate") == true)
  261. {
  262. std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
  263. std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
  264. std::cout << "Del " << archivedir << "* " << archivedir << "partial/*"<< std::endl
  265. << "Del " << listsdir << "partial/*" << std::endl
  266. << "Del " << pkgcache << " " << srcpkgcache << std::endl;
  267. return true;
  268. }
  269. pkgAcquire Fetcher;
  270. Fetcher.GetLock(archivedir);
  271. Fetcher.Clean(archivedir);
  272. Fetcher.Clean(archivedir + "partial/");
  273. Fetcher.GetLock(listsdir);
  274. Fetcher.Clean(listsdir + "partial/");
  275. pkgCacheFile::RemoveCaches();
  276. return true;
  277. }
  278. /*}}}*/
  279. // DoAutoClean - Smartly remove downloaded archives /*{{{*/
  280. // ---------------------------------------------------------------------
  281. /* This is similar to clean but it only purges things that cannot be
  282. downloaded, that is old versions of cached packages. */
  283. class LogCleaner : public pkgArchiveCleaner
  284. {
  285. protected:
  286. virtual void Erase(const char *File, std::string Pkg, std::string Ver,struct stat &St) APT_OVERRIDE
  287. {
  288. c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << std::endl;
  289. if (_config->FindB("APT::Get::Simulate") == false)
  290. unlink(File);
  291. };
  292. };
  293. bool DoAutoClean(CommandLine &)
  294. {
  295. // Lock the archive directory
  296. FileFd Lock;
  297. if (_config->FindB("Debug::NoLocking",false) == false)
  298. {
  299. int lock_fd = GetLock(_config->FindDir("Dir::Cache::Archives") + "lock");
  300. if (lock_fd < 0)
  301. return _error->Error(_("Unable to lock the download directory"));
  302. Lock.Fd(lock_fd);
  303. }
  304. CacheFile Cache;
  305. if (Cache.Open() == false)
  306. return false;
  307. LogCleaner Cleaner;
  308. return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
  309. Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
  310. }
  311. /*}}}*/