main.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 "SharedHandle.h"
  37. #include "LogFactory.h"
  38. #include "Logger.h"
  39. #include "Util.h"
  40. #include "BitfieldManFactory.h"
  41. #include "AuthConfigFactory.h"
  42. #include "CookieBoxFactory.h"
  43. #include "FeatureConfig.h"
  44. #include "MultiUrlRequestInfo.h"
  45. #include "SimpleRandomizer.h"
  46. #include "Netrc.h"
  47. #include "FatalException.h"
  48. #include "File.h"
  49. #include "CUIDCounter.h"
  50. #include "UriListParser.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 "ParameterizedStringParser.h"
  59. #include "PStringBuildVisitor.h"
  60. #include "SingleFileDownloadContext.h"
  61. #include "DefaultBtContext.h"
  62. #include "FileEntry.h"
  63. #include "RequestGroup.h"
  64. #ifdef ENABLE_METALINK
  65. # include "MetalinkHelper.h"
  66. # include "Metalink2RequestGroup.h"
  67. # include "MetalinkEntry.h"
  68. #endif // ENABLE_METALINK
  69. #ifdef ENABLE_MESSAGE_DIGEST
  70. # include "MessageDigestHelper.h"
  71. #endif // ENABLE_MESSAGE_DIGEST
  72. #include <deque>
  73. #include <signal.h>
  74. #include <unistd.h>
  75. #include <fstream>
  76. #include <iostream>
  77. extern char* optarg;
  78. extern int optind, opterr, optopt;
  79. #include <getopt.h>
  80. #ifdef HAVE_LIBSSL
  81. // for SSL
  82. # include <openssl/err.h>
  83. # include <openssl/ssl.h>
  84. #endif // HAVE_LIBSSL
  85. #ifdef HAVE_LIBGNUTLS
  86. # include <gnutls/gnutls.h>
  87. #endif // HAVE_LIBGNUTLS
  88. namespace aria2 {
  89. std::deque<std::string> unfoldURI(const std::deque<std::string>& args)
  90. {
  91. std::deque<std::string> nargs;
  92. ParameterizedStringParser p;
  93. PStringBuildVisitor v;
  94. for(std::deque<std::string>::const_iterator itr = args.begin(); itr != args.end();
  95. ++itr) {
  96. v.reset();
  97. p.parse(*itr)->accept(&v);
  98. nargs.insert(nargs.end(), v.getURIs().begin(), v.getURIs().end());
  99. }
  100. return nargs;
  101. }
  102. RequestGroupHandle createRequestGroup(const Option* op, const std::deque<std::string>& uris,
  103. const std::string& ufilename = "")
  104. {
  105. RequestGroupHandle rg = new RequestGroup(op, uris);
  106. SingleFileDownloadContextHandle dctx =
  107. new SingleFileDownloadContext(op->getAsInt(PREF_SEGMENT_SIZE),
  108. 0,
  109. "",
  110. ufilename);
  111. dctx->setDir(op->get(PREF_DIR));
  112. rg->setDownloadContext(dctx);
  113. return rg;
  114. }
  115. extern Option* option_processing(int argc, char* const argv[]);
  116. #ifdef ENABLE_BITTORRENT
  117. int32_t downloadBitTorrent(Option* op, const std::deque<std::string>& uri)
  118. {
  119. std::deque<std::string> nargs;
  120. if(op->get(PREF_PARAMETERIZED_URI) == V_TRUE) {
  121. nargs = unfoldURI(uri);
  122. } else {
  123. nargs = uri;
  124. }
  125. std::deque<std::string> xargs;
  126. ncopy(nargs.begin(), nargs.end(), op->getAsInt(PREF_SPLIT),
  127. std::back_inserter(xargs));
  128. RequestGroupHandle rg = new RequestGroup(op, xargs);
  129. DefaultBtContextHandle btContext = new DefaultBtContext();
  130. btContext->load(op->get(PREF_TORRENT_FILE));
  131. if(op->defined(PREF_PEER_ID_PREFIX)) {
  132. btContext->setPeerIdPrefix(op->get(PREF_PEER_ID_PREFIX));
  133. }
  134. btContext->setDir(op->get(PREF_DIR));
  135. rg->setDownloadContext(btContext);
  136. btContext->setOwnerRequestGroup(rg.get());
  137. RequestGroups groups;
  138. groups.push_back(rg);
  139. return MultiUrlRequestInfo(groups, op).execute();
  140. }
  141. #endif // ENABLE_BITTORRENT
  142. #ifdef ENABLE_METALINK
  143. int32_t downloadMetalink(Option* op)
  144. {
  145. RequestGroups groups = Metalink2RequestGroup(op).generate(op->get(PREF_METALINK_FILE));
  146. if(groups.empty()) {
  147. throw new FatalException("No files to download.");
  148. }
  149. return MultiUrlRequestInfo(groups, op).execute();
  150. }
  151. #endif // ENABLE_METALINK
  152. int32_t downloadUriList(Option* op, std::istream& in)
  153. {
  154. UriListParser p;
  155. RequestGroups groups;
  156. while(in) {
  157. std::deque<std::string> uris = p.parseNext(in);
  158. if(uris.size() == 1 && op->get(PREF_PARAMETERIZED_URI) == V_TRUE) {
  159. std::deque<std::string> unfoldedURIs = unfoldURI(uris);
  160. for(std::deque<std::string>::const_iterator itr = unfoldedURIs.begin();
  161. itr != unfoldedURIs.end(); ++itr) {
  162. std::deque<std::string> xuris;
  163. ncopy(itr, itr+1, op->getAsInt(PREF_SPLIT), std::back_inserter(xuris));
  164. SharedHandle<RequestGroup> rg = createRequestGroup(op, xuris);
  165. groups.push_back(rg);
  166. }
  167. } else if(uris.size() > 0) {
  168. std::deque<std::string> xuris;
  169. ncopy(uris.begin(), uris.end(), op->getAsInt(PREF_SPLIT),
  170. std::back_inserter(xuris));
  171. SharedHandle<RequestGroup> rg = createRequestGroup(op, xuris);
  172. groups.push_back(rg);
  173. }
  174. }
  175. return MultiUrlRequestInfo(groups, op).execute();
  176. }
  177. int32_t downloadUriList(Option* op)
  178. {
  179. if(op->get(PREF_INPUT_FILE) == "-") {
  180. return downloadUriList(op, std::cin);
  181. } else {
  182. if(!File(op->get(PREF_INPUT_FILE)).isFile()) {
  183. throw new FatalException(EX_FILE_OPEN, op->get(PREF_INPUT_FILE).c_str(), "No such file");
  184. }
  185. std::ifstream f(op->get(PREF_INPUT_FILE).c_str());
  186. return downloadUriList(op, f);
  187. }
  188. }
  189. int32_t downloadUri(Option* op, const std::deque<std::string>& uris)
  190. {
  191. std::deque<std::string> nargs;
  192. if(op->get(PREF_PARAMETERIZED_URI) == V_TRUE) {
  193. nargs = unfoldURI(uris);
  194. } else {
  195. nargs = uris;
  196. }
  197. RequestGroups groups;
  198. if(op->get(PREF_FORCE_SEQUENTIAL) == V_TRUE) {
  199. for(std::deque<std::string>::const_iterator itr = nargs.begin();
  200. itr != nargs.end(); ++itr) {
  201. std::deque<std::string> xuris;
  202. ncopy(itr, itr+1, op->getAsInt(PREF_SPLIT),
  203. std::back_inserter(xuris));
  204. RequestGroupHandle rg = createRequestGroup(op, xuris);
  205. groups.push_back(rg);
  206. }
  207. } else {
  208. std::deque<std::string> xargs;
  209. ncopy(nargs.begin(), nargs.end(), op->getAsInt(PREF_SPLIT),
  210. std::back_inserter(xargs));
  211. RequestGroupHandle rg = createRequestGroup(op, xargs, op->get(PREF_OUT));
  212. groups.push_back(rg);
  213. }
  214. return MultiUrlRequestInfo(groups, op).execute();
  215. }
  216. int main(int argc, char* argv[])
  217. {
  218. Option* op = option_processing(argc, argv);
  219. std::deque<std::string> args(argv+optind, argv+argc);
  220. SimpleRandomizer::init();
  221. BitfieldManFactory::setDefaultRandomizer(SimpleRandomizer::getInstance());
  222. if(op->getAsBool(PREF_STDOUT_LOG)) {
  223. LogFactory::setLogFile(DEV_STDOUT);
  224. } else if(op->get(PREF_LOG).size()) {
  225. LogFactory::setLogFile(op->get(PREF_LOG));
  226. } else {
  227. LogFactory::setLogFile(DEV_NULL);
  228. }
  229. int32_t exitStatus = EXIT_SUCCESS;
  230. try {
  231. Logger* logger = LogFactory::getInstance();
  232. logger->info("%s %s %s", PACKAGE, PACKAGE_VERSION, TARGET);
  233. logger->info(MSG_LOGGING_STARTED);
  234. AuthConfigFactoryHandle authConfigFactory = new AuthConfigFactory(op);
  235. File netrccf(op->get(PREF_NETRC_PATH));
  236. if(!op->getAsBool(PREF_NO_NETRC) && netrccf.isFile()) {
  237. mode_t mode = netrccf.mode();
  238. if(mode&(S_IRWXG|S_IRWXO)) {
  239. logger->notice(MSG_INCORRECT_NETRC_PERMISSION,
  240. op->get(PREF_NETRC_PATH).c_str());
  241. } else {
  242. NetrcHandle netrc = new Netrc();
  243. netrc->parse(op->get(PREF_NETRC_PATH));
  244. authConfigFactory->setNetrc(netrc);
  245. }
  246. }
  247. CookieBoxFactoryHandle cookieBoxFactory = new CookieBoxFactory();
  248. CookieBoxFactorySingletonHolder::instance(cookieBoxFactory);
  249. if(op->defined(PREF_LOAD_COOKIES)) {
  250. File cookieFile(op->get(PREF_LOAD_COOKIES));
  251. if(cookieFile.isFile()) {
  252. std::ifstream in(op->get(PREF_LOAD_COOKIES).c_str());
  253. CookieBoxFactorySingletonHolder::instance()->loadDefaultCookie(in);
  254. } else {
  255. logger->error(MSG_LOADING_COOKIE_FAILED, op->get(PREF_LOAD_COOKIES).c_str());
  256. exit(EXIT_FAILURE);
  257. }
  258. }
  259. AuthConfigFactorySingleton::instance(authConfigFactory);
  260. CUIDCounterHandle cuidCounter = new CUIDCounter();
  261. CUIDCounterSingletonHolder::instance(cuidCounter);
  262. #ifdef ENABLE_MESSAGE_DIGEST
  263. MessageDigestHelper::staticSHA1DigestInit();
  264. #endif // ENABLE_MESSAGE_DIGEST
  265. #ifdef SIGPIPE
  266. Util::setGlobalSignalHandler(SIGPIPE, SIG_IGN, 0);
  267. #endif
  268. int32_t returnValue = 0;
  269. #ifdef ENABLE_BITTORRENT
  270. if(op->defined(PREF_TORRENT_FILE)) {
  271. if(op->get(PREF_SHOW_FILES) == V_TRUE) {
  272. DefaultBtContextHandle btContext = new DefaultBtContext();
  273. btContext->load(op->get(PREF_TORRENT_FILE));
  274. std::cout << btContext << std::endl;
  275. } else {
  276. returnValue = downloadBitTorrent(op, args);
  277. }
  278. }
  279. else
  280. #endif // ENABLE_BITTORRENT
  281. #ifdef ENABLE_METALINK
  282. if(op->defined(PREF_METALINK_FILE)) {
  283. if(op->get(PREF_SHOW_FILES) == V_TRUE) {
  284. Util::toStream(std::cout, MetalinkEntry::toFileEntry(MetalinkHelper::parseAndQuery(op->get(PREF_METALINK_FILE), op)));
  285. } else {
  286. returnValue = downloadMetalink(op);
  287. }
  288. }
  289. else
  290. #endif // ENABLE_METALINK
  291. if(op->defined(PREF_INPUT_FILE)) {
  292. returnValue = downloadUriList(op);
  293. } else {
  294. returnValue = downloadUri(op, args);
  295. }
  296. if(returnValue == 1) {
  297. exitStatus = EXIT_FAILURE;
  298. }
  299. } catch(Exception* ex) {
  300. std::cerr << EX_EXCEPTION_CAUGHT << "\n" << *ex << std::endl;
  301. delete ex;
  302. exitStatus = EXIT_FAILURE;
  303. }
  304. delete op;
  305. LogFactory::release();
  306. FeatureConfig::release();
  307. return exitStatus;
  308. }
  309. } // namespace aria2
  310. int main(int argc, char* argv[]) {
  311. #ifdef HAVE_WINSOCK2_H
  312. aria2::Platform platform;
  313. #endif // HAVE_WINSOCK2_H
  314. #ifdef ENABLE_NLS
  315. setlocale (LC_CTYPE, "");
  316. setlocale (LC_MESSAGES, "");
  317. bindtextdomain (PACKAGE, LOCALEDIR);
  318. textdomain (PACKAGE);
  319. #endif // ENABLE_NLS
  320. #ifdef HAVE_LIBSSL
  321. // for SSL initialization
  322. SSL_load_error_strings();
  323. SSL_library_init();
  324. #endif // HAVE_LIBSSL
  325. #ifdef HAVE_LIBGNUTLS
  326. gnutls_global_init();
  327. #endif // HAVE_LIBGNUTLS
  328. int r = aria2::main(argc, argv);
  329. #ifdef HAVE_LIBGNUTLS
  330. gnutls_global_deinit();
  331. #endif // HAVE_LIBGNUTLS
  332. return r;
  333. }