main.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "DefaultBtContext.h"
  60. #include "FileEntry.h"
  61. #include "RequestGroup.h"
  62. #include "ConsoleStatCalc.h"
  63. #include "NullStatCalc.h"
  64. #include "download_helper.h"
  65. #include "Exception.h"
  66. #include "ProtocolDetector.h"
  67. #include "RecoverableException.h"
  68. #include "SocketCore.h"
  69. #ifdef ENABLE_METALINK
  70. # include "MetalinkHelper.h"
  71. # include "MetalinkEntry.h"
  72. #endif // ENABLE_METALINK
  73. #ifdef ENABLE_MESSAGE_DIGEST
  74. # include "MessageDigestHelper.h"
  75. #endif // ENABLE_MESSAGE_DIGEST
  76. extern char* optarg;
  77. extern int optind, opterr, optopt;
  78. namespace aria2 {
  79. // output stream to /dev/null
  80. std::ofstream nullout(DEV_NULL);
  81. SharedHandle<StatCalc> getStatCalc(const Option* op)
  82. {
  83. SharedHandle<StatCalc> statCalc;
  84. if(op->getAsBool(PREF_QUIET)) {
  85. statCalc.reset(new NullStatCalc());
  86. } else {
  87. statCalc.reset(new ConsoleStatCalc(op->getAsInt(PREF_SUMMARY_INTERVAL)));
  88. }
  89. return statCalc;
  90. }
  91. std::ostream& getSummaryOut(const Option* op)
  92. {
  93. if(op->getAsBool(PREF_QUIET)) {
  94. return nullout;
  95. } else {
  96. return std::cout;
  97. }
  98. }
  99. #ifdef ENABLE_BITTORRENT
  100. static void showTorrentFile(const std::string& uri)
  101. {
  102. SharedHandle<DefaultBtContext> btContext(new DefaultBtContext());
  103. btContext->load(uri);
  104. std::cout << btContext << std::endl;
  105. }
  106. #endif // ENABLE_BITTORRENT
  107. #ifdef ENABLE_METALINK
  108. static void showMetalinkFile(const std::string& uri, const Option& op)
  109. {
  110. std::deque<SharedHandle<MetalinkEntry> > metalinkEntries;
  111. MetalinkHelper::parseAndQuery(metalinkEntries, uri, &op);
  112. std::deque<SharedHandle<FileEntry> > fileEntries;
  113. MetalinkEntry::toFileEntry(fileEntries, metalinkEntries);
  114. Util::toStream(std::cout, fileEntries);
  115. std::cout << std::endl;
  116. }
  117. #endif // ENABLE_METALINK
  118. #if defined ENABLE_BITTORRENT || defined ENABLE_METALINK
  119. static void showFiles(const std::deque<std::string>& uris, const Option& op)
  120. {
  121. ProtocolDetector dt;
  122. for(std::deque<std::string>::const_iterator i = uris.begin();
  123. i != uris.end(); ++i) {
  124. printf(">>> ");
  125. printf(MSG_SHOW_FILES, (*i).c_str());
  126. printf("\n");
  127. try {
  128. #ifdef ENABLE_BITTORRENT
  129. if(dt.guessTorrentFile(*i)) {
  130. showTorrentFile(*i);
  131. } else
  132. #endif // ENABLE_BITTORRENT
  133. #ifdef ENABLE_METALINK
  134. if(dt.guessMetalinkFile(*i)) {
  135. showMetalinkFile(*i, op);
  136. } else
  137. #endif // ENABLE_METALINK
  138. {
  139. printf(MSG_NOT_TORRENT_METALINK);
  140. printf("\n\n");
  141. }
  142. } catch(RecoverableException& e) {
  143. std::cout << e.stackTrace() << std::endl;
  144. }
  145. }
  146. }
  147. #endif // ENABLE_BITTORRENT || ENABLE_METALINK
  148. extern void option_processing(Option& option, std::deque<std::string>& uris,
  149. int argc, char* const argv[]);
  150. DownloadResult::RESULT main(int argc, char* argv[])
  151. {
  152. std::deque<std::string> args;
  153. Option op;
  154. option_processing(op, args, argc, argv);
  155. SimpleRandomizer::init();
  156. BitfieldManFactory::setDefaultRandomizer(SimpleRandomizer::getInstance());
  157. if(op.get(PREF_LOG) == "-") {
  158. LogFactory::setLogFile(DEV_STDOUT);
  159. } else if(!op.get(PREF_LOG).empty()) {
  160. LogFactory::setLogFile(op.get(PREF_LOG));
  161. } else {
  162. LogFactory::setLogFile(DEV_NULL);
  163. }
  164. LogFactory::setLogLevel(op.get(PREF_LOG_LEVEL));
  165. if(op.getAsBool(PREF_QUIET)) {
  166. LogFactory::setConsoleOutput(false);
  167. }
  168. #ifdef HAVE_EPOLL
  169. if(op.get(PREF_EVENT_POLL) == V_EPOLL) {
  170. SocketCore::useEpoll();
  171. } else
  172. #endif // HAVE_EPOLL
  173. if(op.get(PREF_EVENT_POLL) == V_SELECT) {
  174. SocketCore::useSelect();
  175. }
  176. DownloadResult::RESULT exitStatus = DownloadResult::FINISHED;
  177. try {
  178. Logger* logger = LogFactory::getInstance();
  179. logger->info("<<--- --- --- ---");
  180. logger->info(" --- --- --- ---");
  181. logger->info(" --- --- --- --->>");
  182. logger->info("%s %s %s", PACKAGE, PACKAGE_VERSION, TARGET);
  183. logger->info(MSG_LOGGING_STARTED);
  184. #ifdef ENABLE_MESSAGE_DIGEST
  185. MessageDigestHelper::staticSHA1DigestInit();
  186. #endif // ENABLE_MESSAGE_DIGEST
  187. #ifdef SIGPIPE
  188. Util::setGlobalSignalHandler(SIGPIPE, SIG_IGN, 0);
  189. #endif
  190. std::deque<SharedHandle<RequestGroup> > requestGroups;
  191. #ifdef ENABLE_BITTORRENT
  192. if(!op.blank(PREF_TORRENT_FILE)) {
  193. if(op.get(PREF_SHOW_FILES) == V_TRUE) {
  194. showTorrentFile(op.get(PREF_TORRENT_FILE));
  195. return exitStatus;
  196. } else {
  197. createRequestGroupForBitTorrent(requestGroups, op, args);
  198. }
  199. }
  200. else
  201. #endif // ENABLE_BITTORRENT
  202. #ifdef ENABLE_METALINK
  203. if(!op.blank(PREF_METALINK_FILE)) {
  204. if(op.get(PREF_SHOW_FILES) == V_TRUE) {
  205. showMetalinkFile(op.get(PREF_METALINK_FILE), op);
  206. return exitStatus;
  207. } else {
  208. createRequestGroupForMetalink(requestGroups, op);
  209. }
  210. }
  211. else
  212. #endif // ENABLE_METALINK
  213. if(!op.blank(PREF_INPUT_FILE)) {
  214. createRequestGroupForUriList(requestGroups, op);
  215. #if defined ENABLE_BITTORRENT || defined ENABLE_METALINK
  216. } else if(op.get(PREF_SHOW_FILES) == V_TRUE) {
  217. showFiles(args, op);
  218. #endif // ENABLE_METALINK || ENABLE_METALINK
  219. } else {
  220. createRequestGroupForUri(requestGroups, op, args);
  221. }
  222. if(requestGroups.empty()) {
  223. std::cout << MSG_NO_FILES_TO_DOWNLOAD << std::endl;
  224. } else {
  225. exitStatus = MultiUrlRequestInfo(requestGroups, &op, getStatCalc(&op),
  226. getSummaryOut(&op)).execute();
  227. }
  228. } catch(Exception& ex) {
  229. std::cerr << EX_EXCEPTION_CAUGHT << "\n" << ex.stackTrace() << std::endl;
  230. exitStatus = DownloadResult::UNKNOWN_ERROR;
  231. }
  232. LogFactory::release();
  233. return exitStatus;
  234. }
  235. } // namespace aria2
  236. int main(int argc, char* argv[]) {
  237. aria2::Platform platform;
  238. aria2::DownloadResult::RESULT r = aria2::main(argc, argv);
  239. return r;
  240. }