apt-helper.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. pkgAcquire Fetcher;
  44. AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet",0));
  45. Fetcher.Setup(&Stat);
  46. std::string download_uri = CmdL.FileList[1];
  47. std::string targetfile = CmdL.FileList[2];
  48. std::string hash;
  49. if (CmdL.FileSize() > 3)
  50. hash = CmdL.FileList[3];
  51. // we use download_uri as descr and targetfile as short-descr
  52. new pkgAcqFile(&Fetcher, download_uri, hash, 0, download_uri, targetfile,
  53. "dest-dir-ignored", targetfile);
  54. Fetcher.Run();
  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(Args.data(),_config);
  93. if (pkgInitConfig(*_config) == false ||
  94. CmdL.Parse(argc,argv) == false ||
  95. pkgInitSystem(*_config,_system) == false)
  96. {
  97. if (_config->FindB("version") == true)
  98. ShowHelp(CmdL);
  99. _error->DumpErrors();
  100. return 100;
  101. }
  102. // See if the help should be shown
  103. if (_config->FindB("help") == true ||
  104. _config->FindB("version") == true ||
  105. CmdL.FileSize() == 0)
  106. {
  107. ShowHelp(CmdL);
  108. return 0;
  109. }
  110. InitOutput();
  111. // Match the operation
  112. CmdL.DispatchArg(Cmds);
  113. // Print any errors or warnings found during parsing
  114. bool const Errors = _error->PendingError();
  115. if (_config->FindI("quiet",0) > 0)
  116. _error->DumpErrors();
  117. else
  118. _error->DumpErrors(GlobalError::DEBUG);
  119. return Errors == true ? 100 : 0;
  120. }
  121. /*}}}*/