private-download.cc 11 KB

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