main.cc 9.1 KB

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