private-download.cc 11 KB

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