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