apt-helper.cc 5.3 KB

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