main.cc 9.7 KB

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