apt-helper.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* #####################################################################
  4. apt-helper - cmdline helpers
  5. ##################################################################### */
  6. /*}}}*/
  7. // Include Files /*{{{*/
  8. #include <config.h>
  9. #include <apt-pkg/configuration.h>
  10. #include <apt-pkg/cmndline.h>
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/init.h>
  13. #include <apt-pkg/strutl.h>
  14. #include <apt-pkg/pkgsystem.h>
  15. #include <apt-pkg/fileutl.h>
  16. #include <apt-pkg/acquire.h>
  17. #include <apt-pkg/acquire-item.h>
  18. #include <apt-pkg/proxy.h>
  19. #include <apt-private/acqprogress.h>
  20. #include <apt-private/private-output.h>
  21. #include <apt-private/private-download.h>
  22. #include <apt-private/private-cmndline.h>
  23. #include <iostream>
  24. #include <string>
  25. #include <vector>
  26. #include <apti18n.h>
  27. /*}}}*/
  28. static bool DoAutoDetectProxy(CommandLine &CmdL)
  29. {
  30. if (CmdL.FileSize() != 2)
  31. return _error->Error(_("Need one URL as argument"));
  32. URI ServerURL(CmdL.FileList[1]);
  33. AutoDetectProxy(ServerURL);
  34. std::string SpecificProxy = _config->Find("Acquire::"+ServerURL.Access+"::Proxy::" + ServerURL.Host);
  35. ioprintf(std::cout, "Using proxy '%s' for URL '%s'\n",
  36. SpecificProxy.c_str(), std::string(ServerURL).c_str());
  37. return true;
  38. }
  39. static bool DoDownloadFile(CommandLine &CmdL)
  40. {
  41. if (CmdL.FileSize() <= 2)
  42. return _error->Error(_("Must specify at least one pair url/filename"));
  43. AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet",0));
  44. pkgAcquire Fetcher(&Stat);
  45. std::string download_uri = CmdL.FileList[1];
  46. std::string targetfile = CmdL.FileList[2];
  47. std::string hash;
  48. if (CmdL.FileSize() > 3)
  49. hash = CmdL.FileList[3];
  50. // we use download_uri as descr and targetfile as short-descr
  51. new pkgAcqFile(&Fetcher, download_uri, hash, 0, download_uri, targetfile,
  52. "dest-dir-ignored", targetfile);
  53. // Disable drop-privs if "_apt" can not write to the target dir
  54. CheckDropPrivsMustBeDisabled(Fetcher);
  55. bool Failed = false;
  56. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true ||
  57. FileExists(targetfile) == false)
  58. return _error->Error(_("Download Failed"));
  59. return true;
  60. }
  61. static bool ShowHelp(CommandLine &)
  62. {
  63. ioprintf(std::cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
  64. COMMON_ARCH,__DATE__,__TIME__);
  65. if (_config->FindB("version") == true)
  66. return true;
  67. std::cout <<
  68. _("Usage: apt-helper [options] command\n"
  69. " apt-helper [options] download-file uri target-path\n"
  70. "\n"
  71. "apt-helper is a internal helper for apt\n"
  72. "\n"
  73. "Commands:\n"
  74. " download-file - download the given uri to the target-path\n"
  75. " auto-detect-proxy - detect proxy using apt.conf\n"
  76. "\n"
  77. " This APT helper has Super Meep Powers.\n");
  78. return true;
  79. }
  80. int main(int argc,const char *argv[]) /*{{{*/
  81. {
  82. CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
  83. {"download-file", &DoDownloadFile},
  84. {"auto-detect-proxy", &DoAutoDetectProxy},
  85. {0,0}};
  86. std::vector<CommandLine::Args> Args = getCommandArgs(
  87. "apt-download", CommandLine::GetCommand(Cmds, argc, argv));
  88. // Set up gettext support
  89. setlocale(LC_ALL,"");
  90. textdomain(PACKAGE);
  91. // Parse the command line and initialize the package library
  92. CommandLine CmdL;
  93. ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
  94. InitOutput();
  95. // Match the operation
  96. CmdL.DispatchArg(Cmds);
  97. // Print any errors or warnings found during parsing
  98. bool const Errors = _error->PendingError();
  99. if (_config->FindI("quiet",0) > 0)
  100. _error->DumpErrors();
  101. else
  102. _error->DumpErrors(GlobalError::DEBUG);
  103. return Errors == true ? 100 : 0;
  104. }
  105. /*}}}*/