apt-helper.cc 6.6 KB

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