main.cc 9.9 KB

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