main.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 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 "common.h"
  36. #include <signal.h>
  37. #include <unistd.h>
  38. #include <getopt.h>
  39. #include <numeric>
  40. #include <vector>
  41. #include <iostream>
  42. #include "SharedHandle.h"
  43. #include "LogFactory.h"
  44. #include "Logger.h"
  45. #include "util.h"
  46. #include "FeatureConfig.h"
  47. #include "MultiUrlRequestInfo.h"
  48. #include "SimpleRandomizer.h"
  49. #include "File.h"
  50. #include "message.h"
  51. #include "prefs.h"
  52. #include "Option.h"
  53. #include "a2algo.h"
  54. #include "a2io.h"
  55. #include "a2time.h"
  56. #include "Platform.h"
  57. #include "FileEntry.h"
  58. #include "RequestGroup.h"
  59. #include "ConsoleStatCalc.h"
  60. #include "NullStatCalc.h"
  61. #include "download_helper.h"
  62. #include "Exception.h"
  63. #include "ProtocolDetector.h"
  64. #include "RecoverableException.h"
  65. #include "SocketCore.h"
  66. #include "DownloadContext.h"
  67. #include "fmt.h"
  68. #include "NullOutputFile.h"
  69. #include "console.h"
  70. #include "UriListParser.h"
  71. #ifdef ENABLE_BITTORRENT
  72. # include "bittorrent_helper.h"
  73. #endif // ENABLE_BITTORRENT
  74. #ifdef ENABLE_METALINK
  75. # include "metalink_helper.h"
  76. # include "MetalinkEntry.h"
  77. #endif // ENABLE_METALINK
  78. #ifdef ENABLE_MESSAGE_DIGEST
  79. # include "message_digest_helper.h"
  80. #endif // ENABLE_MESSAGE_DIGEST
  81. extern char* optarg;
  82. extern int optind, opterr, optopt;
  83. namespace aria2 {
  84. SharedHandle<StatCalc> getStatCalc(const SharedHandle<Option>& op)
  85. {
  86. SharedHandle<StatCalc> statCalc;
  87. if(op->getAsBool(PREF_QUIET)) {
  88. statCalc.reset(new NullStatCalc());
  89. } else {
  90. SharedHandle<ConsoleStatCalc> impl
  91. (new ConsoleStatCalc(op->getAsInt(PREF_SUMMARY_INTERVAL),
  92. op->getAsBool(PREF_HUMAN_READABLE)));
  93. impl->setReadoutVisibility(op->getAsBool(PREF_SHOW_CONSOLE_READOUT));
  94. impl->setTruncate(op->getAsBool(PREF_TRUNCATE_CONSOLE_READOUT));
  95. statCalc = impl;
  96. }
  97. return statCalc;
  98. }
  99. SharedHandle<OutputFile> getSummaryOut(const SharedHandle<Option>& op)
  100. {
  101. if(op->getAsBool(PREF_QUIET)) {
  102. return SharedHandle<OutputFile>(new NullOutputFile());
  103. } else {
  104. return global::cout();
  105. }
  106. }
  107. #ifdef ENABLE_BITTORRENT
  108. namespace {
  109. void showTorrentFile(const std::string& uri)
  110. {
  111. SharedHandle<Option> op(new Option());
  112. SharedHandle<DownloadContext> dctx(new DownloadContext());
  113. bittorrent::load(uri, dctx, op);
  114. bittorrent::print(*global::cout(), dctx);
  115. }
  116. } // namespace
  117. #endif // ENABLE_BITTORRENT
  118. #ifdef ENABLE_METALINK
  119. namespace {
  120. void showMetalinkFile
  121. (const std::string& uri, const SharedHandle<Option>& op)
  122. {
  123. std::vector<SharedHandle<MetalinkEntry> > metalinkEntries;
  124. metalink::parseAndQuery(metalinkEntries, uri, op.get(),
  125. op->get(PREF_METALINK_BASE_URI));
  126. std::vector<SharedHandle<FileEntry> > fileEntries;
  127. MetalinkEntry::toFileEntry(fileEntries, metalinkEntries);
  128. util::toStream(fileEntries.begin(), fileEntries.end(), *global::cout());
  129. global::cout()->write("\n");
  130. global::cout()->flush();
  131. }
  132. } // namespace
  133. #endif // ENABLE_METALINK
  134. #if defined ENABLE_BITTORRENT || defined ENABLE_METALINK
  135. namespace {
  136. void showFiles
  137. (const std::vector<std::string>& uris, const SharedHandle<Option>& op)
  138. {
  139. ProtocolDetector dt;
  140. for(std::vector<std::string>::const_iterator i = uris.begin(),
  141. eoi = uris.end(); i != eoi; ++i) {
  142. printf(">>> ");
  143. printf(MSG_SHOW_FILES, (*i).c_str());
  144. printf("\n");
  145. try {
  146. #ifdef ENABLE_BITTORRENT
  147. if(dt.guessTorrentFile(*i)) {
  148. showTorrentFile(*i);
  149. } else
  150. #endif // ENABLE_BITTORRENT
  151. #ifdef ENABLE_METALINK
  152. if(dt.guessMetalinkFile(*i)) {
  153. showMetalinkFile(*i, op);
  154. } else
  155. #endif // ENABLE_METALINK
  156. {
  157. printf(MSG_NOT_TORRENT_METALINK);
  158. printf("\n\n");
  159. }
  160. } catch(RecoverableException& e) {
  161. global::cout()->printf("%s\n", e.stackTrace().c_str());
  162. }
  163. }
  164. }
  165. } // namespace
  166. #endif // ENABLE_BITTORRENT || ENABLE_METALINK
  167. extern void option_processing(Option& option, std::vector<std::string>& uris,
  168. int argc, char* argv[]);
  169. error_code::Value main(int argc, char* argv[])
  170. {
  171. std::vector<std::string> args;
  172. SharedHandle<Option> op(new Option());
  173. option_processing(*op.get(), args, argc, argv);
  174. SimpleRandomizer::init();
  175. #ifdef ENABLE_BITTORRENT
  176. bittorrent::generateStaticPeerId(op->get(PREF_PEER_ID_PREFIX));
  177. #endif // ENABLE_BITTORRENT
  178. LogFactory::setLogFile(op->get(PREF_LOG));
  179. LogFactory::setLogLevel(op->get(PREF_LOG_LEVEL));
  180. if(op->getAsBool(PREF_QUIET)) {
  181. LogFactory::setConsoleOutput(false);
  182. }
  183. LogFactory::reconfigure();
  184. error_code::Value exitStatus = error_code::FINISHED;
  185. A2_LOG_INFO("<<--- --- --- ---");
  186. A2_LOG_INFO(" --- --- --- ---");
  187. A2_LOG_INFO(" --- --- --- --->>");
  188. A2_LOG_INFO(fmt("%s %s %s", PACKAGE, PACKAGE_VERSION, TARGET));
  189. A2_LOG_INFO(MSG_LOGGING_STARTED);
  190. if(op->getAsBool(PREF_DISABLE_IPV6)) {
  191. SocketCore::setProtocolFamily(AF_INET);
  192. // Get rid of AI_ADDRCONFIG. It causes name resolution error
  193. // when none of network interface has IPv4 address.
  194. setDefaultAIFlags(0);
  195. }
  196. // Bind interface
  197. if(!op->get(PREF_INTERFACE).empty()) {
  198. std::string iface = op->get(PREF_INTERFACE);
  199. SocketCore::bindAddress(iface);
  200. }
  201. #ifdef SIGPIPE
  202. util::setGlobalSignalHandler(SIGPIPE, SIG_IGN, 0);
  203. #endif
  204. #ifdef SIGCHLD
  205. // Avoid to create zombie process when forked child processes are
  206. // died.
  207. util::setGlobalSignalHandler(SIGCHLD, SIG_IGN, 0);
  208. #endif // SIGCHILD
  209. std::vector<SharedHandle<RequestGroup> > requestGroups;
  210. SharedHandle<UriListParser> uriListParser;
  211. #ifdef ENABLE_BITTORRENT
  212. if(!op->blank(PREF_TORRENT_FILE)) {
  213. if(op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
  214. showTorrentFile(op->get(PREF_TORRENT_FILE));
  215. return exitStatus;
  216. } else {
  217. createRequestGroupForBitTorrent(requestGroups, op, args,
  218. op->get(PREF_TORRENT_FILE));
  219. }
  220. }
  221. else
  222. #endif // ENABLE_BITTORRENT
  223. #ifdef ENABLE_METALINK
  224. if(!op->blank(PREF_METALINK_FILE)) {
  225. if(op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
  226. showMetalinkFile(op->get(PREF_METALINK_FILE), op);
  227. return exitStatus;
  228. } else {
  229. createRequestGroupForMetalink(requestGroups, op);
  230. }
  231. }
  232. else
  233. #endif // ENABLE_METALINK
  234. if(!op->blank(PREF_INPUT_FILE)) {
  235. if(op->getAsBool(PREF_DEFERRED_INPUT)) {
  236. uriListParser = openUriListParser(op->get(PREF_INPUT_FILE));
  237. } else {
  238. createRequestGroupForUriList(requestGroups, op);
  239. }
  240. #if defined ENABLE_BITTORRENT || defined ENABLE_METALINK
  241. } else if(op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
  242. showFiles(args, op);
  243. return exitStatus;
  244. #endif // ENABLE_METALINK || ENABLE_METALINK
  245. } else {
  246. createRequestGroupForUri(requestGroups, op, args, false, false, true);
  247. }
  248. // Remove option values which is only valid for URIs specified in
  249. // command-line. If they are left, because op is used as a template
  250. // for new RequestGroup(such as created in RPC command), they causes
  251. // unintentional effect.
  252. op->remove(PREF_OUT);
  253. op->remove(PREF_FORCE_SEQUENTIAL);
  254. op->remove(PREF_INPUT_FILE);
  255. op->remove(PREF_INDEX_OUT);
  256. op->remove(PREF_SELECT_FILE);
  257. op->remove(PREF_PAUSE);
  258. op->remove(PREF_CHECKSUM);
  259. if(!op->getAsBool(PREF_ENABLE_RPC) && requestGroups.empty() &&
  260. !uriListParser) {
  261. global::cout()->printf("%s\n", MSG_NO_FILES_TO_DOWNLOAD);
  262. } else {
  263. exitStatus = MultiUrlRequestInfo(requestGroups, op, getStatCalc(op),
  264. getSummaryOut(op),
  265. uriListParser).execute();
  266. }
  267. return exitStatus;
  268. }
  269. } // namespace aria2
  270. int main(int argc, char* argv[])
  271. {
  272. aria2::error_code::Value r;
  273. try {
  274. aria2::Platform platform;
  275. r = aria2::main(argc, argv);
  276. } catch(aria2::Exception& ex) {
  277. aria2::global::cerr()->printf("%s\n%s\n",
  278. EX_EXCEPTION_CAUGHT,
  279. ex.stackTrace().c_str());
  280. r = ex.getErrorCode();
  281. }
  282. return r;
  283. }