main.cc 9.9 KB

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