You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

144 lines
4.2 KiB

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