apt-helper.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. HashString hash;
  39. if (CmdL.FileSize() > 3)
  40. hash = HashString(CmdL.FileList[3]);
  41. new pkgAcqFile(&Fetcher, download_uri, "", 0, "desc", "short-desc",
  42. "dest-dir-ignored", targetfile);
  43. Fetcher.Run();
  44. if (!FileExists(targetfile))
  45. {
  46. _error->Error(_("Download Failed"));
  47. return false;
  48. }
  49. if(hash.empty() == false)
  50. {
  51. if(hash.VerifyFile(targetfile) == false)
  52. {
  53. _error->Error(_("HashSum Failed"));
  54. Rename(targetfile, targetfile+".failed");
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. bool ShowHelp(CommandLine &CmdL)
  61. {
  62. ioprintf(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. 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. "\n"
  75. " This APT helper has Super Meep Powers.\n");
  76. return true;
  77. }
  78. int main(int argc,const char *argv[]) /*{{{*/
  79. {
  80. CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
  81. {"download-file", &DoDownloadFile},
  82. {0,0}};
  83. std::vector<CommandLine::Args> Args = getCommandArgs(
  84. "apt-download", CommandLine::GetCommand(Cmds, argc, argv));
  85. // Set up gettext support
  86. setlocale(LC_ALL,"");
  87. textdomain(PACKAGE);
  88. // Parse the command line and initialize the package library
  89. CommandLine CmdL(Args.data(),_config);
  90. if (pkgInitConfig(*_config) == false ||
  91. CmdL.Parse(argc,argv) == false ||
  92. pkgInitSystem(*_config,_system) == false)
  93. {
  94. if (_config->FindB("version") == true)
  95. ShowHelp(CmdL);
  96. _error->DumpErrors();
  97. return 100;
  98. }
  99. // See if the help should be shown
  100. if (_config->FindB("help") == true ||
  101. _config->FindB("version") == true ||
  102. CmdL.FileSize() == 0)
  103. {
  104. ShowHelp(CmdL);
  105. return 0;
  106. }
  107. InitOutput();
  108. // Match the operation
  109. CmdL.DispatchArg(Cmds);
  110. // Print any errors or warnings found during parsing
  111. bool const Errors = _error->PendingError();
  112. if (_config->FindI("quiet",0) > 0)
  113. _error->DumpErrors();
  114. else
  115. _error->DumpErrors(GlobalError::DEBUG);
  116. return Errors == true ? 100 : 0;
  117. }
  118. /*}}}*/