apt-helper.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <apt-pkg/srvrec.h>
  23. #include <iostream>
  24. #include <string>
  25. #include <vector>
  26. #include <apti18n.h>
  27. /*}}}*/
  28. static bool DoDownloadFile(CommandLine &CmdL)
  29. {
  30. if (CmdL.FileSize() <= 2)
  31. return _error->Error(_("Must specify at least one pair url/filename"));
  32. pkgAcquire Fetcher;
  33. AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet",0));
  34. Fetcher.Setup(&Stat);
  35. std::string download_uri = CmdL.FileList[1];
  36. std::string targetfile = CmdL.FileList[2];
  37. std::string hash;
  38. if (CmdL.FileSize() > 3)
  39. hash = CmdL.FileList[3];
  40. new pkgAcqFile(&Fetcher, download_uri, hash, 0, "desc", "short-desc",
  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 DoSrvLookup(CommandLine &CmdL)
  50. {
  51. if (CmdL.FileSize() < 1)
  52. return _error->Error(_("Must specifc at least one srv record"));
  53. std::vector<SrvRec> srv_records;
  54. for(int i=1; CmdL.FileList[i] != NULL; i++)
  55. {
  56. if(GetSrvRecords(CmdL.FileList[i], srv_records) == false)
  57. _error->Warning(_("GetSrvRec failed for %s"), CmdL.FileList[i]);
  58. for (std::vector<SrvRec>::const_iterator I = srv_records.begin();
  59. I != srv_records.end(); ++I)
  60. {
  61. c1out << (*I).target.c_str() << " "
  62. << (*I).priority << " "
  63. << (*I).weight << " "
  64. << (*I).port << " "
  65. << std::endl;
  66. }
  67. }
  68. return true;
  69. }
  70. static bool ShowHelp(CommandLine &)
  71. {
  72. ioprintf(std::cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
  73. COMMON_ARCH,__DATE__,__TIME__);
  74. if (_config->FindB("version") == true)
  75. return true;
  76. std::cout <<
  77. _("Usage: apt-helper [options] command\n"
  78. " apt-helper [options] download-file uri target-path\n"
  79. "\n"
  80. "apt-helper is a internal helper for apt\n"
  81. "\n"
  82. "Commands:\n"
  83. " download-file - download the given uri to the target-path\n"
  84. "\n"
  85. " This APT helper has Super Meep Powers.\n");
  86. return true;
  87. }
  88. int main(int argc,const char *argv[]) /*{{{*/
  89. {
  90. CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
  91. {"download-file", &DoDownloadFile},
  92. {"srv-lookup", &DoSrvLookup},
  93. {0,0}};
  94. std::vector<CommandLine::Args> Args = getCommandArgs(
  95. "apt-download", CommandLine::GetCommand(Cmds, argc, argv));
  96. // Set up gettext support
  97. setlocale(LC_ALL,"");
  98. textdomain(PACKAGE);
  99. // Parse the command line and initialize the package library
  100. CommandLine CmdL(Args.data(),_config);
  101. if (pkgInitConfig(*_config) == false ||
  102. CmdL.Parse(argc,argv) == false ||
  103. pkgInitSystem(*_config,_system) == false)
  104. {
  105. if (_config->FindB("version") == true)
  106. ShowHelp(CmdL);
  107. _error->DumpErrors();
  108. return 100;
  109. }
  110. // See if the help should be shown
  111. if (_config->FindB("help") == true ||
  112. _config->FindB("version") == true ||
  113. CmdL.FileSize() == 0)
  114. {
  115. ShowHelp(CmdL);
  116. return 0;
  117. }
  118. InitOutput();
  119. // Match the operation
  120. CmdL.DispatchArg(Cmds);
  121. // Print any errors or warnings found during parsing
  122. bool const Errors = _error->PendingError();
  123. if (_config->FindI("quiet",0) > 0)
  124. _error->DumpErrors();
  125. else
  126. _error->DumpErrors(GlobalError::DEBUG);
  127. return Errors == true ? 100 : 0;
  128. }
  129. /*}}}*/