apt-helper.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. Fetcher.Run();
  54. bool Failed = false;
  55. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true ||
  56. FileExists(targetfile) == false)
  57. return _error->Error(_("Download Failed"));
  58. return true;
  59. }
  60. static bool ShowHelp(CommandLine &)
  61. {
  62. ioprintf(std::cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
  63. COMMON_ARCH,__DATE__,__TIME__);
  64. if (_config->FindB("version") == true)
  65. return true;
  66. std::cout <<
  67. _("Usage: apt-helper [options] command\n"
  68. " apt-helper [options] download-file uri target-path\n"
  69. "\n"
  70. "apt-helper is a internal helper for apt\n"
  71. "\n"
  72. "Commands:\n"
  73. " download-file - download the given uri to the target-path\n"
  74. " auto-detect-proxy - detect proxy using apt.conf\n"
  75. "\n"
  76. " This APT helper has Super Meep Powers.\n");
  77. return true;
  78. }
  79. int main(int argc,const char *argv[]) /*{{{*/
  80. {
  81. CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
  82. {"download-file", &DoDownloadFile},
  83. {"auto-detect-proxy", &DoAutoDetectProxy},
  84. {0,0}};
  85. std::vector<CommandLine::Args> Args = getCommandArgs(
  86. "apt-download", CommandLine::GetCommand(Cmds, argc, argv));
  87. // Set up gettext support
  88. setlocale(LC_ALL,"");
  89. textdomain(PACKAGE);
  90. // Parse the command line and initialize the package library
  91. CommandLine CmdL(Args.data(),_config);
  92. if (pkgInitConfig(*_config) == false ||
  93. CmdL.Parse(argc,argv) == false ||
  94. pkgInitSystem(*_config,_system) == false)
  95. {
  96. if (_config->FindB("version") == true)
  97. ShowHelp(CmdL);
  98. _error->DumpErrors();
  99. return 100;
  100. }
  101. // See if the help should be shown
  102. if (_config->FindB("help") == true ||
  103. _config->FindB("version") == true ||
  104. CmdL.FileSize() == 0)
  105. {
  106. ShowHelp(CmdL);
  107. return 0;
  108. }
  109. InitOutput();
  110. // Match the operation
  111. CmdL.DispatchArg(Cmds);
  112. // Print any errors or warnings found during parsing
  113. bool const Errors = _error->PendingError();
  114. if (_config->FindI("quiet",0) > 0)
  115. _error->DumpErrors();
  116. else
  117. _error->DumpErrors(GlobalError::DEBUG);
  118. return Errors == true ? 100 : 0;
  119. }
  120. /*}}}*/