main.cc 11 KB

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