apt-helper.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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(std::cout, ScreenWidth,_config->FindI("quiet",0));
  44. pkgAcquire Fetcher(&Stat);
  45. size_t fileind = 0;
  46. std::vector<std::string> targetfiles;
  47. while (fileind + 2 <= CmdL.FileSize())
  48. {
  49. std::string download_uri = CmdL.FileList[fileind + 1];
  50. std::string targetfile = CmdL.FileList[fileind + 2];
  51. std::string hash;
  52. if (CmdL.FileSize() > fileind + 3)
  53. hash = CmdL.FileList[fileind + 3];
  54. // we use download_uri as descr and targetfile as short-descr
  55. new pkgAcqFile(&Fetcher, download_uri, hash, 0, download_uri, targetfile,
  56. "dest-dir-ignored", targetfile);
  57. targetfiles.push_back(targetfile);
  58. fileind += 3;
  59. }
  60. // Disable drop-privs if "_apt" can not write to the target dir
  61. CheckDropPrivsMustBeDisabled(Fetcher);
  62. bool Failed = false;
  63. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
  64. return _error->Error(_("Download Failed"));
  65. if (targetfiles.empty() == false)
  66. for (std::vector<std::string>::const_iterator f = targetfiles.begin(); f != targetfiles.end(); ++f)
  67. if (FileExists(*f) == false)
  68. return _error->Error(_("Download Failed"));
  69. return true;
  70. }
  71. static bool ShowHelp(CommandLine &)
  72. {
  73. ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
  74. if (_config->FindB("version") == true)
  75. return true;
  76. std::cout <<
  77. _("Usage: apt-helper [options] command\n"
  78. " apt-helper [options] download-file uri target-path\n"
  79. "\n"
  80. "apt-helper is a internal helper for apt\n"
  81. "\n"
  82. "Commands:\n"
  83. " download-file - download the given uri to the target-path\n"
  84. " auto-detect-proxy - detect proxy using apt.conf\n"
  85. "\n"
  86. " This APT helper has Super Meep Powers.\n");
  87. return true;
  88. }
  89. int main(int argc,const char *argv[]) /*{{{*/
  90. {
  91. CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
  92. {"download-file", &DoDownloadFile},
  93. {"auto-detect-proxy", &DoAutoDetectProxy},
  94. {0,0}};
  95. std::vector<CommandLine::Args> Args = getCommandArgs(
  96. "apt-download", CommandLine::GetCommand(Cmds, argc, argv));
  97. // Set up gettext support
  98. setlocale(LC_ALL,"");
  99. textdomain(PACKAGE);
  100. // Parse the command line and initialize the package library
  101. CommandLine CmdL;
  102. ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
  103. InitOutput();
  104. // Match the operation
  105. CmdL.DispatchArg(Cmds);
  106. // Print any errors or warnings found during parsing
  107. bool const Errors = _error->PendingError();
  108. if (_config->FindI("quiet",0) > 0)
  109. _error->DumpErrors();
  110. else
  111. _error->DumpErrors(GlobalError::DEBUG);
  112. return Errors == true ? 100 : 0;
  113. }
  114. /*}}}*/