apt-helper.cc 3.3 KB

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