Context.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "Context.h"
  36. #include <unistd.h>
  37. #include <getopt.h>
  38. #ifdef HAVE_SYS_RESOURCE_H
  39. #include <sys/resource.h>
  40. #endif // HAVE_SYS_RESOURCE_H
  41. #include <numeric>
  42. #include <vector>
  43. #include <iostream>
  44. #include "LogFactory.h"
  45. #include "Logger.h"
  46. #include "util.h"
  47. #include "FeatureConfig.h"
  48. #include "MultiUrlRequestInfo.h"
  49. #include "SimpleRandomizer.h"
  50. #include "File.h"
  51. #include "message.h"
  52. #include "prefs.h"
  53. #include "Option.h"
  54. #include "a2algo.h"
  55. #include "a2io.h"
  56. #include "a2time.h"
  57. #include "Platform.h"
  58. #include "FileEntry.h"
  59. #include "RequestGroup.h"
  60. #include "download_helper.h"
  61. #include "Exception.h"
  62. #include "ProtocolDetector.h"
  63. #include "RecoverableException.h"
  64. #include "SocketCore.h"
  65. #include "DownloadContext.h"
  66. #include "fmt.h"
  67. #include "console.h"
  68. #include "UriListParser.h"
  69. #include "message_digest_helper.h"
  70. #ifdef ENABLE_BITTORRENT
  71. #include "bittorrent_helper.h"
  72. #endif // ENABLE_BITTORRENT
  73. #ifdef ENABLE_METALINK
  74. #include "metalink_helper.h"
  75. #include "MetalinkEntry.h"
  76. #endif // ENABLE_METALINK
  77. extern char* optarg;
  78. extern int optind, opterr, optopt;
  79. namespace aria2 {
  80. #ifdef ENABLE_BITTORRENT
  81. namespace {
  82. void showTorrentFile(const std::string& uri)
  83. {
  84. auto op = std::make_shared<Option>();
  85. auto dctx = std::make_shared<DownloadContext>();
  86. bittorrent::load(uri, dctx, op);
  87. bittorrent::print(*global::cout(), dctx);
  88. }
  89. } // namespace
  90. #endif // ENABLE_BITTORRENT
  91. #ifdef ENABLE_METALINK
  92. namespace {
  93. void showMetalinkFile(const std::string& uri, const std::shared_ptr<Option>& op)
  94. {
  95. auto fileEntries = MetalinkEntry::toFileEntry(
  96. metalink::parseAndQuery(uri, op.get(), op->get(PREF_METALINK_BASE_URI)));
  97. util::toStream(std::begin(fileEntries), std::end(fileEntries),
  98. *global::cout());
  99. global::cout()->write("\n");
  100. global::cout()->flush();
  101. }
  102. } // namespace
  103. #endif // ENABLE_METALINK
  104. #if defined(ENABLE_BITTORRENT) || defined(ENABLE_METALINK)
  105. namespace {
  106. void showFiles(const std::vector<std::string>& uris,
  107. const std::shared_ptr<Option>& op)
  108. {
  109. ProtocolDetector dt;
  110. for (const auto& uri : uris) {
  111. printf(">>> ");
  112. printf(MSG_SHOW_FILES, (uri).c_str());
  113. printf("\n");
  114. try {
  115. #ifdef ENABLE_BITTORRENT
  116. if (dt.guessTorrentFile(uri)) {
  117. showTorrentFile(uri);
  118. }
  119. else
  120. #endif // ENABLE_BITTORRENT
  121. #ifdef ENABLE_METALINK
  122. if (dt.guessMetalinkFile(uri)) {
  123. showMetalinkFile(uri, op);
  124. }
  125. else
  126. #endif // ENABLE_METALINK
  127. {
  128. printf("%s\n\n", MSG_NOT_TORRENT_METALINK);
  129. }
  130. }
  131. catch (RecoverableException& e) {
  132. global::cout()->printf("%s\n", e.stackTrace().c_str());
  133. }
  134. }
  135. }
  136. } // namespace
  137. #endif // ENABLE_BITTORRENT || ENABLE_METALINK
  138. extern error_code::Value option_processing(Option& option, bool standalone,
  139. std::vector<std::string>& uris,
  140. int argc, char** argv,
  141. const KeyVals& options);
  142. Context::Context(bool standalone, int argc, char** argv, const KeyVals& options)
  143. {
  144. std::vector<std::string> args;
  145. auto op = std::make_shared<Option>();
  146. error_code::Value rv;
  147. rv = option_processing(*op.get(), standalone, args, argc, argv, options);
  148. if (rv != error_code::FINISHED) {
  149. if (standalone) {
  150. exit(rv);
  151. }
  152. else {
  153. throw DL_ABORT_EX("Option processing failed");
  154. }
  155. }
  156. #ifdef ENABLE_BITTORRENT
  157. bittorrent::generateStaticPeerId(op->get(PREF_PEER_ID_PREFIX));
  158. #endif // ENABLE_BITTORRENT
  159. LogFactory::setLogFile(op->get(PREF_LOG));
  160. LogFactory::setLogLevel(op->get(PREF_LOG_LEVEL));
  161. LogFactory::setConsoleLogLevel(op->get(PREF_CONSOLE_LOG_LEVEL));
  162. LogFactory::setColorOutput(op->getAsBool(PREF_ENABLE_COLOR));
  163. if (op->getAsBool(PREF_QUIET)) {
  164. LogFactory::setConsoleOutput(false);
  165. }
  166. LogFactory::reconfigure();
  167. A2_LOG_INFO("<<--- --- --- ---");
  168. A2_LOG_INFO(" --- --- --- ---");
  169. A2_LOG_INFO(" --- --- --- --->>");
  170. A2_LOG_INFO(fmt("%s %s", PACKAGE, PACKAGE_VERSION));
  171. A2_LOG_INFO(usedCompilerAndPlatform());
  172. A2_LOG_INFO(getOperatingSystemInfo());
  173. A2_LOG_INFO(usedLibs());
  174. A2_LOG_INFO(MSG_LOGGING_STARTED);
  175. #if defined(HAVE_SYS_RESOURCE_H) && defined(RLIMIT_NOFILE)
  176. rlimit r = {0, 0};
  177. if (getrlimit(RLIMIT_NOFILE, &r) >= 0 && r.rlim_cur != RLIM_INFINITY) {
  178. // Thanks portability, for making it easy :p
  179. auto rlim_new = r.rlim_cur; // So we get the right type for free.
  180. if (r.rlim_cur != RLIM_INFINITY) {
  181. rlim_new = op->getAsInt(PREF_RLIMIT_NOFILE);
  182. rlim_new = std::max(r.rlim_cur, rlim_new);
  183. if (r.rlim_max != RLIM_INFINITY) {
  184. rlim_new = std::min(r.rlim_max, rlim_new);
  185. }
  186. }
  187. if (rlim_new != r.rlim_cur) {
  188. if (setrlimit(RLIMIT_NOFILE, &r) != 0) {
  189. int errNum = errno;
  190. A2_LOG_WARN(fmt("Failed to set rlimit NO_FILE from %" PRIu64 " to "
  191. "%" PRIu64 ": %s",
  192. (uint64_t)r.rlim_cur, (uint64_t)rlim_new,
  193. util::safeStrerror(errNum).c_str()));
  194. }
  195. else {
  196. A2_LOG_DEBUG(fmt("Set rlimit NO_FILE from %" PRIu64 " to %" PRIu64,
  197. (uint64_t)r.rlim_cur, (uint64_t)rlim_new));
  198. }
  199. }
  200. else {
  201. rlim_new = op->getAsInt(PREF_RLIMIT_NOFILE);
  202. A2_LOG_DEBUG(fmt("Not setting rlimit NO_FILE: %" PRIu64 " >= %" PRIu64,
  203. (uint64_t)r.rlim_cur, (uint64_t)rlim_new));
  204. }
  205. }
  206. #endif // defined(HAVE_SYS_RESOURCE_H) && defined(RLIMIT_NOFILE)
  207. if (op->getAsBool(PREF_DISABLE_IPV6)) {
  208. SocketCore::setProtocolFamily(AF_INET);
  209. }
  210. SocketCore::setIpDscp(op->getAsInt(PREF_DSCP));
  211. SocketCore::setSocketRecvBufferSize(
  212. op->getAsInt(PREF_SOCKET_RECV_BUFFER_SIZE));
  213. net::checkAddrconfig();
  214. if (!net::getIPv4AddrConfigured() && !net::getIPv6AddrConfigured()) {
  215. // Get rid of AI_ADDRCONFIG. It causes name resolution error when
  216. // none of network interface has IPv4/v6 address.
  217. setDefaultAIFlags(0);
  218. }
  219. // Bind interface
  220. if (!op->get(PREF_INTERFACE).empty()) {
  221. std::string iface = op->get(PREF_INTERFACE);
  222. SocketCore::bindAddress(iface);
  223. }
  224. // Bind multiple interfaces
  225. if (!op->get(PREF_MULTIPLE_INTERFACE).empty() &&
  226. op->get(PREF_INTERFACE).empty()) {
  227. std::string ifaces = op->get(PREF_MULTIPLE_INTERFACE);
  228. SocketCore::bindAllAddress(ifaces);
  229. }
  230. std::vector<std::shared_ptr<RequestGroup>> requestGroups;
  231. std::shared_ptr<UriListParser> uriListParser;
  232. #ifdef ENABLE_BITTORRENT
  233. if (!op->blank(PREF_TORRENT_FILE)) {
  234. if (op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
  235. showTorrentFile(op->get(PREF_TORRENT_FILE));
  236. return;
  237. }
  238. else {
  239. createRequestGroupForBitTorrent(requestGroups, op, args,
  240. op->get(PREF_TORRENT_FILE));
  241. }
  242. }
  243. else
  244. #endif // ENABLE_BITTORRENT
  245. #ifdef ENABLE_METALINK
  246. if (!op->blank(PREF_METALINK_FILE)) {
  247. if (op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
  248. showMetalinkFile(op->get(PREF_METALINK_FILE), op);
  249. return;
  250. }
  251. else {
  252. createRequestGroupForMetalink(requestGroups, op);
  253. }
  254. }
  255. else
  256. #endif // ENABLE_METALINK
  257. if (!op->blank(PREF_INPUT_FILE)) {
  258. if (op->getAsBool(PREF_DEFERRED_INPUT)) {
  259. uriListParser = openUriListParser(op->get(PREF_INPUT_FILE));
  260. }
  261. else {
  262. createRequestGroupForUriList(requestGroups, op);
  263. }
  264. #if defined(ENABLE_BITTORRENT) || defined(ENABLE_METALINK)
  265. }
  266. else if (op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
  267. showFiles(args, op);
  268. return;
  269. #endif // ENABLE_METALINK || ENABLE_METALINK
  270. }
  271. else {
  272. createRequestGroupForUri(requestGroups, op, args, false, false, true);
  273. }
  274. // Remove option values which is only valid for URIs specified in
  275. // command-line. If they are left, because op is used as a template
  276. // for new RequestGroup(such as created in RPC command), they causes
  277. // unintentional effect.
  278. op->remove(PREF_OUT);
  279. op->remove(PREF_FORCE_SEQUENTIAL);
  280. op->remove(PREF_INPUT_FILE);
  281. op->remove(PREF_INDEX_OUT);
  282. op->remove(PREF_SELECT_FILE);
  283. op->remove(PREF_PAUSE);
  284. op->remove(PREF_CHECKSUM);
  285. op->remove(PREF_GID);
  286. if (standalone && !op->getAsBool(PREF_ENABLE_RPC) && requestGroups.empty() &&
  287. !uriListParser) {
  288. global::cout()->printf("%s\n", MSG_NO_FILES_TO_DOWNLOAD);
  289. }
  290. else {
  291. if (!requestGroups.empty()) {
  292. A2_LOG_NOTICE(fmt("Downloading %" PRId64 " items",
  293. static_cast<uint64_t>(requestGroups.size())));
  294. }
  295. reqinfo = std::make_shared<MultiUrlRequestInfo>(std::move(requestGroups),
  296. op, uriListParser);
  297. }
  298. }
  299. Context::~Context() {}
  300. } // namespace aria2