private-download.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. if (!YnPrompt(_("Install these packages without verification?"), 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"), 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. chmod(filename.c_str(), 0644);
  197. }
  198. }
  199. return Failed == false;
  200. }
  201. /*}}}*/
  202. // DoChangelog - Get changelog from the command line /*{{{*/
  203. bool DoChangelog(CommandLine &CmdL)
  204. {
  205. CacheFile Cache;
  206. if (Cache.ReadOnlyOpen() == false)
  207. return false;
  208. APT::CacheSetHelper helper;
  209. APT::VersionList verset = APT::VersionList::FromCommandLine(Cache,
  210. CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper);
  211. if (verset.empty() == true)
  212. return false;
  213. bool const downOnly = _config->FindB("APT::Get::Download-Only", false);
  214. bool const printOnly = _config->FindB("APT::Get::Print-URIs", false);
  215. if (printOnly)
  216. _config->CndSet("Acquire::Changelogs::AlwaysOnline", true);
  217. aptAcquireWithTextStatus Fetcher;
  218. for (APT::VersionList::const_iterator Ver = verset.begin();
  219. Ver != verset.end();
  220. ++Ver)
  221. {
  222. if (printOnly)
  223. new pkgAcqChangelog(&Fetcher, Ver, "/dev/null");
  224. else if (downOnly)
  225. new pkgAcqChangelog(&Fetcher, Ver, ".");
  226. else
  227. new pkgAcqChangelog(&Fetcher, Ver);
  228. }
  229. if (printOnly == false)
  230. {
  231. bool Failed = false;
  232. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
  233. return false;
  234. }
  235. if (downOnly == false || printOnly == true)
  236. {
  237. bool Failed = false;
  238. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I)
  239. {
  240. if (printOnly)
  241. {
  242. if ((*I)->ErrorText.empty() == false)
  243. {
  244. Failed = true;
  245. _error->Error("%s", (*I)->ErrorText.c_str());
  246. }
  247. else
  248. std::cout << '\'' << (*I)->DescURI() << "' " << flNotDir((*I)->DestFile) << std::endl;
  249. }
  250. else
  251. DisplayFileInPager((*I)->DestFile);
  252. }
  253. return Failed == false;
  254. }
  255. return true;
  256. }
  257. /*}}}*/
  258. // DoClean - Remove download archives /*{{{*/
  259. bool DoClean(CommandLine &)
  260. {
  261. std::string const archivedir = _config->FindDir("Dir::Cache::archives");
  262. std::string const listsdir = _config->FindDir("Dir::state::lists");
  263. if (_config->FindB("APT::Get::Simulate") == true)
  264. {
  265. std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
  266. std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
  267. std::cout << "Del " << archivedir << "* " << archivedir << "partial/*"<< std::endl
  268. << "Del " << listsdir << "partial/*" << std::endl
  269. << "Del " << pkgcache << " " << srcpkgcache << std::endl;
  270. return true;
  271. }
  272. pkgAcquire Fetcher;
  273. if (archivedir.empty() == false && FileExists(archivedir) == true &&
  274. Fetcher.GetLock(archivedir) == true)
  275. {
  276. Fetcher.Clean(archivedir);
  277. Fetcher.Clean(archivedir + "partial/");
  278. }
  279. if (listsdir.empty() == false && FileExists(listsdir) == true &&
  280. Fetcher.GetLock(listsdir) == true)
  281. {
  282. Fetcher.Clean(listsdir + "partial/");
  283. }
  284. pkgCacheFile::RemoveCaches();
  285. return true;
  286. }
  287. /*}}}*/
  288. // DoAutoClean - Smartly remove downloaded archives /*{{{*/
  289. // ---------------------------------------------------------------------
  290. /* This is similar to clean but it only purges things that cannot be
  291. downloaded, that is old versions of cached packages. */
  292. class LogCleaner : public pkgArchiveCleaner
  293. {
  294. protected:
  295. virtual void Erase(const char *File, std::string Pkg, std::string Ver,struct stat &St) APT_OVERRIDE
  296. {
  297. c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << std::endl;
  298. if (_config->FindB("APT::Get::Simulate") == false)
  299. RemoveFile("Cleaner::Erase", File);
  300. };
  301. };
  302. bool DoAutoClean(CommandLine &)
  303. {
  304. std::string const archivedir = _config->FindDir("Dir::Cache::Archives");
  305. if (FileExists(archivedir) == false)
  306. return true;
  307. // Lock the archive directory
  308. FileFd Lock;
  309. if (_config->FindB("Debug::NoLocking",false) == false)
  310. {
  311. int lock_fd = GetLock(flCombine(archivedir, "lock"));
  312. if (lock_fd < 0)
  313. return _error->Error(_("Unable to lock the download directory"));
  314. Lock.Fd(lock_fd);
  315. }
  316. CacheFile Cache;
  317. if (Cache.Open() == false)
  318. return false;
  319. LogCleaner Cleaner;
  320. return Cleaner.Go(archivedir, *Cache) &&
  321. Cleaner.Go(flCombine(archivedir, "partial/"), *Cache);
  322. }
  323. /*}}}*/