apt-helper.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <apt-private/private-main.h>
  24. #include <apt-pkg/srvrec.h>
  25. #include <iostream>
  26. #include <string>
  27. #include <vector>
  28. #include <apti18n.h>
  29. /*}}}*/
  30. static bool DoAutoDetectProxy(CommandLine &CmdL) /*{{{*/
  31. {
  32. if (CmdL.FileSize() != 2)
  33. return _error->Error(_("Need one URL as argument"));
  34. URI ServerURL(CmdL.FileList[1]);
  35. AutoDetectProxy(ServerURL);
  36. std::string SpecificProxy = _config->Find("Acquire::"+ServerURL.Access+"::Proxy::" + ServerURL.Host);
  37. ioprintf(std::cout, "Using proxy '%s' for URL '%s'\n",
  38. SpecificProxy.c_str(), std::string(ServerURL).c_str());
  39. return true;
  40. }
  41. /*}}}*/
  42. static bool DoDownloadFile(CommandLine &CmdL) /*{{{*/
  43. {
  44. if (CmdL.FileSize() <= 2)
  45. return _error->Error(_("Must specify at least one pair url/filename"));
  46. aptAcquireWithTextStatus Fetcher;
  47. size_t fileind = 0;
  48. std::vector<std::string> targetfiles;
  49. while (fileind + 2 <= CmdL.FileSize())
  50. {
  51. std::string download_uri = CmdL.FileList[fileind + 1];
  52. std::string targetfile = CmdL.FileList[fileind + 2];
  53. std::string hash;
  54. if (CmdL.FileSize() > fileind + 3)
  55. hash = CmdL.FileList[fileind + 3];
  56. // we use download_uri as descr and targetfile as short-descr
  57. new pkgAcqFile(&Fetcher, download_uri, hash, 0, download_uri, targetfile,
  58. "dest-dir-ignored", targetfile);
  59. targetfiles.push_back(targetfile);
  60. fileind += 3;
  61. }
  62. bool Failed = false;
  63. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
  64. return _error->Error(_("Download Failed"));
  65. if (targetfiles.empty() == false)
  66. for (std::vector<std::string>::const_iterator f = targetfiles.begin(); f != targetfiles.end(); ++f)
  67. if (FileExists(*f) == false)
  68. return _error->Error(_("Download Failed"));
  69. return true;
  70. }
  71. /*}}}*/
  72. static bool DoSrvLookup(CommandLine &CmdL) /*{{{*/
  73. {
  74. if (CmdL.FileSize() <= 1)
  75. return _error->Error("Must specify at least one SRV record");
  76. for(size_t i = 1; CmdL.FileList[i] != NULL; ++i)
  77. {
  78. std::vector<SrvRec> srv_records;
  79. std::string const name = CmdL.FileList[i];
  80. c0out << "# Target\tPriority\tWeight\tPort # for " << name << std::endl;
  81. size_t const found = name.find(":");
  82. if (found != std::string::npos)
  83. {
  84. std::string const host = name.substr(0, found);
  85. size_t const port = atoi(name.c_str() + found + 1);
  86. if(GetSrvRecords(host, port, srv_records) == false)
  87. _error->Error(_("GetSrvRec failed for %s"), name.c_str());
  88. }
  89. else if(GetSrvRecords(name, srv_records) == false)
  90. _error->Error(_("GetSrvRec failed for %s"), name.c_str());
  91. for (SrvRec const &I : srv_records)
  92. c1out << I.target << "\t" << I.priority << "\t" << I.weight << "\t" << I.port << std::endl;
  93. }
  94. return true;
  95. }
  96. /*}}}*/
  97. bool ShowHelp(CommandLine &, aptDispatchWithHelp const * Cmds) /*{{{*/
  98. {
  99. ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
  100. if (_config->FindB("version") == true)
  101. return true;
  102. std::cout <<
  103. _("Usage: apt-helper [options] command\n"
  104. " apt-helper [options] download-file uri target-path\n"
  105. "\n"
  106. "apt-helper is a internal helper for apt\n")
  107. << std::endl
  108. << _("Commands:") << std::endl;
  109. for (; Cmds->Handler != nullptr; ++Cmds)
  110. {
  111. if (Cmds->Help == nullptr)
  112. continue;
  113. std::cout << " " << Cmds->Match << " - " << Cmds->Help << std::endl;
  114. }
  115. std::cout << std::endl <<
  116. _("This APT helper has Super Meep Powers.") << std::endl;
  117. return true;
  118. }
  119. /*}}}*/
  120. std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
  121. {
  122. return {
  123. {"download-file", &DoDownloadFile, _("download the given uri to the target-path")},
  124. {"srv-lookup", &DoSrvLookup, _("lookup a SRV record (e.g. _http._tcp.ftp.debian.org)")},
  125. {"auto-detect-proxy", &DoAutoDetectProxy, _("detect proxy using apt.conf")},
  126. {nullptr, nullptr, nullptr}
  127. };
  128. }
  129. /*}}}*/
  130. int main(int argc,const char *argv[]) /*{{{*/
  131. {
  132. InitLocale();
  133. CommandLine CmdL;
  134. auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_HELPER, &_config, &_system, argc, argv);
  135. InitOutput();
  136. return DispatchCommandLine(CmdL, Cmds);
  137. }
  138. /*}}}*/