apt-helper.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. 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 for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
  74. COMMON_ARCH,__DATE__,__TIME__);
  75. if (_config->FindB("version") == true)
  76. return true;
  77. std::cout <<
  78. _("Usage: apt-helper [options] command\n"
  79. " apt-helper [options] download-file uri target-path\n"
  80. "\n"
  81. "apt-helper is a internal helper for apt\n"
  82. "\n"
  83. "Commands:\n"
  84. " download-file - download the given uri to the target-path\n"
  85. " auto-detect-proxy - detect proxy using apt.conf\n"
  86. "\n"
  87. " This APT helper has Super Meep Powers.\n");
  88. return true;
  89. }
  90. int main(int argc,const char *argv[]) /*{{{*/
  91. {
  92. CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
  93. {"download-file", &DoDownloadFile},
  94. {"auto-detect-proxy", &DoAutoDetectProxy},
  95. {0,0}};
  96. std::vector<CommandLine::Args> Args = getCommandArgs(
  97. "apt-download", CommandLine::GetCommand(Cmds, argc, argv));
  98. // Set up gettext support
  99. setlocale(LC_ALL,"");
  100. textdomain(PACKAGE);
  101. // Parse the command line and initialize the package library
  102. CommandLine CmdL;
  103. ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
  104. InitOutput();
  105. // Match the operation
  106. CmdL.DispatchArg(Cmds);
  107. // Print any errors or warnings found during parsing
  108. bool const Errors = _error->PendingError();
  109. if (_config->FindI("quiet",0) > 0)
  110. _error->DumpErrors();
  111. else
  112. _error->DumpErrors(GlobalError::DEBUG);
  113. return Errors == true ? 100 : 0;
  114. }
  115. /*}}}*/