main.cc 8.6 KB

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