apt-helper.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <stdlib.h>
  29. #include <apti18n.h>
  30. /*}}}*/
  31. static bool DoAutoDetectProxy(CommandLine &CmdL) /*{{{*/
  32. {
  33. if (CmdL.FileSize() != 2)
  34. return _error->Error(_("Need one URL as argument"));
  35. URI ServerURL(CmdL.FileList[1]);
  36. if (AutoDetectProxy(ServerURL) == false)
  37. return false;
  38. std::string SpecificProxy = _config->Find("Acquire::"+ServerURL.Access+"::Proxy::" + ServerURL.Host);
  39. ioprintf(std::cout, "Using proxy '%s' for URL '%s'\n",
  40. SpecificProxy.c_str(), std::string(ServerURL).c_str());
  41. return true;
  42. }
  43. /*}}}*/
  44. static bool DoDownloadFile(CommandLine &CmdL) /*{{{*/
  45. {
  46. if (CmdL.FileSize() <= 2)
  47. return _error->Error(_("Must specify at least one pair url/filename"));
  48. aptAcquireWithTextStatus Fetcher;
  49. size_t fileind = 0;
  50. std::vector<std::string> targetfiles;
  51. while (fileind + 2 <= CmdL.FileSize())
  52. {
  53. std::string download_uri = CmdL.FileList[fileind + 1];
  54. std::string targetfile = CmdL.FileList[fileind + 2];
  55. std::string hash;
  56. if (CmdL.FileSize() > fileind + 3)
  57. hash = CmdL.FileList[fileind + 3];
  58. // we use download_uri as descr and targetfile as short-descr
  59. new pkgAcqFile(&Fetcher, download_uri, hash, 0, download_uri, targetfile,
  60. "dest-dir-ignored", targetfile);
  61. targetfiles.push_back(targetfile);
  62. fileind += 3;
  63. }
  64. bool Failed = false;
  65. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
  66. return _error->Error(_("Download Failed"));
  67. if (targetfiles.empty() == false)
  68. for (std::vector<std::string>::const_iterator f = targetfiles.begin(); f != targetfiles.end(); ++f)
  69. if (FileExists(*f) == false)
  70. return _error->Error(_("Download Failed"));
  71. return true;
  72. }
  73. /*}}}*/
  74. static bool DoSrvLookup(CommandLine &CmdL) /*{{{*/
  75. {
  76. if (CmdL.FileSize() <= 1)
  77. return _error->Error("Must specify at least one SRV record");
  78. for(size_t i = 1; CmdL.FileList[i] != NULL; ++i)
  79. {
  80. std::vector<SrvRec> srv_records;
  81. std::string const name = CmdL.FileList[i];
  82. c0out << "# Target\tPriority\tWeight\tPort # for " << name << std::endl;
  83. size_t const found = name.find(":");
  84. if (found != std::string::npos)
  85. {
  86. std::string const host = name.substr(0, found);
  87. size_t const port = atoi(name.c_str() + found + 1);
  88. if(GetSrvRecords(host, port, srv_records) == false)
  89. _error->Error(_("GetSrvRec failed for %s"), name.c_str());
  90. }
  91. else if(GetSrvRecords(name, srv_records) == false)
  92. _error->Error(_("GetSrvRec failed for %s"), name.c_str());
  93. for (SrvRec const &I : srv_records)
  94. ioprintf(c1out, "%s\t%d\t%d\t%d\n", I.target.c_str(), I.priority, I.weight, I.port);
  95. }
  96. return true;
  97. }
  98. /*}}}*/
  99. static const APT::Configuration::Compressor *FindCompressor(std::vector<APT::Configuration::Compressor> const & compressors, std::string name) /*{{{*/
  100. {
  101. APT::Configuration::Compressor const * compressor = NULL;
  102. for (auto const & c : compressors)
  103. {
  104. if (compressor != NULL && c.Cost >= compressor->Cost)
  105. continue;
  106. if (c.Name == name || c.Extension == name || (!c.Extension.empty() && c.Extension.substr(1) == name))
  107. compressor = &c;
  108. }
  109. return compressor;
  110. }
  111. /*}}}*/
  112. static bool DoCatFile(CommandLine &CmdL) /*{{{*/
  113. {
  114. FileFd fd;
  115. FileFd out;
  116. std::string const compressorName = _config->Find("Apt-Helper::Cat-File::Compress", "");
  117. if (compressorName.empty() == false)
  118. {
  119. auto const compressors = APT::Configuration::getCompressors();
  120. auto const compressor = FindCompressor(compressors, compressorName);
  121. if (compressor == NULL)
  122. return _error->Error("Could not find compressor: %s", compressorName.c_str());
  123. if (out.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly, *compressor) == false)
  124. return false;
  125. } else
  126. {
  127. if (out.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly) == false)
  128. return false;
  129. }
  130. if (CmdL.FileSize() <= 1)
  131. {
  132. if (fd.OpenDescriptor(STDIN_FILENO, FileFd::ReadOnly) == false)
  133. return false;
  134. if (CopyFile(fd, out) == false)
  135. return false;
  136. return true;
  137. }
  138. for(size_t i = 1; CmdL.FileList[i] != NULL; ++i)
  139. {
  140. std::string const name = CmdL.FileList[i];
  141. if (name != "-")
  142. {
  143. if (fd.Open(name, FileFd::ReadOnly, FileFd::Extension) == false)
  144. return false;
  145. }
  146. else
  147. {
  148. if (fd.OpenDescriptor(STDIN_FILENO, FileFd::ReadOnly) == false)
  149. return false;
  150. }
  151. if (CopyFile(fd, out) == false)
  152. return false;
  153. }
  154. return true;
  155. }
  156. /*}}}*/
  157. static bool ShowHelp(CommandLine &) /*{{{*/
  158. {
  159. std::cout <<
  160. _("Usage: apt-helper [options] command\n"
  161. " apt-helper [options] cat-file file ...\n"
  162. " apt-helper [options] download-file uri target-path\n"
  163. "\n"
  164. "apt-helper bundles a variety of commands for shell scripts to use\n"
  165. "e.g. the same proxy configuration or acquire system as APT would.\n");
  166. return true;
  167. }
  168. /*}}}*/
  169. static std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
  170. {
  171. return {
  172. {"download-file", &DoDownloadFile, _("download the given uri to the target-path")},
  173. {"srv-lookup", &DoSrvLookup, _("lookup a SRV record (e.g. _http._tcp.ftp.debian.org)")},
  174. {"cat-file", &DoCatFile, _("concatenate files, with automatic decompression")},
  175. {"auto-detect-proxy", &DoAutoDetectProxy, _("detect proxy using apt.conf")},
  176. {nullptr, nullptr, nullptr}
  177. };
  178. }
  179. /*}}}*/
  180. int main(int argc,const char *argv[]) /*{{{*/
  181. {
  182. CommandLine CmdL;
  183. auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_HELPER, &_config, &_system, argc, argv, &ShowHelp, &GetCommands);
  184. InitOutput();
  185. return DispatchCommandLine(CmdL, Cmds);
  186. }
  187. /*}}}*/