apt-helper.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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-private/acqprogress.h>
  19. #include <apt-private/private-output.h>
  20. #include <apt-private/private-download.h>
  21. #include <apt-private/private-cmndline.h>
  22. #include <iostream>
  23. #include <string>
  24. #include <vector>
  25. #include <apti18n.h>
  26. /*}}}*/
  27. static bool DoDownloadFile(CommandLine &CmdL)
  28. {
  29. if (CmdL.FileSize() <= 2)
  30. return _error->Error(_("Must specify at least one pair url/filename"));
  31. pkgAcquire Fetcher;
  32. AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet",0));
  33. Fetcher.Setup(&Stat);
  34. std::string download_uri = CmdL.FileList[1];
  35. std::string targetfile = CmdL.FileList[2];
  36. std::string hash;
  37. if (CmdL.FileSize() > 3)
  38. hash = CmdL.FileList[3];
  39. // we use download_uri as descr and targetfile as short-descr
  40. new pkgAcqFile(&Fetcher, download_uri, hash, 0, download_uri, targetfile,
  41. "dest-dir-ignored", targetfile);
  42. Fetcher.Run();
  43. bool Failed = false;
  44. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true ||
  45. FileExists(targetfile) == false)
  46. return _error->Error(_("Download Failed"));
  47. return true;
  48. }
  49. static bool ShowHelp(CommandLine &)
  50. {
  51. ioprintf(std::cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
  52. COMMON_ARCH,__DATE__,__TIME__);
  53. if (_config->FindB("version") == true)
  54. return true;
  55. std::cout <<
  56. _("Usage: apt-helper [options] command\n"
  57. " apt-helper [options] download-file uri target-path\n"
  58. "\n"
  59. "apt-helper is a internal helper for apt\n"
  60. "\n"
  61. "Commands:\n"
  62. " download-file - download the given uri to the target-path\n"
  63. "\n"
  64. " This APT helper has Super Meep Powers.\n");
  65. return true;
  66. }
  67. int main(int argc,const char *argv[]) /*{{{*/
  68. {
  69. CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
  70. {"download-file", &DoDownloadFile},
  71. {0,0}};
  72. std::vector<CommandLine::Args> Args = getCommandArgs(
  73. "apt-download", CommandLine::GetCommand(Cmds, argc, argv));
  74. // Set up gettext support
  75. setlocale(LC_ALL,"");
  76. textdomain(PACKAGE);
  77. // Parse the command line and initialize the package library
  78. CommandLine CmdL(Args.data(),_config);
  79. if (pkgInitConfig(*_config) == false ||
  80. CmdL.Parse(argc,argv) == false ||
  81. pkgInitSystem(*_config,_system) == false)
  82. {
  83. if (_config->FindB("version") == true)
  84. ShowHelp(CmdL);
  85. _error->DumpErrors();
  86. return 100;
  87. }
  88. // See if the help should be shown
  89. if (_config->FindB("help") == true ||
  90. _config->FindB("version") == true ||
  91. CmdL.FileSize() == 0)
  92. {
  93. ShowHelp(CmdL);
  94. return 0;
  95. }
  96. InitOutput();
  97. // Match the operation
  98. CmdL.DispatchArg(Cmds);
  99. // Print any errors or warnings found during parsing
  100. bool const Errors = _error->PendingError();
  101. if (_config->FindI("quiet",0) > 0)
  102. _error->DumpErrors();
  103. else
  104. _error->DumpErrors(GlobalError::DEBUG);
  105. return Errors == true ? 100 : 0;
  106. }
  107. /*}}}*/