You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

165 lines
5.2 KiB

  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 &)
  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. "\n"
  105. "Commands:\n"
  106. " download-file - download the given uri to the target-path\n"
  107. " srv-lookup - lookup a SRV record (e.g. _http._tcp.ftp.debian.org)\n"
  108. " auto-detect-proxy - detect proxy using apt.conf\n"
  109. "\n"
  110. " This APT helper has Super Meep Powers.\n");
  111. return true;
  112. }
  113. int main(int argc,const char *argv[]) /*{{{*/
  114. {
  115. CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
  116. {"download-file", &DoDownloadFile},
  117. {"srv-lookup", &DoSrvLookup},
  118. {"auto-detect-proxy", &DoAutoDetectProxy},
  119. {0,0}};
  120. std::vector<CommandLine::Args> Args = getCommandArgs(
  121. "apt-download", CommandLine::GetCommand(Cmds, argc, argv));
  122. // Set up gettext support
  123. setlocale(LC_ALL,"");
  124. textdomain(PACKAGE);
  125. // Parse the command line and initialize the package library
  126. CommandLine CmdL;
  127. ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
  128. InitOutput();
  129. // Match the operation
  130. CmdL.DispatchArg(Cmds);
  131. // Print any errors or warnings found during parsing
  132. bool const Errors = _error->PendingError();
  133. if (_config->FindI("quiet",0) > 0)
  134. _error->DumpErrors();
  135. else
  136. _error->DumpErrors(GlobalError::DEBUG);
  137. return Errors == true ? 100 : 0;
  138. }
  139. /*}}}*/