You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

379 lines
11 KiB

  1. // Include Files /*{{{*/
  2. #include <config.h>
  3. #include <apt-pkg/acquire-item.h>
  4. #include <apt-pkg/acquire.h>
  5. #include <apt-pkg/cacheset.h>
  6. #include <apt-pkg/clean.h>
  7. #include <apt-pkg/cmndline.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/acqprogress.h>
  13. #include <apt-private/private-cachefile.h>
  14. #include <apt-private/private-download.h>
  15. #include <apt-private/private-output.h>
  16. #include <apt-private/private-utils.h>
  17. #include <fstream>
  18. #include <string>
  19. #include <vector>
  20. #include <fcntl.h>
  21. #include <pwd.h>
  22. #include <sys/types.h>
  23. #include <unistd.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 <errno.h>
  33. #include <sys/stat.h>
  34. #include <sys/statvfs.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. return true;
  71. }
  72. return _error->Error(_("There were unauthenticated packages and -y was used without --allow-unauthenticated"));
  73. }
  74. /*}}}*/
  75. bool AcquireRun(pkgAcquire &Fetcher, int const PulseInterval, bool * const Failure, bool * const TransientNetworkFailure)/*{{{*/
  76. {
  77. pkgAcquire::RunResult res;
  78. if(PulseInterval > 0)
  79. res = Fetcher.Run(PulseInterval);
  80. else
  81. res = Fetcher.Run();
  82. if (res == pkgAcquire::Failed)
  83. return false;
  84. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin();
  85. I != Fetcher.ItemsEnd(); ++I)
  86. {
  87. if ((*I)->Status == pkgAcquire::Item::StatDone &&
  88. (*I)->Complete == true)
  89. continue;
  90. if (TransientNetworkFailure != NULL && (*I)->Status == pkgAcquire::Item::StatIdle)
  91. {
  92. *TransientNetworkFailure = true;
  93. continue;
  94. }
  95. ::URI uri((*I)->DescURI());
  96. uri.User.clear();
  97. uri.Password.clear();
  98. std::string descUri = std::string(uri);
  99. _error->Error(_("Failed to fetch %s %s"), descUri.c_str(),
  100. (*I)->ErrorText.c_str());
  101. if (Failure != NULL)
  102. *Failure = true;
  103. }
  104. return true;
  105. }
  106. /*}}}*/
  107. bool CheckFreeSpaceBeforeDownload(std::string const &Dir, unsigned long long FetchBytes)/*{{{*/
  108. {
  109. uint32_t const RAMFS_MAGIC = 0x858458f6;
  110. /* Check for enough free space, but only if we are actually going to
  111. download */
  112. if (_config->FindB("APT::Get::Print-URIs", false) == true ||
  113. _config->FindB("APT::Get::Download", true) == false)
  114. return true;
  115. struct statvfs Buf;
  116. if (statvfs(Dir.c_str(),&Buf) != 0) {
  117. if (errno == EOVERFLOW)
  118. return _error->WarningE("statvfs",_("Couldn't determine free space in %s"),
  119. Dir.c_str());
  120. else
  121. return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
  122. Dir.c_str());
  123. }
  124. else
  125. {
  126. unsigned long long const FreeBlocks = _config->Find("APT::Sandbox::User").empty() ? Buf.f_bfree : Buf.f_bavail;
  127. if (FreeBlocks < (FetchBytes / Buf.f_bsize))
  128. {
  129. struct statfs Stat;
  130. if (statfs(Dir.c_str(),&Stat) != 0
  131. #ifdef HAVE_STRUCT_STATFS_F_TYPE
  132. || Stat.f_type != RAMFS_MAGIC
  133. #endif
  134. )
  135. return _error->Error(_("You don't have enough free space in %s."),
  136. Dir.c_str());
  137. }
  138. }
  139. return true;
  140. }
  141. /*}}}*/
  142. aptAcquireWithTextStatus::aptAcquireWithTextStatus() : pkgAcquire::pkgAcquire(),
  143. Stat(std::cout, ScreenWidth, _config->FindI("quiet",0))
  144. {
  145. SetLog(&Stat);
  146. }
  147. // DoDownload - download a binary /*{{{*/
  148. bool DoDownload(CommandLine &CmdL)
  149. {
  150. CacheFile Cache;
  151. if (Cache.ReadOnlyOpen() == false)
  152. return false;
  153. APT::CacheSetHelper helper;
  154. APT::VersionSet verset = APT::VersionSet::FromCommandLine(Cache,
  155. CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper);
  156. if (verset.empty() == true)
  157. return false;
  158. pkgRecords Recs(Cache);
  159. pkgSourceList *SrcList = Cache.GetSourceList();
  160. // reuse the usual acquire methods for deb files, but don't drop them into
  161. // the usual directories - keep everything in the current directory
  162. aptAcquireWithTextStatus Fetcher;
  163. std::vector<std::string> storefile(verset.size());
  164. std::string const cwd = SafeGetCWD();
  165. _config->Set("Dir::Cache::Archives", cwd);
  166. int i = 0;
  167. for (APT::VersionSet::const_iterator Ver = verset.begin();
  168. Ver != verset.end(); ++Ver, ++i)
  169. {
  170. pkgAcquire::Item *I = new pkgAcqArchive(&Fetcher, SrcList, &Recs, *Ver, storefile[i]);
  171. if (storefile[i].empty())
  172. continue;
  173. std::string const filename = cwd + flNotDir(storefile[i]);
  174. storefile[i].assign(filename);
  175. I->DestFile.assign(filename);
  176. }
  177. // Just print out the uris and exit if the --print-uris flag was used
  178. if (_config->FindB("APT::Get::Print-URIs") == true)
  179. {
  180. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  181. for (; I != Fetcher.UriEnd(); ++I)
  182. std::cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  183. I->Owner->FileSize << ' ' << I->Owner->HashSum() << std::endl;
  184. return true;
  185. }
  186. if (_error->PendingError() == true || CheckAuth(Fetcher, false) == false)
  187. return false;
  188. bool Failed = false;
  189. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false)
  190. return false;
  191. // copy files in local sources to the current directory
  192. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I)
  193. {
  194. std::string const filename = cwd + flNotDir((*I)->DestFile);
  195. if ((*I)->Local == true &&
  196. filename != (*I)->DestFile &&
  197. (*I)->Status == pkgAcquire::Item::StatDone &&
  198. dynamic_cast<pkgAcqArchive*>(*I) != nullptr)
  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 _error->Error(_("No packages found"));
  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 pkgArchiveCleaner2
  300. {
  301. protected:
  302. virtual void Erase(int const dirfd, char const * const File, std::string const &Pkg, std::string const &Ver,struct stat const &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. RemoveFileAt("Cleaner::Erase", dirfd, 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) == false)
  325. return false;
  326. LogCleaner Cleaner;
  327. return Cleaner.Go(archivedir, *Cache) &&
  328. Cleaner.Go(flCombine(archivedir, "partial/"), *Cache);
  329. }
  330. /*}}}*/